-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralsLFBGS.py
More file actions
176 lines (122 loc) · 4 KB
/
Copy pathspiralsLFBGS.py
File metadata and controls
176 lines (122 loc) · 4 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
import matplotlib.colors as mcolors
import numpy as np
import matplotlib.pyplot as plt
#parameters
R = 4
k = 8
N = R
S = k * N
Nt = S * N
# inizialization
x0 = 0
y0 = 0
X = np.zeros((N, S))
Y = np.zeros((N, S))
Regions = np.zeros((N, S))
#coord
for s in range(1, S + 1):
r = np.floor((s - 1) * R / S + 1)
for n in range(1, N + 1):
a0 = n / N * 2 * np.pi / k
a = a0 + s * 2 * np.pi / S
b = a0
X[n-1, s-1] = b * np.cos(a) + x0
Y[n-1, s-1] = b * np.sin(a) + y0
Regions[n-1, s-1] = r
Coordinates = np.vstack((X.flatten('F'), Y.flatten('F')))
Regions_flat = Regions.flatten('F')
# draw spiral
plt.figure(figsize=(6, 6), facecolor='white')
plt.axis('off')
colors = ['m', 'g', 'c', 'y']
for r in range(1, R + 1):
I = (Regions_flat == r)
plt.plot(Coordinates[0, I], Coordinates[1, I], 'o',
markeredgecolor='k',
markerfacecolor=colors[r-1],
markersize=6)
plt.axis('equal')
plt.title('Dane Treningowe - Spirale ')
plt.show()
# one hot encoding
Probabilities = np.zeros((R, Nt))
for n in range(Nt):
Probabilities[int(Regions_flat[n]) - 1, n] = 1
X_tensor = torch.tensor(Coordinates.T, dtype=torch.float32)
Y_tensor = torch.tensor(Probabilities.T, dtype=torch.float32)
#model
model = nn.Sequential(
nn.Linear(2, 16),
nn.Tanh(),
nn.Linear(16, 8),
nn.Tanh(),
nn.Linear(8, R),
nn.Tanh(),
nn.Linear(R, R),
)
criterion = nn.MSELoss()
epochs = 200
min_mse = 1e-2
optimizer = optim.LBFGS(model.parameters(), lr=0.1, max_iter=20)
loss_history = []
#training
for epoch in range(epochs):
def closure():
optimizer.zero_grad()
outputs = model(X_tensor)
loss = criterion(outputs, Y_tensor)
loss.backward()
return loss
optimizer.step(closure)
with torch.no_grad():
outputs = model(X_tensor)
loss = criterion(outputs, Y_tensor)
avg_loss = loss.item()
loss_history.append(avg_loss)
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"epoch {epoch+1}/{epochs}, MSE: {avg_loss:.4f}")
if avg_loss < min_mse:
break
print("Trenowanie zakończone.")
# loss
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(loss_history) + 1), loss_history, marker='+', linestyle='-', color='b', label='Training')
plt.axhline(y=min_mse, color='g', linestyle='-', label='Minimum MSE (1e-2)')
plt.yscale('log')
plt.grid(True, which="both", ls="-", alpha=0.5)
plt.xlabel('Epoch')
plt.ylabel('Mean square error')
plt.title('Training algorithm: L-BFGS')
plt.legend()
plt.show()
#test
N2 = 300
M = (1 + 1 / (2 * N)) / k * 2 * np.pi
V_grid = np.linspace(-M, M, N2)
X2, Y2 = np.meshgrid(V_grid + x0, V_grid + y0, indexing='ij')
Coordinates2 = np.vstack((X2.flatten('F'), Y2.flatten('F')))
X2_tensor = torch.tensor(Coordinates2.T, dtype=torch.float32)
model.eval()
with torch.no_grad():
Probabilities2 = model(X2_tensor)
_, Regions2_pred = torch.max(Probabilities2, 1)
Regions2_pred = Regions2_pred.numpy() + 1
Regions2_grid = Regions2_pred.reshape((N2, N2), order='F')
cmap_background = mcolors.ListedColormap(['red', 'green', 'blue', 'yellow'])
plt.figure(figsize=(8, 8), facecolor='white')
plt.axis('off')
plt.pcolormesh(X2, Y2, Regions2_grid, cmap=cmap_background, alpha=0.25, shading='auto')
colors_dots = ['m', 'g', 'c', 'y']
for r in range(1, R + 1):
I = (Regions_flat == r)
plt.plot(Coordinates[0, I], Coordinates[1, I], 'o',
markeredgecolor='k', markerfacecolor=colors_dots[r-1], markersize=6)
plt.axis('equal')
plt.title('Test Sieci: Wyuczone Granice Decyzyjne (L-BFGS)')
plt.show()