Package: megnet
Table of ContentsIntroductionMEGNet FrameworkInstallationUsageDatasetsImplementation detailsComputing requirementsKnown limitationsContributorsReferencesIntroductionThis repository represents the efforts of theMaterials Virtual Labin developing graph networks for machine learning in materials science. It is a
work in progress and the models we have developed thus far are only based on
our best efforts. We welcome efforts by anyone to build and test models using
our code and data, all of which are publicly available. Any comments or
suggestions are also welcome (please post on the Github Issues page.)A web app using our pre-trained MEGNet models for property prediction in
crystals is available athttp://megnet.crystals.ai. For tutorials, please visitnotebooksin this repo. We have also established an online simulation tool and a tutorial lecture at nanoHUB (https://nanohub.org/resources/megnet).Note: ADGL implementation of MEGNetis now available. For users
trying to build their own MEGNet models, it is highly recommended you check this version out, which may be easier to
work with and extend in future.MEGNet frameworkThe MatErials Graph Network (MEGNet) is an implementation of DeepMind's graph
networks[1] for universal machine learning in materials science. We have
demonstrated its success in achieving very low prediction errors in a broad
array of properties in both molecules and crystals (see"Graph Networks as a Universal Machine Learning Framework for Molecules and Crystals"[2]). New releases have included our recent work on multi-fidelity materials property modeling (See"Learning properties of ordered and disordered materials from multi-fidelity data"[3]).Briefly, Figure 1 shows the sequential update steps of the graph network,
whereby bonds, atoms, and global state attributes are updated using information
from each other, generating an output graph.Figure 1. The graph network update function.Figure 2 shows the overall schematic of the MEGNet. Each graph network module
is preceded by two multi-layer perceptrons (known as Dense layers in Keras
terminology), constituting a MEGNet block. Multiple MEGNet blocks can be
stacked, allowing for information flow across greater spatial distances. The
number of blocks required depend on the range of interactions necessary to
predict a target property. In the final step, aset2setis used to map the
output to a scalar/vector property.Figure 2. Schematic of MatErials Graph Network.InstallationMegnet can be installed via pip for the latest stable version:pipinstallmegnetFor the latest dev version, please clone this repo and install using:pythonsetup.pydevelopUsageOur current implementation supports a variety of use cases for users with
different requirements and experience with deep learning. Please also visit
thenotebooks directoryfor Jupyter notebooks with more detailed code examples.Using pre-built modelsIn our work, we have already built MEGNet models for the QM9 data set and
Materials Project dataset. These models are provided as serialized HDF5+JSON
files. Users who are purely interested in using these models for prediction
can quickly load and use them via the convenientMEGNetModel.from_filemethod. These models are available in themvl_modelsfolder of this repo.
The following models are available:QM9 molecule data:HOMO: Highest occupied molecular orbital energyLUMO: Lowest unoccupied molecular orbital energyGap: energy gapZPVE: zero point vibrational energyµ: dipole momentα: isotropic polarizability<R2>: electronic spatial extentU0: internal energy at 0 KU: internal energy at 298 KH: enthalpy at 298 KG: Gibbs free energy at 298 KCv: heat capacity at 298 Kω1: highest vibrational frequency.Materials Project data:Formation energy from the elementsBand gapLog 10 of Bulk Modulus (K)Log 10 of Shear Modulus (G)The MAEs on the various models are given below:Performance of QM9 MEGNet-Simple modelsPropertyUnitsMAEHOMOeV0.043LUMOeV0.044GapeV0.066ZPVEmeV1.43µDebye0.05αBohr^30.081<R2>Bohr^20.302U0eV0.012UeV0.013HeV0.012GeV0.012Cvcal/(molK)0.029ω1cm^-11.18Performance of MP-2018.6.1PropertyUnitsMAEEfeV/atom0.028EgeV0.33K_VRHlog10(GPa)0.050G_VRHlog10(GPa)0.079Performance of MP-2019.4.1PropertyUnitsMAEEfeV/atom0.026EfermieV0.288New models will be added as they are developed in themvl_modelsfolder. Each folder contains a summary of model details and benchmarks. For
the initial models and bencharmks comparison to previous models,
please refer to"Graph Networks as a Universal Machine Learning Framework for Molecules and Crystals"[2].Below is an example of crystal model usage:frommegnet.utils.modelsimportload_modelfrompymatgen.coreimportStructure,Lattice# load a model in megnet.utils.models.AVAILABLE_MODELSmodel=load_model("logK_MP_2018")# We can construct a structure using pymatgenstructure=Structure(Lattice.cubic(3.167),['Mo','Mo'],[[0,0,0],[0.5,0.5,0.5]])# Use the model to predict bulk modulus K. Note that the model is trained on# log10 K. So a conversion is necessary.predicted_K=10**model.predict_structure(structure).ravel()[0]print(f'The predicted K for{structure.composition.reduced_formula}is{predicted_K:.0f}GPa.')A full example is innotebooks/crystal_example.ipynb.For molecular models, we have an example innotebooks/qm9_pretrained.ipynb.
We support prediction directly from a pymatgen molecule object. With a few more
lines of code, the model can predict fromSMILESrepresentation of molecules,
as shown in the example. It is also straightforward to load axyzmolecule
file with pymatgen and predict the properties using the models. However, the
users are generally not advised to use theqm9molecule models for other
molecules outside theqm9datasets, since the training data coverage is
limited.Below is an example of predicting the "HOMO" of a smiles representationfrommegnet.utils.moleculeimportget_pmg_mol_from_smilesfrommegnet.modelsimportMEGNetModel# same model API for molecule and crystals, you can also use the load_model method# as in previous examplemodel=MEGNetModel.from_file('mvl_models/qm9-2018.6.1/HOMO.hdf5')# Need to convert SMILES into pymatgen Moleculemol=get_pmg_mol_from_smiles("C")model.predict_structure(mol)Training a new MEGNetModel from structuresFor users who wish to build a new model from a set of crystal structures with
corresponding properties, there is a convenientMEGNetModelclass for setting
up and training the model. By default, the number of MEGNet blocks is 3 and the
atomic number Z is used as the only node feature (with embedding).frommegnet.modelsimportMEGNetModelfrommegnet.data.crystalimportCrystalGraphimportnumpyasnpnfeat_bond=10r_cutoff=5gaussian_centers=np.linspace(0,r_cutoff+1,nfeat_bond)gaussian_width=0.5graph_converter=CrystalGraph(cutoff=r_cutoff)model=MEGNetModel(graph_converter=graph_converter,centers=gaussian_centers,width=gaussian_width)# Model training# Here, `structures` is a list of pymatgen Structure objects.# `targets` is a corresponding list of properties.model.train(structures,targets,epochs=10)# Predict the property of a new structurepred_target=model.predict_structure(new_structure)Note that for realistic models, thenfeat_bondcan be set to 100 andepochscan be 1000.
In some cases, some structures within the training pool may not be valid (containing isolated atoms),
then one needs to usetrain_from_graphsmethod by training only on the valid graphs.Following the previous example,model=MEGNetModel(graph_converter=graph_converter,centers=gaussian_centers,width=gaussian_width)graphs_valid=[]targets_valid=[]structures_invalid=[]fors,pinzip(structures,targets):try:graph=model.graph_converter.convert(s)graphs_valid.append(graph)targets_valid.append(p)except:structures_invalid.append(s)# train the model using valid graphs and targetsmodel.train_from_graphs(graphs_valid,targets_valid)For model details and benchmarks, please refer to"Graph Networks as a Universal Machine Learning Framework for Molecules and Crystals"[2]Training multi-fidelity graph networksPlease see the foldermultifidelityfor specific examples.Pre-trained elemental embeddingsA key finding of our work is that element embeddings from trained formation
energy models encode useful chemical information that can be transferred
learned to develop models with smaller datasets (e.g. elastic constants,
band gaps), with better converegence and lower errors. These embeddings are
also potentially useful in developing other ML models and applications. These
embeddings have been made available via the following code:frommegnet.data.crystalimportget_elemental_embeddingsel_embeddings=get_elemental_embeddings()An example of transfer learning using the elemental embedding from formation
energy to other models, please checknotebooks/transfer_learning.ipynb.Customized Graph Network ModelsFor users who are familiar with deep learning and Keras and wish to build
customized graph network based models, the following example outlines how a
custom model can be constructed fromMEGNetLayer, which is essentially our
implementation of the graph network using neural networks:fromtensorflow.keras.layersimportInput,Densefromtensorflow.keras.modelsimportModelfrommegnet.layersimportMEGNetLayer,Set2Setn_atom_feature=20n_bond_feature=10n_global_feature=2# Define model inputsint32='int32'x1=Input(shape=(None,n_atom_feature))# atom feature placeholderx2=Input(shape=(None,n_bond_feature))# bond feature placeholderx3=Input(shape=(None,n_global_feature))# global feature placeholderx4=Input(shape=(None,),dtype=int32)# bond index1 placeholderx5=Input(shape=(None,),dtype=int32)# bond index2 placeholderx6=Input(shape=(None,),dtype=int32)# atom_ind placeholderx7=Input(shape=(None,),dtype=int32)# bond_ind placeholderxs=[x1,x2,x3,x4,x5,x6,x7]# Pass the inputs to the MEGNetLayer layer# Here the list are the hidden units + the output unit,# you can have others like [n1] or [n1, n2, n3 ...] if you want.out=MEGNetLayer([32,16],[32,16],[32,16],pool_method='mean',activation='relu')(xs)# the output is a tuple of new graphs V, E and u# Since u is a per-structure quantity,# we can directly use it to predict per-structure propertyout=Dense(1)(out[2])# Set up the model and compile it!model=Model(inputs=xs,outputs=out)model.compile(loss='mse',optimizer='adam')With less than 20 lines of code, you have built a graph network model that is
ready for materials property prediction!Implementation detailsGraph networks[1] are a superclass of graph-based neural networks. There are a
few innovations compared to conventional graph-based neural neworks.Global state attributes are added to the node/edge graph representation.
These features work as a portal for structure-independent features such as
temperature, pressure etc and also are an information exchange placeholder
that facilitates information passing across longer spatial domains.The update function involves the message interchange among all three levels
of information, i.e., the node, bond and state information. It is therefore a
highly general model.TheMEGNetmodel implements two major components: (a) thegraph networklayer and (b) theset2setlayer.[4] The layers are based onkerasAPI and is thus compatible with other keras modules.Different crystals/molecules have different number of atoms. Therefore it is
impossible to use data batches without padding the structures to make them
uniform in atom number.MEGNettakes a different approach. Instead of making
structure batches, we assemble many structures into one giant structure, and
this structure has a vector output with each entry being the target value for
the corresponding structure. Therefore, the batch number is always 1.Assuming a structure has N atoms and M bonds, a structure graph is represented
asV(nodes/vertices, representing atoms),E(edges, representing bonds)
andu(global state vector).Vis a N*Nv matrix.Ecomprises of a
M*Nm matrix for the bond attributes and index pairs (rk, sk) for atoms
connected by each bond.uis a vector with length Nu. We vectorize rk and
sk to formindex1andindex2, both are vectors with length M. In summary,
the graph is a data structure withV(N*Nv),E(M*Nm),u(Nu, ),index1(M, ) andindex2(M, ).We then assemble several structures together. ForV, we directly append the
atomic attributes from all structures, forming a matrix (1*N'*Nv), where
N' > N. To indicate the belongingness of each atom attribute vector, we use aatom_indvector. For example ifN'=5and the first 3 atoms belongs to the
first structure and the remainings the second structure, ouratom_indvector
would be[0, 0, 0, 1, 1]. For the bond attribute, we perform the same
appending method, and usebond_indvector to indicate the bond belongingness.
Forindex1andindex2, we need to shift the integer values. For example,
ifindex1andindex2are[0, 0, 1, 1]and[1, 1, 0, 0]for structure 1
and are[0, 0, 1, 1]and[1, 1, 0, 0]for structure two. The assembled
indices are[0, 0, 1, 1, 2, 2, 3, 3]and[1, 1, 0, 0, 3, 3, 2, 2]. Finally,uexpands a new dimension to take into account of the number of structures,
and becomes a 1*Ng*Nu tensor, where Ng is the number of structures.1is
added as the first dimension of all inputs because we fixed the batch size to
be 1 (1 giant graph) to comply the keras inputs requirements.In summary the inputs for the model isV(1*N'*Nv),E(1*M'*Nm),u(1*Ng*Nu),index1(1*M'),index2(1*M'),atom_ind(1*N'), andbond_ind(1*M'). For Z-only atomic features,Vis a (1*N') vector.Data setsTo aid others in reproducing (and improving on) our results, we have provided
our MP-crystals-2018.6.1 crystal data set viafigshare[5].
The MP-crystals-2018.6.1 data set comprises the DFT-computed energies and
band gaps of 69,640 crystals from theMaterials Projectobtained via thePython Materials Genomics (pymatgen)interface to the Materials Application Programming Interface (API)[6] on
June 1, 2018. The crystal graphs were constructed using a radius cut-oﬀ of 4
angstroms. Using this cut-oﬀ, 69,239 crystals do not form isolated atoms and
are used in the models. A subset of 5,830 structures have elasticity data that
do not have calculation warnings and will be used for elasticity models.The molecule data set used in this work is the QM9 data set 30 processed by
Faber et al.[7] It contains the B3LYP/6-31G(2df,p)-level DFT calculation
results on 130,462 small organic molecules containing up to 9 heavy atoms.Computing requirementsTraining: It should be noted that training MEGNet models, like other deep
learning models, is fairly computationally intensive with large datasets. In
our work, we use dedicated GPU resources to train MEGNet models with 100,000
crystals/molecules. It is recommended that you do the same.Prediction: Once trained, prediction using MEGNet models are fairly cheap.
For example, thehttp://megnet.crystals.aiweb app runs on a single hobby dyno
on Heroku and provides the prediction for any crystal within seconds.Known limitationsisolated atomserror. This error occurs when using the given cutoff in the model (4A for
2018 models and 5A for 2019 models), the crystal structure contains isolated atoms, i.e.,
no neighboring atoms are within the distance ofcutoff. Most of the time, we can just
discard the structure, since we found that those structures tend to have a high energy above
hull (less stable). If you think this error is an essential issue for a particular problem,
please feel free to email us and we will consider releasing a new model with increased cutoff.ContributorsChi Chen from the Materials Virtual Lab is the lead developer of MEGNet.Shyue Ping Ong and other members of the Materials Virtual Lab contributes to general improvements
of MEGNet and its applications.Logan Ward has made extensive contributions, especially to the development of molecular graph
portions of MEGNet.ReferencesBattaglia, P. W.; Hamrick, J. B.; Bapst, V.; Sanchez-Gonzalez, A.;
Zambaldi, V.; Malinowski, M.; Tacchetti, A.; Raposo, D.; Santoro, A.;
Faulkner, R.; et al. Relational inductive biases, deep learning, and graph
networks. 2018, 1–38.arXiv:1806.01261Chen, C.; Ye, W.; Zuo, Y.; Zheng, C.; Ong, S. P. Graph Networks as a
Universal Machine Learning Framework for Molecules and Crystals. Chemistry
of Materials 2019, 31(9), 3564-3572.doi:10.1021/acs.chemmater.9b01294Chen, C.; Zuo, Y.; Ye, W.; Li, X.G.; Ong, S. P. Learning properties of ordered and
disordered materials from multi-fidelity data. Nature Computational Science 2021,
1, 46–53doi:10.1038/s43588-020-00002-x.Vinyals, O.; Bengio, S.; Kudlur, M. Order Matters: Sequence to sequence for
sets. 2015, arXiv preprint.arXiv:1511.06391https://figshare.com/articles/Graphs_of_materials_project/7451351Ong, S. P.; Cholia, S.; Jain, A.; Brafman, M.; Gunter, D.; Ceder, G.;
Persson, K. A. The Materials Application Programming Interface (API): A
simple, flexible and efficient API for materials data based on
REpresentational State Transfer (REST) principles. Comput. Mater. Sci. 2015,
97, 209–215 DOI:10.1016/j.commatsci.2014.10.037.Faber, F. A.; Hutchison, L.; Huang, B.; Gilmer, J.; Schoenholz, S. S.;
Dahl, G. E.; Vinyals, O.; Kearnes, S.; Riley, P. F.; von Lilienfeld, O. A.
Prediction errors of molecular machine learning models lower than hybrid DFT
error. Journal of Chemical Theory and Computation 2017, 13, 5255–5264.
DOI:10.1021/acs.jctc.7b00577