Pytorch中tensor常用语法

Miracle 2019年4月11日00:05:56干货教程Pytorch中tensor常用语法已关闭评论12,83626636字阅读22分7秒阅读模式

Tensor求和以及按索引求和:torch.sum() torch.Tensor.indexadd()文章源自联网快讯-https://x1995.cn/4397.html

Tensor元素乘积:torch.prod(input)文章源自联网快讯-https://x1995.cn/4397.html

对Tensor求均值、方差、极值:文章源自联网快讯-https://x1995.cn/4397.html

torch.mean() torch.var()文章源自联网快讯-https://x1995.cn/4397.html

torch.max() torch.min()文章源自联网快讯-https://x1995.cn/4397.html

最后还有在NLP领域经常用到的:文章源自联网快讯-https://x1995.cn/4397.html

求Tensor的平方根倒数、线性插值、双曲正切文章源自联网快讯-https://x1995.cn/4397.html

torch.rsqrt(input) torch.lerp(star,end,weight)文章源自联网快讯-https://x1995.cn/4397.html

torch.tanh(input, out=None)文章源自联网快讯-https://x1995.cn/4397.html

张量数学运算

元素求和

torch.sum(input) → float文章源自联网快讯-https://x1995.cn/4397.html

返回输入向量input中所有元素的和。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
1.5796 0.4102 -0.2885
[torch.FloatTensor of size 1x3]
torch.sum(a)
1.7013892233371735文章源自联网快讯-https://x1995.cn/4397.html

torch.sum(input, dim, keepdim=False, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中指定维度dim中每行的和。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • dim (int) - 指定维度
  • keepdim (bool) - 输出张量是否保持与输入张量有相同数量的维度
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.rand(4, 4)
a
0.6117 0.2066 0.1838 0.5582
0.7751 0.5258 0.8898 0.4822
0.8238 0.4217 0.2266 0.2178
0.2121 0.6614 0.4635 0.0368
[torch.FloatTensor of size 4x4]
torch.sum(a, 1, True)
1.5602
2.6728
1.6900
1.3737
[torch.FloatTensor of size 4x1]文章源自联网快讯-https://x1995.cn/4397.html

元素乘积

torch.prod(input) → float文章源自联网快讯-https://x1995.cn/4397.html

返回输入张量input所有元素的乘积。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
0.3618 1.2095 -0.3403
[torch.FloatTensor of size 1x3]
torch.prod(a)
-0.14892165020372308文章源自联网快讯-https://x1995.cn/4397.html

torch.prod(input, dim, keepdim=False, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中指定维度dim中每行的乘积。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • dim (int) - 指定维度
  • keepdim (bool) - 输出张量是否保持与输入张量有相同数量的维度
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4, 2)
a
-1.8626 -0.5725
-0.6924 -0.8738
-0.2659 0.3540
-0.4500 1.4647
[torch.FloatTensor of size 4x2]
torch.prod(a, 1, True)
1.0664
0.6050
-0.0941
-0.6592
[torch.FloatTensor of size 4x1]文章源自联网快讯-https://x1995.cn/4397.html

按索引求和

torch.Tensor.indexadd(dim, index, tensor) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

按索引参数index中所确定的顺序,将参数张量tensor中的元素与执行本方法的张量的元素逐个相加。参数tensor的尺寸必须严格地与执行方法的张量匹配,否则会发生错误。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • dim (int) - 索引index所指向的维度
  • index (LongTensor) - 包含索引数的张量
  • tensor (Tensor) - 含有相加元素的张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

x = torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
x
1 1 1
1 1 1
1 1 1
[torch.FloatTensor of size 3x3]
t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
index = torch.LongTensor([0, 2, 1])
x.index_add_(0, index, t)
x
2 3 4
8 9 10
5 6 7
[torch.FloatTensor of size 3x3]文章源自联网快讯-https://x1995.cn/4397.html

平均数

torch.mean(input)文章源自联网快讯-https://x1995.cn/4397.html

返回输入张量input中每个元素的平均值。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) – 输入张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
0.3361 -0.7302 0.5432
[torch.FloatTensor of size 1x3]
torch.mean(a)
0.04971998929977417文章源自联网快讯-https://x1995.cn/4397.html

torch.mean(input, dim, keepdim=False, out=None)文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包含输入张量input指定维度dim中每行的平均值。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量
  • dim (int) - 指定进行均值计算的维度
  • keepdim (bool, optional) - 输出张量是否保持与输入张量有相同数量的维度
  • out (Tensor) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4, 5)
a
0.3168 0.4953 -0.6758 -0.5559 -0.6906
0.2241 2.2450 1.5735 -1.3815 -1.5199
0.0033 0.5236 -0.9070 -0.5961 -2.1281
0.9605 1.5314 -0.6555 -1.2584 -0.4160
[torch.FloatTensor of size 4x5]
torch.mean(a, 1, True)
-0.2220
0.2283
-0.6209
0.0324
[torch.FloatTensor of size 4x1]文章源自联网快讯-https://x1995.cn/4397.html

方差

torch.var(input, unbiased=True) → float文章源自联网快讯-https://x1995.cn/4397.html

返回输入向量input中所有元素的方差。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量
  • unbiased (bool) - 是否使用基于修正贝塞尔函数的无偏估计

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
0.4680 -0.5237 0.9480
[torch.FloatTensor of size 1x3]
torch.var(a)
0.5632950516419974文章源自联网快讯-https://x1995.cn/4397.html

torch.var(input, dim, keepdim=False, unbiased=True, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中指定维度dim中每行的方差。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • dim (int) - 指定维度
  • keepdim (bool) - 输出张量是否保持与输入张量有相同数量的维度
  • unbiased (bool) - 是否使用基于修正贝塞尔函数的无偏估计
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4, 4)
a
0.4364 -0.5140 1.6462 0.7626
-1.2074 -0.3692 0.8664 -0.3861
0.7429 1.2400 -1.8987 1.9651
-0.6547 0.1685 -0.0441 -1.3670
[torch.FloatTensor of size 4x4]
torch.var(a, 1, True)
0.7958
0.7311
2.8353
0.4759
[torch.FloatTensor of size 4x1]文章源自联网快讯-https://x1995.cn/4397.html

最大值

torch.max(input) → float文章源自联网快讯-https://x1995.cn/4397.html

返回输入张量所有元素的最大值。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
0.0504 -1.1855 -0.3644
[torch.FloatTensor of size 1x3]
torch.max(a)
0.0504443496465683文章源自联网快讯-https://x1995.cn/4397.html

torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中指定维度dim中每行的最大值,同时返回每个最大值的位置索引。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • dim (int) - 指定维度
  • keepdim (bool) - 输出张量是否保持与输入张量有相同数量的维度
  • out (tuple,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4, 4)
a
-0.1219 -0.4721 0.4744 -0.5892
0.8172 0.3205 -0.5200 0.3159
-0.6057 0.1025 -0.9742 1.4213
-0.6369 -2.5809 -1.7359 1.3458
[torch.FloatTensor of size 4x4]
torch.max(a, 1, True)
(
0.4744
0.8172
1.4213
1.3458
[torch.FloatTensor of size 4x1],
2
0
3
3
[torch.LongTensor of size 4x1])文章源自联网快讯-https://x1995.cn/4397.html

torch.max(input, other, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

逐个元素比较张量input与张量other,将比较出的最大值保存到输出张量中。
两个张量尺寸不需要完全相同,但需要支持自动扩展法则。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • other (Tensor) - 另一个输入的Tensor
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4)
a
-0.1958
0.3234
1.1194
0.4719
[torch.FloatTensor of size 4]
b = torch.randn(1)
b
-0.3273
[torch.FloatTensor of size 1]
torch.max(a, b)
-0.1958
0.3234
1.1194
0.4719
[torch.FloatTensor of size 4]文章源自联网快讯-https://x1995.cn/4397.html

最小值

torch.min(input) → float文章源自联网快讯-https://x1995.cn/4397.html

返回输入张量所有元素的最小值。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(1, 3)
a
-1.0276 0.5545 0.7714
[torch.FloatTensor of size 1x3]
torch.min(a)
-1.0276073217391968文章源自联网快讯-https://x1995.cn/4397.html

torch.min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中指定维度dim中每行的最小值,同时返回每个最小值的位置索引。文章源自联网快讯-https://x1995.cn/4397.html

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • dim (int) - 指定维度
  • keepdim (bool) - 输出张量是否保持与输入张量有相同数量的维度
  • out (tuple,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4, 4)
a
-0.7407 1.0871 1.1944 0.9493
0.1087 -0.0116 -1.6010 1.1703
-0.3727 -0.3540 -0.6574 0.1487
0.2522 1.9859 1.2496 -0.6711
[torch.FloatTensor of size 4x4]
torch.min(a, 1, True)
(
-0.7407
-1.6010
-0.6574
-0.6711
[torch.FloatTensor of size 4x1],
0
2
2
3
[torch.LongTensor of size 4x1])文章源自联网快讯-https://x1995.cn/4397.html

torch.min(input, other, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

逐个元素比较张量input与张量other,将比较出的最小值保存到输出张量中。
两个张量尺寸不需要完全相同,但需要支持自动扩展法则。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入Tensor
  • other (Tensor) - 另一个输入的Tensor
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4)
a
-0.3359
-2.1027
0.5735
0.1926
[torch.FloatTensor of size 4]
b = torch.randn(1)
b
0.6484
[torch.FloatTensor of size 1]
torch.min(a, b)
-0.3359
-2.1027
0.5735
0.1926
[torch.FloatTensor of size 4]文章源自联网快讯-https://x1995.cn/4397.html

平方根倒数

torch.rsqrt(input) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包含input张量每个元素平方根的倒数。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) – 输入张量
  • out (Tensor, optional) – 输出张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4)
a
0.5471
0.3988
0.5291
-0.3464
[torch.FloatTensor of size 4]
torch.rsqrt(a)
1.3520
1.5836
1.3747
nan
[torch.FloatTensor of size 4]文章源自联网快讯-https://x1995.cn/4397.html

线性插值

torch.lerp(star,end,weight) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

基于weight对输入的两个张量start与end逐个元素计算线性插值,结果返回至输出张量。文章源自联网快讯-https://x1995.cn/4397.html

返回结果是: Pytorch中tensor常用语法文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • start (Tensor) – 起始点张量
  • end (Tensor) – 终止点张量
  • weight (float) – 插入公式的 weight
  • out (Tensor, optional) – 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

start = torch.arange(1, 5)
end = torch.Tensor(4).fill_(10)
start
1
2
3
4
[torch.FloatTensor of size 4]
end
10
10
10
10
[torch.FloatTensor of size 4]
torch.lerp(start, end, 0.5)
5.5000
6.0000
6.5000
7.0000
[torch.FloatTensor of size 4]文章源自联网快讯-https://x1995.cn/4397.html

双曲正切

torch.tanh(input, out=None) → Tensor文章源自联网快讯-https://x1995.cn/4397.html

返回新的张量,其中包括输入张量input中每个元素的双曲正切。文章源自联网快讯-https://x1995.cn/4397.html

参数:文章源自联网快讯-https://x1995.cn/4397.html

  • input (Tensor) - 输入张量
  • out (Tensor,optional) - 结果张量

例子:文章源自联网快讯-https://x1995.cn/4397.html

a = torch.randn(4)
a
-1.6516
-0.7318
-0.5905
0.0520
[torch.FloatTensor of size 4]
torch.tanh(a)
-0.9291
-0.6242
-0.5303
0.0520
[torch.FloatTensor of size 4]文章源自联网快讯-https://x1995.cn/4397.html

文章源自联网快讯-https://x1995.cn/4397.html
继续阅读
Miracle
  • 本文由 发表于 2019年4月11日00:05:56