Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number #113

Open
zhuker opened this issue Dec 17, 2018 · 14 comments

Comments

@zhuker
Copy link

zhuker commented Dec 17, 2018

pytorch 1.0

python3 main.py --inference --model FlowNet2 --save_flow --inference_dataset ImagesFromFolder --inference_dataset_root ~/fs24_512/ --resume mycheckpoints/FlowNet2_checkpoint.pth.tar

Initializing Datasets
[384, 512]
  [0.012s] Inference Dataset: ImagesFromFolder
  [0.025s] Inference Input: [3, 2, 384, 512]
  [0.037s] Inference Targets: [3, 2, 384, 512]
  [0.037s] Operation finished

Building FlowNet2 model
  [2.646s] Effective Batch Size: 16
  [2.647s] Number of parameters: 162518834
  [2.647s] Initializing CUDA
  [4.606s] Parallelizing
  [4.606s] Loading checkpoint 'mycheckpoints/FlowNet2_checkpoint.pth.tar'
  [4.907s] Loaded checkpoint 'mycheckpoints/FlowNet2_checkpoint.pth.tar' (at epoch 0)
  [4.907s] Initializing save directory: ./work
  [4.912s] Operation finished

Initializing Adam Optimizer
  [0.001s] amsgrad = False (<class 'bool'>)
  [0.001s] lr = 0.001 (<class 'float'>)
  [0.001s] eps = 1e-08 (<class 'float'>)
  [0.001s] weight_decay = 0 (<class 'int'>)
  [0.001s] betas = (0.9, 0.999) (<class 'tuple'>)
  [0.001s] Operation finished

Overall Progress:   0%|                                                       | 0/1 [00:00<?, ?it/s]/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/modules/upsampling.py:129: UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))t/s]
/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/functional.py:2423: 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))
/home/ubuntu/.local/lib/python3.5/site-packages/torch/nn/parallel/_functions.py:61: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn('Was asked to gather along dimension 0, but all '
Traceback (most recent call last):
  File "main.py", line 403, in <module>
    stats = inference(args=args, epoch=epoch - 1, data_loader=inference_loader, model=model_and_loss, offset=offset)
  File "main.py", line 371, in inference
    total_loss += loss_val.data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
@huangbiubiu
Copy link

huangbiubiu commented Jan 2, 2019

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.

@toprocker
Copy link

`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.

@huangbiubiu
Copy link

huangbiubiu commented Jan 4, 2019

@toprocker This is another error caused by incorrect compile parameters. See #86 (comment) for the solution.

@AbdelnasserMostafa
Copy link

@huangbiubiu, Thank you very much, that fixed my problem.

@sahinurlaskar
Copy link

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

@sahinurlaskar
Copy link

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.

@sahinurlaskar
Copy link

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?

@sahinurlaskar
Copy link

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

@zisuina
Copy link

zisuina commented Aug 21, 2019

total_loss += loss_val.item()

@caramelslice
Copy link

huangbiubiu that fixed my problem too.

@azraar
Copy link

azraar commented Oct 12, 2019

@huangbiubiu, Thank you very much, that fixed my problem.

@iroshansam78
Copy link

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.

@gjian0115
Copy link

@huangbiubiu Thank you very much, that fixed my problem.

@Light--
Copy link

Light-- commented Jun 1, 2020

recommend solution: https://blog.csdn.net/LYKXHTP/article/details/81565453

ceyhanyilmaz added a commit to ceyhanyilmaz/PyTorch-AutoNEB that referenced this issue Jan 21, 2022
change to 
data[0] = self.coords[offset:offset + size].item() =>    data = self.coords

because of IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number error 

solution from NVIDIA/flownet2-pytorch#113 (comment)
des520mond added a commit to des520mond/Pytorch-MNIST-colab that referenced this issue May 16, 2022
loss_val.data[0] does not work for PyTorch>=0.5. Check NVIDIA/flownet2-pytorch#113.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests