- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对孪生神经网络还很陌生,最近发现了 this example和 Colab notebook .
运行代码时出现以下错误:
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
在线:
result=torch.max(res,1)[1][0][0][0].data[0].tolist()
我发现了一些关于 tensor.item()
的东西,但我真的不知道如何在这里使用它。
编辑:
test_dataloader = DataLoader(test_dataset,num_workers=6,batch_size=1,shuffle=True)
accuracy=0
counter=0
correct=0
for i, data in enumerate(test_dataloader,0):
x0, x1 , label = data
# onehsot applies in the output of 128 dense vectors which is then converted to 2 dense vectors
output1,output2 = model(x0.to(device),x1.to(device))
res=torch.abs(output1.cuda() - output2.cuda())
label=label[0].tolist()
label=int(label[0])
result=torch.max(res,1)[1][0][0][0].data.item().tolist()
if label == result:
correct=correct+1
counter=counter+1
# if counter ==20:
# break
accuracy=(correct/len(test_dataloader))*100
print("Accuracy:{}%".format(accuracy))
这就是我得到错误的代码。
最佳答案
此错误消息的意思是您正在尝试索引到一个数组,其中只有一个项目。例如,
In [10]: aten = torch.tensor(2)
In [11]: aten
Out[11]: tensor(2)
In [12]: aten[0]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-12-5c40f6ab046a> in <module>
----> 1 aten[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim
tensor to a Python number
在上面的例子中,aten
是一个只有一个数字的张量。因此,使用索引(或更多)检索该数字会抛出 IndexError
。
从张量中提取数字(项目)的正确方法是使用tensor.item()
,这里是aten.item()
:
In [14]: aten.item()
Out[14]: 2
关于python - 如何使用 tensor.item() ? IndexError : invalid index of a 0-dim tensor. 使用 tensor.item() 将 0-dim 张量转换为 Python 数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993899/
VBA 的奇怪行为: 这会在第 3 行出现错误 94“非法使用 Null”: Function test1() Dim a As String a = Null test1 = a End
我有以下代码 double[,] var = new double[5,10]; double[] var2 = new double[10]; int a; for (a=0;a<10;a++)
如何链接CSVTypeColumn = CSVType01 = "AL" ? 如果 CSVTypeColumn.value = CSVType01然后CSVTypeColumn.value = "AL
在 VBA 中,我可以通过以下两种方式之一创建对象: 'First way Dim myCol1 As New Collection 'Second way Dim myCol2 As Collect
在假设将分配不必要的大量内存的情况下,我总是害怕将事物声明为变体。 最近致力于提高电子表格的性能,但我得到了相反的印象(见下面的编辑):Dim myarray() as Variant与 Dim my
这可能听起来微不足道,但它们之间的区别是什么? Dim v As String() 和 Dim v() As String 在 VB.NET 中? 最佳答案 没有不同。来自 VB.NET Langua
在此代码中: Dim files() As String = Directory.GetFiles("C:/") Dim files As String() = Directory.GetFiles(
如何将二维 Numpy 数组中的所有列堆叠到一维数组中。 即我有: x = np.array([[1, 3, 5],[2, 4, 6]]) 我想得到: np.array([1, 2, 3, 4, 5,
我是 Theano 的初学者,我正在使用另一个代码的示例,大概在某个时候可以工作(但是,我修改了它......但我很确定我的修改无关目前出了什么问题)。 无论如何,我正在尝试调试 Theano 扫描.
在 Matlab 中没有一维数组的概念。所有数组至少有两个维度。所有“向量”都是“行向量”(1xn 数组)或“列向量”(nx1 数组)。 另一方面,在 NumPy 中,数组也可以是一维的。于是就有了“
有什么区别: Dim s As String 和 Dim s As [String] 最佳答案 没有区别。 在 VB 中,您可以将标识符括在括号中,以强制将其解析为标识符而不是关键字。 例如,你可以写
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What’s different between Dim files() As String and Dim fil
我的应用程序已经能够设置显示器关闭的超时时间,以及设置全部内容,并且还可以设置当前的亮度。 Windows 有一项附加功能,可以在一段时间后调暗显示器(高级电源使用方案设置中的“稍后调暗显示器”和“显
如前所述,我有一个一维矩阵向量,例如: P_predefined = [[.3 .4 .2 .1], [.2 .3 .5 0.], [.1 0. .8 .1], [.4 0. 0. .6]] 我想把它
Duplicate Whats the difference between declaring as new and as something new something i 遗憾的是我不知道这一点
这是一个简单的概念,但我从未被教导过。有人可以解释一下以下两种说法之间的区别吗: Dim c as MyClass Dim c as New MyClass 最佳答案 Dim c as New MyC
在我们的 VB.net 代码库中,我偶尔会看到 Dim x as new Y() ,其中 Y 是一个类。 这段代码是做什么的,它与更常见的 Dim x as Y = new Y()" 有何不同? ?
def __init__(self): super().__init__() self.conv = nn.Sequential( nn.Con
ndarray crate 的示例使用以下代码,其中 Array::random返回类型是 ArrayBase : let a = Array::random((hash_size, inp_dime
在 python 中,我希望从 1-dim 数组中逐行减去 2-dim 数组。 我知道如何使用“for”循环和索引来完成它,但我想使用 numpy 函数可能会更快。但是我没有找到办法。这是一个带有“f
我是一名优秀的程序员,十分优秀!