forked from rronan/Boltzmann-s-Cuisine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimator.py
229 lines (188 loc) · 7.58 KB
/
estimator.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 12 17:26:21 2015
@author: Ronan
"""
import timeit
import numpy as np
import theano
import theano.tensor as T
from RBM import RBM
from load_data import shared_dataset
from theano.tensor.shared_randomstreams import RandomStreams
import os
class Estimator:
def __init__(self,
n_labels = 20,
learning_rate=0.01,
training_epochs=10,
batch_size=20,
n_chains=20,
n_samples=10,
n_hidden=2,
k=15,
do_report = True,
report_folder='reports',
report_name='report',
scoring='accuracy'):
self.n_labels = n_labels
self.learning_rate=learning_rate
self.training_epochs=training_epochs
self.batch_size=batch_size
self.n_chains=n_chains
self.n_samples=n_samples
self.n_hidden=n_hidden
self.k=k
self.do_report=do_report
self.report_folder=report_folder
self.report_name=report_name
self.scoring=scoring
def get_params():
pass
def set_params(self,
learning_rate=0.01,
training_epochs=10,
batch_size=20,
n_chains=20,
n_samples=10,
n_hidden=2,
k=15):
self.learning_rate=learning_rate
self.training_epochs=training_epochs
self.batch_size=batch_size
self.n_chains=n_chains
self.n_samples=n_samples
self.n_hidden=n_hidden
self.k=k
def fit(self, X, Y):
# Create a report to be saved at the end of execution
# (when running on the remote server)
if self.do_report:
report = {"learning_rate":self.learning_rate,
"training_epochs":self.training_epochs,
"batch_size":self.batch_size,
"n_chains":self.n_chains,
"n_samples":self.n_samples,
"n_hidden":self.n_hidden,
"k":self.k,
"costs":np.zeros(self.training_epochs),
# "accuracy":np.zeros(self.training_epochs),
"pretraining_time":0}
train_data = np.hstack([Y,X])
n_visible = train_data.shape[1]
# Building of theano format datasets
train_set = shared_dataset(train_data)
# compute number of minibatches for training, validation and testing
n_train_batches = train_set.get_value(borrow=True).shape[0] / \
self.batch_size
# allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch
x = T.matrix('x') # the data
rng = np.random.RandomState(123)
theano_rng = RandomStreams(rng.randint(2 ** 30))
# initialize storage for the persistent chain (state = hidden
# layer of chain)
persistent_chain = theano.shared(np.zeros((self.batch_size,
self.n_hidden),
dtype=theano.config.floatX),
borrow=True)
# construct the RBM class
self.rbm = RBM(input=x,
n_visible=n_visible,
n_labels=self.n_labels,
n_hidden=self.n_hidden,
np_rng=rng,
theano_rng=theano_rng)
# get the cost and the gradient corresponding to one step of CD-k
cost, updates = self.rbm.get_cost_updates(lr=self.learning_rate,
persistent=persistent_chain,
k=self.k)
# accuracy = self.rbm.get_cv_error()
#%%====================================================================
# Training the RBM
#======================================================================
# it is ok for a theano function to have no output
# the purpose of train_rbm is solely to update the RBM parameters
train_rbm = theano.function(
[index],
cost,
updates=updates,
givens={
x: train_set[index * self.batch_size: \
(index + 1) * self.batch_size]
},
name='train_rbm'
)
start_time = timeit.default_timer()
max_score = -np.inf
argmax_score = RBM(input=x,
n_visible=n_visible,
n_labels=self.n_labels,
n_hidden=self.n_hidden,
np_rng=rng,
theano_rng=theano_rng)
# count = 0
## go through training epochs
for epoch in xrange(self.training_epochs):
# go through the training set
mean_cost = []
for batch_index in xrange(n_train_batches):
mean_cost += [train_rbm(batch_index)]
print 'Training epoch %d, cost is ' % epoch, np.mean(mean_cost)
score = np.mean(mean_cost)
if score>max_score:
max_score = score
argmax_score.clone(self.rbm)
# acc = accuracy.eval()
#
# if self.scoring=='cost':
# score = np.mean(mean_cost)
# elif self.scoring=='accuracy':
# score = acc
# else:
# raise Warning('''scoring must be cost or accuracy,
# set to accuracy''')
# score = acc
#
# if score>max_score:
# max_score = score
# argmax_score.clone(self.rbm)
# count = 0
# else:
# count += 1
#
# if count>2:
# break
if self.do_report:
report["costs"][epoch] = np.mean(mean_cost)
# report["accuracy"][epoch] = acc
end_time = timeit.default_timer()
pretraining_time = (end_time - start_time)
report['pretraining_time'] = pretraining_time
self.rbm = argmax_score
if self.do_report:
try:
np.save(self.report_folder+'/'+self.report_name, report)
except OSError:
os.mkdir(self.report_folder)
np.save(self.report_folder+'/'+self.report_name, report)
def predict(self, X):
# make a prediction for an unlablled sample.
t_unlabelled = T.tensor3("unlabelled")
# This is not needed only if we want to make predictions from numpy arrays.
predict = theano.function(
[t_unlabelled],
self.rbm.predict(t_unlabelled),
name='predict'
)
pred,conf = predict([X])
return pred
#%%============================================================================
# Training the RBM
#==============================================================================
data = np.load('train_data.npy')
X = data[:,20:]
Y = data[:,:20]
est = Estimator(training_epochs = 2)
est.fit(X, Y)
pred = est.predict(X)