Created
June 14, 2016 09:04
-
-
Save harujoh/12d36e3a4a7ee8ffd97bba1803f33990 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
from chainer import cuda | |
from chainer import optimizers | |
import chainer | |
import chainer.functions as F | |
import data | |
import numpy as np | |
import time | |
import six | |
gpu_flag = -1 | |
if gpu_flag >= 0: | |
cuda.check_cuda_available() | |
xp = cuda.cupy if gpu_flag >= 0 else np | |
print ('load MNIST dataset') | |
mnist = data.load_mnist_data() | |
mnist['data'] = mnist['data'].astype(np.float32) | |
mnist['data'] /= 255 | |
mnist['data'] = mnist['data'].reshape(70000, 1,28,28) | |
mnist['target'] = mnist['target'].astype(np.int32) | |
N = 60000 | |
x_train, x_test = np.split(mnist['data'], [N]) | |
y_train, y_test = np.split(mnist['target'], [N]) | |
N_test = y_test.size | |
model = chainer.FunctionSet(conv1=F.Convolution2D(1,32,5,pad=2), | |
conv2=F.Convolution2D(32,64,5,pad=2), | |
fl3=F.Linear(7*7*64,1024), | |
fl4=F.Linear(1024,10)) | |
#model = chainer.FunctionSet(cv1=F.Convolution2D(1,30, 3), | |
# bn2 = F.BatchNormalization( 30), | |
# ln3=F.Linear(5070, 1000), | |
# ln4=F.Linear(1000, 10)) | |
if gpu_flag >= 0: | |
cuda.get_device(gpu_flag).use() | |
model.to_gpu() | |
#def forward(x_data, y_data, train=True): | |
# # Neural net architecture | |
# x, t = chainer.Variable(x_data), chainer.Variable(y_data) | |
# h = F.max_pooling_2d(F.dropout(F.relu(model.bn2(model.cv1(x))), train=train),2) | |
# h = F.dropout(F.relu(model.ln3(h)), train=train) | |
# y = model.ln4(h) | |
# return F.softmax_cross_entropy(y, t), F.accuracy(y, t) | |
def forward(x_data, y_data, train=True): | |
x, t = chainer.Variable(x_data), chainer.Variable(y_data) | |
h_conv1 = F.relu(model.conv1(x)) | |
h_pool1 = F.max_pooling_2d(h_conv1, 2) | |
h_conv2 = F.relu(model.conv2(h_pool1)) | |
h_pool2 = F.max_pooling_2d(h_conv2, 2) | |
h_fc1 = F.relu(model.fl3(h_pool2)) | |
h_fc1_drop = F.dropout(h_fc1, ratio=0.5,train=train) | |
y = model.fl4(h_fc1_drop) | |
return F.softmax_cross_entropy(y, t), F.accuracy(y, t) | |
# setup optimizer | |
optimizer = optimizers.Adam() | |
optimizer.setup(model) | |
n_epoch = 20 #20000 | |
batchsize= 50 | |
start_time = time.clock() | |
for epoch in six.moves.range(1, n_epoch + 1): | |
print('epoch', epoch) | |
# training | |
perm = np.random.permutation(N) | |
sum_accuracy = 0 | |
sum_loss = 0 | |
for i in six.moves.range(0, N, batchsize): | |
x_batch = xp.asarray(x_train[perm[i:i + batchsize]]) | |
y_batch = xp.asarray(y_train[perm[i:i + batchsize]]) | |
optimizer.zero_grads() | |
print ('step {0}/{1} forward'.format(int(i/50)+1, int(N/batchsize))) | |
loss, acc = forward(x_batch, y_batch) | |
print ('step {0}/{1} backward'.format(int(i/50)+1, int(N/batchsize))) | |
loss.backward() | |
print ('step {0}/{1} update'.format(int(i/50)+1, int(N/batchsize))) | |
optimizer.update() | |
sum_loss += float(loss.data) * len(y_batch) | |
sum_accuracy += float(acc.data) * len(y_batch) | |
print('train mean loss={}, accuracy={}'.format(sum_loss / N, sum_accuracy / N)) | |
# evaluation | |
sum_accuracy = 0 | |
sum_loss = 0 | |
for i in six.moves.range(0, N_test, batchsize): | |
x_batch = xp.asarray(x_test[i:i + batchsize]) | |
y_batch = xp.asarray(y_test[i:i + batchsize]) | |
loss, acc = forward(x_batch, y_batch, train=False) | |
sum_loss += float(loss.data) * len(y_batch) | |
sum_accuracy += float(acc.data) * len(y_batch) | |
print('test mean loss={}, accuracy={}'.format(sum_loss / N_test, sum_accuracy / N_test)) | |
end_time = time.clock() | |
print (end_time - start_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment