MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / AutoEncoder

Class AutoEncoder

unsupervised_class2/autoencoder.py:36–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35
36class AutoEncoder(object):
37 def __init__(self, M, an_id):
38 self.M = M
39 self.id = an_id
40
41 def fit(self, X, learning_rate=0.5, mu=0.99, epochs=1, batch_sz=100, show_fig=False):
42 # cast to float
43 mu = np.float32(mu)
44 learning_rate = np.float32(learning_rate)
45
46 N, D = X.shape
47 n_batches = N // batch_sz
48
49 W0 = init_weights((D, self.M))
50 self.W = theano.shared(W0, 'W_%s' % self.id)
51 self.bh = theano.shared(np.zeros(self.M, dtype=np.float32), 'bh_%s' % self.id)
52 self.bo = theano.shared(np.zeros(D, dtype=np.float32), 'bo_%s' % self.id)
53 self.params = [self.W, self.bh, self.bo]
54 self.forward_params = [self.W, self.bh]
55
56 # TODO: technically these should be reset before doing backprop
57 self.dW = theano.shared(np.zeros(W0.shape), 'dW_%s' % self.id)
58 self.dbh = theano.shared(np.zeros(self.M), 'dbh_%s' % self.id)
59 self.dbo = theano.shared(np.zeros(D), 'dbo_%s' % self.id)
60 self.dparams = [self.dW, self.dbh, self.dbo]
61 self.forward_dparams = [self.dW, self.dbh]
62
63 X_in = T.matrix('X_%s' % self.id)
64 X_hat = self.forward_output(X_in)
65
66 # attach it to the object so it can be used later
67 # must be sigmoidal because the output is also a sigmoid
68 H = T.nnet.sigmoid(X_in.dot(self.W) + self.bh)
69 self.hidden_op = theano.function(
70 inputs=[X_in],
71 outputs=H,
72 )
73
74 # save this for later so we can call it to
75 # create reconstructions of input
76 self.predict = theano.function(
77 inputs=[X_in],
78 outputs=X_hat,
79 )
80
81 cost = -(X_in * T.log(X_hat) + (1 - X_in) * T.log(1 - X_hat)).flatten().mean()
82 cost_op = theano.function(
83 inputs=[X_in],
84 outputs=cost,
85 )
86
87
88
89 updates = momentum_updates(cost, self.params, mu, learning_rate)
90 train_op = theano.function(
91 inputs=[X_in],
92 updates=updates,
93 )

Callers 3

createFromArraysMethod · 0.70
createFromArraysMethod · 0.70
test_single_autoencoderFunction · 0.70

Calls

no outgoing calls

Tested by 1

test_single_autoencoderFunction · 0.56