Mercurial > pylearn
annotate test_mlp.py @ 225:8bc16220b29a
deprecation note for mlp
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Fri, 23 May 2008 17:11:39 -0400 |
parents | ebbb0e749565 |
children |
rev | line source |
---|---|
121 | 1 |
2 from mlp import * | |
133 | 3 import dataset |
186
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
4 import nnet_ops |
121 | 5 |
183
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
6 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
7 from functools import partial |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
8 def separator(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
9 print "===================" |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
10 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
11 def what(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
12 print "#%i" % i, node |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
13 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
14 def parents(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
15 print [input.step for input in node.inputs] |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
16 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
17 def input_shapes(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
18 print "input shapes: ", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
19 for r in node.inputs: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
20 if hasattr(r.value, 'shape'): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
21 print r.value.shape, |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
22 else: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
23 print "no_shape", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
24 print |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
25 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
26 def input_types(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
27 print "input types: ", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
28 for r in node.inputs: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
29 print r.type, |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
30 print |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
31 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
32 def output_shapes(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
33 print "output shapes:", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
34 for r in node.outputs: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
35 if hasattr(r.value, 'shape'): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
36 print r.value.shape, |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
37 else: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
38 print "no_shape", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
39 print |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
40 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
41 def output_types(debugger, i, node, *ths): |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
42 print "output types:", |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
43 for r in node.outputs: |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
44 print r.type, |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
45 print |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
46 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
47 |
121 | 48 def test0(): |
183
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
49 linker = 'c|py' |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
50 #linker = partial(theano.gof.DebugLinker, linkers = [theano.gof.OpWiseCLinker], |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
51 # debug_pre = [separator, what, parents, input_types, input_shapes], |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
52 # debug_post = [output_shapes, output_types], |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
53 # compare_fn = lambda x, y: numpy.all(x == y)) |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
54 |
25d0a0c713da
did some debugging of test_mlp
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
180
diff
changeset
|
55 nnet = OneHiddenLayerNNetClassifier(10,2,.001,1000, linker = linker) |
133 | 56 training_set = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
57 [0, 1, 1], | |
58 [1, 0, 1], | |
59 [1, 1, 1]]), | |
60 {'input':slice(2),'target':2}) | |
61 fprop=nnet(training_set) | |
121 | 62 |
135
0d8e721cc63c
Fixed bugs in dataset to make test_mlp.py work
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
133
diff
changeset
|
63 output_ds = fprop(training_set) |
0d8e721cc63c
Fixed bugs in dataset to make test_mlp.py work
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
133
diff
changeset
|
64 |
0d8e721cc63c
Fixed bugs in dataset to make test_mlp.py work
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
133
diff
changeset
|
65 for fieldname in output_ds.fieldNames(): |
0d8e721cc63c
Fixed bugs in dataset to make test_mlp.py work
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
133
diff
changeset
|
66 print fieldname+"=",output_ds[fieldname] |
121 | 67 |
186
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
68 def test1(): |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
69 nnet = ManualNNet(2, 10,3,.1,1000) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
70 training_set = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
71 [0, 1, 1], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
72 [1, 0, 1], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
73 [1, 1, 1]]), |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
74 {'input':slice(2),'target':2}) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
75 fprop=nnet(training_set) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
76 |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
77 output_ds = fprop(training_set) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
78 |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
79 for fieldname in output_ds.fieldNames(): |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
80 print fieldname+"=",output_ds[fieldname] |
121 | 81 |
186
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
82 def test2(): |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
83 training_set = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
84 [0, 1, 1], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
85 [1, 0, 1], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
86 [1, 1, 1]]), |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
87 {'input':slice(2),'target':2}) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
88 nin, nhid=2, 10 |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
89 def sigm_layer(input): |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
90 W1 = t.matrix('W1') |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
91 b1 = t.vector('b1') |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
92 return (nnet_ops.sigmoid(b1 + t.dot(input, W1)), |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
93 [W1, b1], |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
94 [(numpy.random.rand(nin, nhid) -0.5) * 0.001, numpy.zeros(nhid)]) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
95 nnet = ManualNNet(nin, nhid, 3, .1, 1000, hidden_layer=sigm_layer) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
96 fprop=nnet(training_set) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
97 |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
98 output_ds = fprop(training_set) |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
99 |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
100 for fieldname in output_ds.fieldNames(): |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
101 print fieldname+"=",output_ds[fieldname] |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
102 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
103 def test_interface_0(): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
104 learner = ManualNNet(2, 10, 3, .1, 1000) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
105 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
106 model = learner(training_set) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
107 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
108 model2 = learner(training_set) # trains model a second time |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
109 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
110 learner.update(additional_data) # modifies nnet and model by side-effect |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
111 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
112 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
113 def test_interface2_1(): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
114 learn_algo = ManualNNet(2, 10, 3, .1, 1000) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
115 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
116 prior = learn_algo() |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
117 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
118 model1 = learn_algo(training_set1) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
119 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
120 model2 = learn_algo(training_set2) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
121 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
122 model2.update(additional_data) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
123 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
124 n_match = 0 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
125 for o1, o2 in zip(model1.use(test_data), model2.use(test_data)): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
126 n_match += (o1 == o2) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
127 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
128 print n_match |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
186
diff
changeset
|
129 |
186
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
130 test1() |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
131 test2() |
562f308873f0
added ManualNNet
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
183
diff
changeset
|
132 |