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
63cae6a0
Unverified
Commit
63cae6a0
authored
Oct 30, 2018
by
Gao, Xiang
Committed by
GitHub
Oct 30, 2018
Browse files
Code quality improvements (#137)
parent
45252be6
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
28 additions
and
35 deletions
+28
-35
.dockerignore
.dockerignore
+0
-1
Dockerfile
Dockerfile
+0
-6
tests/test_data.py
tests/test_data.py
+4
-4
torchani/ignite.py
torchani/ignite.py
+7
-7
torchani/neurochem/__init__.py
torchani/neurochem/__init__.py
+15
-15
torchani/nn.py
torchani/nn.py
+2
-2
No files found.
.dockerignore
deleted
100644 → 0
View file @
45252be6
.git
Dockerfile
deleted
100644 → 0
View file @
45252be6
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
pip
install
tensorboardX sphinx-gallery ase codecov nose
&&
pip2
install
tensorboardX sphinx-gallery ase codecov nose
COPY
. /torchani
RUN
cd
torchani
&&
pip
install
.
RUN
cd
torchani
&&
pip2
install
.
tests/test_data.py
View file @
63cae6a0
...
...
@@ -58,8 +58,8 @@ class TestData(unittest.TestCase):
def
testTensorShape
(
self
):
for
i
in
self
.
ds
:
input
,
output
=
i
species
,
coordinates
=
torchani
.
utils
.
pad_coordinates
(
input
)
input
_
,
output
=
i
species
,
coordinates
=
torchani
.
utils
.
pad_coordinates
(
input
_
)
energies
=
output
[
'energies'
]
self
.
assertEqual
(
len
(
species
.
shape
),
2
)
self
.
assertLessEqual
(
species
.
shape
[
0
],
batch_size
)
...
...
@@ -72,8 +72,8 @@ class TestData(unittest.TestCase):
def
testNoUnnecessaryPadding
(
self
):
for
i
in
self
.
ds
:
for
input
in
i
[
0
]:
species
,
_
=
input
for
input
_
in
i
[
0
]:
species
,
_
=
input
_
non_padding
=
(
species
>=
0
)[:,
-
1
].
nonzero
()
self
.
assertGreater
(
non_padding
.
numel
(),
0
)
...
...
torchani/ignite.py
View file @
63cae6a0
...
...
@@ -50,8 +50,8 @@ class DictLoss(_Loss):
self
.
key
=
key
self
.
loss
=
loss
def
forward
(
self
,
input
,
other
):
return
self
.
loss
(
input
[
self
.
key
],
other
[
self
.
key
])
def
forward
(
self
,
input
_
,
other
):
return
self
.
loss
(
input
_
[
self
.
key
],
other
[
self
.
key
])
class
PerAtomDictLoss
(
DictLoss
):
...
...
@@ -60,9 +60,9 @@ class PerAtomDictLoss(DictLoss):
by the caller. Currently the only reduce operation supported is averaging.
"""
def
forward
(
self
,
input
,
other
):
loss
=
self
.
loss
(
input
[
self
.
key
],
other
[
self
.
key
])
num_atoms
=
(
input
[
'species'
]
>=
0
).
sum
(
dim
=
1
)
def
forward
(
self
,
input
_
,
other
):
loss
=
self
.
loss
(
input
_
[
self
.
key
],
other
[
self
.
key
])
num_atoms
=
(
input
_
[
'species'
]
>=
0
).
sum
(
dim
=
1
)
loss
/=
num_atoms
.
to
(
loss
.
dtype
).
to
(
loss
.
device
)
n
=
loss
.
numel
()
return
loss
.
sum
()
/
n
...
...
@@ -102,8 +102,8 @@ class TransformedLoss(_Loss):
self
.
origin
=
origin
self
.
transform
=
transform
def
forward
(
self
,
input
,
other
):
return
self
.
transform
(
self
.
origin
(
input
,
other
))
def
forward
(
self
,
input
_
,
other
):
return
self
.
transform
(
self
.
origin
(
input
_
,
other
))
def
RMSEMetric
(
key
):
...
...
torchani/neurochem/__init__.py
View file @
63cae6a0
...
...
@@ -101,11 +101,11 @@ def load_atomic_network(filename):
"""Returns an instance of :class:`torch.nn.Sequential` with hyperparameters
and parameters loaded NeuroChem's .nnf, .wparam and .bparam files."""
def
decompress_nnf
(
buffer
):
while
buffer
[
0
]
!=
b
'='
[
0
]:
buffer
=
buffer
[
1
:]
buffer
=
buffer
[
2
:]
return
bz2
.
decompress
(
buffer
)[:
-
1
].
decode
(
'ascii'
).
strip
()
def
decompress_nnf
(
buffer
_
):
while
buffer
_
[
0
]
!=
b
'='
[
0
]:
buffer
_
=
buffer
_
[
1
:]
buffer
_
=
buffer
_
[
2
:]
return
bz2
.
decompress
(
buffer
_
)[:
-
1
].
decode
(
'ascii'
).
strip
()
def
parse_nnf
(
nnf_file
):
# parse input file
...
...
@@ -200,9 +200,9 @@ def load_atomic_network(filename):
networ_dir
=
os
.
path
.
dirname
(
filename
)
with
open
(
filename
,
'rb'
)
as
f
:
buffer
=
f
.
read
()
buffer
=
decompress_nnf
(
buffer
)
layer_setups
=
parse_nnf
(
buffer
)
buffer
_
=
f
.
read
()
buffer
_
=
decompress_nnf
(
buffer
_
)
layer_setups
=
parse_nnf
(
buffer
_
)
layers
=
[]
for
s
in
layer_setups
:
...
...
@@ -225,18 +225,18 @@ def load_atomic_network(filename):
return
torch
.
nn
.
Sequential
(
*
layers
)
def
load_model
(
species
,
dir
):
def
load_model
(
species
,
dir
_
):
"""Returns an instance of :class:`torchani.ANIModel` loaded from
NeuroChem's network directory.
Arguments:
species (:class:`collections.abc.Sequence`): Sequence of strings for
chemical symbols of each supported atom type in correct order.
dir (str): String for directory storing network configurations.
dir
_
(str): String for directory storing network configurations.
"""
models
=
[]
for
i
in
species
:
filename
=
os
.
path
.
join
(
dir
,
'ANN-{}.nnf'
.
format
(
i
))
filename
=
os
.
path
.
join
(
dir
_
,
'ANN-{}.nnf'
.
format
(
i
))
models
.
append
(
load_atomic_network
(
filename
))
return
ANIModel
(
models
)
...
...
@@ -439,7 +439,7 @@ class Trainer:
return
TreeExec
().
transform
(
tree
)
def
_construct
(
self
,
network_setup
,
params
):
dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
self
.
filename
))
dir
_
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
self
.
filename
))
# delete ignored params
def
del_if_exists
(
key
):
...
...
@@ -468,14 +468,14 @@ class Trainer:
assert_param
(
'ntwshr'
,
0
)
# load parameters
self
.
const_file
=
os
.
path
.
join
(
dir
,
params
[
'sflparamsfile'
])
self
.
const_file
=
os
.
path
.
join
(
dir
_
,
params
[
'sflparamsfile'
])
self
.
consts
=
Constants
(
self
.
const_file
)
self
.
aev_computer
=
AEVComputer
(
**
self
.
consts
)
del
params
[
'sflparamsfile'
]
self
.
sae_file
=
os
.
path
.
join
(
dir
,
params
[
'atomEnergyFile'
])
self
.
sae_file
=
os
.
path
.
join
(
dir
_
,
params
[
'atomEnergyFile'
])
self
.
shift_energy
=
load_sae
(
self
.
sae_file
)
del
params
[
'atomEnergyFile'
]
network_dir
=
os
.
path
.
join
(
dir
,
params
[
'ntwkStoreDir'
])
network_dir
=
os
.
path
.
join
(
dir
_
,
params
[
'ntwkStoreDir'
])
if
not
os
.
path
.
exists
(
network_dir
):
os
.
makedirs
(
network_dir
)
self
.
model_checkpoint
=
os
.
path
.
join
(
network_dir
,
self
.
checkpoint_name
)
...
...
torchani/nn.py
View file @
63cae6a0
...
...
@@ -43,8 +43,8 @@ class ANIModel(torch.nn.ModuleList):
dtype
=
aev
.
dtype
)
for
i
in
present_species
:
mask
=
(
species_
==
i
)
input
=
aev
.
index_select
(
0
,
mask
.
nonzero
().
squeeze
())
output
.
masked_scatter_
(
mask
,
self
[
i
](
input
).
squeeze
())
input
_
=
aev
.
index_select
(
0
,
mask
.
nonzero
().
squeeze
())
output
.
masked_scatter_
(
mask
,
self
[
i
](
input
_
).
squeeze
())
output
=
output
.
view_as
(
species
)
return
species
,
self
.
reducer
(
output
,
dim
=
1
)
...
...
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