loss.backward question

883 views
Skip to first unread message

Kurt

unread,
Aug 26, 2017, 6:07:07 AM8/26/17
to Chainer User Group
Hi,  I've tried the sigmoid cross entropy to compute the loss. Now I have the loss but it says error: AttributeError: 'tuple' object has no attribute 'backward' 
Could you point out to me how to debug this to see where it has gone wrong? Thanks. My main code is this:

predictor = Predictor(n_vocab, n_labels, embed_dim, hidden_dim, dropout)
model
= Classifier(predictor, n_labels)
model
.predictor.load_glove(embedding_path, train_word2id)
optimizer
= optimizers.SGD()
optimizer
.setup(model)
updater
= training.StandardUpdater(train_iter, optimizer)
trainer
= training.Trainer(updater, (20, 'epoch'), out='result')
# trainer.extend(extensions.Evaluator(test_iter, model))
# trainer.extend(extensions.LogReport())
# trainer.extend(extensions.PrintReport(['epoch','main/accuracy', 'validation/accuracy']))
# trainer.extend(extensions.ProgressBar())
trainer
.run()


and the complete error trace is this:

'---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-e8eadb847f69> in <module>()
    43 # trainer.extend(extensions.PrintReport(['
epoch','main/accuracy', 'validation/accuracy']))
    44 # trainer.extend(extensions.ProgressBar())
---> 45 trainer.run()
/Users/kurt/anaconda/lib/python3.6/site-packages/chainer/training/trainer.py in run(self)
   294                 self.observation = {}
   295                 with reporter.scope(self.observation):
--> 296                     update()
   297                     for name, entry in extensions:
   298                         if entry.trigger(self):
/Users/kurt/anaconda/lib/python3.6/site-packages/chainer/training/updater.py in update(self)
   175
   176     def update(self):
--> 177         self.update_core()
   178         self.iteration += 1
   179
/Users/kurt/anaconda/lib/python3.6/site-packages/chainer/training/updater.py in update_core(self)
   190             optimizer.update(loss_func, **in_arrays)
   191         else:
--> 192             optimizer.update(loss_func, in_arrays)
   193
   194     def serialize(self, serializer):
/Users/kurt/anaconda/lib/python3.6/site-packages/chainer/optimizer.py in update(self, lossfun, *args, **kwds)
   530             else:
   531                 self.target.zerograds()
--> 532             loss.backward()
   533             del loss
   534
AttributeError: '
tuple' object has no attribute 'backward''

I've printed the loss and it's a variable. I wonder why it says it's a tuple.
variable([ 0.68693405])

Thanks for your time. 

Kenta Oono

unread,
Aug 28, 2017, 1:41:22 AM8/28/17
to Chainer User Group
Hi,

Did you check the type of loss just before the error was raised?
It could be possible the type can be changed during training or evaluation time.

By the way, if you use `L.Classifier` with the default setting, the shape of dataset the loss variable holds should be a scalar(i.e. the shape is (), not (1,)), although I cannot say it is related to the current problem.

Best
Kenta

2017年8月26日土曜日 19時07分07秒 UTC+9 Kurt:

Kurt Junshean Espinosa

unread,
Aug 28, 2017, 10:57:53 AM8/28/17
to Kenta Oono, Chainer User Group
Dear Kenta, 

Thanks for your reply. 

I printed the type and the value of the loss variable:

print(type(self.loss), self.loss.shape)

<class 'chainer.variable.Variable'> ()

and it is is of type Variable (as indicated in the documentation) and the value is a scalar.

So, I’m not quite sure why I get the error I mentioned..

Do I need to implement my own updater? But I used the same updater as the default MNIST example so it should have worked fine.

Would you have any idea how to debug this further? 


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-8a190db1d7e0> in <module>()
--
You received this message because you are subscribed to the Google Groups "Chainer User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chainer+u...@googlegroups.com.
To post to this group, send email to cha...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chainer/5fcdbea8-2e53-462c-ad12-0a17d51468cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cheers,
— Kurt 
www.kurtespinosa.com



Kenta Oono

unread,
Aug 28, 2017, 11:57:08 AM8/28/17
to Kurt Junshean Espinosa, Chainer User Group
Hi, Kurt

Is it possible to share the code with me?

Best
Kenta
--
Preferred Networks Inc. (PFN)
Kenta Oono
oo...@preferred.jp

Otemachi Bldg. 2F, 1-6-1, Otemachi, Chiyoda-ku, Tokyo, Japan 100-0004
http://preferred-networks.jp
Reply all
Reply to author
Forward
0 new messages