为什么会弹出以下错误?这个范围内应该是什么,为什么? -2 维是什么意思?
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.mm :
Performs a matrix multiplication of the matrices mat1 and mat2.
If mat1 is a (n×m) tensor, mat2 is a (m×p) tensor, out will be a (n×p) tensor.
torch.bmm :
Performs a batch matrix-matrix product of matrices stored in batch1 and batch2.
batch1 and batch2 must be 3-D tensors each containing the same number of matrices.
If batch1 is a (b×n×m) tensor, batch2 is a (b×m×p) tensor, out will be a (b×n×p) tensor.
以下代码片段有效。
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))
关于pytorch - 维度超出范围(预期在 [-2, 1] 范围内,但得到 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50776744/