Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Pytorch 尺寸超出范围(预计在[-2,1]范围内,但得到2)_Pytorch - Fatal编程技术网

Pytorch 尺寸超出范围(预计在[-2,1]范围内,但得到2)

Pytorch 尺寸超出范围(预计在[-2,1]范围内,但得到2),pytorch,Pytorch,为什么会弹出以下错误?什么应该在这个范围内,为什么?二维是什么意思 RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 2) 此代码将产生错误 import torch torch.bmm(torch.randn(1000, 784) , torch.randn(784, 10)) 方法torch.bmm实现批量矩阵乘积。对于普通矩阵乘积,您需要两个具有两个2D矩阵的矩阵才能创

为什么会弹出以下错误?什么应该在这个范围内,为什么?二维是什么意思

RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 2)
此代码将产生错误

import torch 

torch.bmm(torch.randn(1000, 784) , torch.randn(784, 10))

方法
torch.bmm
实现批量矩阵乘积。对于普通矩阵乘积,您需要两个具有两个2D矩阵的矩阵才能创建乘积

使用
torch.bmm
您可以创建产品,甚至批处理wize,但当然您需要包含批处理维度,因此您需要输入两个三维矩阵

关于如何在
torch.bmm
中使用尺寸:

如果batch1是(b×n×m)张量, batch2是一个(b×m×p)张量,输出将是一个(b×n×p)张量

执行矩阵mat1和mat2的矩阵乘法

如果mat1是(n×m)张量,mat2是(m×p)张量,out将是(n×p) 张量

:

执行batch1中存储的矩阵的批矩阵积 第二批

batch1和batch2必须是三维张量,每个张量包含相同的数字 矩阵

如果batch1是(b×n×m)张量,batch2是(b×m×p)张量,out将是 a(b×n×p)张量

下面的代码片段可以工作

import torch

x = torch.mm(torch.randn(100, 78) , torch.randn(78, 10))
bsize = 16
x = torch.bmm(torch.randn(bsize, 100, 78) , torch.randn(bsize, 78, 10))

我不明白每个人说的错误。但我明白了为什么会出现错误消息。两批中的矩阵数量必须相同。有bmm的广播。如果您尝试使用这个
torch.bmm(torch.randn(1000784,1),torch.rand(10001784))
它可以工作。