pytorch广播如何工作? [英] How does pytorch broadcasting work?

查看:118
本文介绍了pytorch广播如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

torch.add(torch.ones(4,1), torch.randn(4))

产生一个张量为torch.Size([4,4])的张量.

produces a Tensor with size: torch.Size([4,4]).

有人可以为此提供逻辑依据吗?

Can someone provide a logic behind this?

推荐答案

PyTorch broadcasting 基于 numpy广播语义,可以通过阅读

PyTorch broadcasting is based on numpy broadcasting semantics which can be understood by reading numpy broadcasting rules or PyTorch broadcasting guide. Expounding the concept with an example would be intuitive to understand it better. So, please see the example below:

In [27]: t_rand
Out[27]: tensor([ 0.23451,  0.34562,  0.45673])

In [28]: t_ones
Out[28]: 
tensor([[ 1.],
        [ 1.],
        [ 1.],
        [ 1.]])

现在> torch.add(t_rand, t_ones) ,将其可视化为:

Now for torch.add(t_rand, t_ones), visualize it like:

               # shape of (3,)
               tensor([ 0.23451,      0.34562,       0.45673])
      # (4, 1)          | | | |       | | | |        | | | |
      tensor([[ 1.],____+ | | |   ____+ | | |    ____+ | | |
              [ 1.],______+ | |   ______+ | |    ______+ | |
              [ 1.],________+ |   ________+ |    ________+ |
              [ 1.]])_________+   __________+    __________+

应该将输出的张量形状为(4,3)的形式设置为:

which should give the output with tensor of shape (4,3) as:

# shape of (4,3)
In [33]: torch.add(t_rand, t_ones)
Out[33]: 
tensor([[ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673]])


另外,请注意,即使与上一个参数相反的顺序传递参数,我们也会得到完全相同的结果:


Also, note that we get exactly the same result even if we pass the arguments in a reverse order as compared to the previous one:

# shape of (4, 3)
In [34]: torch.add(t_ones, t_rand)
Out[34]: 
tensor([[ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673],
        [ 1.23451,  1.34562,  1.45673]])

无论如何,我更喜欢前一种理解方式,以实现更直接的直观性.

Anyway, I prefer the former way of understanding for more straightforward intuitiveness.

为了便于理解,我挑选出更多示例,如下所示:

For pictorial understanding, I culled out more examples which are enumerated below:

Example-1:

Example-1:

Example-2: :

Example-2::

TF分别代表TrueFalse,并指明我们允许广播的尺寸(来源:

T and F respectively stand for True and False and indicate along which dimensions we allow broadcasting (source: Theano).

Example-3:

Example-3:

在某些形状中,广播了数组b 以匹配数组a的形状.

Here are some shapes where array b is broadcasted appropriately to match the shape of array a.

这篇关于pytorch广播如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆