comparison algorithms/regressor.py @ 476:8fcd0f3d9a17

added a few algorithms
author Olivier Breuleux <breuleuo@iro.umontreal.ca>
date Mon, 27 Oct 2008 17:26:00 -0400
parents
children 2b0e10ac6929
comparison
equal deleted inserted replaced
475:11e0357f06f4 476:8fcd0f3d9a17
1
2 import theano
3 from theano import tensor as T
4 from theano.tensor import nnet as NN
5 import numpy as N
6
7 class Regressor(theano.FancyModule):
8
9 def __init__(self, input = None, target = None, regularize = True):
10 super(Regressor, self).__init__()
11
12 # MODEL CONFIGURATION
13 self.regularize = regularize
14
15 # ACQUIRE/MAKE INPUT AND TARGET
16 self.input = theano.External(input) if input else T.matrix('input')
17 self.target = theano.External(target) if target else T.matrix('target')
18
19 # HYPER-PARAMETERS
20 self.lr = theano.Member(T.scalar())
21
22 # PARAMETERS
23 self.w = theano.Member(T.matrix())
24 self.b = theano.Member(T.vector())
25
26 # OUTPUT
27 self.output_activation = T.dot(self.input, self.w) + self.b
28 self.output = self.build_output()
29
30 # REGRESSION COST
31 self.regression_cost = self.build_regression_cost()
32
33 # REGULARIZATION COST
34 self.regularization = self.build_regularization()
35
36 # TOTAL COST
37 self.cost = self.regression_cost
38 if self.regularize:
39 self.cost = self.cost + self.regularization
40
41 # GRADIENTS AND UPDATES
42 self.params = self.w, self.b
43 gradients = T.grad(self.cost, self.params)
44 updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gradients))
45
46 # INTERFACE METHODS
47 self.update = theano.Method([self.input, self.target], self.cost, updates)
48 self.predict = theano.Method(self.input, self.output)
49
50 self.build_extensions()
51
52 def _instance_initialize(self, obj, input_size = None, output_size = None, seed = None, **init):
53 if seed is not None:
54 R = N.random.RandomState(seed)
55 else:
56 R = N.random
57 if (input_size is None) ^ (output_size is None):
58 raise ValueError("Must specify input_size and output_size or neither.")
59 super(Regressor, self)._instance_initialize(obj, **init)
60 if input_size is not None:
61 sz = (input_size, output_size)
62 range = 1/N.sqrt(input_size)
63 obj.w = R.uniform(size = sz, low = -range, high = range)
64 obj.b = N.zeros(output_size)
65 obj.__hide__ = ['params']
66
67 def _instance_flops_approx(self, obj):
68 return obj.w.size
69
70 def build_extensions(self):
71 pass
72
73 def build_output(self):
74 raise NotImplementedError('override in subclass')
75
76 def build_regression_cost(self):
77 raise NotImplementedError('override in subclass')
78
79 def build_regularization(self):
80 return T.zero() # no regularization!
81
82
83 class BinRegressor(Regressor):
84
85 def build_extensions(self):
86 self.classes = T.iround(self.output)
87 self.classify = theano.Method(self.input, self.classes)
88
89 def build_output(self):
90 return NN.sigmoid(self.output_activation)
91
92 def build_regression_cost(self):
93 self.regression_cost_matrix = self.target * T.log(self.output) + (1.0 - self.target) * T.log(1.0 - self.output)
94 self.regression_costs = -T.sum(self.regression_cost_matrix, axis=1)
95 return T.mean(self.regression_costs)
96
97 def build_regularization(self):
98 self.l2_coef = theano.Member(T.scalar())
99 return self.l2_coef * T.sum(self.w * self.w)
100
101 def _instance_initialize(self, obj, input_size = None, output_size = 1, seed = None, **init):
102 init.setdefault('l2_coef', 0)
103 super(BinRegressor, self)._instance_initialize(obj, input_size, output_size, seed, **init)