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
8046b81f
Commit
8046b81f
authored
Aug 23, 2019
by
Lei Wang
Browse files
more grad method
parent
7a91c2ad
Changes
1
Hide whitespace changes
Inline
Side-by-side
test_grad.py
View file @
8046b81f
...
@@ -7,8 +7,8 @@ import torch
...
@@ -7,8 +7,8 @@ import torch
torch
.
manual_seed
(
42
)
torch
.
manual_seed
(
42
)
N
=
10
N
=
10
Niter
=
2
0
Niter
=
3
0
A
=
torch
.
rand
(
N
,
N
,
dtype
=
torch
.
float64
)
A
=
torch
.
rand
n
(
N
,
N
,
dtype
=
torch
.
float64
)
T
=
torch
.
nn
.
Parameter
(
A
@
A
.
t
())
# a symmetric positive definite transfer matrix
T
=
torch
.
nn
.
Parameter
(
A
@
A
.
t
())
# a symmetric positive definite transfer matrix
lnZ
=
0.0
lnZ
=
0.0
...
@@ -39,6 +39,20 @@ exact_grad = ((T.grad + T.grad.t())/2) # need to symmetrize since it is an upper
...
@@ -39,6 +39,20 @@ exact_grad = ((T.grad + T.grad.t())/2) # need to symmetrize since it is an upper
#or, HF theorem
#or, HF theorem
eigenvector_grad
=
torch
.
ger
(
v
[:,
-
1
],
v
[:,
-
1
])
/
w
[
-
1
]
# outer product of the leading eigenvector and its transpose
eigenvector_grad
=
torch
.
ger
(
v
[:,
-
1
],
v
[:,
-
1
])
/
w
[
-
1
]
# outer product of the leading eigenvector and its transpose
#(5) lnZ using envioment
w
,
v
=
torch
.
symeig
(
T
,
eigenvectors
=
True
)
T
.
grad
.
zero_
()
#try this
e
=
v
[:,
-
1
]
#environment
#and this
#e = v[:, -1].detach() #environment
loss
=
torch
.
log
(
e
@
T
@
e
)
loss
.
backward
()
environment_grad
=
T
.
grad
.
clone
()
print
((
impurity_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
impurity_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
lnZ_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
lnZ_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
eigenvector_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
eigenvector_grad
-
exact_grad
).
abs
().
max
().
item
())
print
((
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