-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial.py
41 lines (28 loc) · 825 Bytes
/
initial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import torch
import torch.optim as optim
from dataset import Dataset
from objective_functions import DistToLines2D
import shapes
# torch.set_printoptions(precision=5)
# torch.random.manual_seed(5367838)
n_samples = 100
points = torch.empty(n_samples, 2).uniform_(0, 1)
ds = Dataset(points)
lines = shapes.star()
# sum of distances
sod = DistToLines2D(lines)
optimizer = optim.SGD(ds.parameters(), lr=1e-2)
def iteration(i):
optimizer.zero_grad()
error = sod(ds())
error.backward()
optimizer.step()
particules = ds.points.detach().numpy()
plt.cla()
plt.scatter(particules[:, 0], particules[:, 1])
plt.xlim((0, 1))
plt.ylim((0, 1))
ani = FuncAnimation(plt.gcf(), iteration, interval=100)
plt.show()