-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
57 lines (48 loc) · 2.25 KB
/
classifier.py
File metadata and controls
57 lines (48 loc) · 2.25 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
import numpy as np
import pickle
import theano
import theano.tensor as T
class Classifier(object):
def errors(self, x, y):
raise NotImplementedError
def pred_label(self, x):
raise NotImplementedError
def train(self, train_x, train_y, test_x, test_y, valid_x, valid_y, alpha=0.13, batch_size=500, l1_reg=0., l2_reg=0.0, n_epochs=1000):
raise NotImplementedError
def run_batches(self, train_x, train_y, test_x, test_y, valid_x, valid_y, x, y, train_model_func, batch_size=500, n_epochs=1000):
n_train_batches = train_x.get_value(borrow=True).shape[0] // batch_size
n_valid_batches = valid_x.get_value(borrow=True).shape[0] // batch_size
n_test_batches = test_x.get_value(borrow=True).shape[0] // batch_size
index = T.lscalar()
test_model = theano.function(
inputs=[index],
outputs=self.errors(x, y),
givens={
x: test_x[index * batch_size:(index+1)*batch_size],
y: test_y[index * batch_size:(index+1)*batch_size],
}
)
validate_model = theano.function(
inputs=[index],
outputs=self.errors(x, y),
givens={
x: valid_x[index * batch_size:(index+1)*batch_size],
y: valid_y[index * batch_size:(index+1)*batch_size],
}
)
best_loss = float('inf')
for epoch in range(n_epochs):
for minibatch_index in range(n_train_batches):
train_model_func(minibatch_index)
if epoch % 5 == 0:
validation_losses = [validate_model(i) for i in range(n_valid_batches)]
avg_validation_loss = np.mean(validation_losses)
if avg_validation_loss < best_loss:
best_loss = avg_validation_loss
test_losses = [test_model(i) for i in range(n_test_batches)]
avg_test_loss = np.mean(test_losses)
print('test error: {}'.format(avg_test_loss))
with open('best_model.pkl', 'w') as f:
pickle.dump(self, f)
print('epoch {} -> validation error: {} (best loss={})'.format(epoch, avg_validation_loss, best_loss))
return best_loss