.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/neurochem_trainer.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_neurochem_trainer.py: .. _neurochem-training: Train Neural Network Potential From NeuroChem Input File ======================================================== This example shows how to use TorchANI's NeuroChem trainer to read and run NeuroChem's training config file to train a neural network potential. .. GENERATED FROM PYTHON SOURCE LINES 13-14 To begin with, let's first import the modules we will use: .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: default import torchani import torch import os import sys import tqdm .. GENERATED FROM PYTHON SOURCE LINES 21-29 Now let's setup path for the dataset and NeuroChem input file. Note that these paths assumes the user run this script under the ``examples`` directory of TorchANI's repository. If you download this script, you should manually set the path of these files in your system before this script can run successfully. Also note that here for our demo purpose, we set both training set and validation set the ``ani_gdb_s01.h5`` in TorchANI's repository. This allows this program to finish very quick, because that dataset is very small. But this is wrong and should be avoided for any serious training. .. GENERATED FROM PYTHON SOURCE LINES 29-38 .. code-block:: default try: path = os.path.dirname(os.path.realpath(__file__)) except NameError: path = os.getcwd() cfg_path = os.path.join(path, '../tests/test_data/inputtrain.ipt') training_path = os.path.join(path, '../dataset/ani1-up_to_gdb4/ani_gdb_s01.h5') # noqa: E501 validation_path = os.path.join(path, '../dataset/ani1-up_to_gdb4/ani_gdb_s01.h5') # noqa: E501 .. GENERATED FROM PYTHON SOURCE LINES 39-40 We also need to set the device to run the training: .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: default device_str = 'cuda' if torch.cuda.is_available() else 'cpu' device = torch.device(device_str) trainer = torchani.neurochem.Trainer(cfg_path, device, True, 'runs') trainer.load_data(training_path, validation_path) .. rst-class:: sphx-glr-script-out .. code-block:: none => loading /home/runner/work/torchani/torchani/examples/../dataset/ani1-up_to_gdb4/ani_gdb_s01.h5, total molecules: 1 1/1 [==============================] - 0.0s 2/1 [============================================================] - 0.0s 3/1 [==========================================================================================] - 0.1s=> loading /home/runner/work/torchani/torchani/examples/../dataset/ani1-up_to_gdb4/ani_gdb_s01.h5, total molecules: 1 1/1 [==============================] - 0.0s 2/1 [============================================================] - 0.0s 3/1 [==========================================================================================] - 0.1s .. GENERATED FROM PYTHON SOURCE LINES 49-53 Once everything is set up, running NeuroChem is very easy. We simplify need a ``trainer.run()``. But here, in order for sphinx-gallery to be able to capture the output of tqdm, let's do some hacking first to make tqdm to print its progressbar to stdout. .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: default def my_tqdm(*args, **kwargs): return tqdm.tqdm(*args, **kwargs, file=sys.stdout) trainer.tqdm = my_tqdm .. GENERATED FROM PYTHON SOURCE LINES 60-61 Now, let's go! .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: default trainer.run() .. rst-class:: sphx-glr-script-out .. code-block:: none epoch 1: 0%| | 0/5 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: neurochem_trainer.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_