- 主题:pytorch里,为何允许标量和张量相加?
import torch
a = torch.tensor([2., 3.], requires_grad=True)
b = a + 3
print(b)
这里,输出:tensor([5., 6.], grad_fn=<AddBackward0>)
我就不明白,维数不同的两个张量(分别是0维和1维的),也能相加吗?那是不是说,任意的两个维度的张量,都可以相加?那不是乱套了吗?大佬,解释一下?
--
FROM 120.242.253.*
a = torch.tensor([2., 3.], requires_grad=True)
le=torch.tensor([[2., 3.],[1,1]], requires_grad=True)
#print(le+3)
print(le+a)
似乎采取了自动扩充的策略,把0维扩成1维,1维扩成2维的。比如上面的结果,输出:
tensor([[4., 6.],
[3., 4.]], grad_fn=<AddBackward0>)
【 在 feng321 的大作中提到: 】
: import torch
: a = torch.tensor([2., 3.], requires_grad=True)
: b = a + 3
: ...................
--
FROM 120.242.253.*
数学上没有这么写的。数学上,一般矩阵都用大写字母表示,比如X+3E,E必须写,否则算错。这个框架,他自己定义了一套东西。我看理解成“加常量乘单位阵”,不如理解成扩充,就是将3扩充成和X相同维度的张量
【 在 iMx 的大作中提到: 】
: 为了方便
: 找本本科矩阵论的教材看看?
: 加常量,就是加常量乘单位阵
: ...................
--
FROM 120.242.253.*
好奇,你这里讲的“虚数”,是指如 100j 这样的纯虚数?还是指 12+100j这样的虚数?
【 在 vabc3 的大作中提到: 】
: python传统艺能,看个更离谱的numpy.r_:
: If slice notation is used, the syntax start:stop:step is equivalent to np.arange(start, stop, step) inside of the brackets. However, if step is an imaginary number (i.e. 100j) then its integer portion is interpreted as a number-of-points desired and the start and stop are inclusive. In other words start:stop:stepj is interpreted as np.linspace(start, stop, step, endpoint=1) inside of the brackets. After expansion of slice notation, all comma separated sequences are concatenated together.
: 第三个参数step可以是实数也可以是虚数:实数表示步长,虚数的时候去掉j表示总共要多少步。
--
FROM 120.242.253.*