Skip to content

Commit 41e3af7

Browse files
fix load and save
1 parent 6b43685 commit 41e3af7

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

unsupervised_class2/unsupervised.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,24 @@ def forward(self, X):
4242
current_input = Z
4343
return current_input
4444

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):
4646
# This is not very flexible, as you would ideally
4747
# like to be able to activate any node in any hidden
4848
# layer, not just the last layer.
4949
# Exercise for students: modify this function to be able
5050
# to activate neurons in the middle layers.
5151
X0 = init_weights((1, self.D))
5252
X = theano.shared(X0, 'X_shared')
53+
dX = theano.shared(np.zeros(X0.shape), 'dX_shared')
5354
Y = self.forward(X)
5455
t = np.zeros(self.hidden_layers[-1].M)
5556
t[k] = 1
5657

5758
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+
]
5963
train = theano.function(
6064
inputs=[],
6165
outputs=cost,
@@ -74,19 +78,22 @@ def fit_to_input(self, k, learning_rate=1.0, epochs=100000):
7478
return X.eval()
7579

7680
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]
7882
np.savez(filename, *arrays)
7983

8084
@staticmethod
8185
def load(filename, UnsupervisedModel=AutoEncoder):
82-
dbn = DBN(0, UnsupervisedModel)
86+
dbn = DBN([], UnsupervisedModel)
8387
npz = np.load(filename)
8488
dbn.hidden_layers = []
8589
count = 0
8690
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]
9097

9198
ae = UnsupervisedModel.createFromArrays(W, bh, bo, count)
9299
dbn.hidden_layers.append(ae)

unsupervised_class2/visualize_features.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
def main(loadfile=None, savefile=None):
1515
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
1616
if loadfile:
17+
dbn = DBN.load(loadfile)
18+
else:
1719
dbn = DBN([1000, 750, 500, 10]) # AutoEncoder is default
1820
dbn = DBN([1000, 750, 500, 10], UnsupervisedModel=RBM)
1921
dbn.fit(Xtrain, pretrain_epochs=15)
20-
else:
21-
dbn.load(loadfile)
2222

2323
if savefile:
2424
dbn.save(savefile)
2525

26+
# first layer features
2627
# initial weight is D x M
2728
# W = dbn.hidden_layers[0].W.eval()
2829
# for i in xrange(dbn.hidden_layers[0].M):
@@ -32,10 +33,7 @@ def main(loadfile=None, savefile=None):
3233
# if should_quit == 'n':
3334
# break
3435

35-
# TODO: save the weights so I can initialize from them later
36-
# and just do the last step
37-
38-
# print features learned in the last layer
36+
# features learned in the last layer
3937
for k in xrange(dbn.hidden_layers[-1].M):
4038
# activate the kth node
4139
X = dbn.fit_to_input(k)
@@ -47,4 +45,11 @@ def main(loadfile=None, savefile=None):
4745

4846

4947
if __name__ == '__main__':
50-
main()
48+
# to load a saved file
49+
main(loadfile='saved.npz')
50+
51+
# to neither load nor save
52+
# main()
53+
54+
# to save a trained unsupervised deep network
55+
# main(savefile='saved.npz')

0 commit comments

Comments
 (0)