Skip to content

Commit

Permalink
Fix CalculatorTorchSeparateSpecies zeta is [] bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mjwen committed Feb 14, 2020
1 parent 9863d0c commit 6e0b821
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 14 deletions.
Binary file modified docs/source/auto_examples/auto_examples_jupyter.zip
Binary file not shown.
Binary file modified docs/source/auto_examples/auto_examples_python.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions docs/source/auto_examples/example_nn_Si.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
"source": [
"model.save(\"./final_model.pkl\")\nloss.save_optimizer_stat(\"./optimizer_stat.pkl\")\n\nmodel.write_kim_model()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\"><h4>Note</h4><p>Now we have trained an NN for a single specie Si. If you have multiple species in\n your system and want to use different parameters for different species,\n take a look at the SiC_ example.</p></div>\n\n\n"
]
}
],
"metadata": {
Expand Down
9 changes: 9 additions & 0 deletions docs/source/auto_examples/example_nn_Si.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@
loss.save_optimizer_stat("./optimizer_stat.pkl")

model.write_kim_model()


##########################################################################################
# .. note::
# Now we have trained an NN for a single specie Si. If you have multiple species in
# your system and want to use different parameters for different species,
# take a look at the SiC_ example.
#
# .. _SiC: https://github.com/mjwen/kliff/blob/master/examples/eg_nn_SiC.py
2 changes: 1 addition & 1 deletion docs/source/auto_examples/example_nn_Si.py.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
99c760f443633dcf0ce755d5995447f7
6baccb59d251b6e52d886f0ec66aa7ae
11 changes: 10 additions & 1 deletion docs/source/auto_examples/example_nn_Si.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ codes such as LAMMPS via the KIM API.
.. rst-class:: sphx-glr-script-out

Out:
Expand All @@ -253,10 +255,17 @@ codes such as LAMMPS via the KIM API.
.. note::
Now we have trained an NN for a single specie Si. If you have multiple species in
your system and want to use different parameters for different species,
take a look at the SiC_ example.

.. _SiC: https://github.com/mjwen/kliff/blob/master/examples/eg_nn_SiC.py


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

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


.. _sphx_glr_download_auto_examples_example_nn_Si.py:
Expand Down
Binary file modified docs/source/auto_examples/example_nn_Si_codeobj.pickle
Binary file not shown.
8 changes: 4 additions & 4 deletions docs/source/auto_examples/sg_execution_times.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

Computation times
=================
**01:57.516** total execution time for **auto_examples** files:
**00:16.575** total execution time for **auto_examples** files:

+-----------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_example_kim_SW_Si.py` (``example_kim_SW_Si.py``) | 01:40.195 | 0.0 MB |
| :ref:`sphx_glr_auto_examples_example_nn_Si.py` (``example_nn_Si.py``) | 00:16.575 | 0.0 MB |
+-----------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_example_nn_Si.py` (``example_nn_Si.py``) | 00:16.523 | 0.0 MB |
| :ref:`sphx_glr_auto_examples_example_kim_SW_Si.py` (``example_kim_SW_Si.py``) | 00:00.000 | 0.0 MB |
+-----------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_example_linear_regression.py` (``example_linear_regression.py``) | 00:00.798 | 0.0 MB |
| :ref:`sphx_glr_auto_examples_example_linear_regression.py` (``example_linear_regression.py``) | 00:00.000 | 0.0 MB |
+-----------------------------------------------------------------------------------------------+-----------+--------+
4 changes: 2 additions & 2 deletions examples/eg_nn_SiC.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
.. _tut_nn:
.. _tut_nn_multi_spec:
Train a neural network potential
================================
In this tutorial, we train a neural network (NN) potential for silicon
In this tutorial, we train a neural network (NN) potential for SiC
"""


Expand Down
9 changes: 9 additions & 0 deletions examples/example_nn_Si.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@
loss.save_optimizer_stat("./optimizer_stat.pkl")

model.write_kim_model()


##########################################################################################
# .. note::
# Now we have trained an NN for a single specie Si. If you have multiple species in
# your system and want to use different parameters for different species,
# take a look at the SiC_ example.
#
# .. _SiC: https://github.com/mjwen/kliff/blob/master/examples/eg_nn_SiC.py
2 changes: 1 addition & 1 deletion kliff/calculators/calculator_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def compute(self, batch):
for s, zeta in zeta_by_species.items():

# have no species "s" in this batch of data
if zeta is []:
if not zeta: # zeta == []
continue

z_tensor = torch.stack(zeta) # convert a list of tensor to tensor
Expand Down
4 changes: 2 additions & 2 deletions kliff/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def forward(self, input):
shape_4D = (1, *shape, 1)
elif dim == 3:
if shape[0] != 1:
raise Exception('Shape[0] needs to be 1 for a 3D tensor.')
raise Exception("Shape[0] needs to be 1 for a 3D tensor.")
shape_4D = (*shape, 1)

else:
raise Exception(
'Input need to be 2D or 3D tensor, but got a ' '{}D tensor.'.format(dim)
"Input need to be 2D or 3D tensor, but got a " "{}D tensor.".format(dim)
)
x = torch.reshape(input, shape_4D)
x = torch.transpose(x, 1, 2)
Expand Down
6 changes: 3 additions & 3 deletions kliff/wrapcalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _update_params(self):
"""Write parameters to file KIM_MODEL_PARAMS + modelname, and also give its path to the
enviroment variable that has the same name: KIM_MODEL_PARAMS + modelname.
"""
name = 'KIM_MODEL_PARAMS_' + self.params._modelname
name = "KIM_MODEL_PARAMS_" + self.params._modelname
self.params.echo_params(name, print_size=True)
path = os.getcwd() + os.path.sep + name
os.environ[name] = path
Expand All @@ -59,13 +59,13 @@ def _parse_edn(self, fname, keys):
Keyword in the edn format(think it as a dictionary), whose value will be returned.
"""

with open(fname, 'r') as fin:
with open(fname, "r") as fin:
lines = fin.read()
parsed = edn_format.loads(lines)
values = []
for k in keys:
try:
v = parsed[k]['source-value']
v = parsed[k]["source-value"]
except KeyError:
raise KeyError('Keyword "{}" not found in {}.'.format(k, self.outname))
# make it a 1D array
Expand Down

0 comments on commit 6e0b821

Please sign in to comment.