import numpy as np
import matplotlib.pyplot as plt
import torch
import seaborn as sns
from functools import partial
sns.reset_defaults()="talk", font_scale=1)
sns.set_context(context%matplotlib inline
%config InlineBackend.figure_format='retina'
Basic Imports
= torch.linspace(-5, 5, 200).unsqueeze(-1)
X = torch.linspace(-5, 5, 200).unsqueeze(0)
Y = torch.Size((X.shape[0], Y.shape[1]))
shape = X.expand(shape)
X = Y.expand(shape) Y
def f(x, y):
return torch.abs(x+y) + 3*torch.abs(y-x)
1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31])
plt.contourf(X.numpy(), Y.numpy(), f(X, Y).numpy(), ["x")
plt.xlabel("y")
plt.ylabel(
plt.colorbar()
"equal") plt.gca().set_aspect(
def plot_point(x, y):
=[1, 4, 7, 10, 13, 16, 19] )
plt.contour(X.numpy(), Y.numpy(), f(X, Y).numpy(), levels
=5, color='k', s=100)
plt.scatter(x, y, zorder"x")
plt.xlabel("y")
plt.ylabel("equal") plt.gca().set_aspect(
= -4, 2
x_start, y_start plot_point(x_start, y_start)
Fix x2 = 2
= -4, 2
x_start, y_start
plot_point(x_start, y_start)2, color='red') plt.axhline(
= partial(f, y=2)
g g
functools.partial(<function f at 0x136323310>, y=2)
= torch.tensor(0., requires_grad = True)
x_learn = torch.optim.Adam([x_learn], lr=0.0005)
optim
for i in range(3000):
= g(x_learn)
loss
loss.backward()
optim.step()if i%100==0:
print(i, x_learn.item(), loss.item())
0 0.0004999999655410647 8.0
100 0.06779850274324417 7.865949630737305
200 0.14714795351028442 7.707312107086182
300 0.22784578800201416 7.545924663543701
400 0.3085353970527649 7.384539604187012
500 0.38880792260169983 7.223984718322754
600 0.4684963822364807 7.0645952224731445
700 0.5475249886512756 6.906523704528809
800 0.6258596777915955 6.74984073638916
900 0.7034878730773926 6.594569683074951
1000 0.7804093360900879 6.440712928771973
1100 0.8566311597824097 6.288255214691162
1200 0.9321646094322205 6.137174606323242
1300 1.0070239305496216 5.987442493438721
1400 1.0812252759933472 5.839026927947998
1500 1.1547856330871582 5.691894054412842
1600 1.2277231216430664 5.546006679534912
1700 1.3000539541244507 5.401332855224609
1800 1.371799349784851 5.257830619812012
1900 1.4429757595062256 5.115466594696045
2000 1.5136009454727173 4.974205493927002
2100 1.583693265914917 4.834010124206543
2200 1.6532690525054932 4.694848537445068
2300 1.7223458290100098 4.556684970855713
2400 1.7909395694732666 4.419487953186035
2500 1.8590670824050903 4.283223628997803
2600 1.926743507385254 4.147861957550049
2700 1.9939842224121094 4.01337194442749
2800 2.058466672897339 4.231417655944824
2900 2.11668062210083 4.464505195617676
Hard to optimize!
= torch.linspace(-5, 5, 400)
x_dummy
plt.plot(x_dummy, g(x_dummy))f"f(x, 2)")
plt.ylabel("x") plt.xlabel(
Text(0.5, 0, 'x')
= 2, 2
x_start, y_start
plot_point(x_start, y_start)2, color='red') plt.axvline(
https://stats.stackexchange.com/questions/146317/coordinate-vs-gradient-descent