Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 索引器:0维张量的索引无效。使用tensor.item()将0维张量转换为Python数字_Python 3.x_Pytorch - Fatal编程技术网

Python 3.x 索引器:0维张量的索引无效。使用tensor.item()将0维张量转换为Python数字

Python 3.x 索引器:0维张量的索引无效。使用tensor.item()将0维张量转换为Python数字,python-3.x,pytorch,Python 3.x,Pytorch,但它不起作用我在github问题中找到了解决方案 尝试改变 i=order.item() 到 应将循环体更改为: i = order # works for PyTorch>=0.5. 当顺序中只剩下一个元素时,代码i=order[0]给出错误。我试图使用PyTorch在MNIST上运行标准卷积神经网络(LeNet)。我犯了这个错误 while order.numel() > 0: if order.numel() == 1: break

但它不起作用

我在github问题中找到了解决方案

尝试改变

i=order.item()


应将循环体更改为:

i = order # works for PyTorch>=0.5.

顺序中只剩下一个元素时,代码
i=order[0]
给出错误。我试图使用PyTorch在MNIST上运行标准卷积神经网络(LeNet)。我犯了这个错误

while order.numel() > 0:
        if order.numel() == 1:
            break
        i = order[0]
        keep.append(i)
改变

IndexError                                Traceback (most recent call last

 79         y = net.forward(train_x, dropout_value)
 80         loss = net.loss(y,train_y,l2_regularization)
 81         loss_train = loss.data[0]
 82         loss_train += loss_val.data

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

loss_train = loss.data[0]

修复了问题。

此答案看起来像是与原始问题不匹配的问答。你可能应该问一个新问题,如果你自己提供答案,那么就添加答案。
IndexError                                Traceback (most recent call last

 79         y = net.forward(train_x, dropout_value)
 80         loss = net.loss(y,train_y,l2_regularization)
 81         loss_train = loss.data[0]
 82         loss_train += loss_val.data

 IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 
 0-dim tensor to a Python number
loss_train = loss.data[0]
loss_train = loss.data