Skip to content

Instantly share code, notes, and snippets.

@koreyou
Created July 20, 2017 06:23
Show Gist options
  • Save koreyou/78dddf5a250c9ff1a7dca0d1f9afcefe to your computer and use it in GitHub Desktop.
Save koreyou/78dddf5a250c9ff1a7dca0d1f9afcefe to your computer and use it in GitHub Desktop.
import chainer
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions
# Network definition
class MLP(chainer.Chain):
def __init__(self, n_out):
super(MLP, self).__init__()
with self.init_scope():
self.l1 = L.Linear(None, n_out)
def __call__(self, x):
return self.l1(x)
# Set up a neural network to train
# Classifier reports softmax cross entropy loss and accuracy at every
# iteration, which will be used by the PrintReport extension below.
model = L.Classifier(MLP(10))
# Make a specified GPU current
chainer.cuda.get_device_from_id(0).use()
model.to_gpu() # Copy the model to the GPU
# Setup an optimizer
optimizer = chainer.optimizers.Adam()
optimizer.setup(model)
# Load the MNIST dataset
train, _ = chainer.datasets.get_mnist()
train_iter = chainer.iterators.SerialIterator(train, 32)
# Set up a trainer
updater = training.StandardUpdater(train_iter, optimizer, device=0)
trainer = training.Trainer(updater, (5, 'epoch'), out='result')
trainer.extend(extensions.ParameterStatistics(model))
# Write a log of evaluation statistics for each epoch
trainer.extend(extensions.LogReport())
# Run the training
trainer.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment