-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruconsolidator.py
213 lines (193 loc) · 8.6 KB
/
ruconsolidator.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
213
# -*- coding: utf-8 -*
import numpy as np
import theano
import theano.tensor as T
import lasagne
# документация: https://namenaro.gitbooks.io/struktura-proekta/content/chapter1.html
#константы алгоритма
ERROR_THR = 0.5 #при какой точности распознавания считать, что обучение удалось
NUM_HIDDEN_UNITS = 4
BATCH_SIZE = 6
LEARNING_RATE = 0.1
NUM_EPOCHS = 600
class RuConsolidator:
def __init__(self, x, y, log_enabled=True, test_function=None):
self.log_enabled = log_enabled
self.test_function = test_function
self.X_train = np.array(x)
self.Y_train = np.array(y)
self.print_data()
self.W_in_hid = None
self.W_hid_out = None
self.b_in_hid = None
self.b_hid_out = None
def log(self, msg):
if self.log_enabled:
print "[RuConsolidator] " + msg
def data_as_str(self):
data = "X_train:" \
+ np.array_str(self.X_train, precision=2)\
+ "\nY_train:"\
+ np.array_str(self.Y_train, precision=2)
return data
def print_data(self):
self.log(self.data_as_str())
def print_params(self):
print "Weights input -> hidden:"
print np.array_str(self.W_in_hid, precision=2)
print "Weights hidden -> output:"
print np.array_str(self.W_hid_out, precision=2)
print "bias hidden:"
print np.array_str(self.b_in_hid, precision=2)
print "bias output:"
print np.array_str(self.b_hid_out, precision=2)
def _build_model(self, input_var=None):
classes_num = self.Y_train.shape[1]
input_data_len = self.X_train.shape[1]
print str(input_data_len) + " ======================"
l_in = lasagne.layers.InputLayer(shape=(None, input_data_len),
input_var=input_var)
l_hidden = lasagne.layers.DenseLayer(l_in, num_units=NUM_HIDDEN_UNITS,
nonlinearity=lasagne.nonlinearities.sigmoid,
W=lasagne.init.GlorotUniform(),
name="hidden_layer")
l_out = lasagne.layers.DenseLayer(l_hidden, num_units=classes_num,
nonlinearity=lasagne.nonlinearities.sigmoid,
name="output_layer")
return l_out
#скопипастено не глядя из https://github.com/Lasagne/Lasagne/blob/master/examples/mnist.py
def _iterate_minibatches(self, inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
yield inputs[excerpt], targets[excerpt]
def consolidate(self):
self.save_data_to_file()
success = False
# символьные входные/выходные переменные
input_var = T.matrix(name='inputs')
target_var = T.matrix(name='targets')
# символьная оптимизируемая функция
network = self._build_model(input_var)
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.squared_error(prediction, target_var)
loss = loss.mean()
# какие параметры оптимизируем и как
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(
loss, params, learning_rate=LEARNING_RATE, momentum=0.9)
train_fn = theano.function(inputs=[input_var, target_var],
outputs=loss,
updates=updates,
allow_input_downcast=True) # float64 ->float32
self.log("consolidation staring...")
# наконец -- само обучение
for epoch in range(NUM_EPOCHS):
train_error = 0
num_batches = 0
for batch in self._iterate_minibatches(self.X_train, self.Y_train, BATCH_SIZE, shuffle=True):
inputs, targets = batch
train_error += train_fn(inputs, targets)
num_batches += 1
avg_err_over_epoch = train_error / num_batches
self.log("err: " + str(avg_err_over_epoch))
if avg_err_over_epoch <= ERROR_THR:
# да-да, без тестовой части датасета.
# тестироваться сетка будет после встраивания в граф - будет предказывать и сверять с истиной
success = True
self.W_in_hid = lasagne.layers.get_all_param_values(network)[0]
self.W_hid_out = lasagne.layers.get_all_param_values(network)[2]
self.b_in_hid = lasagne.layers.get_all_param_values(network)[1]
self.b_hid_out = lasagne.layers.get_all_param_values(network)[3]
break
if success:
self.log("SUCESSFULLY!")
else:
self.log("consolidation was not successfull")
#проверка сети
if self.test_function is not None:
self.test_function(network, input_var)
return success
def get_trained_weights(self):
return self.W_in_hid, self.W_hid_out, self.b_in_hid, self.b_hid_out
def save_data_to_file(self):
from datetime import datetime
time = str(datetime.now().strftime("%I_%M_%S%p"))
file_name = "consolidation_" + time + ".txt"
text_file = open(file_name, "w")
text_file.write(self.data_as_str())
text_file.close()
class ConsolidatorTest:
def __init__(self):
self.X = [[0.0, 0, 0.1, 0.7], #1 (1)
[0.0, 0, 0.0, 0.9], #1 (2)
[0.0, 0, 0.3, 0.7], #1 (3)
[0.0, 0, 0.2, 0.8], #1 (4)
[0.0, 0, 0.2, 0.9], #1 (5)
[0.0, 0, 0.1, 0.7], #1 (6)
[0.0, 0, 0.2, 0.8], #1 (7)
[0.0, 0, 0.0, 0.9], #1 (8)
[0.0, 0, 0.2, 0.8], #1 (9)
[0.0, 0.0, 0.3, 0.6], #1 (10)
[0.0, 0.0, 0.2, 0.9], #1 (11)
[0, 0.1, 0.2, 1.0], #1 (12)
[0.8, 0.0, 0.0, 0.0], #2 (1)
[0.9, 0.2, 0.0, 0.0], #2 (2)
[0.7, 0.1, 0.0, 0.0], #2 (3)
[0.8, 0.2, 0.0, 0.0], #2 (4)
[0.9, 0.3, 0.1, 0.0], #2 (5)
[0.8, 0.0, 0.0, 0.0], #2 (6)
[0.8, 0.2, 0.0, 0.0], #2 (7)
[0.8, 0.0, 0.0, 0.0], #2 (8)
[1.0, 0.3, 0.0, 0.0], #2 (9)
[1.0, 0.0, 0.0, 0.0], #2 (10)
[1.0, 0.1, 0.0, 0.0], #2 (11)
[0.9, 0.2, 0.1, 0.0] #2 (12)
]
self.Y = [[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1]]
def test_function(self, network, input_var):
test_prediction = lasagne.layers.get_output(network, deterministic=True)
test = theano.function(inputs=[input_var], outputs=test_prediction, allow_input_downcast=True)
raw_x_1 = [0., 0.1, 0.2, 0.9]
print raw_x_1
print "Classified as: %s" % test([raw_x_1])
raw_x_2 = [1.0, 0.1, 0.2, 0.]
print raw_x_2
print "Classified as: %s" % test([raw_x_2])
def train(self):
consolidator = RuConsolidator(x=self.X, y=self.Y, test_function=self.test_function)
res = consolidator.consolidate()
if res:
consolidator.print_params()
if __name__ == "__main__":
mtest = ConsolidatorTest()
mtest.train()