Created
May 31, 2018 09:23
-
-
Save stiv-yakovenko/f35e31cbf076b6a1359cf43afa5040f8 to your computer and use it in GitHub Desktop.
rule110
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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