| 128 | self.prediction = tf.argmax(input=logits, axis=1) |
| 129 | |
| 130 | def fit(self, X, Y, Xtest, Ytest, pretrain=True, epochs=1, batch_sz=100): |
| 131 | N = len(X) |
| 132 | |
| 133 | # greedy layer-wise training of autoencoders |
| 134 | pretrain_epochs = 1 |
| 135 | if not pretrain: |
| 136 | pretrain_epochs = 0 |
| 137 | |
| 138 | current_input = X |
| 139 | for ae in self.hidden_layers: |
| 140 | ae.fit(current_input, epochs=pretrain_epochs) |
| 141 | |
| 142 | # create current_input for the next layer |
| 143 | current_input = ae.transform(current_input) |
| 144 | |
| 145 | n_batches = N // batch_sz |
| 146 | costs = [] |
| 147 | print("supervised training...") |
| 148 | for i in range(epochs): |
| 149 | print("epoch:", i) |
| 150 | X, Y = shuffle(X, Y) |
| 151 | for j in range(n_batches): |
| 152 | Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz)] |
| 153 | Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz)] |
| 154 | self.session.run( |
| 155 | self.train_op, |
| 156 | feed_dict={self.X: Xbatch, self.Y: Ybatch} |
| 157 | ) |
| 158 | c, p = self.session.run( |
| 159 | (self.cost, self.prediction), |
| 160 | feed_dict={self.X: Xtest, self.Y: Ytest |
| 161 | }) |
| 162 | error = error_rate(p, Ytest) |
| 163 | if j % 10 == 0: |
| 164 | print("j / n_batches:", j, "/", n_batches, "cost:", c, "error:", error) |
| 165 | costs.append(c) |
| 166 | plt.plot(costs) |
| 167 | plt.show() |
| 168 | |
| 169 | def forward(self, X): |
| 170 | current_input = X |