[Pytorch]Pytorch中tensor常用语法
原文地址: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常用语法的更多相关文章
- pytorch中tensor数据和numpy数据转换中注意的一个问题
转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...
- pytorch中tensor张量数据基础入门
pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...
- PyTorch官方中文文档:torch.nn
torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...
- 【小白学PyTorch】9 tensor数据结构与存储结构
文章来自微信公众号[机器学习炼丹术]. 上一节课,讲解了MNIST图像分类的一个小实战,现在我们继续深入学习一下pytorch的一些有的没的的小知识来作为只是储备. 参考目录: @ 目录 1 pyto ...
- PyTorch官方中文文档:torch.optim 优化器参数
内容预览: step(closure) 进行单次优化 (参数更新). 参数: closure (callable) –...~ 参数: params (iterable) – 待优化参数的iterab ...
- PHP中Smarty引擎的常用语法
PHP中Smarty引擎的常用语法 输出今天的日期: {$smarty.now|date_format:"%H:%M %A, %B %e, %Y"} 实际上用到了PHP的time( ...
- [Pytorch]PyTorch Dataloader自定义数据读取
整理一下看到的自定义数据读取的方法,较好的有一下三篇文章, 其实自定义的方法就是把现有数据集的train和test分别用 含有图像路径与label的list返回就好了,所以需要根据数据集随机应变. 所 ...
- [pytorch] PyTorch Hook
PyTorch Hook¶ 为什么要引入hook? -> hook可以做什么? 都有哪些hook? 如何使用hook? 1. 为什么引入hook?¶ 参考:Pytorch中autogra ...
- Markdown通用的常用语法说明
前言 Markdown 是一种轻量级的 标记语言,语法简洁明了.学习容易,还具有其他很多优点,目前被越来越多的人用来写作使用. Markdown具有一系列衍生版本,用于扩展Markdown的功能(如表 ...
随机推荐
- http如何301到https呢?
HTTPS协议的站点信息更加安全,同时可降低网站被劫持的风险,Firefox和chrome浏览器对访问一些非https站点会提示风险,BD等搜索引擎也明确表态了对https站点的友好.那么我们如何部署 ...
- 测试Celery 在Windows中搭建和使用的版本
官网:http://docs.celeryproject.org/en/latest/faq.html#does-celery-support-windows 描述如下:表示Celery 4.0版本以 ...
- AspxGridView点滴
1:页码设置 1>: <SettingsPager Summary-Text="当前第 {0} 页 总共 {1} 页 ({2} 条记录)"></Settin ...
- Intro to Python for Data Science Learning 6 - NumPy
NumPy From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=1 ...
- 使用IP连接SQL SERVER或者配置为连接字符串失败
使用IP连接SQL SERVER或者配置为连接字符串失败 情景一:当在webconfig文件中使用 <add key="ConnectionString" value=& ...
- kafka存储数据量过大,导致磁盘爆满
问题: 注意到自己负责kafka的某个topic最小的偏移量为0,而最大的偏移量都7亿多了,说明存储在kafka里面的数据没有定时删除,通过登陆到kafka服务器,查看配置文件services.pro ...
- 如何合并两个Git仓库
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- Python装饰器的高级用法(翻译)
原文地址 https://www.codementor.io/python/tutorial/advanced-use-python-decorators-class-function 介绍 我写这篇 ...
- PHP 验证码:扭曲+粘连+变形
一,绪论 由于项目需要,需要加强目前的验证码,我们参照的对象是支付宝. 基于PHP CodeIgniter 框架,代码放置在下面的路径下. /application/libraries 二,主要代码 ...
- 编译时错误之 error C2338: tuple_element index out of bounds
part 1 编译器 vs2015 VC++. 完整的错误信息粘贴如下: d:\program files (x86)\microsoft visual studio 14.0\vc\include\ ...