原文地址:https://zhuanlan.zhihu.com/p/31494491

上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别。

这次我把常用的Tensor的数学运算总结到这里,以防自己在使用PyTorch做实验时,忘记这些方法应该传什么参数。

总结的方法包括:

Tensor求和以及按索引求和:torch.sum() torch.Tensor.indexadd()

Tensor元素乘积:torch.prod(input)

对Tensor求均值、方差、极值:

torch.mean() torch.var()

torch.max() torch.min()

最后还有在NLP领域经常用到的:

求Tensor的平方根倒数、线性插值、双曲正切

torch.rsqrt(input) torch.lerp(star,end,weight)

torch.tanh(input, out=None)

张量数学运算

元素求和

torch.sum(input) → float

返回输入向量input中所有元素的和。

参数:

  • input (Tensor) - 输入张量

例子:

a = torch.randn(1, 3)
a
1.5796 0.4102 -0.2885
[torch.FloatTensor of size 1x3]
torch.sum(a)
1.7013892233371735

torch.sum(input, dim, keepdim=False, out=None) → Tensor

返回新的张量,其中包括输入张量input中指定维度dim中每行的和。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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]

元素乘积

torch.prod(input) → float

返回输入张量input所有元素的乘积。

参数:

  • input (Tensor) - 输入张量

例子:

a = torch.randn(1, 3)
a
0.3618 1.2095 -0.3403
[torch.FloatTensor of size 1x3]
torch.prod(a)
-0.14892165020372308

torch.prod(input, dim, keepdim=False, out=None) → Tensor

返回新的张量,其中包括输入张量input中指定维度dim中每行的乘积。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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]

按索引求和

torch.Tensor.indexadd(dim, index, tensor) → Tensor

按索引参数index中所确定的顺序,将参数张量tensor中的元素与执行本方法的张量的元素逐个相加。参数tensor的尺寸必须严格地与执行方法的张量匹配,否则会发生错误。

参数:

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

例子:

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]

平均数

torch.mean(input)

返回输入张量input中每个元素的平均值。

参数:

  • input (Tensor) – 输入张量

例子:

a = torch.randn(1, 3)
a
0.3361 -0.7302 0.5432
[torch.FloatTensor of size 1x3]
torch.mean(a)
0.04971998929977417

torch.mean(input, dim, keepdim=False, out=None)

返回新的张量,其中包含输入张量input指定维度dim中每行的平均值。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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]

方差

torch.var(input, unbiased=True) → float

返回输入向量input中所有元素的方差。

参数:

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

例子:

a = torch.randn(1, 3)
a
0.4680 -0.5237 0.9480
[torch.FloatTensor of size 1x3]
torch.var(a)
0.5632950516419974

torch.var(input, dim, keepdim=False, unbiased=True, out=None) → Tensor

返回新的张量,其中包括输入张量input中指定维度dim中每行的方差。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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]

最大值

torch.max(input) → float

返回输入张量所有元素的最大值。

参数:

  • input (Tensor) - 输入张量

例子:

a = torch.randn(1, 3)
a
0.0504 -1.1855 -0.3644
[torch.FloatTensor of size 1x3]
torch.max(a)
0.0504443496465683

torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

返回新的张量,其中包括输入张量input中指定维度dim中每行的最大值,同时返回每个最大值的位置索引。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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])

torch.max(input, other, out=None) → Tensor

逐个元素比较张量input与张量other,将比较出的最大值保存到输出张量中。
两个张量尺寸不需要完全相同,但需要支持自动扩展法则。

参数:

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

例子:

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]

最小值

torch.min(input) → float

返回输入张量所有元素的最小值。

参数:

  • input (Tensor) - 输入张量

例子:

a = torch.randn(1, 3)
a
-1.0276 0.5545 0.7714
[torch.FloatTensor of size 1x3]
torch.min(a)
-1.0276073217391968

torch.min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

返回新的张量,其中包括输入张量input中指定维度dim中每行的最小值,同时返回每个最小值的位置索引。

若keepdim值为True,则在输出张量中,除了被操作的dim维度值降为1,其它维度与输入张量input相同。否则,dim维度相当于被执行torch.squeeze()维度压缩操作,导致此维度消失,最终输出张量会比输入张量少一个维度。

参数:

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

例子:

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])

torch.min(input, other, out=None) → Tensor

逐个元素比较张量input与张量other,将比较出的最小值保存到输出张量中。
两个张量尺寸不需要完全相同,但需要支持自动扩展法则。

参数:

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

例子:

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]

平方根倒数

torch.rsqrt(input) → Tensor

返回新的张量,其中包含input张量每个元素平方根的倒数。

参数:

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

例子:

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]

线性插值

torch.lerp(star,end,weight) → Tensor

基于weight对输入的两个张量start与end逐个元素计算线性插值,结果返回至输出张量。

返回结果是:

参数:

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

例子:

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]

双曲正切

torch.tanh(input, out=None) → Tensor

返回新的张量,其中包括输入张量input中每个元素的双曲正切。

参数:

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

例子:

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]


[Pytorch]Pytorch中tensor常用语法的更多相关文章

  1. pytorch中tensor数据和numpy数据转换中注意的一个问题

    转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...

  2. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  3. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  4. 【小白学PyTorch】9 tensor数据结构与存储结构

    文章来自微信公众号[机器学习炼丹术]. 上一节课,讲解了MNIST图像分类的一个小实战,现在我们继续深入学习一下pytorch的一些有的没的的小知识来作为只是储备. 参考目录: @ 目录 1 pyto ...

  5. PyTorch官方中文文档:torch.optim 优化器参数

    内容预览: step(closure) 进行单次优化 (参数更新). 参数: closure (callable) –...~ 参数: params (iterable) – 待优化参数的iterab ...

  6. PHP中Smarty引擎的常用语法

    PHP中Smarty引擎的常用语法 输出今天的日期: {$smarty.now|date_format:"%H:%M %A, %B %e, %Y"} 实际上用到了PHP的time( ...

  7. [Pytorch]PyTorch Dataloader自定义数据读取

    整理一下看到的自定义数据读取的方法,较好的有一下三篇文章, 其实自定义的方法就是把现有数据集的train和test分别用 含有图像路径与label的list返回就好了,所以需要根据数据集随机应变. 所 ...

  8. [pytorch] PyTorch Hook

      PyTorch Hook¶ 为什么要引入hook? -> hook可以做什么? 都有哪些hook? 如何使用hook?   1. 为什么引入hook?¶ 参考:Pytorch中autogra ...

  9. Markdown通用的常用语法说明

    前言 Markdown 是一种轻量级的 标记语言,语法简洁明了.学习容易,还具有其他很多优点,目前被越来越多的人用来写作使用. Markdown具有一系列衍生版本,用于扩展Markdown的功能(如表 ...

随机推荐

  1. memcached-session-manager 教程实现session共享

    1简单介绍     1.1决定用什么序列化策略.     1.2配置tomcat         1.2.1加入 memcached-session-manager jar 包到tomcat中.    ...

  2. spring boot读取配置文件

    一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文     ...

  3. [py][mx]django使用class写views-免去判断方法的烦恼

    修改views使用class模式 类模式写views - 免去了函数模式的判断的烦恼 users/views.py from django.views.generic import View clas ...

  4. OS-96

    print('os.access(path,mode):检验权限模式----------------------------------------------------------------') ...

  5. Qt计时器

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. ■.QObject类的定时器QObje ...

  6. css样式表--样式表分类

    样式表分类 1.内联式.写在body里.控制精确,可重复性差. <body> <div style="color:#90F">更好发挥的返回结果还 < ...

  7. http-equiv="Refresh" 实现定时刷新页面

    ***.html自动跳转文件代码如下: <HTML> <HEAD><META http-equiv="Refresh" content="5 ...

  8. C# 如何把dataTable以参数的形式传入 sql 存储过程

    ==================================================-- sql代码 示例:CREATE TYPE dbo.Content AS TABLE( ID i ...

  9. 有关padding的二三事~~

    浏览器支持 所有浏览器都支持 padding 属性. 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit". 定义和用法 pa ...

  10. 隐马尔科夫模型(HMM)学习笔记二

    这里接着学习笔记一中的问题2,说实话问题2中的Baum-Welch算法编程时矩阵转换有点烧脑,开始编写一直不对(编程还不熟练hh),后面在纸上仔细推了一遍,由特例慢慢改写才运行成功,所以代码里面好多处 ...