【问题标题】:PyTorch Broadcasting Failing. "Rules" FollowedPyTorch 广播失败。 “规矩”紧随其后
【发布时间】:2018-03-19 10:54:00
【问题描述】:

我有一些 PyTorch 示例让我感到困惑,希望能把它弄清楚。

首先,根据 PyTorch 页面,我希望这些示例能够像它们的 numpy 等价物一样工作,即 these。第一个例子非常直观。这些与广播兼容:

Image  (3d array): 256 x 256 x 3
Scale  (1d array):             3
Result (3d array): 256 x 256 x 3

以这些为例:

torch.Tensor([[1,2,3]])/torch.Tensor([1,2,3])
Out[5]: 

 1  1  1
[torch.FloatTensor of size 1x3]
torch.Tensor([[1,2,3]])/torch.Tensor([1,2,3])
Out[6]: 

  1  1  1
[torch.FloatTensor of size 1x3]

torch.Tensor([[[1,2,3]]])/torch.Tensor([1,2,3])
Out[7]: 

(0 ,.,.) = 
  1  1  1
[torch.FloatTensor of size 1x1x3]

但是,这是 numpy 示例的结果:

torch.randn(256,256,3)/torch.Tensor([1,2,3])
Traceback (most recent call last):

  File "<ipython-input-12-4c328d453e24>", line 1, in <module>
    torch.randn(256,256,3)/torch.Tensor([1,2,3])

  File "/home/nick/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 313, in __div__
    return self.div(other)

RuntimeError: inconsistent tensor size at /opt/conda/conda-bld/pytorch_1501971235237/work/pytorch-0.1.12/torch/lib/TH/generic/THTensorMath.c:873

Here is an excerpt which says this ought to work:

如果以下规则成立,则两个张量是“可广播的”:

  1. 每个张量至少有一个维度。
  2. 在迭代维度大小时,从尾随维度开始,维度大小必须相等,其中一个为 1,或者其中一个不存在。

如果将张量转换为 numpy 数组,则算术将按预期工作。

怎么了?我是否误解了文档,如果有,什么语法会产生相同的结果?

【问题讨论】:

    标签: python numpy torch pytorch


    【解决方案1】:

    确认您使用的是正确版本的 pytorch。必须是 0.2.0,也就是在 pytorch 中引入广播的时候。

    In[2]: import torch
    In[3]: torch.__version__
    
    Out[3]: '0.2.0_4'
    

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 1970-01-01
      • 2023-04-04
      • 2020-05-01
      • 2023-03-04
      • 2019-11-25
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多