Open zhuker opened 5 years ago
That's because in PyTorch>=0.5, the index of 0-dim tensor is invalid. The master branch is designed for PyTorch 0.4.1, loss_val.data[0]
works well.
Try to change
total_loss += loss_val.data[0]
loss_values = [v.data[0] for v in losses]
to
total_loss += loss_val.data
loss_values = [v.data for v in losses]
might fix the problem.
`Overall Progress: 0%| | 0/1 [00:00<?, ?it/s] Inferencing : 0%| | 0/1041.0 [00:00<?, ?it/s]/home/toprocker/.local/lib/python3.6/site-packages/torch/nn/modules/upsampling.py:122: UserWarning: nn.Upsampling is deprecated. Use nn.functional.interpolate instead. warnings.warn("nn.Upsampling is deprecated. Use nn.functional.interpolate instead.") /home/toprocker/.local/lib/python3.6/site-packages/torch/nn/functional.py:1961: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details. "See the documentation of nn.Upsample for details.".format(mode))
Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 0/1041.0 [00:02<?, ?it/s] Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 1/1041.0 [00:02<49:24, 2.85s/it] Inference Averages for Epoch 0: L1: 0.205, EPE: 0.326: 0%| | 1/1041.0 [00:02<49:24, 2.85s/it]error in correlation_forward_cuda_kernel: no kernel image is available for execution on the device Traceback (most recent call last):`
I tried your method, the two warnings disappeared, but the problem has not been solved.
@toprocker This is another error caused by incorrect compile parameters. See https://github.com/NVIDIA/flownet2-pytorch/issues/86#issuecomment-449621437 for the solution.
@huangbiubiu, Thank you very much, that fixed my problem.
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number How i can fix this error.
File "/content/MultimodalNMT/onmt/TrainerMultimodal.py", line 286, in _gradient_accumulation trunc_size, self.shard_size, normalization) File "/content/MultimodalNMT/onmt/Loss.py", line 122, in sharded_compute_loss loss, stats = self._compute_loss(batch, **shard) File "/content/MultimodalNMT/onmt/Loss.py", line 207, in _compute_loss stats = self._stats(loss_data, scores.data, target.view(-1).data) File "/content/MultimodalNMT/onmt/Loss.py", line 144, in _stats return onmt.Statistics(loss[0], non_padding.sum(), num_correct) IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
How can i solve this issue?
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
total_loss += loss_val.item()
huangbiubiu that fixed my problem too.
@huangbiubiu, Thank you very much, that fixed my problem.
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
for i, data in enumerate(dataloader, 0): # We iterate over the images of the dataset.
# 1st Step: Updating the weights of the neural network of the discriminator
netD.zero_grad() # We initialize to 0 the gradients of the discriminator with respect to the weights.
# Training the discriminator with a real image of the dataset
real, _ = data # We get a real image of the dataset which will be used to train the discriminator.
input = Variable(real) # We wrap it in a variable.
target = Variable(torch.ones(input.size()[0])) # We get the target.
output = netD(input) # We forward propagate this real image into the neural network of the discriminator to get the prediction (a value between 0 and 1).
errD_real = criterion(output, target) # We compute the loss between the predictions (output) and the target (equal to 1).
# Training the discriminator with a fake image generated by the generator
noise = Variable(torch.randn(input.size()[0], 100, 1, 1)) # We make a random input vector (noise) of the generator.
fake = netG(noise) # We forward propagate this random input vector into the neural network of the generator to get some fake generated images.
target = Variable(torch.zeros(input.size()[0])) # We get the target.
output = netD(fake.detach()) # We forward propagate the fake generated images into the neural network of the discriminator to get the prediction (a value between 0 and 1).
errD_fake = criterion(output, target) # We compute the loss between the prediction (output) and the target (equal to 0).
# Backpropagating the total error
errD = errD_real + errD_fake # We compute the total error of the discriminator.
errD.backward() # We backpropagate the loss error by computing the gradients of the total error with respect to the weights of the discriminator.
optimizerD.step() # We apply the optimizer to update the weights according to how much they are responsible for the loss error of the discriminator.
# 2nd Step: Updating the weights of the neural network of the generator
netG.zero_grad() # We initialize to 0 the gradients of the generator with respect to the weights.
target = Variable(torch.ones(input.size()[0])) # We get the target.
output = netD(fake) # We forward propagate the fake generated images into the neural network of the discriminator to get the prediction (a value between 0 and 1).
errG = criterion(output, target) # We compute the loss between the prediction (output between 0 and 1) and the target (equal to 1).
errG.backward() # We backpropagate the loss error by computing the gradients of the total error with respect to the weights of the generator.
optimizerG.step() # We apply the optimizer to update the weights according to how much they are responsible for the loss error of the generator.
# 3rd Step: Printing the losses and saving the real images and the generated images of the minibatch every 100 steps
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f' % (epoch, 25, i, len(dataloader), errD.data[0], errG.data[0])) # We print les losses of the discriminator (Loss_D) and the generator (Loss_G).
if i % 100 == 0: # Every 100 steps:
vutils.save_image(real, '%s/real_samples.png' % "./results", normalize = True) # We save the real images of the minibatch.
fake = netG(noise) # We get our fake generated images.
vutils.save_image(fake.data, '%s/fake_samples_epoch_%03d.png' % ("./results", epoch), normalize = True) # We also save the fake generated images of the minibatch.
@huangbiubiu Thank you very much, that fixed my problem.
recommend solution: https://blog.csdn.net/LYKXHTP/article/details/81565453
pytorch 1.0