comparison mlp_factory_approach.py @ 206:f2ddc795ec49

changes made with Pascal but should probably be discarded
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Fri, 16 May 2008 16:36:27 -0400
parents ebbb0e749565
children c5a7105fa40b
comparison
equal deleted inserted replaced
205:d1359de1ea13 206:f2ddc795ec49
25 raise NotImplementedError() 25 raise NotImplementedError()
26 else: 26 else:
27 for i in xrange(100): 27 for i in xrange(100):
28 for input, target in trainset.minibatches(['input', 'target'], 28 for input, target in trainset.minibatches(['input', 'target'],
29 minibatch_size=min(32, len(trainset))): 29 minibatch_size=min(32, len(trainset))):
30 dummy = update_fn(input, target[:,0], *params) 30 results = update_fn(input, target[:,0], *params)
31 if 0: print dummy[0] #the nll 31 if 0: print results[0]
32 # print params['b']
32 33
33 def __call__(self, testset, 34 def __call__(self, testset,
34 output_fieldnames=['output_class'], 35 output_fieldnames=['output_class'],
35 test_stats_collector=None, 36 test_stats_collector=None,
36 copy_inputs=False, 37 copy_inputs=False,
37 put_stats_in_output_dataset=True, 38 put_stats_in_output_dataset=True,
38 output_attributes=[]): 39 output_attributes=[]):
39 """Apply this model (as a function) to new data""" 40 """Apply this model (as a function) to new data"""
40 inputs = [self.nnet.v.input, self.nnet.v.target] + self.nnet.v.params 41 inputs = [self.nnet.v.input, self.nnet.v.target] + self.nnet.v.params
41 fn = _function(inputs, [getattr(self.nnet.v, name) for name in output_fieldnames]) 42 fn = _function(inputs, [getattr(self.nnet.v, name) for name in output_fieldnames])
42 if 'target' in testset.fields(): 43 if 'target' in testset.fieldNames():
43 return dataset.ApplyFunctionDataSet(testset, 44 return dataset.ApplyFunctionDataSet(testset,
44 lambda input, target: fn(input, target[:,0], *self.params), 45 lambda input, target: fn(input, target[:,0], *self.params),
45 output_fieldnames) 46 output_fieldnames)
46 else: 47 else:
47 return dataset.ApplyFunctionDataSet(testset, 48 return dataset.ApplyFunctionDataSet(testset,
48 lambda input: fn(input, numpy.zeros(1,dtype='int64'), *self.params), 49 lambda input: fn(input, numpy.zeros(1,dtype='int64'), *self.params),
49 output_fieldnames) 50 output_fieldnames)
50 51
51 def __init__(self, ninputs, nhid, nclass, lr, nepochs, 52 def __init__(self, ninputs, nhid, nclass, lr, nepochs,
52 l2coef=0.0, 53 l2coef=0.0,
53 linker='c&yp', 54 linker='c&yp',
54 hidden_layer=None): 55 hidden_layer=None):
56 if not hidden_layer:
57 hidden_layer = AffineSigmoidLayer("hidden",ninputs,nhid,l2coef)
55 class Vars: 58 class Vars:
56 def __init__(self, lr, l2coef): 59 def __init__(self, lr, l2coef):
57 lr = t.constant(lr) 60 lr = t.constant(lr)
58 l2coef = t.constant(l2coef) 61 l2coef = t.constant(l2coef)
59 input = t.matrix('input') # n_examples x n_inputs 62 input = t.matrix('input') # n_examples x n_inputs
60 target = t.ivector('target') # n_examples x 1 63 target = t.ivector('target') # n_examples x 1
61 W2 = t.matrix('W2') 64 W2 = t.matrix('W2')
62 b2 = t.vector('b2') 65 b2 = t.vector('b2')
63 66
64 if hidden_layer: 67 hid = hidden_layer(input)
65 hid, hid_params, hid_ivals, hid_regularization = hidden_layer(input) 68 hid_params = hidden_layer.params()
66 else: 69 hid_params_init_vals = hidden_layer.params_ivals()
67 W1 = t.matrix('W1') 70 hid_regularization = hidden_layer.regularization()
68 b1 = t.vector('b1') 71
69 hid = t.tanh(b1 + t.dot(input, W1))
70 hid_params = [W1, b1]
71 hid_regularization = l2coef * t.sum(W1*W1)
72 hid_ivals = lambda : [_randshape(ninputs, nhid), _randshape(nhid)]
73
74 params = [W2, b2] + hid_params 72 params = [W2, b2] + hid_params
75 nll, predictions = nnet_ops.crossentropy_softmax_1hot( b2 + t.dot(hid, W2), target) 73 nll, predictions = nnet_ops.crossentropy_softmax_1hot( b2 + t.dot(hid, W2), target)
76 regularization = l2coef * t.sum(W2*W2) + hid_regularization 74 regularization = l2coef * t.sum(W2*W2) + hid_regularization
77 output_class = t.argmax(predictions,1) 75 output_class = t.argmax(predictions,1)
78 loss_01 = t.neq(output_class, target) 76 loss_01 = t.neq(output_class, target)
79 g_params = t.grad(nll + regularization, params) 77 g_params = t.grad(nll + regularization, params)
80 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] 78 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)]
81 self.__dict__.update(locals()); del self.self 79 setattr_and_name(self, locals())
82 self.nhid = nhid 80 self.nhid = nhid
83 self.nclass = nclass 81 self.nclass = nclass
84 self.nepochs = nepochs 82 self.nepochs = nepochs
85 self.v = Vars(lr, l2coef) 83 self.v = Vars(lr, l2coef)
86 self.params = None 84 self.params = None
87 85
88 def __call__(self, trainset=None, iparams=None): 86 def __call__(self, trainset=None, iparams=None):
89 if iparams is None: 87 if iparams is None:
90 iparams = [_randshape(self.nhid, self.nclass), _randshape(self.nclass)]\ 88 iparams = LookupList(["W","b"],[_randshape(self.nhid, self.nclass), _randshape(self.nclass)])
91 + self.v.hid_ivals() 89 + self.v.hid_params_init_vals()
92 rval = NeuralNet.Model(self, iparams) 90 rval = NeuralNet.Model(self, iparams)
93 if trainset: 91 if trainset:
94 rval.update(trainset) 92 rval.update(trainset)
95 return rval 93 return rval
94
95
96 def setattr_and_name(self, dict):
97 """This will do a self.__setattr__ for all elements in the dict
98 (except for element self). In addition it will make sure that
99 each element's .name (if it exists) is set to the element's key
100 in the dicitonary.
101 Typical usage: setattr_and_name(self, locals()) """
102 for varname,var in locals.items():
103 if var is not self:
104 if hasattr(var,"name") and not var.name:
105 var.name=varname
106 self.__setattr__(varname,var)
96 107
97 108
98 if __name__ == '__main__': 109 if __name__ == '__main__':
99 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], 110 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0],
100 [0, 1, 1], 111 [0, 1, 1],
110 [0, 1, 1], 121 [0, 1, 1],
111 [1, 0, 0], 122 [1, 0, 0],
112 [1, 1, 1]]), 123 [1, 1, 1]]),
113 {'input':slice(2)}) 124 {'input':slice(2)})
114 125
126
115 learn_algo = NeuralNet(2, 10, 3, .1, 1000) 127 learn_algo = NeuralNet(2, 10, 3, .1, 1000)
128
129 model = learn_algo()
116 130
117 model1 = learn_algo(training_set1) 131 model1 = learn_algo(training_set1)
118 132
119 model2 = learn_algo(training_set2) 133 model2 = learn_algo(training_set2)
120 134