Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
torchani
Commits
76646a9c
Unverified
Commit
76646a9c
authored
Oct 28, 2018
by
Gao, Xiang
Committed by
GitHub
Oct 28, 2018
Browse files
Fix autograd for ASE calculator (#123)
parent
f9db30c3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
12 deletions
+40
-12
Dockerfile
Dockerfile
+1
-1
tests/test_energies.py
tests/test_energies.py
+9
-2
tests/test_forces.py
tests/test_forces.py
+22
-2
torchani/ase.py
torchani/ase.py
+8
-7
No files found.
Dockerfile
View file @
76646a9c
FROM
zasdfgbnm/pytorch-master
FROM
zasdfgbnm/pytorch-master
RUN
pacman
-Sy
--noconfirm
python-sphinx python2-sphinx python-tqdm python2-tqdm python2-matplotlib python-matplotlib python-pillow python2-pillow flake8
RUN
pacman
-Sy
--noconfirm
python-sphinx python2-sphinx python-tqdm python2-tqdm python2-matplotlib python-matplotlib python-pillow python2-pillow flake8
RUN
pip
install
tensorboardX sphinx-gallery
&&
pip2
install
tensorboardX sphinx-gallery
RUN
pip
install
tensorboardX sphinx-gallery
ase
&&
pip2
install
tensorboardX sphinx-gallery
ase
COPY
. /torchani
COPY
. /torchani
RUN
cd
torchani
&&
pip
install
.
RUN
cd
torchani
&&
pip
install
.
RUN
cd
torchani
&&
pip2
install
.
RUN
cd
torchani
&&
pip2
install
.
tests/test_energies.py
View file @
76646a9c
...
@@ -14,10 +14,10 @@ class TestEnergies(unittest.TestCase):
...
@@ -14,10 +14,10 @@ class TestEnergies(unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
tolerance
=
5e-5
self
.
tolerance
=
5e-5
builtins
=
torchani
.
neurochem
.
Builtins
()
builtins
=
torchani
.
neurochem
.
Builtins
()
aev_computer
=
builtins
.
aev_computer
self
.
aev_computer
=
builtins
.
aev_computer
nnp
=
builtins
.
models
[
0
]
nnp
=
builtins
.
models
[
0
]
shift_energy
=
builtins
.
energy_shifter
shift_energy
=
builtins
.
energy_shifter
self
.
model
=
torch
.
nn
.
Sequential
(
aev_computer
,
nnp
,
shift_energy
)
self
.
model
=
torch
.
nn
.
Sequential
(
self
.
aev_computer
,
nnp
,
shift_energy
)
def
testIsomers
(
self
):
def
testIsomers
(
self
):
for
i
in
range
(
N
):
for
i
in
range
(
N
):
...
@@ -45,5 +45,12 @@ class TestEnergies(unittest.TestCase):
...
@@ -45,5 +45,12 @@ class TestEnergies(unittest.TestCase):
self
.
assertLess
(
max_diff
,
self
.
tolerance
)
self
.
assertLess
(
max_diff
,
self
.
tolerance
)
class
TestEnergiesASEComputer
(
TestEnergies
):
def
setUp
(
self
):
super
(
TestEnergiesASEComputer
,
self
).
setUp
()
self
.
aev_computer
.
neighborlist
=
torchani
.
ase
.
NeighborList
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
tests/test_forces.py
View file @
76646a9c
...
@@ -13,15 +13,21 @@ class TestForce(unittest.TestCase):
...
@@ -13,15 +13,21 @@ class TestForce(unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
tolerance
=
1e-5
self
.
tolerance
=
1e-5
builtins
=
torchani
.
neurochem
.
Builtins
()
builtins
=
torchani
.
neurochem
.
Builtins
()
aev_computer
=
builtins
.
aev_computer
self
.
aev_computer
=
builtins
.
aev_computer
nnp
=
builtins
.
models
[
0
]
nnp
=
builtins
.
models
[
0
]
self
.
model
=
torch
.
nn
.
Sequential
(
aev_computer
,
nnp
)
self
.
model
=
torch
.
nn
.
Sequential
(
self
.
aev_computer
,
nnp
)
def
transform
(
self
,
x
):
return
x
def
testIsomers
(
self
):
def
testIsomers
(
self
):
for
i
in
range
(
N
):
for
i
in
range
(
N
):
datafile
=
os
.
path
.
join
(
path
,
'test_data/{}'
.
format
(
i
))
datafile
=
os
.
path
.
join
(
path
,
'test_data/{}'
.
format
(
i
))
with
open
(
datafile
,
'rb'
)
as
f
:
with
open
(
datafile
,
'rb'
)
as
f
:
coordinates
,
species
,
_
,
_
,
_
,
forces
=
pickle
.
load
(
f
)
coordinates
,
species
,
_
,
_
,
_
,
forces
=
pickle
.
load
(
f
)
coordinates
=
self
.
transform
(
coordinates
)
species
=
self
.
transform
(
species
)
forces
=
self
.
transform
(
forces
)
coordinates
.
requires_grad_
(
True
)
coordinates
.
requires_grad_
(
True
)
_
,
energies
=
self
.
model
((
species
,
coordinates
))
_
,
energies
=
self
.
model
((
species
,
coordinates
))
derivative
=
torch
.
autograd
.
grad
(
energies
.
sum
(),
derivative
=
torch
.
autograd
.
grad
(
energies
.
sum
(),
...
@@ -36,6 +42,9 @@ class TestForce(unittest.TestCase):
...
@@ -36,6 +42,9 @@ class TestForce(unittest.TestCase):
datafile
=
os
.
path
.
join
(
path
,
'test_data/{}'
.
format
(
i
))
datafile
=
os
.
path
.
join
(
path
,
'test_data/{}'
.
format
(
i
))
with
open
(
datafile
,
'rb'
)
as
f
:
with
open
(
datafile
,
'rb'
)
as
f
:
coordinates
,
species
,
_
,
_
,
_
,
forces
=
pickle
.
load
(
f
)
coordinates
,
species
,
_
,
_
,
_
,
forces
=
pickle
.
load
(
f
)
coordinates
=
self
.
transform
(
coordinates
)
species
=
self
.
transform
(
species
)
forces
=
self
.
transform
(
forces
)
coordinates
.
requires_grad_
(
True
)
coordinates
.
requires_grad_
(
True
)
species_coordinates
.
append
((
species
,
coordinates
))
species_coordinates
.
append
((
species
,
coordinates
))
coordinates_forces
.
append
((
coordinates
,
forces
))
coordinates_forces
.
append
((
coordinates
,
forces
))
...
@@ -50,5 +59,16 @@ class TestForce(unittest.TestCase):
...
@@ -50,5 +59,16 @@ class TestForce(unittest.TestCase):
self
.
assertLess
(
max_diff
,
self
.
tolerance
)
self
.
assertLess
(
max_diff
,
self
.
tolerance
)
class
TestForceASEComputer
(
TestForce
):
def
setUp
(
self
):
super
(
TestForceASEComputer
,
self
).
setUp
()
self
.
aev_computer
.
neighborlist
=
torchani
.
ase
.
NeighborList
()
def
transform
(
self
,
x
):
"""To reduce the size of test cases for faster test speed"""
return
x
[:
3
,
...]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
torchani/ase.py
View file @
76646a9c
...
@@ -40,17 +40,18 @@ class NeighborList:
...
@@ -40,17 +40,18 @@ class NeighborList:
atoms
=
s
.
shape
[
0
]
atoms
=
s
.
shape
[
0
]
atoms_object
=
ase
.
Atoms
(
atoms_object
=
ase
.
Atoms
(
'C'
*
atoms
,
# chemical symbols are not important here
'C'
*
atoms
,
# chemical symbols are not important here
positions
=
c
.
numpy
(),
positions
=
c
.
detach
().
numpy
(),
pbc
=
self
.
pbc
,
pbc
=
self
.
pbc
,
cell
=
self
.
cell
)
cell
=
self
.
cell
)
idx1
,
idx2
,
d
,
D
=
ase
.
neighborlist
.
neighbor_list
(
idx1
,
idx2
=
ase
.
neighborlist
.
neighbor_list
(
'ijdD'
,
atoms_object
,
cutoff
)
'ij'
,
atoms_object
,
cutoff
)
# NB: The absolute distance and distance vectors computed by
# `neighbor_list`can not be used since it does not preserve
# gradient information
idx1
=
torch
.
from_numpy
(
idx1
).
to
(
coordinates
.
device
)
idx1
=
torch
.
from_numpy
(
idx1
).
to
(
coordinates
.
device
)
idx2
=
torch
.
from_numpy
(
idx2
).
to
(
coordinates
.
device
)
idx2
=
torch
.
from_numpy
(
idx2
).
to
(
coordinates
.
device
)
d
=
torch
.
from_numpy
(
d
).
to
(
coordinates
.
device
)
\
D
=
c
.
index_select
(
0
,
idx2
)
-
c
.
index_select
(
0
,
idx1
)
.
to
(
coordinates
.
dtype
)
d
=
D
.
norm
(
2
,
-
1
)
D
=
torch
.
from_numpy
(
D
).
to
(
coordinates
.
device
)
\
.
to
(
coordinates
.
dtype
)
neighbor_species1
=
[]
neighbor_species1
=
[]
neighbor_distances1
=
[]
neighbor_distances1
=
[]
neighbor_vecs1
=
[]
neighbor_vecs1
=
[]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment