Advanced Computing Platform for Theoretical Physics

commit大文件会使得服务器变得不稳定,请大家尽量只commit代码,不要commit大的文件。

Commit 8046b81f authored by Lei Wang's avatar Lei Wang
Browse files

more grad method

parent 7a91c2ad
...@@ -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 = 20 Niter = 30
A = torch.rand(N, N, dtype=torch.float64) A = torch.randn(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())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment