
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/example_nn_SiC.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_example_nn_SiC.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_example_nn_SiC.py:


.. _tut_nn_multi_spec:

Train a neural network potential for SiC
========================================

In this tutorial, we train a neural network (NN) potential for a system containing two
species: Si and C. This is very similar to the training for systems containing a single
specie (take a look at :ref:`tut_nn` for Si if you haven't yet).

.. GENERATED FROM PYTHON SOURCE LINES 11-74

.. code-block:: default



    from kliff import nn
    from kliff.calculators.calculator_torch import CalculatorTorchSeparateSpecies
    from kliff.dataset import Dataset
    from kliff.descriptors import SymmetryFunction
    from kliff.loss import Loss
    from kliff.models import NeuralNetwork
    from kliff.utils import download_dataset

    descriptor = SymmetryFunction(
        cut_name="cos",
        cut_dists={"Si-Si": 5.0, "C-C": 5.0, "Si-C": 5.0},
        hyperparams="set51",
        normalize=True,
    )

    N1 = 10
    N2 = 10
    model_si = NeuralNetwork(descriptor)
    model_si.add_layers(
        # first hidden layer
        nn.Linear(descriptor.get_size(), N1),
        nn.Tanh(),
        # second hidden layer
        nn.Linear(N1, N2),
        nn.Tanh(),
        # output layer
        nn.Linear(N2, 1),
    )
    model_si.set_save_metadata(prefix="./kliff_saved_model_si", start=5, frequency=2)


    N1 = 10
    N2 = 10
    model_c = NeuralNetwork(descriptor)
    model_c.add_layers(
        # first hidden layer
        nn.Linear(descriptor.get_size(), N1),
        nn.Tanh(),
        # second hidden layer
        nn.Linear(N1, N2),
        nn.Tanh(),
        # output layer
        nn.Linear(N2, 1),
    )
    model_c.set_save_metadata(prefix="./kliff_saved_model_c", start=5, frequency=2)


    # training set
    dataset_path = download_dataset(dataset_name="SiC_training_set")
    tset = Dataset(dataset_path)
    configs = tset.get_configs()

    # calculator
    calc = CalculatorTorchSeparateSpecies({"Si": model_si, "C": model_c})
    calc.create(configs, reuse=False)

    # loss
    loss = Loss(calc, residual_data={"forces_weight": 0.3})
    result = loss.minimize(method="Adam", num_epochs=10, batch_size=4, lr=0.001)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    "SymmetryFunction" descriptor initialized.
    10 configurations read from /Users/mjwen/Applications/kliff/examples/SiC_training_set
    Found existing fingerprints "fingerprints.pkl".
    Delete existing fingerprints "fingerprints.pkl"
    Start generating fingerprints.
    Calculating mean and stdev.
    Pickling fingerprints to "fingerprints.pkl"
    Processing configuration: 0.
    Pickle 10 configurations finished.
    Finish generating fingerprints.
    Start minimization using optimization method: Adam.
    Epoch = 0       loss = 5.7247632980e+01
    Epoch = 1       loss = 5.7215625763e+01
    Epoch = 2       loss = 5.7186323166e+01
    Epoch = 3       loss = 5.7158138275e+01
    Epoch = 4       loss = 5.7130514145e+01
    Epoch = 5       loss = 5.7103128433e+01
    Epoch = 6       loss = 5.7075778961e+01
    Epoch = 7       loss = 5.7048318863e+01
    Epoch = 8       loss = 5.7020624161e+01
    Epoch = 9       loss = 5.6992567062e+01
    Epoch = 10      loss = 5.6973577499e+01
    Finish minimization using optimization method: Adam.




.. GENERATED FROM PYTHON SOURCE LINES 75-78

We can save the trained model to disk, and later can load it back if we want. We can
also write the trained model to a KIM model such that it can be used in other simulation
codes such as LAMMPS via the KIM API.

.. GENERATED FROM PYTHON SOURCE LINES 78-82

.. code-block:: default


    model_si.save("final_model_si.pkl")
    model_c.save("final_model_c.pkl")
    loss.save_optimizer_state("optimizer_stat.pkl")








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  1.113 seconds)


.. _sphx_glr_download_auto_examples_example_nn_SiC.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: example_nn_SiC.py <example_nn_SiC.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: example_nn_SiC.ipynb <example_nn_SiC.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
