Python 3.x 索引器:0维张量的索引无效。使用tensor.item()将0维张量转换为Python数字
但它不起作用我在github问题中找到了解决方案 尝试改变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
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