-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback_alignment.py
212 lines (165 loc) · 5.89 KB
/
feedback_alignment.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
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
# coding:utf-8
import numpy as np
import cupy as cp
import chainer
from chainer.backends import cuda
from chainer import Function, gradient_check, report, training, utils, Variable
from chainer import datasets, iterators, optimizers, serializers
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
from chainer.training import extensions
import PIL
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
# Load the MNIST dataset
train, test = chainer.datasets.get_mnist()
x_train, t_train = train._datasets
x_test, t_test = test._datasets
x_train = cp.asarray(x_train)
x_test = cp.asarray(x_test)
t_train = cp.identity(10)[t_train.astype(int)]
t_test = cp.identity(10)[t_test.astype(int)]
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.reshape(1, y.size)
if t.size == y.size:
t = t.argmax(axis=1)
batch_size = y.shape[0]
return -cp.sum(cp.log(y[cp.arange(batch_size), t] + 1e-7)) / batch_size
def relu(x):
return cp.maximum(0, x)
def relu_grad(x):
x[x <= 0] = 0
x[x > 0] = 1
return x
def softmax(x):
if x.ndim == 2:
x = x.T
x = x - cp.max(x, axis=0)
y = cp.exp(x) / cp.sum(cp.exp(x), axis=0)
return y.T
x = x - cp.max(x)
return cp.exp(x) / cp.sum(cp.exp(x))
# Network definition
hidden_unit = 2000
class MLP:
def __init__(self, weight_init_std=0.01):
self.W_f1 = weight_init_std * cp.random.randn(784, hidden_unit)
self.W_f2 = weight_init_std * cp.random.randn(hidden_unit, 10)
self.B1 = weight_init_std * cp.random.randn(10, hidden_unit)
# self.B2 = weight_init_std * cp.random.randn(hidden_unit, 784)
def predict(self, x):
h1 = cp.dot(x, self.W_f1)
h1 = relu(h1)
h2 = cp.dot(h1, self.W_f2)
output = softmax(h2)
return output
def accuracy(self, x, t):
y = self.predict(x)
y = cp.argmax(y, axis=1)
t = cp.argmax(t, axis=1)
accuracy = cp.sum(y == t) / float(x.shape[0])
return accuracy
def loss(self, x, t):
y = self.predict(x)
return cross_entropy_error(y, t)
def gradient(self, x, target):
h1 = cp.dot(x, self.W_f1)
h1_ = relu(h1)
h2 = cp.dot(h1_, self.W_f2)
output = softmax(h2)
delta2 = (output - target) / batch_size
delta_Wf2 = cp.dot(h1_.T, delta2)
delta1 = relu_grad(h1) * cp.dot(delta2, self.W_f2.T)
delta_Wf1 = cp.dot(x.T, delta1)
alpha = 0.1
self.W_f1 -= alpha * delta_Wf1
self.W_f2 -= alpha * delta_Wf2
def feedback_alignment(self, x, target):
h1 = cp.dot(x, self.W_f1)
h1_ = relu(h1)
h2 = cp.dot(h1_, self.W_f2)
output = softmax(h2)
delta2 = (output - target) / batch_size
delta_Wf2 = cp.dot(h1_.T, delta2)
delta1 = relu_grad(h1) * cp.dot(delta2, self.B1)
delta_Wf1 = cp.dot(x.T, delta1)
alpha = 0.1
self.W_f1 -= alpha * delta_Wf1
self.W_f2 -= alpha * delta_Wf2
"""
mlp = MLP()
train_loss_list = []
test_loss_list = []
train_acc_list = []
test_acc_list = []
train_size = x_train.shape[0]
batch_size = 100
iter_per_epoch = 100
for i in range(100000):
batch_mask = cp.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
mlp.gradient(x_batch, t_batch)
# mlp.feedback_alignment(x_batch,t_batch)
if i % iter_per_epoch == 0:
train_acc = mlp.accuracy(x_train, t_train)
test_acc = mlp.accuracy(x_test, t_test)
train_loss = mlp.loss(x_train, t_train)
test_loss = mlp.loss(x_test, t_test)
train_loss_list.append(cuda.to_cpu(train_loss))
test_loss_list.append(cuda.to_cpu(test_loss))
train_acc_list.append(cuda.to_cpu(train_acc))
test_acc_list.append(cuda.to_cpu(test_acc))
print("epoch:", int(i / iter_per_epoch), " train acc, test acc | " + str(train_acc) + ", " + str(test_acc))
"""
mlp = MLP()
train_loss_list_FA = []
test_loss_list_FA = []
train_acc_list_FA = []
test_acc_list_FA = []
train_size = x_train.shape[0]
batch_size = 100
iter_per_epoch = 100
for i in range(100000):
batch_mask = cp.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
# mlp.gradient(x_batch, t_batch)
mlp.feedback_alignment(x_batch,t_batch)
if i % iter_per_epoch == 0:
train_acc = mlp.accuracy(x_train, t_train)
test_acc = mlp.accuracy(x_test, t_test)
train_loss = mlp.loss(x_train, t_train)
test_loss = mlp.loss(x_test, t_test)
train_loss_list_FA.append(cuda.to_cpu(train_loss))
test_loss_list_FA.append(cuda.to_cpu(test_loss))
train_acc_list_FA.append(cuda.to_cpu(train_acc))
test_acc_list_FA.append(cuda.to_cpu(test_acc))
print("epoch:", int(i / iter_per_epoch), " train acc, test acc | " + str(train_acc) + ", " + str(test_acc))
"""
plt.plot(train_acc_list, label="BP train acc", linestyle="dashed", color="blue")
plt.plot(test_acc_list, label="BP test acc", color="blue")
# plt.title("BP for MNIST")
# plt.legend()
# plt.savefig("mnistBP.png")
plt.plot(train_acc_list_FA, label="RFA train acc", linestyle="dotted", color="orange")
plt.plot(test_acc_list_FA, label="RFA test acc", color="orange")
plt.title("BP/RFA for MNIST")
plt.legend()
plt.savefig("./result/BP-RFA_for_mnist.png")
plt.figure()
plt.plot(train_acc_list[20:], label="BP train acc", linestyle="dotted", color="blue")
plt.plot(test_acc_list[20:], label="BP test acc", color="blue")
# plt.title("BP for MNIST")
# plt.legend()
# plt.savefig("mnistBP.png")
plt.plot(train_acc_list_FA[20:], label="RFA train acc", linestyle="dashed", color="orange")
plt.plot(test_acc_list_FA[20:], label="RFA test acc", color="orange")
plt.title("BP/RFA for MNIST relu")
plt.legend()
plt.savefig("./result/BP-RFA_for_mnist_20start.png")
"""