正文

invalid index of a 0-dim tensor.

(2019-06-21 12:50:41) 下一個

           

MURA_deep_learning/Train.py:239: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).

  label_v = Variable(torch.tensor(label_v[0]))

Traceback (most recent call last):

  File "Main.py", line 44, in <module>

    main()

  File "Main.py", line 39, in main

    runVal(args.data_dir, args.pathtotrainmodel)

  File "/home/t/isic/derm/MURA_deep_learning/Train.py", line 598, in runVal

    validate_loss,roc_auc = validate(validate_loader, model, criterion, epoch)

  File "/home/t/isic/derm/MURA_deep_learning/Train.py", line 244, in validate

    validate_losses.update(loss.data[0], images.size(0))

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

Exception KeyError: KeyError(<weakref at 0x7f4068fba050; to 'tqdm' at 0x7f406aaf5b90>,) in <bound method tqdm.__del__ of   0%|                                                                                                                                            | 2/3197 [00:08<02:41, 19.81it/s]> ignored

 

 

 

     validate_losses.update(loss.data, images.size(0))

validate_accuracy.update(prec1[0], images.size(0))

 

 

 

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.dataloss_values = [v.data for v in losses]

might fix the problem.

[ 打印 ]
閱讀 ()評論 (0)
評論
目前還沒有任何評論
登錄後才可評論.