Jump to content

Requests for technical support from the VASP team should be posted in the VASP Forum.

Machine learning force field: Theory

From VASP Wiki

Here we present the theory for on-the-fly machine learning force fields. The theory will be presented in a very condensed manner and for a more detailed description of the methods, we refer the readers to Refs. [1], [2] and [3].

Introduction

Molecular dynamics is one of the most important methods for the determination of dynamic properties. The quality of the molecular dynamics simulation depends very strongly on the accuracy of the calculational method, but higher accuracy usually comes at the cost of increased computational demand. A very accurate method is ab initio molecular dynamics, where the interactions of atoms and electrons are calculated fully quantum mechanically from ab initio calculations (such as e.g. DFT). Unfortunately, this method is very often limited to small simulation cells and simulation times. One way to hugely speed up these calculations is by using force fields, which are parametrizations of the potential energy. These parametrizations can range from very simple functions with only empirical parameters, to very complex functions parametrized using thousands of ab initio calculations. Usually making accurate force fields manually is a very time-consuming task that needs tremendous expertise and know-how.

Another way to greatly reduce computational cost and required human intervention is by machine learning. Here, in the prediction of the target property, the method automatically interpolates between known training systems that were previously calculated ab initio. This way the generation of force fields is already significantly simplified compared to a classical force field which needs manual (or even empirical) adjustment of the parameters. Nevertheless, there is still the problem of how to choose the proper (minimal) training data. One very efficient and automatic way to solve that is to adapt on-the-fly learning. Here an MD calculation is used for the learning. During the run of the MD calculation, ab initio data is picked out and added to the training data. From the existing data, a force field is continuously built up. At each step, it is judged whether to make an ab initio calculation and possibly add the data to the force field or to use the force field for that step and actually skip learning for that step. Hence the name "on the fly" learning. The crucial point here is the probability model for the estimation of errors. This model is built up from newly sampled data. Hence, the more accurate the force field gets the less sampling is needed and the more expensive ab initio steps are skipped. This way not only the convergence of the force field can be controlled but also a very widespread scan through phase space for the training structures can be performed. The crucial point for on-the-fly machine learning which will be explained with the rest of the methodology in the following subsections is to be able to predict errors of the force field on a newly sampled structure without the necessity to perform an ab initio calculation on that structure.

Algorithms

On-the-fly machine-learning algorithm

Fig. 1: On-the-fly machine learning force field generation scheme.

To obtain the machine-learning force field several structure datasets are required. A structure dataset defines the Bravais lattice and the atomic positions of the system and contains the total energy, the forces, and the stress tensor calculated by first principles. Given these structure datasets, the machine identifies local configurations around an atom to learn what force field is appropriate. The local configuration measures the radial and angular distribution of neighboring atoms around this given site and is captured in the so-called descriptors.

The on-the-fly force field generation scheme is given by the following steps (a flowchart of the algorithm is shown in Fig. 1):

  1. The machine predicts the energy, the forces, and the stress tensor and their uncertainties for a given structure using the existing force field.
  2. The machine decides whether to perform a first-principles calculation (proceed with step 3); otherwise we skip to step 5.
  3. Performing the first principles calculation, we obtain a new structure dataset that may improve the force field.
  4. If the number of newly collected structures reaches a certain threshold or if the predicted uncertainty becomes too large, the machine learns an improved force field by including the new structure datasets and local configurations.
  5. Atomic positions and velocities are updated by using the force field (if accurate enough) or the first principles calculation.
  6. If the desired total number of ionic steps (NSW) is reached we are done; otherwise proceed with step 1.

Sampling of training data and local reference configurations

We employ a learning scheme where structures are only added to the list of training structures when local reference configurations are picked for atoms that have an error in the force higher than a given threshold. So in the following, it is implied that whenever a new training structure is obtained, also local reference configurations from this structure are obtained.

Usually one can employ that the force field doesn't necessarily need to be retrained immediately at every step when a training structure with corresponding local configurations is added. Instead, one can also collect candidates and do the learning in a later step for all structures simultaneously (blocking). This way saving significant computational costs. Of course learning after every new configuration or after every block can have different results, but with not too large block sizes the difference should be small. The tag ML_MCONF_NEW sets the block size for learning. The force field is usually not updated at every molecular-dynamics step. The update happens under the following conditions:

  • If there is no force field present, all atoms of a structure are sampled as local reference configurations and a force field is constructed.
  • If the Bayesian error of the force for any atom is above the strict threshold set by ML_CDOUB×ML_CTIFOR, the local reference configurations are sampled and a new force field is constructed.
  • If the Bayesian error of the force for any atom is above the threshold ML_CTIFOR but below ML_CDOUB×ML_CTIFOR, the structure is added to the list of new training structure candidates. Whenever the number of candidates is equal to ML_MCONF_NEW they are added to the entire set of training structures and the force field is updated. To avoid sampling too similar structures, the next step, from which training structures are allowed to be taken as candidates, is set by ML_NMDINT. All ab initio calculations within this distance are skipped if the Bayesian error for the force on all atoms is below ML_CDOUB×ML_CTIFOR.

Threshold for error of forces

Training structures and their corresponding local configurations are only chosen if the error in the forces of any atom exceeds a chosen threshold. The initial threshold is set to the value provided by ML_CTIFOR (the unit is eV/Angstrom). The behavior of how the threshold is further controlled is given by ML_ICRITERIA. The following options are available:

  • ML_ICRITERIA = 0: No update of initial value of ML_CTIFOR is done.
  • ML_ICRITERIA = 1: Update of criteria using an average of the Bayesian errors of the forces from history (see description of the method below).
  • ML_ICRITERIA = 2: Update of criteria using gliding average of Bayesian errors (probably more robust but not well tested).

Generally, it is recommended to automatically update the threshold ML_CTIFOR during machine learning. Details on how and when the update is performed are controlled by ML_CSLOPE, ML_CSIG and ML_MHIS.

Description of ML_ICRITERIA=1:

ML_CTIFOR is updated using the average Bayesian error in the previous steps. Specifically, it is set to

ML_CTIFOR = (average of the stored Bayesian errors) *(1.0 + ML_CX).

The number of entries in the history of the Bayesian errors is controlled by ML_MHIS. To avoid noisy data or an abrupt jump of the Bayesian error causing issues, the standard error of the history must be below the threshold ML_CSIG, for the update to take place. Furthermore, the slope of the stored data must be below the threshold ML_CSLOPE. In practice, the slope and the standard errors are at least to some extent correlated: often the standard error is proportional to ML_MHIS/3 times the slope or somewhat larger. We recommend to vary only ML_CSIG and keep ML_CSLOPE fixed to its default value.

Local energies

The potential energy U of a structure with Na atoms is approximated as

U=i=1NaUi.

The local energies Ui are functionals Ui=F[ρi(𝐫)] of the probability density ρi to find another atom j at the position 𝐫 around the atom i within a cut-off radius Rcut defined as

ρi(𝐫)=j=1Nafcut(rij)g(𝐫𝐫ij),rij=|𝐫ij|=|𝐫j𝐫i|.

Here fcut is a cut-off function that goes to zero for rij>Rcut and g(𝐫𝐫ij) is a delta function.

The atom distribution can also be written as a sum of individual distributions:

ρi(𝐫)=jiNaρij(𝐫).

Here ρij(𝐫) describes the probability of find an atom j at a position 𝐫 with respect to atom i.

Descriptors

Similar to the Smooth Overlap of Atomic Positions[4] (SOAP) method the delta function g(𝐫) is approximated as

g(𝐫)=1σatom2πexp(|𝐫|22σatom2).

Fig. 2: Radial and angular descriptors.


Unfortunately ρi(𝐫) is not rotationally invariant. To deal with this problem intermediate functions or descriptors depending on ρi(𝐫) possessing rotational invariance are introduced:

Radial descriptor

This is the simplest descriptor which relies on the radial distribution function

ρi(2)(r)=14πρi(r𝐫^)d𝐫^,

where 𝐫^ denotes the unit vector of the vector 𝐫 between atoms i and j [see Fig. 2 (a)]. The Radial descriptor can also be regarded as a two-body descriptor.

Angular descriptor

In most cases the radial descriptor is not enough to distinguish different probability densities ρi, since two different ρi can yield the same ρi(2). To improve on this angular information between two radial descriptors is also incorporated within an angular descriptor

ρi(3)(r,s,θ)=d𝐫^d𝐬^δ(𝐫^𝐬^cosθ)j=1NakjNaρik(r𝐫^)ρij(s𝐬^)

where θ denotes the angle between two vectors 𝐫ij and 𝐫ik [see Fig. 2 (b)]. The important difference of the function ρi(3) compared to the angular distribution function (also called power spectrum within the Gaussian Approximation Potential) used in reference [5] is that no self interaction is included, where j and k have the same distance from i and the angle between the two is zero. It can be shown [3] that the self-interaction component is equivalent to the two body descriptors. This means that our angular descriptor is a pure angular descriptor, containing no two-body components and it cannot be expressed as linear combinations of the power spectrum. The advantage of this descriptor is that it enables us to separately control the effects of two- and three-body descriptors.

Basis set expansion and descriptors

The atomic probability density can be also expanded in terms of basis functions

ρi(𝐫)=l=1Lmaxm=lln=1NRlcnlmiχnl(r)Ylm(𝐫^),

where cnlmi, χnl and Ylm denote expansion coefficients, radial basis functions and spherical harmonics, respectively. The indices n, l and m denote radial numbers, angular and magnetic quantum numbers, respectively.

By using the above equation the radial descriptor and angular descriptor can be written as

ρi(2)(r)=14πn=1NR0cn00iχn0(r),

and

ρi(3)(r,s,θ)=l=1Lmaxn=1NRlν=1NRl2l+12pnνliχnl(r)χνl(s)Pl(cosθ),

where χνl and Pl represent normalized spherical Bessel functions and Legendre polynomials of order l, respectively.

The expansion coefficients for the angular descriptor can be converted to

pnνli=8π22l+1m=ll[cnlmicνlmi*jNacnlmijcνlmij*],

where cnlmij denotes expansion coefficients of the distribution ρij(𝐫) with respect to χνl and Pl

ρij(𝐫)=l=1Lmaxm=lln=1NRlcnlmijχnl(r)Ylm(𝐫^)

and

cnlmi=jNacnlmij.

Angular filtering

In many cases χnl is multiplied with an angular filtering function[6] η (ML_IAFILT2), which can noticably reduce the necessary basis set size without losing accuracy in the calculations

ηl,aFILT=11+aFILT[l(l+1)]2

where aFILT (ML_AFILT2) is a parameter controlling the extent of the filtering. A larger value for the parameter should provide more filtering.

Reduced descriptors

A descriptor that reduces the number of descriptors with respect to the number of elements[7] is written as

pnνliJ=8π22l+1m=llcnlmiJJcνlmiJ.

For more details on the effects of this descriptor please see following site: ML_DESC_TYPE.

Potential energy fitting

It is convenient to express the local potential energy of atom i in structure α in terms of linear coefficients wiB and a kernel K(𝐗i,𝐗iB) as follows

Uiα=iB=1NBwiBK(𝐗iα,𝐗iB)

where NB is the basis set size. The kernel K(𝐗i,𝐗iB) measures the similarity between a local configuration from the training set ρi(𝐫) and basis set ρiB(𝐫). Using the radial and angular descriptors it is written as

K(X^i,X^iB)=[βX^i(2)X^iB(2)+(1β)X^i(3)X^iB(3)]ζ.

Here the vectors X^i(2) and X^i(3) contain all coefficients cn00i and pnνli, respectively. The notation X^ indicates that it is a normalized vector. The parameter β (ML_W1) controls the weighting of the radial and angular terms, respectively. The parameter ζ (ML_NHYP) controls the sharpness of the kernel and the order of the many-body interactions.

Normalization

The two and three body descriptors are combined to form a feature concatenated vector

𝐗c=[𝐗(2)𝐗(3)].

||𝐗c|| is used in the normalization of 𝐗(2) and 𝐗(3).

Matrix vector form of linear equations

Similarly to the energy Uiαthe forces and the stress tensor are also described as linear functions of the coefficients wiB. All three are fitted simultaneously which leads to the following matrix-vector form

𝐘=Φ𝐰

where 𝐘 is a super vector consisting of the sub vectors {𝐲α|α=1,...,Nst}. Here each 𝐲α contains the first principle energies per atom, forces and stress tensors for each structure α. Nst denotes the total number of structures. The size of 𝐘 is Nst×(1+3Naα+6).

The matrix Φ is also called as design matrix[8]. The rows of this matrix are blocked for each structure α, where the first line of each block consists of the kernel used to calculate the energy. The subsequent 3Naα lines consist of the derivatives of the kernel with respect to the atomic coordinates used to calculate the forces. The final 6 lines within each structure consist of the derivatives of the kernel with respect to the unit cell coordinates used to calculate the stress tensor components. The overall size of 𝐘 is Nst×(1+3Naα+6)×NB looking like as follows

Φ=[i1Naα=1K(𝐗iα=1,𝐗iB=1)i1Naα=1K(𝐗iα=1,𝐗iB=2)ix1K(𝐗iα=1,𝐗iB=1)ix1K(𝐗iα=1,𝐗iB=2)iy1K(𝐗iα=1,𝐗iB=1)iy1K(𝐗iα=1,𝐗iB=2)iz1K(𝐗iα=1,𝐗iB=1)iz1K(𝐗iα=1,𝐗iB=2)ix2K(𝐗iα=1,𝐗iB=1)ix2K(𝐗iα=1,𝐗iB=2)iy2K(𝐗iα=1,𝐗iB=1)iy2K(𝐗iα=1,𝐗iB=2)iz2K(𝐗iα=1,𝐗iB=1)iz2K(𝐗iα=1,𝐗iB=2)i1Naα=2K(𝐗iα=2,𝐗iB=1)i1Naα=2K(𝐗iα=2,𝐗iB=2)ix1K(𝐗iα=2,𝐗iB=1)ix1K(𝐗iα=2,𝐗iB=2)iy1K(𝐗iα=2,𝐗iB=1)iy1K(𝐗iα=2,𝐗iB=2)iz1K(𝐗iα=2,𝐗iB=1)iz1K(𝐗iα=2,𝐗iB=2)ix2K(𝐗iα=2,𝐗iB=1)ix2K(𝐗iα=2,𝐗iB=2)iy2K(𝐗iα=2,𝐗iB=1)iy2K(𝐗iα=2,𝐗iB=2)iz2K()iz2K()].

Bayesian linear regression

Ultimately for error prediction we want to get the maximized probability of observing a new structure 𝐲 on basis of the training set 𝐘, which is denoted as p(𝐲|𝐘). For this we need to get from the error of the linear fitting coefficients 𝐰 in the reproduction of the training data p(𝐰|𝐘) to p(𝐲|𝐘) which is explained in the following.

First we obtain p(𝐰|𝐘) from the Bayesian theorem

p(𝐰|𝐘)=p(𝐘|𝐰)p(𝐰)p(𝐘),

where we assume multivariate Gaussian distributions 𝒩 for the likelihood function

p(𝐘|𝐰)=𝒩(𝐘|Φ𝐰,σv2𝐈)

and the (conjugate) prior

p(𝐰)=𝒩(𝐰|𝟎,σw2𝐈).

The parameters σw2 and σv2 need to be optimized to balance the accuracy and robustness of the force field (see below). The normalization is obtained by

p(𝐘)=p(𝐘|𝐰)p(𝐰)d𝐰.

Using the equations from above and the completing square method[8] p(𝐰|𝐘) is obtained as follows

p(𝐰|𝐘)=𝒩(𝐰|w¯,Σ).

Since the prior and the likelood function are described by multivariate Gaussian distributions the posterior 𝒩(𝐰|w¯,Σ) describes a multivariate Gaussian written as

𝒩(𝐰|w¯,Σ)=1(2π)NBdetΣexp[(𝐰w¯)TΣ1(𝐰w¯)2]

where we use the following definitions for the center of the Guassian distribution w¯ and the covariance matrix Σ

w¯=svΣΦT𝐘,

Σ1=sw𝐈+svΦTΦ.

Here we have used the following definitions

sv=1σv2

and

sw=1σw2.

Regression

We want to obtain the weights w¯ for the linear equations

𝐘=Φw¯.


Solution via Inversion

By using directly the covariance matrix Σ the equation from above

w¯=svΣΦT𝐘,

can be solved straightforwardly. However, only Σ1 is directly accesible and Σ must be obtained from Σ1 via inversion. This inversion is numerically unstable and leads to lower accuracy. Hence we don't use this method.


Solution via LU factorization

Here we directly use the invers of the covariance matrix Σ1 by solving the following equation for the weights w¯

Σ1w¯=svΦT𝐘.

For that Σ1 is decomposed into it's LU factorized components 𝐏*𝐋*𝐔. After that the 𝐋*𝐔 factors are used to solve the previous linear equation for the weights w¯. This method is noticeably more accurate than the method via inversion of Σ1 while being on the same order of magnitude in terms of computational cost. Hence it is the method used in on-the-fly learning.


Solution via regularized SVD

The regression problem can also be solved by using the singular value decomposition to factorize Φ as

Φ=𝐔Λ𝐕T.

To add the regularization the diagonal matrix Λ containing the singular values is rewritten as

Λ~=Λ+swsvΛ1.

By using the orthogonality of the left and right eigenvector matrices 𝐔T𝐔=𝐈n and 𝐕𝐕T=𝐈n the regression problem has the following solution

𝐰~=𝐕Λ~1𝐔T𝐘.


Evidence approximation

Finally to get the best results and to prevent overfitting the parameters σv2 and σw2 have to be optimized. To achieve this, we use the evidence approximation[9][10][11] (also called as empirical bayes, 2 maximum likelihood or generalized maximum likelihood), which maximizes the evidence function (also called model evidence) defined as

p(𝐘|σv2,σw2)=(12πσv2)M(12πσw2)NBexp[E(𝐰)]d𝐰,

E(𝐰)=sv2||Φ𝐰𝐘||2+sw2||𝐰||2.

The function p(𝐘|σv2,σw2) corresponds to the probability that the regression model with parameters σv2 and σw2 provides the reference data 𝐘. Hence the best fit is optimized by maximizing this probability. The maximization is carried out by simultaneously solving the following equations

sw=γ|w¯|2,

sv=Mγ|𝐓ϕw¯|2,

γ=k=1NBλkλk+sw

where λk are the eigenvalues of the matrix svΦTΦ.

The evidence approximation can be done for any of the above-described regression methods, but we combine it only with the solutions from LU factorization since solutions via SVD are calculationally too expensive to be carried out multiple times.

Error estimation

Error estimates from Bayesian linear regression

By using the relation

p(𝐲|𝐘)=p(𝐲|𝐰)p(𝐰|𝐘)d𝐰

and the completing square method[8] the distribution of p(𝐲|𝐘) is written as

p(𝐲|𝐘)=𝒩(ϕw¯,σ),

σ=1sv𝐈+ϕTΣϕ.

Here ϕ is similar to Φ but only for the new structure 𝐲.

The mean vector ϕw¯ contains the results of the predictions on the dimensionless energy per atom, forces, and stress tensor. The diagonal elements of σ, which correspond to the variances of the predicted results, are used as the uncertainty in the prediction.

Spilling factor

The spilling factor[2][12] si is a measure of the overlap (or similarity) of a given structural environment on an atom 𝐗i with the local reference configurations 𝐗iB written as

si=1iB=1NBi'B=1NBK(𝐗i,𝐗iB)K1(𝐗iB,𝐗i'B)K(𝐗i'B,𝐗i)K(𝐗i,𝐗i).

If 𝐗i is fully overlapping with any of the local reference configurations then the second term on the right hand side of the above equation becomes 1 and s=0. If 𝐗i has no similarities with any of the local reference configurations the second term on the right hand side of the above equations becomes 0 and s=1.

Sparsification

Within the machine learning force field methods the sparsification of local reference configurations and the angular descriptors is supported. The sparsification of local reference configurations is by default used and the extent is mainly controlled by ML_EPS_LOW. This is procedure is important to avoid overcompleteness and to dampen the acquisition of new configurations in special cases. The sparsification of angular descriptors is by default not used and should be used very cautiously and only if it's necessary. The description of the usage of this feature is given in ML_LSPARSDES.

Sparsification of local reference configurations

We start by defining the similarity kernel (or Gram matrix) for the local configurations with each other

𝐊iB,jB=𝐊(𝐗iB,𝐗jB).

The CUR algorithm starts out from the diagonalization of this matrix

𝐔T𝐊𝐔=𝐋=diag(l1,l2,...,lNB),

where 𝐔 is the matrix of the eigenvectors u𝐣

𝐔=(𝐮1,𝐮2,...,𝐮NB),𝐮j=(u1ju2j...uNBj)

In contrast to the original CUR algorithm[13] that was developed to efficiently select a few significant columns of the matrix 𝐊, we search for (few) insignificant local configurations and remove them. We dispose of the columns of 𝐊 that are correlated with the Nlow eigenvectors uχ with the smallest eigenvalues lχ. The correlation is measured by the statistical leverage scoring measured for each column j of 𝐊 as

ωj=1Nlowχ=1NBγχ,j,

γχ,j={ujχ2iflχ>ϵlow0otherwise,

where ϵlow (see also ML_EPS_LOW) is the threshold for the lowest eigenvalues. One can prove (using the orthogonality of the eigenvectors and their completeness relation) that this is equivalent to the usual CUR leverage scoring algorithm, i.e. removing the least significant columns will result in those columns that are most strongly "correlated" to the largest eigenvalues.

Sparsification of angular descriptors

The sparsification of the angular descriptors is done in a similar manner as for the local reference configurations. We start by defining the ND×ND square matrix

𝐀=𝐗𝐗T.

Here ND denotes the number of angular descriptors given by

ND=l=1Lmax12NRl(NRl+1)(Lmax+1).

In this equation the symmetry of the descriptors ρnνl=ρνnl is already taken into account. The matrix 𝐗 is constructed from the vectors of the angular descriptors (𝐱1(3),𝐱2(3),...,𝐱NB(3)). The matrix 𝐗 has dimension ND×NB and the matrix product is done over the NB elements of the local configurations.

In analogy to the local configurations the jth element of the matrix 𝐀 is written as

𝐚j=χ=1ND(ujχlχ)uχ.

In contrast to the sparsification of the local configuration and more in line with the original CUR method, the columns of matrix 𝐀 are kept when they are strongly correlated to the k (see also ML_NRANK_SPARSDES) eigenvectors uχ which have the largest eigenvalues lχ. The correlation of the eigenvalues is then measured via a leverage scoring

ωj=1kχ=1kujχ2.

From the leverage scorings, the ones with the highest values are selected until the ratio of the selected descriptors to the total number of descriptors becomes a previously selected value xspars (see also ML_RDES_SPARSDES).

References