RuntimeError: backward_input can only be called in training mode

Traceback (most recent call last):
File “train_discriminator.py”, line 323, in
main()
File “train_discriminator.py”, line 251, in main
loss_G.backward()
File “/anaconda3/envs/speech/lib/python3.6/site-packages/torch/tensor.py”, line 93, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File “/anaconda3/envs/speech/lib/python3.6/site-packages/torch/autograd/init.py”, line 89, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: backward_input can only be called in training mode

I was trying to train a RNN model. It fails in GPU but success in CPU.
I have checked the model by model.training. It is in training mode.

Can some one help? thanks

1 Like

I get this bug also. Does anyone know of a work-around?

Try this code of C-GRUs https://github.com/bionick87/ConvGRUCell-pytorch

Your code doesn’t call backward, so I am not sure how it is relevant.

The fix seems to be to call model.train() before running the data through the model.

And what prevents you from doing it? :slight_smile:

go to the rows [55-60]:

for e in xrange(max_epoch):
for time in xrange(num_seqs):
h_next = model(input_gru[time], None)
err = MSE_criterion(h_next [0], target_gru[time])
err. backward()