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
2cb357bd
Unverified
Commit
2cb357bd
authored
Jul 31, 2018
by
Gao, Xiang
Committed by
GitHub
Jul 31, 2018
Browse files
make aev computer accept species as tensor (#38)
parent
629bc698
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
26 deletions
+29
-26
tests/test_aev.py
tests/test_aev.py
+1
-0
tests/test_benchmark.py
tests/test_benchmark.py
+2
-1
tests/test_ensemble.py
tests/test_ensemble.py
+0
-1
tests/test_ignite.py
tests/test_ignite.py
+1
-1
torchani/aev.py
torchani/aev.py
+14
-15
torchani/models/ani_model.py
torchani/models/ani_model.py
+11
-8
No files found.
tests/test_aev.py
View file @
2cb357bd
...
@@ -16,6 +16,7 @@ class TestAEV(unittest.TestCase):
...
@@ -16,6 +16,7 @@ class TestAEV(unittest.TestCase):
def
_test_molecule
(
self
,
coordinates
,
species
,
expected_radial
,
def
_test_molecule
(
self
,
coordinates
,
species
,
expected_radial
,
expected_angular
):
expected_angular
):
species
=
self
.
aev
.
species_to_tensor
(
species
)
aev
=
self
.
aev
((
coordinates
,
species
))
aev
=
self
.
aev
((
coordinates
,
species
))
radial
=
aev
[...,
:
self
.
aev
.
radial_length
]
radial
=
aev
[...,
:
self
.
aev
.
radial_length
]
angular
=
aev
[...,
self
.
aev
.
radial_length
:]
angular
=
aev
[...,
self
.
aev
.
radial_length
:]
...
...
tests/test_benchmark.py
View file @
2cb357bd
...
@@ -42,7 +42,8 @@ class TestBenchmark(unittest.TestCase):
...
@@ -42,7 +42,8 @@ class TestBenchmark(unittest.TestCase):
old_timers
=
copy
.
copy
(
module
.
timers
)
old_timers
=
copy
.
copy
(
module
.
timers
)
for
_
in
range
(
self
.
count
):
for
_
in
range
(
self
.
count
):
if
isinstance
(
module
,
torchani
.
aev
.
AEVComputer
):
if
isinstance
(
module
,
torchani
.
aev
.
AEVComputer
):
module
((
self
.
coordinates
,
self
.
species
))
species
=
module
.
species_to_tensor
(
self
.
species
)
module
((
self
.
coordinates
,
species
))
else
:
else
:
module
(
self
.
coordinates
,
self
.
species
)
module
(
self
.
coordinates
,
self
.
species
)
for
i
in
keys
:
for
i
in
keys
:
...
...
tests/test_ensemble.py
View file @
2cb357bd
...
@@ -18,7 +18,6 @@ class TestEnsemble(unittest.TestCase):
...
@@ -18,7 +18,6 @@ class TestEnsemble(unittest.TestCase):
n
=
torchani
.
buildin_ensemble
n
=
torchani
.
buildin_ensemble
prefix
=
torchani
.
buildin_model_prefix
prefix
=
torchani
.
buildin_model_prefix
aev
=
torchani
.
SortedAEV
(
device
=
torch
.
device
(
'cpu'
))
aev
=
torchani
.
SortedAEV
(
device
=
torch
.
device
(
'cpu'
))
coordinates
,
species
=
aev
.
sort_by_species
(
coordinates
,
species
)
ensemble
=
torchani
.
models
.
NeuroChemNNP
(
aev
,
derivative
=
True
,
ensemble
=
torchani
.
models
.
NeuroChemNNP
(
aev
,
derivative
=
True
,
ensemble
=
True
)
ensemble
=
True
)
models
=
[
torchani
.
models
.
models
=
[
torchani
.
models
.
...
...
tests/test_ignite.py
View file @
2cb357bd
...
@@ -11,7 +11,7 @@ if sys.version_info.major >= 3:
...
@@ -11,7 +11,7 @@ if sys.version_info.major >= 3:
path
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
path
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
path
=
os
.
path
.
join
(
path
,
'../dataset/ani_gdb_s01.h5'
)
path
=
os
.
path
.
join
(
path
,
'../dataset/ani_gdb_s01.h5'
)
chunksize
=
8
chunksize
=
4
threshold
=
1e-5
threshold
=
1e-5
dtype
=
torch
.
float32
dtype
=
torch
.
float32
device
=
torch
.
device
(
'cpu'
)
device
=
torch
.
device
(
'cpu'
)
...
...
torchani/aev.py
View file @
2cb357bd
import
torch
import
torch
import
itertools
import
itertools
import
numpy
import
math
from
.env
import
buildin_const_file
,
default_dtype
,
default_device
from
.env
import
buildin_const_file
,
default_dtype
,
default_device
from
.benchmarked
import
BenchmarkedModule
from
.benchmarked
import
BenchmarkedModule
...
@@ -89,26 +89,26 @@ class AEVComputer(BenchmarkedModule):
...
@@ -89,26 +89,26 @@ class AEVComputer(BenchmarkedModule):
self
.
ShfA
=
self
.
ShfA
.
view
(
1
,
1
,
-
1
,
1
)
self
.
ShfA
=
self
.
ShfA
.
view
(
1
,
1
,
-
1
,
1
)
self
.
ShfZ
=
self
.
ShfZ
.
view
(
1
,
1
,
1
,
-
1
)
self
.
ShfZ
=
self
.
ShfZ
.
view
(
1
,
1
,
1
,
-
1
)
def
sort_by_species
(
self
,
data
,
species
):
def
sort_by_species
(
self
,
specie
s
,
*
tensor
s
):
"""Sort the data by its species according to the order in `self.species`
"""Sort the data by its species according to the order in `self.species`
Parameters
Parameters
----------
----------
data
: torch.Tensor
species
: torch.Tensor
Tensor
of shape (conformations, atoms, ...) for data
.
Tensor
storing species of each atom
.
species : list
*tensors : tuple
List storing species of each atom
.
Tensors of shape (conformations, atoms, ...) for data
.
Returns
Returns
-------
-------
(
torch.Tensor, list
)
(
species, ...
)
T
uple of (sorted data,
sorted species
)
.
T
ensors
sorted
by
species.
"""
"""
atoms
=
list
(
zip
(
species
,
torch
.
unbind
(
data
,
1
))
)
species
,
reverse
=
torch
.
sort
(
species
)
atoms
=
sorted
(
atoms
,
key
=
lambda
x
:
self
.
species
.
index
(
x
[
0
]))
new_tensors
=
[]
species
=
[
s
for
s
,
_
in
atoms
]
for
t
in
tensors
:
data
=
torch
.
stack
([
c
for
_
,
c
in
atoms
],
dim
=
1
)
new_tensors
.
append
(
t
.
index_select
(
1
,
reverse
)
)
return
data
,
species
return
(
species
,
*
tensors
)
def
forward
(
self
,
coordinates_species
):
def
forward
(
self
,
coordinates_species
):
"""Compute AEV from coordinates and species
"""Compute AEV from coordinates and species
...
@@ -158,7 +158,7 @@ def _cutoff_cosine(distances, cutoff):
...
@@ -158,7 +158,7 @@ def _cutoff_cosine(distances, cutoff):
"""
"""
return
torch
.
where
(
return
torch
.
where
(
distances
<=
cutoff
,
distances
<=
cutoff
,
0.5
*
torch
.
cos
(
numpy
.
pi
*
distances
/
cutoff
)
+
0.5
,
0.5
*
torch
.
cos
(
math
.
pi
*
distances
/
cutoff
)
+
0.5
,
torch
.
zeros_like
(
distances
)
torch
.
zeros_like
(
distances
)
)
)
...
@@ -484,7 +484,6 @@ class SortedAEV(AEVComputer):
...
@@ -484,7 +484,6 @@ class SortedAEV(AEVComputer):
def
forward
(
self
,
coordinates_species
):
def
forward
(
self
,
coordinates_species
):
coordinates
,
species
=
coordinates_species
coordinates
,
species
=
coordinates_species
species
=
self
.
species_to_tensor
(
species
)
present_species
=
species
.
unique
(
sorted
=
True
)
present_species
=
species
.
unique
(
sorted
=
True
)
radial_terms
,
angular_terms
,
indices_r
,
indices_a
=
\
radial_terms
,
angular_terms
,
indices_r
,
indices_a
=
\
...
...
torchani/models/ani_model.py
View file @
2cb357bd
...
@@ -82,8 +82,8 @@ class ANIModel(BenchmarkedModule):
...
@@ -82,8 +82,8 @@ class ANIModel(BenchmarkedModule):
aev : torch.Tensor
aev : torch.Tensor
Pytorch tensor of shape (conformations, atoms, aev_length) storing
Pytorch tensor of shape (conformations, atoms, aev_length) storing
the computed AEVs.
the computed AEVs.
species :
list of string
species :
torch.Tensor
List of string
storing the species for each atom.
Tensor
storing the species for each atom.
Returns
Returns
-------
-------
...
@@ -93,17 +93,19 @@ class ANIModel(BenchmarkedModule):
...
@@ -93,17 +93,19 @@ class ANIModel(BenchmarkedModule):
"""
"""
conformations
=
aev
.
shape
[
0
]
conformations
=
aev
.
shape
[
0
]
atoms
=
len
(
species
)
atoms
=
len
(
species
)
rev_species
=
species
[::
-
1
]
rev_species
=
species
.
__reversed__
()
species_dedup
=
sorted
(
species_dedup
=
species
.
unique
()
set
(
species
),
key
=
self
.
aev_computer
.
species
.
index
)
per_species_outputs
=
[]
per_species_outputs
=
[]
species
=
species
.
tolist
()
rev_species
=
rev_species
.
tolist
()
for
s
in
species_dedup
:
for
s
in
species_dedup
:
begin
=
species
.
index
(
s
)
begin
=
species
.
index
(
s
)
end
=
atoms
-
rev_species
.
index
(
s
)
end
=
atoms
-
rev_species
.
index
(
s
)
y
=
aev
[:,
begin
:
end
,
:].
reshape
(
-
1
,
self
.
aev_computer
.
aev_length
)
y
=
aev
[:,
begin
:
end
,
:].
reshape
(
-
1
,
self
.
aev_computer
.
aev_length
)
def
apply_model
(
suffix
):
def
apply_model
(
suffix
):
model_X
=
getattr
(
self
,
'model_'
+
s
+
suffix
)
model_X
=
getattr
(
self
,
'model_'
+
self
.
aev_computer
.
species
[
s
]
+
suffix
)
return
model_X
(
y
)
return
model_X
(
y
)
ys
=
[
apply_model
(
suffix
)
for
suffix
in
self
.
suffixes
]
ys
=
[
apply_model
(
suffix
)
for
suffix
in
self
.
suffixes
]
y
=
sum
(
ys
)
/
len
(
ys
)
y
=
sum
(
ys
)
/
len
(
ys
)
...
@@ -145,12 +147,13 @@ class ANIModel(BenchmarkedModule):
...
@@ -145,12 +147,13 @@ class ANIModel(BenchmarkedModule):
when the derivative is off, and the second tensor is a tensor of
when the derivative is off, and the second tensor is a tensor of
shape (conformation, atoms, 3) storing the d(output)/dR.
shape (conformation, atoms, 3) storing the d(output)/dR.
"""
"""
species
=
self
.
aev_computer
.
species_to_tensor
(
species
)
if
not
self
.
derivative
:
if
not
self
.
derivative
:
coordinates
=
coordinates
.
detach
()
coordinates
=
coordinates
.
detach
()
else
:
else
:
coordinates
=
torch
.
tensor
(
coordinates
,
requires_grad
=
True
)
coordinates
=
torch
.
tensor
(
coordinates
,
requires_grad
=
True
)
_coordinates
,
_species
=
self
.
aev_computer
.
sort_by_species
(
_species
,
_coordinates
,
=
self
.
aev_computer
.
sort_by_species
(
coordinat
es
,
speci
es
)
species
,
coordinates
)
aev
=
self
.
aev_computer
((
_coordinates
,
_species
))
aev
=
self
.
aev_computer
((
_coordinates
,
_species
))
output
=
self
.
aev_to_output
(
aev
,
_species
)
output
=
self
.
aev_to_output
(
aev
,
_species
)
if
not
self
.
derivative
:
if
not
self
.
derivative
:
...
...
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