-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMLP.py
More file actions
176 lines (131 loc) · 6.13 KB
/
Copy pathMLP.py
File metadata and controls
176 lines (131 loc) · 6.13 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
from sklearn.utils import check_random_state, gen_batches
from scipy.special import expit as logistic_sigmoid
import warnings
from _base import ACTIVATIONS, DERIVATIVES, LOSS_FUNCTIONS
class mynn(object):
"""docstring for mynn"""
def __init__(self, activation='relu', learning_rate_init=0.001, learning_rule='const', lamb=0.0, max_iter=200,
num_hidden_nodes=[8], num_hidden_layers=1, momentum=0.9, beta=0.0, ro0=0.05, shuffle=True, batch_size="auto", random_state=None):
# self.nonlinear = (sigmoid, dsigmoid)
self.activation = activation
self.learning_rate_init = learning_rate_init
self.lamb = lamb
self.max_iter = max_iter
self.num_input_nodes = None
self.num_res_nodes = None
self.shuffle = shuffle
self.batch_size = batch_size
if not isinstance(num_hidden_nodes, list):
raise TypeError('must be a list!')
self.num_hidden_nodes = num_hidden_nodes
self.num_hidden_layers = num_hidden_layers
self.n_layers_ = num_hidden_layers + 2
self._random_state = check_random_state(random_state)
self.ww = None
self.th = None
def init_param(self, nodes_list):
self.out_activation_ = 'identity'
if self.activation == 'logistic':
init_bound = lambda inb, outb: np.sqrt(2. / (inb + outb))
else:
init_bound = lambda inb, outb: np.sqrt(6. / (inb + outb))
self.ww = [self._random_state.uniform(-init_bound(nodes_list[i], nodes_list[i + 1]), init_bound(nodes_list[i], nodes_list[i + 1]), (nodes_list[i], nodes_list[i + 1]))
for i in xrange(self.n_layers_ - 1)]
self.th = [self._random_state.uniform(-init_bound(nodes_list[i], nodes_list[i + 1]), init_bound(nodes_list[i], nodes_list[i + 1]), (nodes_list[i + 1],))
for i in xrange(self.n_layers_ - 1)]
# self.dww = [np.empty_like(w) for w in self.ww]
# self.dww_last = [np.empty_like(w) for w in self.ww]
# self.dth = [np.empty_like(th) for th in self.th]
# self.z = [np.empty_like(th) for th in self.th]
# self.a = [np.empty_like(th) for th in self.th]
# self.ro = [np.empty_like(th) for th in self.th]
# self.delta = [np.empty_like(th) for th in self.th]
def _forward_pass(self, activations):
hidden_activation = ACTIVATIONS[self.activation]
for layer in xrange(self.n_layers_ - 1):
activations[layer + 1] = np.dot(activations[layer], self.ww[layer])
activations[layer + 1] += self.th[layer]
if (layer + 1) != (self.n_layers_ - 1):
activations[
layer + 1] = hidden_activation(activations[layer + 1])
out_activation = ACTIVATIONS[self.out_activation_]
activations[layer + 1] = out_activation(activations[layer + 1])
return activations
def _backprog(self, X, y, dww, dth, delta, activations):
n_inputs = X.shape[0]
activations = self._forward_pass(activations)
derivative = DERIVATIVES[self.activation]
for rlayer in xrange(self.n_layers_ - 2, -1, -1):
if rlayer == self.n_layers_ - 2:
delta[rlayer] = activations[rlayer + 1] - y
else:
delta[rlayer] = np.dot(
delta[rlayer + 1], self.ww[rlayer + 1].T)
derivative(activations[rlayer + 1], delta[rlayer])
# + self.beta * (-self.ro0 / self.ro[
# rlayer] + (1 - self.ro0) / (1 - self.ro[rlayer]))
dww[rlayer] = np.dot(activations[rlayer].T, delta[rlayer])
dww[rlayer] += (self.lamb * self.ww[rlayer])
dww[rlayer] /= n_inputs
dth[rlayer] = np.mean(delta[rlayer], 0)
return dww, dth
def _fit(self, X, y):
num_input, self.num_input_nodes = X.shape
num_res, self.num_res_nodes = y.shape
if num_input != num_res:
raise('Data set error!')
nodes_list = [self.num_input_nodes] + \
self.num_hidden_nodes + [self.num_res_nodes]
self.init_param(nodes_list)
if self.batch_size == 'auto':
batch_size = min(200, num_input)
else:
batch_size = self.batch_size
activations = [X]
activations.extend( [np.empty((batch_size, n_out_node))
for n_out_node in nodes_list[1:]])
activations = self._forward_pass(activations)
delta = [np.empty_like(a_layer) for a_layer in activations]
dww = [np.empty_like(w) for w in self.ww]
dth = [np.empty_like(th) for th in self.th]
for it in xrange(self.max_iter):
if self.shuffle:
index = np.random.permutation(num_input)
X = X[index]
y = y[index]
for batch in gen_batches(num_input, batch_size):
activations[0] = X[batch]
dww, dth = self._backprog(
X[batch], y[batch], dww, dth, delta, activations)
for layer in xrange(self.n_layers_ - 1):
self.ww[layer] -= self.learning_rate_init * dww[layer]
self.th[layer] -= self.learning_rate_init * dth[layer]
return activations[-1]
def _predict(self, X):
num_input, num_input_nodes = X.shape
nodes_list = [num_input_nodes] + \
self.num_hidden_nodes + [self.num_res_nodes]
activations = [X]
activations.extend([np.empty((num_input, n_out_node))
for n_out_node in nodes_list[1:]])
self._forward_pass(activations)
return (np.sign(activations[-1]-0.5)+1)/2
def test():
from sklearn.neural_network import MLPClassifier
records = np.random.randint(0, 2, (10, 6))
results = np.random.randint(0, 2, (10, 3))
# records = np.eye(6)
# results = records
nn = mynn()
nn2 = MLPClassifier()
nn._fit(records, results)
nn2.fit(records, results)
print results
print nn._predict(records)
print nn2.predict(records)
# print nn.ww
# print results
# print nn.predict(records)
if __name__ == '__main__':
test()