Search code examples
chainer

extensions or even observation empty with Chainer


I am kind of new on Chainer and I have been struggling with a weird situation recently. I have a Chain to compute a CNN which I feed with a labeledDataSet. But no results appears when I use the extensions. When I display the observation value it is empty. But the loss is indeed calculated and the parameters updated (at least they change) so I don't know where is the connection problem.

def convert(batch, device):
    return chainer.dataset.convert.concat_examples(batch, device, padding=0)
def print_obs(t):
    print("trainer.observation", trainer.observation)
    print("updater.loss", updater.loss_func)
    print("conv1", model.predictor.conv1.W[0][0])
    print("conv20", model.predictor.conv20.W[0][0])

model.predictor.train = True
model.predictor.finetune = False  ####or True ??
cuda.get_device(0).use()
model.to_gpu()
optimizer = optimizers.MomentumSGD(lr=learning_rate, momentum=momentum)
optimizer.use_cleargrads()
optimizer.setup(model)
optimizer.add_hook(chainer.optimizer.WeightDecay(weight_decay))

train, test = imageNet_data.train_val_test()
train_iter = iterators.SerialIterator(train, batch_size)
test_iter = iterators.SerialIterator(test, batch_size, repeat=False,shuffle=False)
with chainer.using_config('debug', True):
# Set up a trainer
    updater = training.StandardUpdater(train_iter, optimizer, loss_func=model, converter=convert)
    trainer = training.Trainer(updater, (10, 'epoch'), out="./backup/result")
    trainer.extend(print_obs, trigger=(3, 'iteration'))
    trainer.extend(extensions.LogReport())
    trainer.extend(extensions.PrintReport(
  ['epoch', 'main/loss', 'validation/main/loss',
   'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
trainer.run()

Maybe this is something is miss completely and which is quite obvious.. Thank you for all remarks that would help me a lot.

Chainer4.1, Ubuntu16


Solution

  • If you are using your own Link with the Trainer, you need to report metrics using chainer.report by your own. See https://docs.chainer.org/en/stable/guides/report.html for instructions.

    You can see some examples in Chainer repository: