/
group_gater.py
929 lines (760 loc) · 33.6 KB
/
group_gater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 13 19:56:19 2013
@author: Nicholas Léonard
"""
import time, sys
from pylearn2.utils import serial
from itertools import izip
from pylearn2.utils import safe_zip
from collections import OrderedDict
from pylearn2.utils import safe_union
import numpy as np
import theano.sparse as S
from theano.gof.op import get_debug_values
from theano.printing import Print
from theano import function
from theano import config
from theano.sandbox.rng_mrg import MRG_RandomStreams
from theano import tensor as T
import theano
from pylearn2.linear.matrixmul import MatrixMul
from pylearn2.models.model import Model
from pylearn2.utils import sharedX
from pylearn2.models.mlp import MLP, Softmax, Layer, Linear
from pylearn2.space import VectorSpace, Conv2DSpace, CompositeSpace, Space
def init_balanced_groups(p,size,combine='sum'):
c = (size[0]*size[1]*p)/(size[0]+size[1])
print p,size,c
c = int(c)
assert c > 0
G1 = np.zeros(size, dtype='bool')
G2 = np.zeros(size, dtype='bool')
# a row is a group of members
row = np.asarray([1]*c + (size[1]-c)*[0], dtype='bool')
# a col is a member of groups
col = np.asarray([1]*c + (size[0]-c)*[0], dtype='bool')
for i in xrange(size[0]):
np.random.shuffle(row)
G1[i,:] = row
for j in xrange(size[1]):
np.random.shuffle(col)
G2[:,j] = col
if combine == 'sum':
G = G1.astype(theano.config.floatX) + G2.astype(theano.config.floatX)
elif combine == 'or':
G = np.logical_or(G1,G2).astype(theano.config.floatX)
else:
raise NotImplementedError()
return G
class Group1(Layer):
"""
We use the biased low-variance estimator to estimate gradients of
stochastic neurons of the gater. Each such gater neuron represents a
group of neurons (an expert) in the main part
"""
def __init__(self,
gater_dim,
hidden_dim,
expert_dim,
layer_name,
hidden_activation = 'tanh',
expert_activation = None,
derive_sigmoid = True,
sparsity_target = 0.1,
sparsity_cost_coeff = 1.0,
irange = [None,None,None],
istdev = [None,None,None],
sparse_init = [None,None,None],
sparse_stdev = [1.,1.,1.],
init_bias = [0.,0.,0.],
W_lr_scale = [None,None,None],
b_lr_scale = [None,None,None],
max_col_norm = [None,None,None],
weight_decay_coeff = [None,None,None]):
'''
params
------
dim:
number of units on output layer
hidden_dim:
number of units on hidden layer of non-linear part
hidden_activation:
activation function used on hidden layer of non-linear part
sparsity_target:
target sparsity of the output layer.
sparsity_cost_coeff:
coefficient of the sparsity constraint when summing costs
weight_decay_coeff:
coefficients of L2 weight decay when summing costs
other:
in the lists of params, the first index is for the linear
part, while the second and third indices are for the first
and second layer of the non-linear part, respectively
'''
self.__dict__.update(locals())
del self.self
def get_lr_scalers(self):
rval = OrderedDict()
for i in range(3):
if self.W_lr_scale[i] is not None:
rval[self.W[i]] = self.W_lr_scale[i]
if self.b_lr_scale[i] is not None:
rval[self.b[i]] = self.b_lr_scale[i]
return rval
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
# units per expert times number of experts:
self.dim = self.expert_dim*self.gater_dim
self.output_space = VectorSpace(self.dim)
self.input_dims = [self.input_dim, self.hidden_dim]
self.output_dims = [self.hidden_dim, self.gater_dim]
self.W = [None,None]
self.b = [None,None]
for i in range(2):
self._init_inner_layer(i)
self.W = [None] + self.W
self.b = [None] + self.b
self._init_expert_layer()
self.stoch_grad = sharedX(0)
self.kl_grad = sharedX(0)
self.linear_grad = sharedX(0)
def _init_expert_layer(self, idx=0):
rng = self.mlp.rng
if self.irange[idx] is not None:
assert self.istdev[idx] is None
assert self.sparse_init[idx] is None
W = rng.uniform(-self.irange[idx], self.irange[idx],
(self.input_dim, self.gater_dim, self.expert_dim))
elif self.istdev[idx] is not None:
assert self.sparse_init[idx] is None
W = rng.randn(self.input_dim, self.gater_dim,
self.expert_dim) * self.istdev[idx]
else:
assert self.sparse_init[idx] is not None
raise NotImplementedError()
W = np.zeros((self.input_dim, self.gater_dim, self.expert_dim))
for i in xrange(self.output_dims[idx]):
assert self.sparse_init[idx] <= self.input_dims[idx]
for j in xrange(self.sparse_init[idx]):
idx2 = rng.randint(0, self.input_dims[idx])
while W[idx2, i] != 0:
idx2 = rng.randint(0, self.input_dims[idx])
W[idx2, i] = rng.randn()
W *= self.sparse_stdev[idx]
W = sharedX(W)
W.name = self.layer_name + '_W' + str(idx)
b = sharedX( np.zeros((self.gater_dim,self.expert_dim)) \
+ self.init_bias[idx], \
name = self.layer_name + '_b' + str(idx))
self.W[idx] = W
self.b[idx] = b
def _init_inner_layer(self, idx):
rng = self.mlp.rng
if self.irange[idx] is not None:
assert self.istdev[idx] is None
assert self.sparse_init[idx] is None
W = rng.uniform(-self.irange[idx], self.irange[idx],
(self.input_dims[idx], self.output_dims[idx]))
elif self.istdev[idx] is not None:
assert self.sparse_init[idx] is None
W = rng.randn(self.input_dims[idx], self.output_dims[idx]) \
* self.istdev[idx]
else:
assert self.sparse_init[idx] is not None
W = np.zeros((self.input_dims[idx], self.output_dims[idx]))
for i in xrange(self.output_dims[idx]):
assert self.sparse_init[idx] <= self.input_dims[idx]
for j in xrange(self.sparse_init[idx]):
idx2 = rng.randint(0, self.input_dims[idx])
while W[idx2, i] != 0:
idx2 = rng.randint(0, self.input_dims[idx])
W[idx2, i] = rng.randn()
W *= self.sparse_stdev[idx]
W = sharedX(W)
W.name = self.layer_name + '_W' + str(idx)
b = sharedX( np.zeros((self.output_dims[idx],)) \
+ self.init_bias[idx], \
name = self.layer_name + '_b' + str(idx))
self.W[idx] = W
self.b[idx] = b
def censor_updates(self, updates):
for idx in range(3):
if self.max_col_norm[idx] is not None:
W = self.W[idx]
if W in updates:
updated_W = updates[W]
col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0))
desired_norms = T.clip(col_norms, 0, self.max_col_norm[idx])
updates[W] = updated_W * desired_norms / (1e-7 + col_norms)
def get_params(self):
rval = [self.W[0], self.W[1], self.W[2], self.b[0], self.b[1], self.b[2]]
return rval
def get_weights(self):
rval = []
for i in range(3):
W = self.W[i].get_value()
rval.append(W)
return rval
def set_weights(self, weights):
for i in range(3):
W = self.W[i]
W.set_value(weights[i])
def set_biases(self, biases):
for i in range(3):
self.b[i].set_value(biases[i])
def get_biases(self):
rval = []
for i in range(3):
rval.append(self.b[i].get_value())
return rval
def get_weights_format(self):
return ('v', 'h')
def get_weights_topo(self):
raise NotImplementedError()
def get_monitoring_channels(self):
rval = OrderedDict()
rval['stoch_grad'] = self.stoch_grad
rval['kl_grad'] = self.kl_grad
rval['linear_grad'] = self.linear_grad
for i in range(3):
sq_W = T.sqr(self.W[i])
row_norms = T.sqrt(sq_W.sum(axis=1))
col_norms = T.sqrt(sq_W.sum(axis=0))
rval['row_norms_max'+str(i)] = row_norms.max()
rval['col_norms_max'+str(i)] = col_norms.max()
return rval
def get_monitoring_channels_from_state(self, state, target=None):
rval = OrderedDict()
# sparisty of outputs:
rval['mean_output_sparsity'] = self.m_mean.mean()
# proportion of sigmoids that have prob > 0.5
# good when equal to sparsity
floatX = theano.config.floatX
rval['mean_sparsity_prop'] \
= T.cast(T.gt(self.m_mean, 0.5),floatX).mean()
# same as above but for intermediate thresholds:
rval['mean_sparsity_prop0.2'] \
= T.cast(T.gt(self.m_mean, 0.2),floatX).mean()
rval['mean_sparsity_prop0.3'] \
= T.cast(T.gt(self.m_mean, 0.3),floatX).mean()
rval['mean_sparsity_prop0.4'] \
= T.cast(T.gt(self.m_mean, 0.4),floatX).mean()
# or just plain standard deviation (less is bad):
rval['output_stdev'] = self.m_mean.std()
# stdev of unit stdevs (more is bad)
rval['output_meta_stdev'] = self.m_mean.std(axis=0).std()
# max and min proportion of these probs per unit
prop_per_unit = T.cast(T.gt(self.m_mean, 0.5),floatX).mean(0)
# if this is high, it means a unit is likely always active (bad)
rval['max_unit_sparsity_prop'] = prop_per_unit.max()
rval['min_unit_sparsity_prop'] = prop_per_unit.min()
# in both cases, high means units are popular (bad)
# proportion of units with p>0.5 more than 50% of time:
rval['mean_unit_sparsity_meta_prop'] \
= T.cast(T.gt(prop_per_unit,0.5),floatX).mean()
# proportion of units with p>0.5 more than 75% of time:
rval['mean_unit_sparsity_meta_prop2'] \
= T.cast(T.gt(prop_per_unit,0.75),floatX).mean()
return rval
def fprop(self, state_below, threshold=None, stochastic=True):
self.input_space.validate(state_below)
if self.requires_reformat:
if not isinstance(state_below, tuple):
for sb in get_debug_values(state_below):
if sb.shape[0] != self.dbm.batch_size:
raise ValueError("self.dbm.batch_size is %d but got shape of %d" % (self.dbm.batch_size, sb.shape[0]))
assert reduce(lambda x,y: x * y, sb.shape[1:]) == self.input_dim
state_below = self.input_space.format_as(state_below, self.desired_space)
self.x = state_below
# linear part
if isinstance(self.x, S.SparseVariable):
raise NotImplementedError()
z = S.dot(self.x,self.W[0]) + self.b[0]
else:
# w : (input_dim,gater_dim,expert_dim)
# b : (gater_dim,expert_dim)
# x : (batch_size,input_dim)
# z : (batch_size,gater_dim,expert_dim)
z = T.tensordot(self.x,self.W[0],axes=[[1],[0]]) + self.b[0].dimshuffle('x',0,1)
# activate hidden units of non-linear part
if self.expert_activation is None:
self.z = z
elif self.expert_activation == 'tanh':
self.z = T.tanh(z)
elif self.expert_activation == 'sigmoid':
self.z = T.nnet.sigmoid(z)
elif self.expert_activation == 'rectifiedlinear':
self.z = T.maximum(0, z)
else:
raise NotImplementedError()
# first layer non-linear part
if isinstance(self.x, S.SparseVariable):
h = S.dot(self.x,self.W[1]) + self.b[1]
else:
h = T.dot(self.x,self.W[1]) + self.b[1]
# activate hidden units of non-linear part
if self.hidden_activation is None:
self.h = h
elif self.hidden_activation == 'tanh':
self.h = T.tanh(h)
elif self.hidden_activation == 'sigmoid':
self.h = T.nnet.sigmoid(h)
elif self.hidden_activation == 'softmax':
self.h = T.nnet.softmax(h)
elif self.hidden_activation == 'rectifiedlinear':
self.h = T.maximum(0, h)
else:
raise NotImplementedError()
# second layer non-linear part
self.a = T.dot(self.h,self.W[2]) + self.b[2]
# activate non-linear part to get bernouilli probabilities
self.m_mean = T.nnet.sigmoid(self.a)
if threshold is None:
if stochastic:
# sample from bernouili probs to generate a mask
rng = MRG_RandomStreams(self.mlp.rng.randint(2**15))
self.m = rng.binomial(size = self.m_mean.shape, n = 1,
p = self.m_mean, dtype=self.m_mean.type.dtype)
else:
self.m = self.m_mean
else:
# deterministic mask:
self.m = T.cast(T.gt(self.m_mean, threshold), \
theano.config.floatX)
# mask output of experts part with samples from gater part
# m: (batch_size, gater_dim)
# z: (batch_size, gater_dim, expert_dim)
# p: (batch_size, gater_dim*expert_dim)
self.p = (self.m.dimshuffle(0,1,'x') * self.z).flatten(2)
if self.layer_name is not None:
self.z.name = self.layer_name + '_z'
self.h.name = self.layer_name + '_h'
self.a.name = self.layer_name + '_a'
self.m_mean.name = self.layer_name + '_m_mean'
self.m.name = self.layer_name + '_m'
self.p.name = self.layer_name + '_p'
return self.p
def test_fprop(self, state_below, threshold=None, stochastic=True):
return self.fprop(state_below, threshold, stochastic)
def cost(self, Y, Y_hat):
return self.cost_from_cost_matrix(self.cost_matrix(Y, Y_hat))
def cost_from_cost_matrix(self, cost_matrix):
return cost_matrix.sum(axis=1).mean()
def cost_matrix(self, Y, Y_hat):
return T.sqr(Y - Y_hat)
def get_gradients(self, known_grads, loss):
'''
Computes gradients and updates for this layer given the known
gradients of the upper layers, and the vector of losses for the
batch.
'''
updates = OrderedDict()
cost = self.get_kl_divergence() + self.get_weight_decay()
# gradient of experts.
params = [self.W[0], self.b[0]]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.m, self.x],
disconnected_inputs='raise')
cost_grads = T.grad(cost=cost, wrt=params,
consider_constant=[self.m, self.x],
disconnected_inputs='ignore')
updates[self.linear_grad] = T.abs_(grads[0]).mean()
for i in range(len(grads)):
grads[i] += cost_grads[i]
gradients = OrderedDict(izip(params, grads))
# gradients of non-linear part:
## start by getting gradients at binary mask:
params = [self.m]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.m, self.x],
disconnected_inputs='raise')
print "grads at bin", grads
# estimate gradient at simoid input using above:
grad_m = grads[0]
if self.derive_sigmoid:
# multiplying by derivative of sigmoid is optional:
known_grads[self.a] \
= grad_m * self.m_mean * (1. - self.m_mean)
else:
known_grads[self.a] = grad_m
params = [self.W[1],self.W[2],self.b[1],self.b[2]]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.z, self.x],
disconnected_inputs='raise')
updates[self.stoch_grad] = T.abs_(grads[1]).mean()
cost_grads = T.grad(cost=cost, wrt=params,
consider_constant=[self.z, self.x],
disconnected_inputs='ignore')
updates[self.kl_grad] = T.abs_(cost_grads[1]).mean()
for i in range(len(grads)):
grads[i] += cost_grads[i]
gradients.update(OrderedDict(izip(params, grads)))
return gradients, updates
def get_kl_divergence(self):
'''
Minimize KL-divergence of unit binomial distributions with
binomial distribution of probability self.sparsity_target.
This could also be modified to keep a running average of unit
samples
'''
e = 1e-6
cost = - self.sparsity_cost_coeff * ( \
(self.sparsity_target * T.log(e+self.m_mean.mean(axis=0))) \
+((1.-self.sparsity_target) * T.log(e+(1.-self.m_mean.mean(axis=0)))) \
).sum()
return cost
def get_weight_decay(self):
rval = 0
for i in range(3):
if self.weight_decay_coeff[i] is not None:
rval += self.weight_decay_coeff[i]*T.sqr(self.W[i]).sum()
return rval
class Group2(Layer):
"""
Biased low-variance estimator.
Each expert group is a random set of expert units.
If an expert unit is found in two winning groups, it will have twice
the activation.
Group membership is static. Winning groups are not.
"""
def __init__(self,
dim,
gater_dim,
hidden_dim,
group_prob,
layer_name,
hidden_activation = 'tanh',
expert_activation = None,
derive_sigmoid = True,
sparsity_target = 0.1,
sparsity_cost_coeff = 1.0,
irange = [None,None,None],
istdev = [None,None,None],
sparse_init = [None,None,None],
sparse_stdev = [1.,1.,1.],
init_bias = [0.,0.,0.],
W_lr_scale = [None,None,None],
b_lr_scale = [None,None,None],
max_col_norm = [None,None,None],
weight_decay_coeff = [None,None,None]):
'''
params
------
dim:
number of units on output layer
hidden_dim:
number of units on hidden layer of non-linear part
hidden_activation:
activation function used on hidden layer of non-linear part
sparsity_target:
target sparsity of the output layer.
sparsity_cost_coeff:
coefficient of the sparsity constraint when summing costs
weight_decay_coeff:
coefficients of L2 weight decay when summing costs
other:
in the lists of params, the first index is for the linear
part, while the second and third indices are for the first
and second layer of the non-linear part, respectively
'''
self.__dict__.update(locals())
del self.self
self.groups = init_balanced_groups(group_prob,(gater_dim,dim))
n = sparsity_target/group_prob
print 'choose', n
self.final_sparsity_target = sparsity_target
self.sparsity_target = n/float(gater_dim)
print 'sparsity target', self.sparsity_target
def get_lr_scalers(self):
rval = OrderedDict()
for i in range(3):
if self.W_lr_scale[i] is not None:
rval[self.W[i]] = self.W_lr_scale[i]
if self.b_lr_scale[i] is not None:
rval[self.b[i]] = self.b_lr_scale[i]
return rval
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
self.output_space = VectorSpace(self.dim)
self.input_dims = [self.input_dim, self.input_dim, self.hidden_dim]
self.output_dims = [self.dim, self.hidden_dim, self.gater_dim]
self.W = [None,None,None]
self.b = [None,None,None]
for i in range(3):
self._init_inner_layer(i)
self.stoch_grad = sharedX(0)
self.kl_grad = sharedX(0)
self.linear_grad = sharedX(0)
def _init_inner_layer(self, idx):
rng = self.mlp.rng
if self.irange[idx] is not None:
assert self.istdev[idx] is None
assert self.sparse_init[idx] is None
W = rng.uniform(-self.irange[idx], self.irange[idx],
(self.input_dims[idx], self.output_dims[idx]))
elif self.istdev[idx] is not None:
assert self.sparse_init[idx] is None
W = rng.randn(self.input_dims[idx], self.output_dims[idx]) \
* self.istdev[idx]
else:
assert self.sparse_init[idx] is not None
W = np.zeros((self.input_dims[idx], self.output_dims[idx]))
for i in xrange(self.output_dims[idx]):
assert self.sparse_init[idx] <= self.input_dims[idx]
for j in xrange(self.sparse_init[idx]):
idx2 = rng.randint(0, self.input_dims[idx])
while W[idx2, i] != 0:
idx2 = rng.randint(0, self.input_dims[idx])
W[idx2, i] = rng.randn()
W *= self.sparse_stdev[idx]
W = sharedX(W)
W.name = self.layer_name + '_W' + str(idx)
b = sharedX( np.zeros((self.output_dims[idx],)) \
+ self.init_bias[idx], \
name = self.layer_name + '_b' + str(idx))
self.W[idx] = W
self.b[idx] = b
def censor_updates(self, updates):
for idx in range(3):
if self.max_col_norm[idx] is not None:
W = self.W[idx]
if W in updates:
updated_W = updates[W]
col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0))
desired_norms = T.clip(col_norms, 0, self.max_col_norm[idx])
updates[W] = updated_W * desired_norms / (1e-7 + col_norms)
def get_params(self):
rval = [self.W[0], self.W[1], self.W[2], self.b[0], self.b[1], self.b[2]]
return rval
def get_weights(self):
rval = []
for i in range(3):
W = self.W[i]
rval.append(W.get_value())
return rval
def set_weights(self, weights):
for i in range(3):
W = self.W[i]
W.set_value(weights[i])
def set_biases(self, biases):
for i in range(3):
self.b[i].set_value(biases[i])
def get_biases(self):
rval = []
for i in range(3):
rval.append(self.b[i].get_value())
return rval
def get_weights_format(self):
return ('v', 'h')
def get_weights_topo(self):
raise NotImplementedError()
def get_monitoring_channels(self):
rval = OrderedDict()
rval['stoch_grad'] = self.stoch_grad
rval['kl_grad'] = self.kl_grad
rval['linear_grad'] = self.linear_grad
for i in range(3):
sq_W = T.sqr(self.W[i])
row_norms = T.sqrt(sq_W.sum(axis=1))
col_norms = T.sqrt(sq_W.sum(axis=0))
rval['row_norms_max'+str(i)] = row_norms.max()
rval['col_norms_max'+str(i)] = col_norms.max()
return rval
def get_monitoring_channels_from_state(self, state, target=None):
rval = OrderedDict()
# sparisty of outputs:
rval['mean_output_sparsity'] = self.m_mean.mean()
# proportion of sigmoids that have prob > 0.5
# good when equal to sparsity
floatX = theano.config.floatX
rval['mean_sparsity_prop0.5'] \
= T.cast(T.gt(self.m_mean, 0.5),floatX).mean()
# same as above but for intermediate thresholds:
rval['mean_sparsity_prop0.2'] \
= T.cast(T.gt(self.m_mean, 0.2),floatX).mean()
rval['mean_sparsity_prop0.3'] \
= T.cast(T.gt(self.m_mean, 0.3),floatX).mean()
rval['mean_sparsity_prop0.4'] \
= T.cast(T.gt(self.m_mean, 0.4),floatX).mean()
rval['post_mean_sparsity_prop0'] \
= T.cast(T.gt(self.m2, 0),floatX).mean()
# or just plain standard deviation (less is bad):
rval['output_stdev'] = self.m_mean.std()
# stdev of unit stdevs (more is bad)
rval['output_meta_stdev'] = self.m_mean.std(axis=0).std()
# max and min proportion of these probs per unit
prop_per_unit = T.cast(T.gt(self.m_mean, 0.5),floatX).mean(0)
# if this is high, it means a unit is likely always active (bad)
rval['max_unit_sparsity_prop'] = prop_per_unit.max()
rval['min_unit_sparsity_prop'] = prop_per_unit.min()
# in both cases, high means units are popular (bad)
# proportion of units with p>0.5 more than 50% of time:
rval['mean_unit_sparsity_meta_prop'] \
= T.cast(T.gt(prop_per_unit,0.5),floatX).mean()
# proportion of units with p>0.5 more than 75% of time:
rval['mean_unit_sparsity_meta_prop2'] \
= T.cast(T.gt(prop_per_unit,0.75),floatX).mean()
return rval
def fprop(self, state_below, threshold=None, stochastic=True):
self.input_space.validate(state_below)
if self.requires_reformat:
if not isinstance(state_below, tuple):
for sb in get_debug_values(state_below):
if sb.shape[0] != self.dbm.batch_size:
raise ValueError("self.dbm.batch_size is %d but got shape of %d" % (self.dbm.batch_size, sb.shape[0]))
assert reduce(lambda x,y: x * y, sb.shape[1:]) == self.input_dim
state_below = self.input_space.format_as(state_below, self.desired_space)
self.x = state_below
# experts part
if isinstance(self.x, S.SparseVariable):
z = S.dot(self.x,self.W[0]) + self.b[0]
else:
z = T.dot(self.x,self.W[0]) + self.b[0]
# activate hidden units of gater part
if self.expert_activation is None:
self.z = z
elif self.hidden_activation == 'tanh':
self.z = T.tanh(z)
elif self.expert_activation == 'sigmoid':
self.z = T.nnet.sigmoid(z)
elif self.expert_activation == 'softmax':
self.z = T.nnet.softmax(z)
elif self.expert_activation == 'rectifiedlinear':
self.z = T.maximum(0, z)
else:
raise NotImplementedError()
# first layer of gater
if isinstance(self.x, S.SparseVariable):
h = S.dot(self.x,self.W[1]) + self.b[1]
else:
h = T.dot(self.x,self.W[1]) + self.b[1]
# activate hidden units of gater
if self.hidden_activation is None:
self.h = h
elif self.hidden_activation == 'tanh':
self.h = T.tanh(h)
elif self.hidden_activation == 'sigmoid':
self.h = T.nnet.sigmoid(h)
elif self.hidden_activation == 'softmax':
self.h = T.nnet.softmax(h)
elif self.hidden_activation == 'rectifiedlinear':
self.h = T.maximum(0, h)
else:
raise NotImplementedError()
# second layer gater
self.a = T.dot(self.h,self.W[2]) + self.b[2]
# activate gater output to get bernouilli probabilities
self.m_mean = T.nnet.sigmoid(self.a)
if threshold is None:
if stochastic:
# sample from bernouili probs to generate a mask
rng = MRG_RandomStreams(self.mlp.rng.randint(2**15))
self.m = rng.binomial(size = self.m_mean.shape, n = 1,
p = self.m_mean, dtype=self.m_mean.type.dtype)
else:
self.m = self.m_mean
else:
# deterministic mask:
self.m = T.cast(T.gt(self.m_mean, threshold), \
theano.config.floatX)
self.m2 = T.dot(self.m, self.groups)
# mask expert output with samples from gater
self.p = self.m2 * self.z
if self.layer_name is not None:
self.z.name = self.layer_name + '_z'
self.h.name = self.layer_name + '_h'
self.a.name = self.layer_name + '_a'
self.m_mean.name = self.layer_name + '_m_mean'
self.m.name = self.layer_name + '_m'
self.p.name = self.layer_name + '_p'
return self.p
def test_fprop(self, state_below, threshold=None, stochastic=True):
return self.fprop(state_below, threshold, stochastic)
def cost(self, Y, Y_hat):
return self.cost_from_cost_matrix(self.cost_matrix(Y, Y_hat))
def cost_from_cost_matrix(self, cost_matrix):
return cost_matrix.sum(axis=1).mean()
def cost_matrix(self, Y, Y_hat):
return T.sqr(Y - Y_hat)
def get_gradients(self, known_grads, loss):
'''
Computes gradients and updates for this layer given the known
gradients of the upper layers, and the vector of losses for the
batch.
'''
updates = OrderedDict()
cost = self.get_kl_divergence() + self.get_weight_decay()
# gradient of experts
params = [self.W[0], self.b[0]]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.m2, self.x],
disconnected_inputs='raise')
cost_grads = T.grad(cost=cost, wrt=params,
consider_constant=[self.m2, self.x],
disconnected_inputs='ignore')
updates[self.linear_grad] = T.abs_(grads[0]).mean()
for i in range(len(grads)):
grads[i] += cost_grads[i]
gradients = OrderedDict(izip(params, grads))
# gradients of gater
## start by getting gradients at binary mask:
params = [self.m]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.m, self.x],
disconnected_inputs='raise')
print "grads at bin", grads
# estimate gradient at simoid input using above:
grad_m = grads[0]
if self.derive_sigmoid:
# multiplying by derivative of sigmoid is optional:
known_grads[self.a] \
= grad_m * self.m_mean * (1. - self.m_mean)
else:
known_grads[self.a] = grad_m
params = [self.W[1],self.W[2],self.b[1],self.b[2]]
grads = T.grad(cost=None, wrt=params, known_grads=known_grads,
consider_constant=[self.z, self.x],
disconnected_inputs='raise')
updates[self.stoch_grad] = T.abs_(grads[1]).mean()
cost_grads = T.grad(cost=cost, wrt=params,
consider_constant=[self.z, self.x],
disconnected_inputs='ignore')
updates[self.kl_grad] = T.abs_(cost_grads[1]).mean()
for i in range(len(grads)):
grads[i] += cost_grads[i]
gradients.update(OrderedDict(izip(params, grads)))
return gradients, updates
def get_kl_divergence(self):
'''
Minimize KL-divergence of unit binomial distributions with
binomial distribution of probability self.sparsity_target.
This could also be modified to keep a running average of unit
samples
'''
e = 1e-6
cost = - self.sparsity_cost_coeff * ( \
(self.sparsity_target * T.log(e+self.m_mean.mean(axis=0))) \
+((1.-self.sparsity_target) * T.log(e+(1.-self.m_mean.mean(axis=0)))) \
).sum()
return cost
def get_weight_decay(self):
rval = 0
for i in range(3):
if self.weight_decay_coeff[i] is not None:
rval += self.weight_decay_coeff[i]*T.sqr(self.W[i]).sum()
return rval