def main(): Xtrain, Ytrain = pvml.load_dataset("mnist_train") Xtest, Ytest = pvml.load_dataset("mnist_test") plt.ion() batch_sz = 100 epochs = 250 network = pvml.MLP([Xtrain.shape[1], 128, 64, 10]) errors = [[], []] for epoch in range(1, epochs + 1): steps = Xtrain.shape[0] // batch_sz network.train(Xtrain, Ytrain, lr=1e-4, lambda_=1e-5, steps=steps, batch=batch_sz) predictions = network.inference(Xtrain)[0] training_error = (predictions != Ytrain).mean() predictions = network.inference(Xtest)[0] test_error = (predictions != Ytest).mean() errors[0].append(100 * training_error) errors[1].append(100 * test_error) msg = "Epoch {}, Training err. {:.2f}, Test err. {:.2f}" print(msg.format(epoch, 100 * training_error, 100 * test_error)) plot_errors(errors) show_weights(network.weights) show_confusion_matrix(Ytest, predictions) plt.pause(0.05) network.save("mnist_network.npz") np.savetxt("mnist_errors.txt", np.array(errors).T, fmt="%.2f") plt.ioff() plt.show()
def test_saveload(self): neurons = [2, 3, 4, 5] net1 = pvml.MLP(neurons) f = io.BytesIO() net1.save(f) f.seek(0) net2 = pvml.MLP.load(f) for w1, w2 in zip(net1.weights, net2.weights): self.assertListEqual(list(w1.ravel()), list(w2.ravel())) for b1, b2 in zip(net1.biases, net2.biases): self.assertListEqual(list(b1), list(b2))
def train_step(self, X, Y, steps): if self.net is None: counts = [X.shape[1]] + self.hidden + [Y.max() + 1] self.net = pvml.MLP(counts) self.net.train(X, Y, lr=self.lr, lambda_=self.lambda_, momentum=self.momentum, steps=steps, batch=self.batch)
def test_batch(self): X, Y = test_data.separable_circle_data_set(50, 2) net = pvml.MLP([X.shape[1], 2, 2]) net.train(X, Y, lr=1e-1, steps=1000, batch=10) Yhat, P = net.inference(X) self.assertListEqual(Y.tolist(), Yhat.tolist())