<jupyter_start><jupyter_text>Load sample data<jupyter_code>import json
with open('qm9_sample.json', 'r') as f:
    data = json.load(f)<jupyter_output><empty_output><jupyter_text>Process train_structures and train_targets<jupyter_code>from pymatgen import Molecule
from megnet.utils.molecule import MEGNetMolecule

qm9_ids = list(data.keys())

molecules = [Molecule.from_dict(data[i]['molecule']) for i in qm9_ids]  # this gives a list of pymatgen Molecule

structures = [MEGNetMolecule.from_pymatgen(i) for i in molecules]  # Wrap Molecule to MEGNetMolecule
targets = [data[i]['property']['U0'] for i in qm9_ids]  # We are training U0 here<jupyter_output>WARNING:root:Openbabel is needed for molecule models, try 'conda install -c openbabel openbabel' to install it<jupyter_text>Data splitsIn reality, use train-val-test splits. Validation split can be used to select hyperparameters (e.g., epochs)<jupyter_code>train_structures = structures[:80]
test_structures = structures[80:]
train_targets = targets[:80]
test_targets = targets[80:]<jupyter_output><empty_output><jupyter_text>Construct megnet models<jupyter_code>from megnet.models import MEGNetModel
from megnet.data.graph import GaussianDistance
from megnet.data.crystal import CrystalGraph
from megnet.utils.preprocessing import StandardScaler
import numpy as np
gc = CrystalGraph(bond_converter=GaussianDistance(np.linspace(0, 5, 100), 0.5), cutoff=4)
model = MEGNetModel(100, 2, graph_converter=gc)<jupyter_output><empty_output><jupyter_text>Scale targets to train per atom quantity<jupyter_code>INTENSIVE = False # U0 is an extensive quantity
scaler = StandardScaler.from_training_data(train_structures, train_targets, is_intensive=INTENSIVE)
model.target_scaler = scaler<jupyter_output><empty_output><jupyter_text>Train the model!<jupyter_code>model.train(train_structures, train_targets, epochs=10, verbose=2)  # In reality, use epochs>1000<jupyter_output>/Users/chenchi/miniconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/indexed_slices.py:424: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "<jupyter_text>Predict test data<jupyter_code>predicted_tests = []
for i in test_structures:
    predicted_tests.append(model.predict_structure(i).ravel()[0])
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(test_targets, predicted_tests, 'o')
plt.xlabel('True $U_0$ (eV)')
plt.ylabel('Predicted $U_0$ (eV)')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09867909867909867, 'fraction_numerical': 0.016705516705516704, 'mean_word_length': 7.879310344827586, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '418507', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 936, 'n_tokens_neox': 859, 'n_words': 203, 'path': 'notebooks/qm9_simple_model.ipynb', 'repo_name': 'Lalf-Klein/megnet', 'size': '2574'}