【发布时间】: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,或者其中一个不存在。
如果将张量转换为 numpy 数组,则算术将按预期工作。
怎么了?我是否误解了文档,如果有,什么语法会产生相同的结果?
【问题讨论】:
标签: python numpy torch pytorch