pytorch - 维度超出范围(预期在 [-2, 1] 范围内,但得到 2)

标签 pytorch

为什么会弹出以​​下错误?这个范围内应该是什么,为什么? -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/

相关文章:

python - 如何在 Pytorch 中迭代层

python - 何时在 pytorch Lightning 中使用prepare_data 与 setup?

python - 后向算法隐马尔可夫模型,第 0 个索引(终止步骤)产生错误结果

python - 如何在 PyTorch 中查找和理解 autograd 源代码

python - 如何在pytorch中通过给定的索引和张量生成新的张量?

python - Pytorch 中的填充

python - 在不使用数据加载器的情况下转换每个训练点

python - 新的曼巴环境强制 torch CPU,我不知道为什么

python - 了解使用 Pytorch 训练神经网络时类的使用

python - Caffe2:加载 ONNX 模型,并在多核主机/docker 上推断单线程