.. _lecture6: ===================================================================== Is your data sufficient for ML? Getting data ===================================================================== - :ref:`Previous homework analysis ` - :ref:`Homework 5.1 ` - :ref:`Homework 5.2 ` - :ref:`Homework 5.3 ` - :ref:`Data is everything ` - :ref:`Grid sampling ` - :ref:`Sampling from QM simulations ` - :ref:`Mitigating insufficient data ` - :ref:`Delta-learning ` - :ref:`Getting rid of a QM baseline: transfer learning and hierarchical ML ` - :ref:`Acquiring data: active learning ` - :ref:`Homework ` Slides ------ :download:`Slides <_static/lecture6/Lecture6_wm.pdf>`: .. raw:: html .. _lecture6_homework_analysis: Previous homework analysis -------------------------- .. _lecture5_homework_analysis_5.1: Homework 5.1 ++++++++++++ .. include:: materials/lecture5/homework5.1/homework5.1_nvt.inc The key observation is that in the NVT ensemble the total energy is no longer conserved while in the NVE ensemble is does not deviate much. There is still noticable deviation of the total energy in the NVE ensemble. You can think about the reasons and how you can adjust the settings of the MD simulations to improve the energy conservation in the NVE simulations. The temperature is, strictly speaking, not conserved in either of the simulation but in the NVT it varies narrower and the average should be closer to the target temperature. Very often, in the NVT dynamics, the temperature rises or drops from the initial temperature to some more stable value with the average around the target temperature. Then you usually need to cut the initial (called equilibration) period before it becomes stable and then use for analysis the later part of the MD. .. _lecture5_homework_analysis_5.2: Homework 5.2 ++++++++++++ .. include:: materials/lecture5/homework5.2/homework5.2_0K.inc If you have done everything right, the simulation should lead to the trajectory where the atoms basically do not move. This is obvious if you consider how the MD is propagated - each steps changes the geometry based on the velocity and acceleration (defined by forces). Hence, for 0 K, velocity is zero by definition (kinetic energy should be zero). The molecule in the energy minimum has by definition energy derivatives (negative forces) equal to zero too, i.e., accelerations are zero. Hence, each next position must be the same as previous. You can still see small numerical noise. .. _lecture5_homework_analysis_5.3: Homework 5.3 ++++++++++++ .. include:: materials/lecture5/homework5.3/homework5.3_qct_h2.inc The obvious qualitative difference is that even at 0 K, the QCT still has the motion of atoms in contrast to the classical MD. This is how it should be as the zero-point energy is always present in molecules, even at 0 K. .. _lecture6_intro: Data is everything ------------------ Unless you use the pre-trained ML models, you always need data for training your ML model. You have already trained several MLPs on hydrogen molecule PES. In this lesson, we will explore the importance of data. ML is only as good as (or worse than) your data +++++++++++++++++++++++++++++++++++++++++++++++ In supervised learning, ML only learns from the data it sees. It has many implications. One of the most obvious is that if ML only sees the HF/STO-3G energies, it can be only as good as this level and will not be able to suddenly predict you energies with full CI accuracy. "Garbage in - garbage out". In practice, when you fit your ML on the data, you also loose some accuracy as the fitted function is never exactly the ground-truth function (i.e., HF/STO-3G). While we talked in :ref:`Lecture 4 ` about the last aspect of how to fit as good as possible on the given data, here we will talk about the impact of the data itself on the quality of the ML model. How well ML represents the ground-truth function depends on *how many and what samples* (training points) ML has seen during training and validation. The key point to understand is that ML knows nothing about the ground-truth function itself but try to infer its shape through the samples comprising the set of inputs to the ground-truth function and this function's outputs for the corresponding inputs. Hence, what samples ML sees is crucial. We saw in :ref:`Homework 4.2 ` that if you sample more and more training points, the accuracy of the ML model typically improves. However, let's see if we sample the points differently. .. include:: materials/lecture6/task6.1.inc You see that your models trained with twice as few points but with a better sampling procedure (farthest-point sampling) produces more stable and more accurate result! Note that this result does not invalidate the general trend that more points lead to better accuracy, but this trend only works if the points are sampled from the same distribution (PES region) with the same procedure (random drawing of points). In the above task, you saw *how* (i.e., procedure) you choose the points is extremely important. The farthest-point sampling in this case chooses points better spread over the PES than random sampling which often misses important regions. We will learn later other procedures, among which :ref:`active learning ` is the best way to go. Now let's see what happens if we sample from a different distribution. .. include:: materials/lecture6/task6.2.inc This examples teaches the important aspect that if ML did not see some pattern in the data it usually will not be able to predict it correctly. In other words, ML *is not very good for extrapolation* and such situations should be avoided as much as possible by providing ML with such data that it interpolates. It is more important to have diverse samples representing the ground-truth function than tons of samples from only a small segment of the function. Indeed, the model you trained on :ref:`20 times smaller data set ` (20 points chosen using farthest-point sampling) performed better than the model trained on :ref:`431 points ` but not covering the important regions of PES. Armed with the above knowledge, how would you proceed to create good models when you are limited to small data? Give this question a good thought before continuing reading below about the possible strategies. .. _lecture6_grid_smpl: Grid sampling ------------- The obvious solution for constructing the data for ML is to sample points from regular intervals on a grid or using some sort of farthest-point sampling to choose fewer data from the given dense grid. Only these fewer data has to have labels (e.g., energies calculated at QM level). For example, you can sampled 20 points using this strategy for a small the H\ :sub:`2` molecule. This molecule has only one degree of freedom (interatomic distance) and any reasonable sampling of the grid points would work. This strategy of grid sampling is the most straightforward and robust if it is affordable. The problem is that most of molecules of interest have many atoms and degrees of freedom. As you can see in a :ref:`homework `, the number of points required to ensure the high sampling density growth very fast and is quickly unaffordable. This is called the *curse of dimensionality* which we cannot overcome but only find ways to ameliorate. Luckily, we do have very efficient strategies that deal with this curse. .. _lecture6_qm_simul_smpl: Sampling from QM simulations ---------------------------- Another ingeneous way to sample data is by running first the simulations (typically, MD) with QM method and then use the data generated during the simulation for training ML. Often, only part of these data is used. You might ask what is the benefit of using such an approach - if you already run simulations with the QM method, why would you need to train ML model to run the same simulations? It is a good question and it comes down to practical considerations: this approach is very simple as it samples the PES region which you need while you can run more simulations with ML at a lower cost afterwards. For example, you can run short MD trajectories with QM and then train ML and propagate much longer and many more trajectories with ML. Also, you can perform simulations with a cheap QM method (such as semi-empirical QM) and calculate higher-level QM energies (and forces) for the small portion of the simulation data, so that you can train ML on those higher-level QM data and do simulations with the surrogate ML model representing this high level. This works in many cases but the caveat is that often the ML model may be unstable (i.e., typically lead to unphysical molecular explosion) in the longer simulations and some trajectories. Hence, you need to pay attention to whether the simulations make sense and eventually perform additional checks. .. _lecture6_multiqm: Mitigating insufficient data ---------------------------- Very powerful strategies to deal with the curse of dimensionality is shift the focus from choosing the sufficient data to mitigating the effects of having insufficient data. Here we overview them. .. note:: It is also possible to deal with very little data by incorporating ML into QM model itself. However, this requires tempering with the QM model, knowing the QM foundations, and cannot be applied for *any* QM method. The strategies overviewed here do not require any of this and can be applied to any QM method. .. _delta-learning: Delta-learning ++++++++++++++ The most robust and simplest ones, which does not require tempering with the QM method itself, is `delta-learning `__. In delta-learning, instead of training the model directly on the target QM level (e.g., FCI), we only learn the difference (delta) between the target and baseline QM levels. The baseline QM level is a computationally faster but less accurate method. The rational is that learning differences is easier and that if some PES regions are insufficiently sampled, the predictions fall back to the baseline method which serves as a 'fail-safe'. The operational principle of delta-learning can be seen best in a :ref:`homework `. As you can see, although delta-learning is not a silver bullet and does not resolve all the problems, it does help to mitigate problems seen in the direct learning to a good extend. At least it often is still able to capture the most important features of PES at more or less the right location where direct learning fails. Some of the failures can be completely unphysical, like missing the minimum on PES or having too low dissociation energy leading to molecular explosion during MD. You have alrady used AIQM1 that is a delta-learning model which uses ANI neural network for correcting the semi-empirical QM method baseline. You also saw that although both AIQM1 and ANI-1ccx were trained on the same data (and use similar neural networks!), AIQM1 is usually more accurate and robust while ANI-1ccx sometimes lead to unphysical failures (remember H\ :sub:`2` and its MD!). What is disadvantages of delta-learning you can think about? .. _no-baseline: Getting rid of a QM baseline: transfer learning and hierarchical ML ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ One of the disadvantages of delta-learning is that you have to calculate the properties wit the baseline QM method when you do predictions. Baseline QM is faster than the target QM method but typically much slower than pure ML model trained directly on the target QM data. There are several ways that allow you to speed up the simulations by getting rid of the slow QM baseline. The simplest one is to train another ML model directly on the baseline QM data and use this ML baseline model instead. It only works if you train this model on more baseline data than you have target data, e.g., you can train ML model on entire H\ :sub:`2` data set with 451 points with HF/STO-3G energies along the potential energy curve from 0.5 to 5 Angstrom while only use a fraction of this points with energies at the FCI level. This approach can be generalized to any number of the QM levels and is known as `hierarchical ML `__. A similar and more popular approach is transfer learning. Here you first train an ML model on more data obtained at the baseline QM method and then fine-tune (i.e., train only some/all parameters) on the fewer data at the target QM method. This can be also done, e.g., by training on the experimental data as a target. It is a powerful and easy to use approach and was also used in both AIQM1 and ANI-1ccx. In ANI-1ccx, the model was first trained on the 4.5 mln DFT data and then fine-tuned on 0.5 mln coupled-cluster data. .. note:: MLatom can also be used for transfer learning (but only through Python API at the moment which can be done by `fixing some of NN layers `__...). The problem with all these approaches without baseline QM method, is that there is no guarantee that the baseline ML model will always work as well as the baseline QM method. .. _al: Acquiring data: active learning ------------------------------- Active learning (AL) is the ultimate solution to the data problem where the data is generated as needed. The idea is that ML should itself be used to sample the points. This can come in different flavors but in all of them we need some criterion to decide whether we want to add a point to the data set. Typically, it is done in many iterations. In a pool-based AL, if you already have a grid of points but without labels (i.e., without reference values such as QM energies), then you can use AL to choose the more relevant points. Usually, you do not have the full grid of points. Hence, more usefully, you can train ML on some provided labeled data and then use this ML model to perform required simulations (typically, MD). Then you can select points from the simulations, label them (i.e., calculate reference QM properties such energies and forces), add to the existing labeled data, and repeat the procedure until convergence. Here, many decisions have to be made: what is the initial labeled data and how to get it? How to select points? How to train models? How to judge the convergence? There is no unique answer to these answers and many possible solutions were suggested. While this is topic is very broad, I have to mention that the key is typically the method for choosing the points, which is usually based on some kind of *uncertainty quantification* (UQ) and the points are sampled during simulations when the UQ exceeds some threshold. The UQ is often based on the disagreement (deviation) between several ML models in an *ensemble* (approach called *query by committee*). However, many other approaches, e.g., based on geometric criteria or some other statistical considerations (often, Bayesian formalim) are used too. .. note:: MLatom enables this by providing end-to-end implementation of all the required steps making it automatic. Active learning is currently only possible to do with MLatom Python API. You can find an example in Jupyter notebook ``al_h2.ipynb`` (open in Jupyter Lab) which I placed in the folder ``compchem_course/task6.5``. Other considerations when dealing with data ------------------------------------------------ The topic of data is broad and requires in itself a comprehensive course. Some keywords that you might want to check: - cleaning data (removing outliers, fixing mistakes...). - data formats (json, h5, h5md - many of them are used in MLatom). - FAIR principles (providing meta-data, etc.). - stratification. - databases (there are many existing compiled databases with calcualated QM properties such as QM9, ANI-1ccx, MD17, etc., some of them were used for training AIQM1 and ANI-1ccx as well as testing the performance of MLPs). .. _lecture6_homework: Homework -------- .. include:: materials/lecture6/homework6.1.inc .. include:: materials/lecture6/homework6.2.inc .. handpick geoms in H2