- 主题: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.*
为了方便
找本本科矩阵论的教材看看?
加常量,就是加常量乘单位阵
【 在 feng321 的大作中提到: 】
: import torch
: a = torch.tensor([2., 3.], requires_grad=True)
: b = a + 3
: ...................
--
FROM 119.131.204.*
慌啥。规则定义清楚就没事。
复习一下高等数学。
【 在 feng321 的大作中提到: 】
: import torch
: a = torch.tensor([2., 3.], requires_grad=True)
: b = a + 3
: print(b)
: 这里,输出:tensor([5., 6.], grad_fn=<AddBackward0>)
: 我就不明白,维数不同的两个张量(分别是0维和1维的),也能相加吗?那是不是说,任意的两个维度的张量,都可以相加?那不是乱套了吗?大佬,解释一下?
--
FROM 27.38.197.*
数学上没有这么写的。数学上,一般矩阵都用大写字母表示,比如X+3E,E必须写,否则算错。这个框架,他自己定义了一套东西。我看理解成“加常量乘单位阵”,不如理解成扩充,就是将3扩充成和X相同维度的张量
【 在 iMx 的大作中提到: 】
: 为了方便
: 找本本科矩阵论的教材看看?
: 加常量,就是加常量乘单位阵
: ...................
--
FROM 120.242.253.*
原因就是方便写程序。
另外就是为了和numpy语法保持一致,numpy把这特性叫broadcasting,是写在文档里的
,是feature,不是什么不可理解的东西或bug。
【 在 feng321 的大作中提到: 】
: import torch
: a = torch.tensor([2., 3.], requires_grad=True)
: b = a + 3
: ...................
--
FROM 58.33.81.*
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 167.220.255.*
好奇,你这里讲的“虚数”,是指如 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.*
虚数就是纯虚数吧,你说的这个叫复数?
【 在 feng321 的大作中提到: 】
: 好奇,你这里讲的“虚数”,是指如 100j 这样的纯虚数?还是指 12+100j这样的虚数?
: --
发自「今日水木 on MIX 10」
--
FROM 114.254.3.*
数学本来就是不断发展的
19世纪之前还没有线性代数和矩阵呢
numpy这套用法是学统计语言s和r的
【 在 feng321 的大作中提到: 】
: 数学上没有这么写的。数学上,一般矩阵都用大写字母表示,比如X+3E,E必须写,否则算错。这个框架,他自己定义了一套东西。我看理解成“加常量乘单位阵”,不如理解成扩充,就是将3扩充成和X相同维度的张量
--
修改:roy FROM 114.253.33.*
FROM 114.253.33.*