-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_net.py
More file actions
221 lines (185 loc) · 8.09 KB
/
Copy pathold_net.py
File metadata and controls
221 lines (185 loc) · 8.09 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
class SpatialNeuron:
def __init__(self, input_dim, output_dim, dmin=0.5):
self.input_dim = input_dim
self.output_dim = output_dim
self.weights = np.random.randn(input_dim, output_dim)
self.bias = np.random.randn(1)
self.position = np.array([1,0.5]) # 2D position for visualization
self.dmin = dmin # minimum distance for spatial attention
def forward(self, x):
print(x)
print(x.shape, self.weights.shape)
z = np.dot(x, self.weights) + self.bias
print(z.shape)
input_distances = np.array([
max(np.linalg.norm(self.position - np.array([0, 0])), self.dmin), # d1
max(np.linalg.norm(self.position - np.array([0, 1])), self.dmin), # d2
])
output_distances = np.array([
max(np.linalg.norm(self.position - np.array([2, 0])), self.dmin), # d3
max(np.linalg.norm(self.position - np.array([2, 1])), self.dmin) # d4
])
a = self.sigmoid(z)
# add spatial attention
a *= (1 / (1 + output_distances)) * 1
print(a.shape, a)
return a, z, input_distances, output_distances
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
class SpatialNetwork:
def __init__(self):
self.neuron = SpatialNeuron(2, 2)
self.learning_rate = 0.8
self.position_learning_rate = 0.8
self.dataset = np.array([
# [[0, 0], [0, 0]],
# [[0, 1], [1, 0]],
# [[1, 0], [1, 0]],
# [[1, 1], [0, 1]]
[[0, 0], [1, 0]],
[[0, 1], [1, 0]],
[[1, 0], [1, 0]],
[[1, 1], [1, 0]]
])
self.current_iteration = 0
self.loss_history = []
def train_step(self):
total_loss = 0
total_correct = 0
gradients = {'weights': np.zeros_like(self.neuron.weights),
'bias': np.zeros_like(self.neuron.bias),
'position': np.zeros_like(self.neuron.position)}
for x, y_true in self.dataset:
y_pred, z, inp_distances, out_distances = self.neuron.forward(x)
# Compute loss and accuracy
prediction_loss = np.mean((y_pred - y_true) ** 2)
distance_penalty = np.sum(np.maximum(0, self.neuron.dmin - out_distances))
# print(distance_penalty)
loss = prediction_loss + .5 * distance_penalty # You can adjust the 0.1 factor to control the penalty strength
total_loss += loss
total_correct += np.all(np.round(y_pred) == y_true)
# Compute gradients
d_loss = y_pred - y_true
d_sigmoid = self.neuron.sigmoid_derivative(y_pred)
d_weights = np.outer(x, d_loss * d_sigmoid)
d_bias = np.average(d_loss * d_sigmoid)
gradients['weights'] += d_weights
gradients['bias'] += d_bias
# Gradient for position
d_position = np.zeros(2)
for i, pos in enumerate([(0,0), (0,1), (2,0), (2,1)]):
distance = np.linalg.norm(self.neuron.position - np.array(pos))
if distance < self.neuron.dmin:
d_vector = -0.001 * (np.array(pos) - self.neuron.position) / distance # Repulsive force
else:
if i < 2: # Input distances
d_vector = np.zeros(2) # No gradient for input distances
else: # Output distances
d_vector = -(d_loss[i-2] * d_sigmoid[i-2] * y_pred[i-2]) * (np.array(pos) - self.neuron.position) / (1 + out_distances[i-2])**3
d_position += d_vector
gradients['position'] += d_position
# Update weights, bias, and position
self.neuron.weights -= self.learning_rate * gradients['weights'] / len(self.dataset)
self.neuron.bias -= self.learning_rate * gradients['bias'] / len(self.dataset)
self.neuron.position += self.position_learning_rate * gradients['position'] / len(self.dataset)
accuracy = total_correct / len(self.dataset)
self.loss_history.append(total_loss / len(self.dataset))
self.current_iteration += 1
print(f"Iteration {self.current_iteration}: Loss {total_loss/len(self.dataset):.4f}, Accuracy {accuracy:.2f}")
return accuracy
# Visualization setup
fig: plt.Figure
ax1: plt.Axes
ax2: plt.Axes
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), gridspec_kw={'height_ratios': [3, 1]})
plt.subplots_adjust(bottom=0.2)
network = SpatialNetwork()
# Main plot
input_nodes = ax1.scatter([0, 0], [0, 1], c='orange', s=500, zorder=2)
output_nodes = ax1.scatter([2, 2], [0, 1], c='orange', s=500, zorder=2)
neuron = ax1.scatter([1], [0.5], c='black', s=500, zorder=3)
lines = [
ax1.plot([0,1], [0,0.5], 'gray', linewidth=2, zorder=1)[0],
ax1.plot([0,1], [1,0.5], 'gray', linewidth=2, zorder=1)[0],
ax1.plot([1,2], [0.5,1], 'gray', linewidth=2, zorder=1)[0],
ax1.plot([1,2], [0.5,0], 'gray', linewidth=2, zorder=1)[0]
]
ax1.set_xlim(-0.5, 2.5)
ax1.set_ylim(-0.5, 1.5)
ax1.axis('off')
# Text annotations
texts = {
'a1': ax1.text(-0.1, 0, '', ha='right', va='center'),
'a2': ax1.text(-0.1, 1, '', ha='right', va='center'),
'a3': ax1.text(2.1, 0, '', ha='left', va='center'),
'a4': ax1.text(2.1, 1, '', ha='left', va='center'),
'd1': ax1.text(0.5, 0.1, '', ha='center', va='bottom'),
'd2': ax1.text(0.5, 0.9, '', ha='center', va='bottom'),
'd3': ax1.text(1.5, 0.1, '', ha='center', va='bottom'),
'd4': ax1.text(1.5, 0.9, '', ha='center', va='bottom'),
'w1': ax1.text(0.5, 0.3, '', ha='right', va='bottom'),
'w2': ax1.text(0.5, 0.7, '', ha='right', va='top'),
'w3': ax1.text(1.5, 0.3, '', ha='left', va='bottom'),
'w4': ax1.text(1.5, 0.7, '', ha='left', va='top'),
'b1': ax1.text(1, 0.7, '', ha='center', va='center'),
}
# Loss plot
loss_line, = ax2.plot([], [], 'b-')
loss_text = ax2.text(0.02, 0.95, '', transform=ax2.transAxes)
ax2.set_xlim(0, 500)
ax2.set_ylim(0, 1)
ax2.set_xlabel('Iteration')
ax2.set_ylabel('Loss')
ax2.grid(True)
def update_plot():
neuron.set_offsets(network.neuron.position)
for i, line in enumerate(lines):
neuron_pos = network.neuron.position
start, end = line.get_xydata()
if i < 2:
end = [neuron_pos[0], neuron_pos[1]]
else:
start = [neuron_pos[0], neuron_pos[1]]
line.set_data([start[0], end[0]], [start[1], end[1]])
x, y_true = network.dataset[network.current_iteration % 4]
y_pred, _, inp_distances, out_distances = network.neuron.forward(x)
texts['a1'].set_text(f'a1={x[0]}')
texts['a2'].set_text(f'a2={x[1]}')
texts['a3'].set_text(f'a3={y_pred[0]:.2f}')
texts['a4'].set_text(f'a4={y_pred[1]:.2f}')
for i, d in enumerate(np.append(inp_distances, out_distances)):
texts[f'd{i+1}'].set_text(f'd{i+1}={d:.4f}')
for i, w in enumerate(network.neuron.weights.flatten()):
texts[f'w{i+1}'].set_text(f'w{i+1}={w:.4f}')
texts['b1'].set_text(f'b1={network.neuron.bias[0]:.2f}')
loss_line.set_data(range(len(network.loss_history)), network.loss_history)
ax2.set_xlim(0, max(100, len(network.loss_history)))
loss_text.set_text(f'Current Loss: {network.loss_history[-1]:.2f}')
plt.draw()
def on_click(event):
network.train_step()
update_plot()
button_ax = plt.axes([0.8, 0.05, 0.1, 0.075])
button = Button(button_ax, 'Next Iteration')
button.on_clicked(on_click)
# play pause training with 100ms interval
play_button = Button(plt.axes([0.7, 0.05, 0.1, 0.075]), 'Play')
pause_button = Button(plt.axes([0.6, 0.05, 0.1, 0.075]), 'Pause')
pause = False
def on_play(event):
global pause
pause = False
while not pause:
on_click(None)
plt.pause(0.01)
def on_pause(event):
global pause
pause = True
play_button.on_clicked(on_play)
pause_button.on_clicked(on_pause)
plt.show()