def train(train_data_path, test_data_path, args): vocab = collections.defaultdict(lambda: len(vocab)) vocab['<unk>'] = 0 train_data = babi.read_data(vocab, train_data_path) test_data = babi.read_data(vocab, test_data_path) print('Training data: %s: %d' % (train_data_path, len(train_data))) print('Test data: %s: %d' % (test_data_path, len(test_data))) train_data = memnn.convert_data(train_data, args.max_memory) test_data = memnn.convert_data(test_data, args.max_memory) encoder = memnn.make_encoder(args.sentence_repr) network = memnn.MemNN(args.unit, len(vocab), encoder, args.max_memory, args.hop) model = chainer.links.Classifier(network, label_key='answer') opt = chainer.optimizers.Adam() if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() model.to_gpu() opt.setup(model) train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize) test_iter = chainer.iterators.SerialIterator(test_data, args.batchsize, repeat=False, shuffle=False) updater = chainer.training.StandardUpdater(train_iter, opt, device=args.gpu) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch')) @chainer.training.make_extension() def fix_ignore_label(trainer): network.fix_ignore_label() trainer.extend(fix_ignore_label) trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu)) trainer.extend(extensions.LogReport()) trainer.extend( extensions.PrintReport([ 'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy' ])) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.run() if args.model: memnn.save_model(args.model, model, vocab)
def train(train_data_path, test_data_path, args): device = chainer.get_device(args.device) device.use() vocab = collections.defaultdict(lambda: len(vocab)) vocab['<unk>'] = 0 train_data = babi.read_data(vocab, train_data_path) test_data = babi.read_data(vocab, test_data_path) print('Training data: %s: %d' % (train_data_path, len(train_data))) print('Test data: %s: %d' % (test_data_path, len(test_data))) train_data = memnn.convert_data(train_data, args.max_memory) test_data = memnn.convert_data(test_data, args.max_memory) encoder = memnn.make_encoder(args.sentence_repr) network = memnn.MemNN( args.unit, len(vocab), encoder, args.max_memory, args.hop) model = chainer.links.Classifier(network, label_key='answer') opt = chainer.optimizers.Adam() model.to_device(device) opt.setup(model) train_iter = chainer.iterators.SerialIterator( train_data, args.batchsize) test_iter = chainer.iterators.SerialIterator( test_data, args.batchsize, repeat=False, shuffle=False) updater = chainer.training.StandardUpdater(train_iter, opt, device=device) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch')) @chainer.training.make_extension() def fix_ignore_label(trainer): network.fix_ignore_label() trainer.extend(fix_ignore_label) trainer.extend(extensions.Evaluator(test_iter, model, device=device)) trainer.extend(extensions.LogReport()) trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy'])) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.run() if args.model: memnn.save_model(args.model, model, vocab)