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
bade1181
Unverified
Commit
bade1181
authored
May 25, 2019
by
Gao, Xiang
Committed by
GitHub
May 25, 2019
Browse files
Create graph for force in force training (#238)
parent
c630982f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
4 deletions
+6
-4
examples/nnp_training_force.py
examples/nnp_training_force.py
+6
-4
No files found.
examples/nnp_training_force.py
View file @
bade1181
...
...
@@ -250,10 +250,12 @@ for _ in range(scheduler.last_epoch + 1, max_epochs):
_
,
chunk_energies
=
model
((
chunk_species
,
chunk_coordinates
))
# We can use torch.autograd.grad to compute force. Remember
# to retain graph so that we can backward through it a second
# time when computing gradient w.r.t. parameters.
chunk_forces
=
-
torch
.
autograd
.
grad
(
chunk_energies
.
sum
(),
chunk_coordinates
,
retain_graph
=
True
)[
0
]
# We can use torch.autograd.grad to compute force. Remember to
# create graph so that the loss of the force can contribute to
# the gradient of parameters, and also to retain graph so that
# we can backward through it a second time when computing gradient
# w.r.t. parameters.
chunk_forces
=
-
torch
.
autograd
.
grad
(
chunk_energies
.
sum
(),
chunk_coordinates
,
create_graph
=
True
,
retain_graph
=
True
)[
0
]
# Now let's compute loss for force of this chunk
chunk_force_loss
=
mse
(
chunk_true_forces
,
chunk_forces
).
sum
(
dim
=
(
1
,
2
))
/
chunk_num_atoms
...
...
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