1 """abstract tester methods and common test operations"""
2
3 import unittest
4 import tempfile
5 import numpy as np
6 rng = np.random.RandomState()
7 import theano
8 import theano.tensor as T
9 from .. import archive
10
13 self.assertEqual( np.max( np.abs(x) ), 0 )
14
16 self.assertAlmostEqual( np.max( np.abs(x) ), 0 )
17
23
29
31
32
33
34 self.assertGreater( np.max( np.abs(x-y) ), 0 )
35
38 self.rng = rng
39 self.__rw = None
40
42 raise NotImplementedError("should be overriden")
43
45 raise NotImplementedError("should be overriden")
46
49
51 "save copies of the weights of a model"
52
53 if self.__rw is None:
54 m = self.get_model()
55 self.__rw = []
56 for i in range( len(m) ):
57 self.__rw.append( map(np.copy, m[i].get_weights() ) )
58 self.__rw = tuple( self.__rw )
59 return self.__rw
60
68
70 """dependence of cost on weight L1/L2 norms"""
71
72 M = self.get_model()
73 l1 = rng.randint(0, 10) / 10
74 l2 = rng.randint(0, 10) / 10
75 Y = T.lvector("Y")
76 y = self.get_output()
77
78 x = self.get_input()
79
80 cf = theano.function(inputs=[M[0].input], outputs=M[-1].p_y_given_x)
81 print cf(x)
82 print y
83
84 cf = theano.function(inputs=[M[0].input, Y], outputs=M.cost(Y, l1, l2))
85 cf_00 = theano.function(inputs=[M[0].input, Y], outputs=M.cost(Y, 0, 0))
86 w_summ = l1 * M.weight_norm("l1")
87 w_summ_sq = l2 * M.weight_norm("l2")
88
89 self.assertEqual( cf(x,y) - cf_00(x,y), w_summ + w_summ_sq )
90
91 M = self.zero_model()
92 cf = theano.function(inputs=[M[0].input, Y], outputs=M.cost(Y, l1, l2))
93 cf_00 = theano.function(inputs=[M[0].input, Y], outputs=M.cost(Y, 0, 0))
94 x = self.get_input()
95 self.assertEqual( cf(x,y) , cf_00(x,y) )
96
98 """model can save and load"""
99 modela = self.get_model()
100 modelb = self.get_model()
101
102 are_eq = []
103 for i in range(len(modela)):
104 rw = modela[i].get_weights()
105 zw = modelb[i].get_weights()
106 for (r,z) in zip(rw, zw):
107 are_eq.append( np.all( r==z ) )
108 if are_eq[-1]:
109 print r, z
110 print r-z
111 print
112 self.assertFalse( all(are_eq) )
113
114 with tempfile.NamedTemporaryFile(delete=False, suffix="." + archive.__HDF_SUFFIX__) as fd:
115 path = fd.name + ":model_weights"
116 modela.save( path )
117 modelb.load( path )
118
119 are_eq = []
120 for i in range(len(modela)):
121 rw = modela[i].get_weights()
122 zw = modelb[i].get_weights()
123 for (r,z) in zip(rw, zw):
124 are_eq.append( np.all( r==z) )
125 self.assertTrue( all(are_eq) )
126
127
129 """test class for CPLayer, HiddenLayer and LogisticRegression Layer
130
131 initializes a random number generator. Needs the get_layer method and self.x (input vector)
132 to be defined and from that defines:
133
134 - zero_weights,
135 - rnd_weights,
136 - zero_layer and rnd_layer"""
137
139 self.rng = rng
140 self.__zw = None
141 self.__rl = None
142
144 raise NotImplementedError("should be overriden")
145
148
152
154 "cached zero weights"
155
156 if self.__zw is None:
157 self.__zw = map(np.copy, self.rnd_weights())
158 for i in range(len(self.__zw)):
159 self.__zw[i] -= self.__zw[i]
160 self.__zw = tuple( self.__zw )
161 return self.__zw
162
167
173
175 if self.__rl is None:
176 self.__rl = self.get_layer()
177 return self.__rl
178
182
184 def _weights_speeds_eqto(l, wlst):
185 for z,w,s in zip(wlst, l.get_weights(), l.get_speeds()):
186 self.assertTrue( np.all(w == z) )
187 self.assertTrue( np.all(s == z-z) )
188
189 _weights_speeds_eqto( self.zero_layer(), self.zero_weights() )
190 _weights_speeds_eqto( self.rnd_layer(), self.rnd_weights() )
191
192
193 self.assertTrue( np.all(self.get_layer().get_weights()[1] == 0) )
194
195
206
209