RuntimeError: backward_input can only be called in training mode
MrLinNing opened this issue · 2 comments
MrLinNing commented
In pointer network jupyter notebook,
/pytorch_test/env2/lib/python2.7/site-packages/ipykernel_launcher.py:46: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
Exception NameError: "global name 'FileNotFoundError' is not defined" in <bound method _DataLoaderIter.__del__ of <torch.utils.data.dataloader._DataLoaderIter object at 0x7fdc900d1cd0>> ignored
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-21-0299c228a1ba> in <module>()
15
16 adam.zero_grad()
---> 17 loss.backward()
18 adam.step()
19
/pytorch_test/env2/local/lib/python2.7/site-packages/torch/tensor.pyc in backward(self, gradient, retain_graph, create_graph)
91 products. Defaults to ``False``.
92 """
---> 93 torch.autograd.backward(self, gradient, retain_graph, create_graph)
94
95 def register_hook(self, hook):
/pytorch_test/env2/local/lib/python2.7/site-packages/torch/autograd/__init__.pyc in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
87 Variable._execution_engine.run_backward(
88 tensors, grad_tensors, retain_graph, create_graph,
---> 89 allow_unreachable=True) # allow_unreachable flag
90
91
RuntimeError: backward_input can only be called in training mode
MrLinNing commented
In neural combinatorial optimization jupyter notebook
TypeError Traceback (most recent call last)
<ipython-input-16-bbf1d00a3be2> in <module>()
----> 1 tsp_20_train.train_and_validate(5)
<ipython-input-14-db3174b84dcf> in train_and_validate(self, n_epochs)
30 inputs = inputs.cuda()
31
---> 32 R, probs, actions, actions_idxs = self.model(inputs)
33
34 if batch_id == 0:
/pytorch_test/env2/local/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
<ipython-input-10-a637634e5730> in forward(self, inputs)
34 seq_len = inputs.size(2)
35
---> 36 probs, action_idxs = self.actor(inputs)
37
38 actions = []
/pytorch_test/env2/local/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
<ipython-input-9-816b31f44515> in forward(self, inputs)
76
77
---> 78 idxs = probs.multinomial().squeeze(1)
79 for old_idxs in prev_idxs:
80 if old_idxs.eq(idxs).data.any():
TypeError: multinomial() missing 1 required positional arguments: "num_samples"
taoyouxian commented
In pointer network jupyter notebook,
/pytorch_test/env2/lib/python2.7/site-packages/ipykernel_launcher.py:46: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number Exception NameError: "global name 'FileNotFoundError' is not defined" in <bound method _DataLoaderIter.__del__ of <torch.utils.data.dataloader._DataLoaderIter object at 0x7fdc900d1cd0>> ignored --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-21-0299c228a1ba> in <module>() 15 16 adam.zero_grad() ---> 17 loss.backward() 18 adam.step() 19 /pytorch_test/env2/local/lib/python2.7/site-packages/torch/tensor.pyc in backward(self, gradient, retain_graph, create_graph) 91 products. Defaults to ``False``. 92 """ ---> 93 torch.autograd.backward(self, gradient, retain_graph, create_graph) 94 95 def register_hook(self, hook): /pytorch_test/env2/local/lib/python2.7/site-packages/torch/autograd/__init__.pyc in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables) 87 Variable._execution_engine.run_backward( 88 tensors, grad_tensors, retain_graph, create_graph, ---> 89 allow_unreachable=True) # allow_unreachable flag 90 91 RuntimeError: backward_input can only be called in training mode
You can add pointer.train()
after for batch_id, sample_batch in enumerate(train_loader):
, this action will remove the error.