-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_xor.py
119 lines (82 loc) · 3.2 KB
/
learn_xor.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
import numpy as np
class Layer:
def __init__(self):
self.input=None
self.output=None
def forward_propagation(self, input_data):
raise NotImplementedError
def backward_propagation(self,output_error, learning_rate):
raise NotImplementedError
class FCLayer(Layer):
def __init__(self,input_size, output_size):
self.weights = np.random.rand(input_size,output_size)-0.5
self.bias = np.random.rand(1,output_size)-0.5
def forward_propagation(self, input_data):
self.input = input_data
self.output = np.dot(self.input, self.weights) + self.bias
return self.output
def backward_propagation(self, output_error, learning_rate):
input_error = np.dot(output_error, self.weights.T)
weights_error = np.dot(self.input.T, output_error)
self.weights -= learning_rate * weights_error
self.bias -= learning_rate * output_error
return input_error
class ActivationLayer(Layer):
def __init__(self, activation, activation_prime):
self.activation = activation
self.activation_prime = activation_prime
def forward_propagation(self, input_data):
self.input = input_data
self.output = self.activation(self.input)
return self.output
def backward_propagation(self, output_error, learning_rate):
return self.activation_prime(self.input) * output_error
def tanh(x):
return np.tanh(x)
def tanh_prime(x):
return 1 - np.tanh(x)**2
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,loss, loss_prime):
self.layers = []
self.loss = loss
self.loss_prime = loss_prime
def add(self, layer):
self.layers.append(layer)
def predict(self, input_data):
result = np.zeros((len(input_data),1))
for i in range(len(input_data)):
output = input_data[i]
for layer in self.layers:
output = layer.forward_propagation(output)
result[i]=output[0][0]
return result
def fit(self, X,y,epochs=10,learning_rate=0.1):
for i in range(epochs):
err = 0
for j in range(len(X)):
output = X[j]
for layer in self.layers:
output = layer.forward_propagation(output)
err += self.loss(y[j],output)
error = self.loss_prime(y[j],output)
for layer in reversed(self.layers):
error = layer.backward_propagation(error,learning_rate)
err /= len(X)
print('epoch {}/{} error = {}'.format(i+1,epochs,err))
x_train = np.array([[[0,0]], [[0,1]], [[1,0]], [[1,1]]])
y_train = np.array([[[0]], [[1]], [[1]], [[0]]])
# network
net = Network(mse,mse_prime)
net.add(FCLayer(2, 3))
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(3, 1))
net.add(ActivationLayer(tanh, tanh_prime))
# train
net.fit(x_train, y_train, epochs=1000, learning_rate=0.1)
# test
out = net.predict(x_train)
print(out)