Last active
June 9, 2017 12:33
-
-
Save MannyKayy/a383b35d70124d7069e78255ed67b452 to your computer and use it in GitHub Desktop.
This code doesn't run. pickle and mpi related error
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
#!/usr/bin/env python | |
from __future__ import print_function | |
import argparse | |
import chainer | |
import chainer.functions as F | |
import chainer.links as L | |
from chainer import training | |
from chainer.training import extensions | |
import chainermn | |
import chainercv as cv | |
class MLP(chainer.Chain): | |
def __init__(self, n_units, n_out): | |
super(MLP, self).__init__( | |
# the size of the inputs to each layer will be inferred | |
l1=L.Linear(784, n_units), # n_in -> n_units | |
l2=L.Linear(n_units, n_units), # n_units -> n_units | |
l3=L.Linear(n_units, n_out), # n_units -> n_out | |
) | |
def __call__(self, x): | |
h1 = F.relu(self.l1(x)) | |
h2 = F.relu(self.l2(h1)) | |
return self.l3(h2) | |
def main(): | |
parser = argparse.ArgumentParser(description='ChainerMN example: MNIST') | |
parser.add_argument('--batchsize', '-b', type=int, default=100, | |
help='Number of images in each mini-batch') | |
parser.add_argument('--communicator', type=str, | |
default='hierarchical', help='Type of communicator') | |
parser.add_argument('--epoch', '-e', type=int, default=20, | |
help='Number of sweeps over the dataset to train') | |
parser.add_argument('--gpu', '-g', action='store_true', | |
help='Use GPU') | |
parser.add_argument('--out', '-o', default='result', | |
help='Directory to output the result') | |
parser.add_argument('--resume', '-r', default='', | |
help='Resume the training from snapshot') | |
parser.add_argument('--unit', '-u', type=int, default=1000, | |
help='Number of units') | |
args = parser.parse_args() | |
# Prepare ChainerMN communicator. | |
if args.gpu: | |
if args.communicator == 'naive': | |
print("Error: 'naive' communicator does not support GPU.\n") | |
exit(-1) | |
print('Using {} communicator'.format(args.communicator)) | |
comm = chainermn.create_communicator(args.communicator) | |
device = comm.intra_rank | |
else: | |
if args.communicator != 'naive': | |
print('Warning: using naive communicator ' | |
'because only naive supports CPU-only execution') | |
comm = chainermn.create_communicator('naive') | |
device = -1 | |
if comm.mpi_comm.rank == 0: | |
print('GPU: {}'.format(device)) | |
print('# unit: {}'.format(args.unit)) | |
print('# Minibatch-size: {}'.format(args.batchsize)) | |
print('# epoch: {}'.format(args.epoch)) | |
model = L.Classifier(MLP(args.unit, 10)) | |
if device >= 0: | |
chainer.cuda.get_device(device).use() | |
model.to_gpu() | |
# Create a multi node optimizer from a standard Chainer optimizer. | |
optimizer = chainermn.create_multi_node_optimizer( | |
chainer.optimizers.Adam(), comm) | |
optimizer.setup(model) | |
# Split and distribute the dataset. Only worker 0 loads the whole dataset. | |
# Datasets of worker 0 are evenly split and distributed to all workers. | |
if comm.rank == 0: | |
def transform_train(in_data): | |
img, label = in_data | |
img = cv.transforms.random_rotate(img) | |
return img, label | |
train, test = chainer.datasets.get_mnist() | |
train = cv.datasets.TransformDataset(train, transform_train) | |
else: | |
train, test = None, None | |
train = chainermn.scatter_dataset(train, comm) | |
test = chainermn.scatter_dataset(test, comm) | |
train_iter = chainer.iterators.SerialIterator(train, args.batchsize) | |
test_iter = chainer.iterators.SerialIterator(test, args.batchsize, | |
repeat=False, shuffle=False) | |
updater = training.StandardUpdater(train_iter, optimizer, device=device) | |
trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) | |
# Create a multi node evaluator from a standard Chainer evaluator. | |
evaluator = extensions.Evaluator(test_iter, model, device=device) | |
evaluator = chainermn.create_multi_node_evaluator(evaluator, comm) | |
trainer.extend(evaluator) | |
# Some display and output extensions are necessary only for one worker. | |
# (Otherwise, there would just be repeated outputs.) | |
if comm.rank == 0: | |
trainer.extend(extensions.dump_graph('main/loss')) | |
trainer.extend(extensions.LogReport()) | |
trainer.extend(extensions.PrintReport( | |
['epoch', 'main/loss', 'validation/main/loss', | |
'main/accuracy', 'validation/main/accuracy', 'elapsed_time'])) | |
trainer.extend(extensions.ProgressBar()) | |
if args.resume: | |
chainer.serializers.load_npz(args.resume, trainer) | |
trainer.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment