-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNAParCouche.py
More file actions
220 lines (169 loc) · 6.12 KB
/
RNAParCouche.py
File metadata and controls
220 lines (169 loc) · 6.12 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
# -*- coding: utf-8 -*-
import numpy as np
# Base class
class Couche:
def __init__(self):
self.X = None
self.Y = None
# computes the output Y of a layer for a given input X
def propagation(self, X):
raise NotImplementedError
# computes dE/dX for a given dE/dY (and update parameters if any)
def retropropagation(self, Y, taux):
raise NotImplementedError
# inherit from base class Layer
class CoucheDense(Couche):
# input_size = number of input neurons
# output_size = number of output neurons
def __init__(self, n, m):
self.W = np.random.rand(n,m) - 0.5
self.B = np.random.rand(1, m) - 0.5
# returns output for a given input
def propagation(self, X):
self.X = Y
self.Y = self.B + np.dot(self.X, self.W)
return self.Y
# computes dE/dW, dE/dB for a given output_error=dE/dY. Returns input_error=dE/dX.
def retropropagation(self, dJ_dY, taux):
dJ_dX = np.dot(dJ_dY, self.w.T)
dJ_dW = np.dot(self.x.T, dJ_dY)
dJ_dB = dJ_dY
# update parameters
self.W -= taux * dJ_dW
self.B -= taux * dJ_dB
return dJ_dX
# inherit from base class Layer
class CoucheActivation(Couche):
def __init__(self, fonction_activation, derivee):
self.fonction_activation = fonction_activation
self.derivee = derivee
# returns the activated input
def propagation(self, X):
self.X = X
self.Y = self.fonction_activation(self.X)
return self.Y
# Returns input_error=dE/dX for a given output_error=dE/dY.
# learning_rate is not used because there is no "learnable" parameters.
def retropropagation(self, dJ_dY, learning_rate):
return self.derivee(self.X) * dJ_dY
import numpy as np
# activation function and its derivative
def tanh(x):
return np.tanh(x)
def tanh_prime(x):
return 1-np.tanh(x)**2
import numpy as np
# loss function and its derivative
def mse(y_true, y_pred):
return np.mean(np.power(y_true-y_pred, 2))
def mse_prime(y_true, y_pred):
return 2*(y_pred-y_true)/y_true.size
class Network:
def __init__(self):
self.layers = []
self.loss = None
self.loss_prime = None
# add layer to network
def add(self, layer):
self.layers.append(layer)
# set loss to use
def use(self, loss, loss_prime):
self.loss = loss
self.loss_prime = loss_prime
# predict output for given input
def predict(self, input_data):
# sample dimension first
samples = len(input_data)
result = []
# run network over all samples
for i in range(samples):
# forward propagation
output = input_data[i]
for layer in self.layers:
output = layer.forward_propagation(output)
result.append(output)
return result
# train the network
def fit(self, x_train, y_train, epochs, learning_rate):
# sample dimension first
samples = len(x_train)
# training loop
for i in range(epochs):
err = 0
for j in range(samples):
# forward propagation
output = x_train[j]
for layer in self.layers:
output = layer.forward_propagation(output)
# compute loss (for display purpose only)
err += self.loss(y_train[j], output)
# backward propagation
error = self.loss_prime(y_train[j], output)
for layer in reversed(self.layers):
error = layer.backward_propagation(error, learning_rate)
# calculate average error on all samples
err /= samples
print('epoch %d/%d error=%f' % (i+1, epochs, err))
import numpy as np
from network import Network
from fc_layer import FCLayer
from activation_layer import ActivationLayer
from activations import tanh, tanh_prime
from losses import mse, mse_prime
# training data
x_train = np.array([[[0,0]], [[0,1]], [[1,0]], [[1,1]]])
y_train = np.array([[[0]], [[1]], [[1]], [[0]]])
# network
net = Network()
net.add(FCLayer(2, 3))
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(3, 1))
net.add(ActivationLayer(tanh, tanh_prime))
# train
net.use(mse, mse_prime)
net.fit(x_train, y_train, epochs=1000, learning_rate=0.1)
# test
out = net.predict(x_train)
print(out)
import numpy as np
from network import Network
from fc_layer import FCLayer
from activation_layer import ActivationLayer
from activations import tanh, tanh_prime
from losses import mse, mse_prime
from keras.datasets import mnist
from keras.utils import np_utils
# load MNIST from server
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# training data : 60000 samples
# reshape and normalize input data
x_train = x_train.reshape(x_train.shape[0], 1, 28*28)
x_train = x_train.astype('float32')
x_train /= 255
# encode output which is a number in range [0,9] into a vector of size 10
# e.g. number 3 will become [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
y_train = np_utils.to_categorical(y_train)
# same for test data : 10000 samples
x_test = x_test.reshape(x_test.shape[0], 1, 28*28)
x_test = x_test.astype('float32')
x_test /= 255
y_test = np_utils.to_categorical(y_test)
# Network
net = Network()
net.add(FCLayer(28*28, 100)) # input_shape=(1, 28*28) ; output_shape=(1, 100)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(100, 50)) # input_shape=(1, 100) ; output_shape=(1, 50)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(50, 10)) # input_shape=(1, 50) ; output_shape=(1, 10)
net.add(ActivationLayer(tanh, tanh_prime))
# train on 1000 samples
# as we didn't implemented mini-batch GD, training will be pretty slow if we update at each iteration on 60000 samples...
net.use(mse, mse_prime)
net.fit(x_train[0:1000], y_train[0:1000], epochs=35, learning_rate=0.1)
# test on 3 samples
out = net.predict(x_test[0:3])
print("\n")
print("predicted values : ")
print(out, end="\n")
print("true values : ")
print(y_test[0:3])