@@ -42,20 +42,24 @@ def forward(self, X):
42
42
current_input = Z
43
43
return current_input
44
44
45
- def fit_to_input (self , k , learning_rate = 1.0 , epochs = 100000 ):
45
+ def fit_to_input (self , k , learning_rate = 1.0 , mu = 0.99 , epochs = 100000 ):
46
46
# This is not very flexible, as you would ideally
47
47
# like to be able to activate any node in any hidden
48
48
# layer, not just the last layer.
49
49
# Exercise for students: modify this function to be able
50
50
# to activate neurons in the middle layers.
51
51
X0 = init_weights ((1 , self .D ))
52
52
X = theano .shared (X0 , 'X_shared' )
53
+ dX = theano .shared (np .zeros (X0 .shape ), 'dX_shared' )
53
54
Y = self .forward (X )
54
55
t = np .zeros (self .hidden_layers [- 1 ].M )
55
56
t [k ] = 1
56
57
57
58
cost = - (t * T .log (Y [0 ]) + (1 - t )* (T .log (1 - Y [0 ]))).sum ()
58
- updates = [(X , X - learning_rate * T .grad (cost , X ))]
59
+ updates = [
60
+ (X , X + mu * dX - learning_rate * T .grad (cost , X )),
61
+ (dX , mu * dX - learning_rate * T .grad (cost , X )),
62
+ ]
59
63
train = theano .function (
60
64
inputs = [],
61
65
outputs = cost ,
@@ -74,19 +78,22 @@ def fit_to_input(self, k, learning_rate=1.0, epochs=100000):
74
78
return X .eval ()
75
79
76
80
def save (self , filename ):
77
- arrays = [p .eval () for p in layer . params for layer in self . hidden_layers ]
81
+ arrays = [p .eval () for layer in self . hidden_layers for p in layer . params ]
78
82
np .savez (filename , * arrays )
79
83
80
84
@staticmethod
81
85
def load (filename , UnsupervisedModel = AutoEncoder ):
82
- dbn = DBN (0 , UnsupervisedModel )
86
+ dbn = DBN ([] , UnsupervisedModel )
83
87
npz = np .load (filename )
84
88
dbn .hidden_layers = []
85
89
count = 0
86
90
for i in xrange (0 , len (npz .files ), 3 ):
87
- W = npz [npz [i ]]
88
- bh = npz [npz [i + 1 ]]
89
- bo = npz [npz [i + 2 ]]
91
+ W = npz ['arr_%s' % i ]
92
+ bh = npz ['arr_%s' % (i + 1 )]
93
+ bo = npz ['arr_%s' % (i + 2 )]
94
+
95
+ if i == 0 :
96
+ dbn .D = W .shape [0 ]
90
97
91
98
ae = UnsupervisedModel .createFromArrays (W , bh , bo , count )
92
99
dbn .hidden_layers .append (ae )
0 commit comments