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

Method fit

unsupervised_class2/autoencoder.py:142–214  ·  view source on GitHub ↗
(self, X, Y, Xtest, Ytest,
        pretrain=True,
        train_head_only=False,
        learning_rate=0.1,
        mu=0.99,
        reg=0.0,
        epochs=1,
        batch_sz=100)

Source from the content-addressed store, hash-verified

140
141
142 def fit(self, X, Y, Xtest, Ytest,
143 pretrain=True,
144 train_head_only=False,
145 learning_rate=0.1,
146 mu=0.99,
147 reg=0.0,
148 epochs=1,
149 batch_sz=100):
150
151 # cast to float32
152 learning_rate = np.float32(learning_rate)
153 mu = np.float32(mu)
154 reg = np.float32(reg)
155
156 # greedy layer-wise training of autoencoders
157 pretrain_epochs = 2
158 if not pretrain:
159 pretrain_epochs = 0
160
161 current_input = X
162 for ae in self.hidden_layers:
163 ae.fit(current_input, epochs=pretrain_epochs)
164
165 # create current_input for the next layer
166 current_input = ae.hidden_op(current_input)
167
168 # initialize logistic regression layer
169 N = len(Y)
170 K = len(set(Y))
171 W0 = init_weights((self.hidden_layers[-1].M, K))
172 self.W = theano.shared(W0, "W_logreg")
173 self.b = theano.shared(np.zeros(K, dtype=np.float32), "b_logreg")
174
175 self.params = [self.W, self.b]
176 if not train_head_only:
177 for ae in self.hidden_layers:
178 self.params += ae.forward_params
179
180 X_in = T.matrix('X_in')
181 targets = T.ivector('Targets')
182 pY = self.forward(X_in)
183
184 squared_magnitude = [(p*p).sum() for p in self.params]
185 reg_cost = T.sum(squared_magnitude)
186 cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) ) + reg*reg_cost
187 prediction = self.predict(X_in)
188 cost_predict_op = theano.function(
189 inputs=[X_in, targets],
190 outputs=[cost, prediction],
191 )
192
193 updates = momentum_updates(cost, self.params, mu, learning_rate)
194 train_op = theano.function(
195 inputs=[X_in, targets],
196 updates=updates,
197 )
198
199 n_batches = N // batch_sz

Callers 3

mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95

Calls 5

forwardMethod · 0.95
predictMethod · 0.95
init_weightsFunction · 0.90
error_rateFunction · 0.90
momentum_updatesFunction · 0.70

Tested by

no test coverage detected