(self, X, learning_rate=0.5, mu=0.99, epochs=50, batch_sz=100, show_fig=False)
| 43 | self.hidden_layer_sizes = hidden_layer_sizes |
| 44 | |
| 45 | def fit(self, X, learning_rate=0.5, mu=0.99, epochs=50, batch_sz=100, show_fig=False): |
| 46 | # cast hyperparams |
| 47 | learning_rate = np.float32(learning_rate) |
| 48 | mu = np.float32(mu) |
| 49 | |
| 50 | N, D = X.shape |
| 51 | n_batches = N // batch_sz |
| 52 | |
| 53 | mi = D |
| 54 | self.layers = [] |
| 55 | self.params = [] |
| 56 | for mo in self.hidden_layer_sizes: |
| 57 | layer = Layer(mi, mo) |
| 58 | self.layers.append(layer) |
| 59 | self.params += layer.params |
| 60 | mi = mo |
| 61 | |
| 62 | X_in = T.matrix('X') |
| 63 | X_hat = self.forward(X_in) |
| 64 | |
| 65 | cost = -(X_in * T.log(X_hat) + (1 - X_in) * T.log(1 - X_hat)).mean() |
| 66 | cost_op = theano.function( |
| 67 | inputs=[X_in], |
| 68 | outputs=cost, |
| 69 | ) |
| 70 | |
| 71 | updates = momentum_updates(cost, self.params, mu, learning_rate) |
| 72 | train_op = theano.function( |
| 73 | inputs=[X_in], |
| 74 | outputs=cost, |
| 75 | updates=updates, |
| 76 | ) |
| 77 | |
| 78 | costs = [] |
| 79 | for i in range(epochs): |
| 80 | print("epoch:", i) |
| 81 | X = shuffle(X) |
| 82 | for j in range(n_batches): |
| 83 | batch = X[j*batch_sz:(j*batch_sz + batch_sz)] |
| 84 | c = train_op(batch) |
| 85 | if j % 100 == 0: |
| 86 | print("j / n_batches:", j, "/", n_batches, "cost:", c) |
| 87 | costs.append(c) |
| 88 | if show_fig: |
| 89 | plt.plot(costs) |
| 90 | plt.show() |
| 91 | |
| 92 | def forward(self, X): |
| 93 | Z = X |
no test coverage detected