Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rule110
import numpy as np
import matplotlib.pyplot as plt
import torch
from torch.autograd import Variable
def triangle(x,y,z,v0):
v=(y + y * y + y * y * y - 3. * (1. + x) * y * z + z * (1. + z + z * z)) / 3.
return (v-v0)*(v-v0)
W=15
cnt=0
def eval():
global cnt
s = 0.
for i in range(W - 1):
for j in range(1, W + 1):
xx = x[i, (j - 1) % W]
yy = x[i, j % W]
zz = x[i, (j + 1) % W]
r = x[i + 1, j % W]
s += triangle(xx, yy, zz, r)
for j in range(W - 1): s += x[0, j] * x[0, j]
s += (1 - x[0, W - 1]) * (1 - x[0, W - 1])
cnt=cnt+1
if (cnt%100==1):
plt.ion()
plt.imshow(np.array(x.data.numpy()))
if (cnt==1):plt.show()
plt.pause(0.0001)
print(cnt," s=", s)
return torch.sqrt(s)
x = Variable(torch.DoubleTensor(W,W).zero_(), requires_grad=True)
opt = torch.optim.LBFGS([x],lr=.1)
for i in range(15500):
def closure():
opt.zero_grad()
s=eval()
s.backward()
return s
opt.step(closure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.