Skip to content

Commit 471f7bc

Browse files
updates
1 parent ee38bac commit 471f7bc

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

unsupervised_class2/autoencoder.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=Fa
2424
self.params = [self.W, self.bh, self.bo]
2525
self.forward_params = [self.W, self.bh]
2626

27+
# TODO: technically these should be reset before doing backprop
2728
self.dW = theano.shared(np.zeros(W0.shape), 'dW_%s' % self.id)
2829
self.dbh = theano.shared(np.zeros(self.M), 'dbh_%s' % self.id)
2930
self.dbo = theano.shared(np.zeros(D), 'dbo_%s' % self.id)
@@ -33,11 +34,6 @@ def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=Fa
3334
X_in = T.matrix('X_%s' % self.id)
3435
X_hat = self.forward_output(X_in)
3536

36-
forward_op = theano.function(
37-
inputs=[X_in],
38-
outputs=X_hat,
39-
)
40-
4137
# attach it to the object so it can be used later
4238
# must be sigmoidal because the output is also a sigmoid
4339
H = T.nnet.sigmoid(X_in.dot(self.W) + self.bh)
@@ -71,7 +67,7 @@ def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=Fa
7167
for j in xrange(n_batches):
7268
batch = X[j*batch_sz:(j*batch_sz + batch_sz)]
7369
train_op(batch)
74-
the_cost = cost_op(X)
70+
the_cost = cost_op(X) # technically we could also get the cost for Xtest here
7571
print "j / n_batches:", j, "/", n_batches, "cost:", the_cost
7672
costs.append(the_cost)
7773
if show_fig:
@@ -89,6 +85,16 @@ def forward_output(self, X):
8985
Y = T.nnet.sigmoid(Z.dot(self.W.T) + self.bo)
9086
return Y
9187

88+
@staticmethod
89+
def createFromArrays(W, bh, bo, an_id):
90+
ae = AutoEncoder(W.shape[1], an_id)
91+
ae.W = theano.shared(W, 'W_%s' % ae.id)
92+
ae.bh = theano.shared(bh, 'bh_%s' % ae.id)
93+
ae.bo = theano.shared(bo, 'bo_%s' % ae.id)
94+
ae.params = [ae.W, ae.bh, ae.bo]
95+
ae.forward_params = [ae.W, ae.bh]
96+
return ae
97+
9298

9399
class DNN(object):
94100
def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
@@ -135,14 +141,9 @@ def fit(self, X, Y, Xtest, Ytest, pretrain=True, learning_rate=0.01, mu=0.99, re
135141
targets = T.ivector('Targets')
136142
pY = self.forward(X_in)
137143

138-
forward_op = theano.function(
139-
inputs=[X_in],
140-
outputs=pY,
141-
)
142-
143144
# squared_magnitude = [(p*p).sum() for p in self.params]
144145
# reg_cost = T.sum(squared_magnitude)
145-
cost = -T.mean( T.log(pY)[T.arange(pY.shape[0]), targets] ) #+ reg*reg_cost
146+
cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) ) #+ reg*reg_cost
146147
prediction = self.predict(X_in)
147148
cost_predict_op = theano.function(
148149
inputs=[X_in, targets],
@@ -171,7 +172,6 @@ def fit(self, X, Y, Xtest, Ytest, pretrain=True, learning_rate=0.01, mu=0.99, re
171172
Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz)]
172173
train_op(Xbatch, Ybatch)
173174
the_cost, the_prediction = cost_predict_op(Xtest, Ytest)
174-
# print "prediction:", the_prediction, "test:", Ytest
175175
error = error_rate(the_prediction, Ytest)
176176
print "j / n_batches:", j, "/", n_batches, "cost:", the_cost, "error:", error
177177
costs.append(the_cost)

unsupervised_class2/rbm.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, M, an_id):
1515
self.id = an_id
1616
self.rng = RandomStreams()
1717

18-
def fit(self, X, learning_rate=0.1, epochs=10, batch_sz=100, show_fig=False):
18+
def fit(self, X, learning_rate=0.1, epochs=1, batch_sz=100, show_fig=False):
1919
N, D = X.shape
2020
n_batches = N / batch_sz
2121

@@ -27,6 +27,7 @@ def fit(self, X, learning_rate=0.1, epochs=10, batch_sz=100, show_fig=False):
2727
self.forward_params = [self.W, self.c]
2828

2929
# we won't use this to fit the RBM but we will use these for backpropagation later
30+
# TODO: technically they should be reset before doing backprop
3031
self.dW = theano.shared(np.zeros(W0.shape), 'dW_%s' % self.id)
3132
self.dc = theano.shared(np.zeros(self.M), 'dbh_%s' % self.id)
3233
self.db = theano.shared(np.zeros(D), 'dbo_%s' % self.id)
@@ -75,7 +76,7 @@ def fit(self, X, learning_rate=0.1, epochs=10, batch_sz=100, show_fig=False):
7576
for j in xrange(n_batches):
7677
batch = X[j*batch_sz:(j*batch_sz + batch_sz)]
7778
train_op(batch)
78-
the_cost = cost_op(X)
79+
the_cost = cost_op(X) # technically we could also get the cost for Xtest here
7980
print "j / n_batches:", j, "/", n_batches, "cost:", the_cost
8081
costs.append(the_cost)
8182
if show_fig:
@@ -103,6 +104,16 @@ def forward_output(self, X):
103104
Y = T.nnet.sigmoid(Z.dot(self.W.T) + self.b)
104105
return Y
105106

107+
@staticmethod
108+
def createFromArrays(W, c, b, an_id):
109+
rbm = AutoEncoder(W.shape[1], an_id)
110+
rbm.W = theano.shared(W, 'W_%s' % rbm.id)
111+
rbm.c = theano.shared(c, 'c_%s' % rbm.id)
112+
rbm.b = theano.shared(b, 'b_%s' % rbm.id)
113+
rbm.params = [rbm.W, rbm.c, rbm.b]
114+
rbm.forward_params = [rbm.W, rbm.c]
115+
return rbm
116+
106117

107118
def main():
108119
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()

unsupervised_class2/unsupervised.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
2222
count += 1
2323

2424
def fit(self, X, pretrain_epochs=1):
25+
self.D = X.shape[1] # save for later
26+
2527
current_input = X
2628
for ae in self.hidden_layers:
2729
ae.fit(current_input, epochs=pretrain_epochs)
@@ -39,6 +41,58 @@ def forward(self, X):
3941
current_input = Z
4042
return current_input
4143

44+
def fit_to_input(self, k, learning_rate=1.0, epochs=100000):
45+
# This is not very flexible, as you would ideally
46+
# like to be able to activate any node in any hidden
47+
# layer, not just the last layer.
48+
# Exercise for students: modify this function to be able
49+
# to activate neurons in the middle layers.
50+
X0 = init_weights((1, self.D))
51+
X = theano.shared(X0, 'X_shared')
52+
Y = self.forward(X)
53+
t = np.zeros(self.hidden_layers[-1].M)
54+
t[k] = 1
55+
56+
cost = -(t*T.log(Y[0]) + (1 - t)*(T.log(1 - Y[0]))).sum()
57+
updates = [(X, X - learning_rate*T.grad(cost, X))]
58+
train = theano.function(
59+
inputs=[],
60+
outputs=cost,
61+
updates=updates,
62+
)
63+
64+
costs = []
65+
for i in xrange(epochs):
66+
if i % 1000 == 0:
67+
print "epoch:", i
68+
the_cost = train()
69+
costs.append(the_cost)
70+
plt.plot(costs)
71+
plt.show()
72+
73+
return X.eval()
74+
75+
def save(self, filename):
76+
arrays = [p.eval() for p in layer.params for layer in self.hidden_layers]
77+
np.savez(filename, *arrays)
78+
79+
@staticmethod
80+
def load(filename, UnsupervisedModel=AutoEncoder):
81+
dbn = DBN(0, UnsupervisedModel)
82+
npz = np.load(filename)
83+
dbn.hidden_layers = []
84+
count = 0
85+
for i in xrange(0, len(npz.files), 3):
86+
W = npz[npz[i]]
87+
bh = npz[npz[i+1]]
88+
bo = npz[npz[i+2]]
89+
90+
ae = UnsupervisedModel.createFromArrays(W, bh, bo, count)
91+
dbn.hidden_layers.append(ae)
92+
count += 1
93+
return dbn
94+
95+
4296
def main():
4397
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
4498
dbn = DBN([1000, 750, 500], UnsupervisedModel=AutoEncoder)

0 commit comments

Comments
 (0)