2

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

Betty LD
  • 63
  • 5
  • Could you please describe the expected/actual outputs? Do you mean `trainer.observation` becomes `{}`? – kmaehashi Jun 26 '18 at 16:48
  • Btw, you need to specify `device=0` to `StandardUpdater` to use GPU. – kmaehashi Jun 26 '18 at 16:50
  • Yes I will put device=0 thank you. My real wanted output is the regression which is working but the problem is the extension output is empty. The logReport or PrintReport is only printing the time and the epoch (the run() loop is then working well). So I checked and find out that observation = {} indeed at every iterations – Betty LD Jun 27 '18 at 00:06
  • I see. Do you use `Classifier`, or your own Link? In the latter case, you need to report it by your own. See https://github.com/chainer/chainer/blob/v4.1.0/examples/imagenet/alex.py#L40 and https://github.com/chainer/chainer/blob/v4.1.0/chainer/links/model/classifier.py#L116 . It would be helpful if you share the minimum runnable code to reproduce the issue, so that I can check it for you. – kmaehashi Jun 27 '18 at 04:13
  • 1
    Sorry for the lack of code, I didnt want it to be too messy. This is exactly the answer to my problem, it is working now. thank you a lot, I didnt find these examples before. – Betty LD Jun 27 '18 at 09:12

1 Answers1

2

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:

kmaehashi
  • 879
  • 6
  • 10