Last active
January 11, 2016 15:21
-
-
Save EnsekiTT/3de4c9112d443f5aadd2 to your computer and use it in GitHub Desktop.
Chainer_MNIST_without_datainput.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import numpy as np | |
| import pandas as pd | |
| import chainer | |
| import chainer.functions as F | |
| import chainer.links as L | |
| from chainer import optimizers | |
| from chainer import serializers | |
| batchsize = 100 | |
| n_epoch = 20 | |
| n_units = 1000 | |
| # Prepare network | |
| class MnistMLP(chainer.Chain): | |
| def __init__(self, n_in, n_units, n_out): | |
| super(MnistMLP, self).__init__( | |
| l1=L.Linear(n_in, n_units), | |
| l2=L.Linear(n_units, n_units), | |
| l3=L.Linear(n_units, n_units), | |
| l4=L.Linear(n_units, n_out), | |
| ) | |
| def __call__(self, x): | |
| h1 = F.relu(self.l1(x)) | |
| h2 = F.relu(self.l2(h1)) | |
| h3 = F.relu(self.l3(h2)) | |
| return self.l4(h3) | |
| # Prepare dataset | |
| print('load MNIST dataset') | |
| mnist = pd.read_pickle('mnist.pkl') | |
| mnistd = {'data': [], 'target': []} | |
| mnistd['data'] = np.array(mnist[0][0]).astype(np.float32) | |
| mnistd['data'] /= 255 | |
| mnistd['target'] = np.array(mnist[0][1]).astype(np.int32) | |
| N = 40000 | |
| x_train, x_test = np.split(mnistd['data'], [N]) | |
| y_train, y_test = np.split(mnistd['target'], [N]) | |
| N_test = y_test.size | |
| # Prepare multi-layer perceptron model, defined in net.py | |
| model = L.Classifier(MnistMLP(784, n_units, 10)) | |
| # Setup optimizer | |
| optimizer = optimizers.Adam() | |
| optimizer.setup(model) | |
| # Learning loop | |
| for epoch in range(1, n_epoch + 1): | |
| print('epoch', epoch) | |
| # training | |
| perm = np.random.permutation(N) | |
| sum_accuracy = 0 | |
| sum_loss = 0 | |
| for i in range(0, N, batchsize): | |
| x = chainer.Variable(np.asarray(x_train[perm[i:i + batchsize]])) | |
| t = chainer.Variable(np.asarray(y_train[perm[i:i + batchsize]])) | |
| # Pass the loss function (Classifier defines it) and its arguments | |
| optimizer.update(model, x, t) | |
| sum_loss += float(model.loss.data) * len(t.data) | |
| sum_accuracy += float(model.accuracy.data) * len(t.data) | |
| print('train mean loss={}, accuracy={}'.format( | |
| sum_loss / N, sum_accuracy / N)) | |
| # evaluation | |
| sum_accuracy = 0 | |
| sum_loss = 0 | |
| for i in range(0, N_test, batchsize): | |
| x = chainer.Variable(np.asarray(x_test[i:i + batchsize]), | |
| volatile='on') | |
| t = chainer.Variable(np.asarray(y_test[i:i + batchsize]), | |
| volatile='on') | |
| loss = model(x, t) | |
| sum_loss += float(loss.data) * len(t.data) | |
| sum_accuracy += float(model.accuracy.data) * len(t.data) | |
| print('test mean loss={}, accuracy={}'.format( | |
| sum_loss / N_test, sum_accuracy / N_test)) | |
| # Save the model and the optimizer | |
| print('save the model') | |
| serializers.save_hdf5('mlp.model', model) | |
| print('save the optimizer') | |
| serializers.save_hdf5('mlp.state', optimizer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment