Advanced Computing Platform for Theoretical Physics
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
codes
playwithgrad
Commits
cc16404c
Commit
cc16404c
authored
Aug 23, 2019
by
Lei Wang
Browse files
more grad
parent
8046b81f
Changes
1
Show whitespace changes
Inline
Side-by-side
test_grad.py
View file @
cc16404c
...
...
@@ -24,7 +24,7 @@ lnZ = lnZ + torch.log(torch.trace(M))/(2**Niter)
impurity_grad
=
(
M
@
M
).
t
()
/
torch
.
trace
(
M
@
T
@
M
)
#(2) autograd on lnZ
lnZ
.
backward
()
lnZ
.
backward
(
retain_graph
=
True
)
lnZ_grad
=
T
.
grad
.
clone
()
#(3) direct compute dominant eigenvalue
...
...
@@ -39,7 +39,7 @@ exact_grad = ((T.grad + T.grad.t())/2) # need to symmetrize since it is an upper
#or, HF theorem
eigenvector_grad
=
torch
.
ger
(
v
[:,
-
1
],
v
[:,
-
1
])
/
w
[
-
1
]
# outer product of the leading eigenvector and its transpose
#(5) lnZ using e
nvio
ment
#(5) lnZ using e
igenvector environ
ment
w
,
v
=
torch
.
symeig
(
T
,
eigenvectors
=
True
)
T
.
grad
.
zero_
()
...
...
@@ -49,10 +49,22 @@ e = v[:, -1] #environment
#e = v[:, -1].detach() #environment
loss
=
torch
.
log
(
e
@
T
@
e
)
loss
.
backward
(
retain_graph
=
True
)
v_environment_grad
=
T
.
grad
.
clone
()
#(6) lnZ using matrix enviroment
T
.
grad
.
zero_
()
#try this
#e = M #environment
#and this
e
=
M
.
detach
()
#environment this will be the same as (1)
loss
=
torch
.
log
(
torch
.
trace
(
e
@
T
@
e
))
loss
.
backward
()
environment_grad
=
T
.
grad
.
clone
()
M_
environment_grad
=
T
.
grad
.
clone
()
print
((
impurity_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
lnZ_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
eigenvector_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
environment_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
v_environment_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
M_environment_grad
-
exact_grad
).
abs
().
max
().
item
())
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