Mercurial > pylearn
comparison pylearn/sandbox/scan_inputs_groups.py @ 694:69947f4e9c0e
added a Mask creation Op and fixed some bugs
author | Xavier Glorot <glorotxa@iro.umontreal.ca> |
---|---|
date | Thu, 14 May 2009 19:34:21 -0400 |
parents | 0457dfa6fcad |
children | 33e13d8ca7d3 |
comparison
equal
deleted
inserted
replaced
693:ee7026de9681 | 694:69947f4e9c0e |
---|---|
103 raise NotImplementedError('size of index different of inputs list size',idx_list) | 103 raise NotImplementedError('size of index different of inputs list size',idx_list) |
104 if max(idx_list) >= (len(args)-2)+1 : | 104 if max(idx_list) >= (len(args)-2)+1 : |
105 raise NotImplementedError('index superior to weight list length',idx_list) | 105 raise NotImplementedError('index superior to weight list length',idx_list) |
106 for i in range(len(args[1])): | 106 for i in range(len(args[1])): |
107 if (args[1][i].shape)[0] != batchsize: | 107 if (args[1][i].shape)[0] != batchsize: |
108 raise NotImplementedError('different batchsize in the inputs list',args[1][i]) | 108 raise NotImplementedError('different batchsize in the inputs list',args[1][i].shape) |
109 for i in range(len(args)-2): | 109 for i in range(len(args)-2): |
110 if (args[2+i].shape)[1] != n_hid: | 110 if (args[2+i].shape)[1] != n_hid: |
111 raise NotImplementedError('different length of hidden in the weights list',args[2+i]) | 111 raise NotImplementedError('different length of hidden in the weights list',args[2+i].shape) |
112 | 112 |
113 for i in range(len(idx_list)): | 113 for i in range(len(idx_list)): |
114 if idx_list[i]>0: | 114 if idx_list[i]>0: |
115 if hidcalc: | 115 if hidcalc: |
116 self.m.dotin(args[1][i],args[2+int(idx_list[i]-1)]) | 116 self.m.dotin(args[1][i],args[2+int(idx_list[i]-1)]) |
166 raise NotImplementedError('size of index different of inputs list size',idx_list) | 166 raise NotImplementedError('size of index different of inputs list size',idx_list) |
167 if max(idx_list) >= (len(args)-3)+1 : | 167 if max(idx_list) >= (len(args)-3)+1 : |
168 raise NotImplementedError('index superior to weight list length',idx_list) | 168 raise NotImplementedError('index superior to weight list length',idx_list) |
169 for i in range(len(args[1])): | 169 for i in range(len(args[1])): |
170 if (args[1][i].shape)[0] != batchsize: | 170 if (args[1][i].shape)[0] != batchsize: |
171 raise NotImplementedError('different batchsize in the inputs list',args[1][i]) | 171 raise NotImplementedError('different batchsize in the inputs list',args[1][i].shape) |
172 for i in range(len(args)-3): | 172 for i in range(len(args)-3): |
173 if (args[2+i].shape)[1] != n_hid: | 173 if (args[2+i].shape)[1] != n_hid: |
174 raise NotImplementedError('different length of hidden in the weights list',args[2+i]) | 174 raise NotImplementedError('different length of hidden in the weights list',args[2+i].shape) |
175 | 175 |
176 zcalc = [False for i in range(len(args)-3)] | 176 zcalc = [False for i in range(len(args)-3)] |
177 | 177 |
178 for i in range(len(idx_list)): | 178 for i in range(len(idx_list)): |
179 if idx_list[i]>0: | 179 if idx_list[i]>0: |
232 raise NotImplementedError('index superior to weight list length',idx_list) | 232 raise NotImplementedError('index superior to weight list length',idx_list) |
233 if len(idx_list) != len(args[1]) : | 233 if len(idx_list) != len(args[1]) : |
234 raise NotImplementedError('size of index different of inputs list size',idx_list) | 234 raise NotImplementedError('size of index different of inputs list size',idx_list) |
235 for i in range(len(args)-3): | 235 for i in range(len(args)-3): |
236 if (args[3+i].shape)[0] != n_hid: | 236 if (args[3+i].shape)[0] != n_hid: |
237 raise NotImplementedError('different length of hidden in the weights list',args[3+i]) | 237 raise NotImplementedError('different length of hidden in the weights list',args[3+i].shape) |
238 | 238 |
239 zcalc = [False for i in idx_list] | 239 zcalc = [False for i in idx_list] |
240 z[0] = [None for i in idx_list] | 240 z[0] = [None for i in idx_list] |
241 | 241 |
242 for i in range(len(idx_list)): | 242 for i in range(len(idx_list)): |
302 raise NotImplementedError('index superior to weight list length',idx_list) | 302 raise NotImplementedError('index superior to weight list length',idx_list) |
303 if len(idx_list) != len(args[1]) : | 303 if len(idx_list) != len(args[1]) : |
304 raise NotImplementedError('size of index different of inputs list size',idx_list) | 304 raise NotImplementedError('size of index different of inputs list size',idx_list) |
305 for i in range(len(args)-4): | 305 for i in range(len(args)-4): |
306 if (args[3+i].shape)[0] != n_hid: | 306 if (args[3+i].shape)[0] != n_hid: |
307 raise NotImplementedError('different length of hidden in the weights list',args[3+i]) | 307 raise NotImplementedError('different length of hidden in the weights list',args[3+i].shape) |
308 | 308 |
309 zidx=numpy.zeros((len(idx_list)+1)) | 309 zidx=numpy.zeros((len(idx_list)+1)) |
310 | 310 |
311 for i in range(len(idx_list)): | 311 for i in range(len(idx_list)): |
312 if idx_list[i] == 0: | 312 if idx_list[i] == 0: |
375 def perform(self, node, (idx_list,inputs_list,noise_level_bit,noise_level_group), (y,z)): | 375 def perform(self, node, (idx_list,inputs_list,noise_level_bit,noise_level_group), (y,z)): |
376 | 376 |
377 if len(idx_list) != len(inputs_list) : | 377 if len(idx_list) != len(inputs_list) : |
378 raise NotImplementedError('size of index different of inputs list size',idx_list) | 378 raise NotImplementedError('size of index different of inputs list size',idx_list) |
379 | 379 |
380 y[0] = [-i if (i>0 and not(self.R.noisify_group_bool(noise_level_group))) else i for i in idx_list] | 380 y[0] = numpy.asarray([-i if (i>0 and not(self.R.noisify_group_bool(noise_level_group))) else i for i in idx_list]) |
381 z[0] = [(self.R.noisify_bit(inputs_list[i],noise_level_bit) if y[0][i]>0 else numpy.zeros((inputs_list[i].shape)))\ | 381 z[0] = [(self.R.noisify_bit(inputs_list[i],noise_level_bit) if y[0][i]>0 else numpy.zeros((inputs_list[i].shape)))\ |
382 for i in range(len(inputs_list))] | 382 for i in range(len(inputs_list))] |
383 | 383 |
384 def grad(self,args,gz): | 384 def grad(self,args,gz): |
385 return [None,None,None,None] | 385 return [None,None,None,None] |
386 | 386 |
387 | 387 |
388 def __hash__(self): | 388 def __hash__(self): |
389 return hash(ScanNoise)^hash(self.seed)^hash(self.R)^12254 | 389 return hash(ScanNoise)^hash(self.seed)^hash(self.R.rand)^12254 |
390 | 390 |
391 def __str__(self): | 391 def __str__(self): |
392 return "ScanNoise" | 392 return "ScanNoise" |
393 | 393 |
394 scannoise=ScanNoise() | 394 scannoise=ScanNoise() |
498 def __hash__(self): | 498 def __hash__(self): |
499 return hash(ScanBiasDecGrad)^41256 | 499 return hash(ScanBiasDecGrad)^41256 |
500 | 500 |
501 def __str__(self): | 501 def __str__(self): |
502 return "ScanBiasDecGrad" | 502 return "ScanBiasDecGrad" |
503 | |
504 # Mask construction------------------------------------ | |
505 class ScanMask(Op): | |
506 """This Op takes an index list (as tensor.ivector) and a list of weigths. | |
507 It will construct a list of T.iscalar representing the Mask | |
508 to do the correct regularisation on the weigths""" | |
509 def __init__(self,encbool=True): | |
510 self.encbool = encbool | |
511 | |
512 def make_node(self, idx_list, weights_list): | |
513 idx_list = Checkidx_list(idx_list) | |
514 weights_list = Checkweights_list(weights_list) | |
515 return Apply(self, [idx_list] + weights_list, [T.iscalar() for i in range(len(weights_list))]) | |
516 | |
517 def perform(self, node, args, z): | |
518 if self.encbool: | |
519 idx_list = args[0][args[0]>0] | |
520 dim = 1 | |
521 else: | |
522 idx_list = abs(args[0][args[0] != 0]) | |
523 dim = 0 | |
524 n_hid = args[1].shape[dim] | |
525 | |
526 if max(idx_list) >= (len(args)-1)+1 : | |
527 raise NotImplementedError('index superior to weights list length',idx_listdec) | |
528 for i in range(len(args)-1): | |
529 if args[1+i].shape[dim] != n_hid: | |
530 raise NotImplementedError('different length of hidden in the encoding weights list',args[1+i].shape) | |
531 | |
532 for i in range(len(args[1:])): | |
533 z[i][0] = numpy.asarray((idx_list == i+1).sum(),dtype='int32') | |
534 | |
535 def __hash__(self): | |
536 return hash(ScanMask)^hash(self.encbool)^11447 | |
537 | |
538 def grad(self,args,gz): | |
539 return [None] * len(args) | |
540 | |
541 def __str__(self): | |
542 if self.encbool: | |
543 string = "Enc" | |
544 else: | |
545 string = "Dec" | |
546 return "ScanMask" + string | |
547 | |
548 scanmaskenc=ScanMask(True) | |
549 scanmaskdec=ScanMask(False) |