PyTorch 笔记(05)— Tensor 基本运算(torch.abs、torch.add、torch.clamp、torch.div、torch.mul、torch.pow等)
1. 函数汇总
Tensor
的基本运算会对 tensor
的每一个元素进行操作,此类操作的输入与输出形状一致,常用操作见下表所示。
对于很多操作,例如 div
、mul
、pow
、fmod
、等, PyTorch
都实现了运算符重载,所以可以直接使用运算符,如 a ** 2
等价于 torch.pow(a, 2)
, a *2
等价于 torch.mul(a, 2)
。
2. 函数功能
2.1 torch.abs
将参数传递到 torch.abs
后返回输入参数的绝对值作为输出,输入参数必须是一个 Tensor
数据类型的变量。
import torch
a = torch.randn(2, 3)
print a
b = torch.abs(a)
print b
输出结果:
tensor([[ 0.4873, 0.8172, 0.2003],[ 0.6815, 1.5656, -0.8303]])
tensor([[0.4873, 0.8172, 0.2003],[0.6815, 1.5656, 0.8303]])
2.2 torch.add
将参数传递到 torch.add
后返回输入参数的求和结果作为输出,输入参数既可以全部是 Tensor
数据类型的变量,也可以一个是 Tensor
数据类型的变量,另一个是标量。
a = torch.ones([2, 3])
print a
b = torch.add(a, 10)
print b
c = torch.randn([2, 3])
print c
d = torch.add(a, c)
print d
输出结果:
tensor([[1., 1., 1.],[1., 1., 1.]])
tensor([[11., 11., 11.],[11., 11., 11.]])
tensor([[ 0.4276, 0.4618, -0.9449],[-1.4758, -1.3118, -0.2859]])
tensor([[ 1.4276, 1.4618, 0.0551],[-0.4758, -0.3118, 0.7141]])
2.3 torch.clamp
对输入参数按照自定义的范围进行裁剪,最后将参数裁剪的结果作为输出。所以输入参数一共有三个,分别是需要进行裁剪的 Tensor
数据类型的变量、裁剪的上边界和裁剪的下边界。
具体的裁剪过程是:使用变量中的每个元素分别和裁剪的上边界及裁剪的下边界的值进行比较,如果元素的值小于裁剪的下边界的值,该元素就被重写成裁剪的下边界的值;同理,如果元素的值大于裁剪的上边界的值,该元素就被重写成裁剪的上边界的值。
a = torch.randn(2,3)
print a
b = torch.clamp(a, -0.1, 0.1)
print b
输出结果:
tensor([[-1.0129, -1.3094, 0.4644],[ 1.0542, -0.5259, -0.3575]])
tensor([[-0.1000, -0.1000, 0.1000],[ 0.1000, -0.1000, -0.1000]])
2.4 torch.div
将参数传递到 torch.div
后返回输入参数的求商结果作为输出,同样,参与运算的参数可以全部是 Tensor
数据类型的变量,也可以是 Tensor
数据类型的变量和标量的组合。
a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.div(a, 2)
print b
c = torch.IntTensor([2,4,6,8,10])
d = torch.div(a, c)
print d
输出结果:
tensor([ 2, 4, 6, 8, 10], dtype=torch.int32)
tensor([1, 2, 3, 4, 5], dtype=torch.int32)
tensor([1, 1, 1, 1, 1], dtype=torch.int32)
2.5 torch.mul
将参数传递到 torch.mul
后返回输入参数求积的结果作为输出,参与运算的参数可以全部是 Tensor
数据类型的变量,也可以是 Tensor
数据类型的变量和标量的组合。
a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.mul(a, 2)
print b
c = torch.IntTensor([2,4,6,8,10])
d = torch.mul(a, c)
print d
输出结果:
tensor([ 2, 4, 6, 8, 10], dtype=torch.int32)
tensor([ 4, 8, 12, 16, 20], dtype=torch.int32)
tensor([ 4, 16, 36, 64, 100], dtype=torch.int32)
2.6 torch.pow
将参数传递到 torch.pow
后返回输入参数的求幂结果作为输出,参与运算的参数可以全部是 Tensor
数据类型的变量,也可以是 Tensor
数据类型的变量和标量的组合。
a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.pow(a, 2)
print b
c = torch.IntTensor([3,3,3,3,3])
d = torch.pow(a, c)
print d
输出结果:
tensor([ 2, 4, 6, 8, 10], dtype=torch.int32)
tensor([ 4, 16, 36, 64, 100], dtype=torch.int32)
tensor([ 8, 64, 216, 512, 1000], dtype=torch.int32)
2.7 torch.mm
将参数传递到 torch.mm
后返回输入参数的求积结果作为输出,不过这个求积的方式和之前的 torch.mul
运算方式不太一样, torch.mm
运用矩阵之间的乘法规则进行计算,所以被传入的参数会被当作矩阵进行处理,参数的维度自然也要满足矩阵乘法的前提条件,即前一个矩阵的行数必须和后一个矩阵的列数相等,否则不能进行计算。
a = torch.IntTensor([[1,2,3], [4,5,6]])
print a
b = torch.IntTensor([[1,2], [4,5], [7, 8]])
print b
c = torch.mm(a, b)
print c
输出结果:
tensor([[1, 2, 3],[4, 5, 6]], dtype=torch.int32)
tensor([[1, 2],[4, 5],[7, 8]], dtype=torch.int32)
tensor([[30, 36],[66, 81]], dtype=torch.int32)
2.8 torch.mv
将参数传递到 torch.mv
后返回输入参数的求积结果作为输出,torch.mv
运用矩阵与向量之间的乘法规则进行计算,被传入的参数中的第 1 个参数代表矩阵,第 2 个参数代表向量,顺序不能颠倒。
a = torch.IntTensor([[1,0,0], [0,0,1]])
print a
b = torch.IntTensor([3,3,3])
print b
c = torch.mv(a, b)
print c
输出结果:
tensor([[1, 0, 0],[0, 0, 1]], dtype=torch.int32)
tensor([3, 3, 3], dtype=torch.int32)
tensor([3, 3], dtype=torch.int32)
PyTorch 笔记(05)— Tensor 基本运算(torch.abs、torch.add、torch.clamp、torch.div、torch.mul、torch.pow等)相关推荐
- PYTORCH 笔记 DILATE 代码解读
dilate 完整代码路径:vincent-leguen/DILATE: Code for our NeurIPS 2019 paper "Shape and Time Distortion ...
- PyTorch 笔记(02)— 常用创建 Tensor 方法(torch.Tensor、ones、zeros、eye、arange、linspace、rand、randn、new)
1. Tensor 概念分类 PyTorch 中的张量(Tensor)类似 NumPy 中的 ndarrays,之所以称之为 Tensor 的另一个原因是它可以运行在 GPU 中,以加速运算. 1.1 ...
- PyTorch 笔记(12)— Tensor 持久化、向量化、torch.set_num_threads、torch.set_printoptions
1. 持久化 在 PyTorch中 ,以下对象可以持久化到硬盘,并能通过相应的方法加载到内存中: Tensor Variable nn.Module Optimizer 本质上上述这些信息最终都是保存 ...
- PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...
- pytorch笔记:torch.sparse类
PyTorch 提供了 torch.Tensor 来表示一个包含单一数据类型元素的多维数组. 默认情况下,数组元素连续存储在内存中,从而可以有效地实现各种数组处理算法,这些算法依赖于对数组元素的快速访 ...
- 【 线性回归 Linear-Regression torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人(4/10)】
torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人 深度学习 Pytorch笔记 B站刘二大人(4/10) 介绍 至此开始,深度学习模型构建的预备知识已经完全准备完毕. 从本章开 ...
- 【Torch笔记】Tensor 简介与创建方法
[Torch笔记]Tensor 1 什么是 Tensor? Tensor,又称张量,它是矩阵向任意维度的推广. [Tensor 与 Variable(目前已弃用,但最好了解一下)] Variable ...
- pytorch笔记:torch.nn.functional.pad
1 torch.nn.functional.pad函数 torch.nn.functional.pad是pytorch内置的tensor扩充函数,便于对数据集图像或中间层特征进行维度扩充 torch. ...
- torch的拼接函数_从零开始深度学习Pytorch笔记(13)—— torch.optim
前文传送门: 从零开始深度学习Pytorch笔记(1)--安装Pytorch 从零开始深度学习Pytorch笔记(2)--张量的创建(上) 从零开始深度学习Pytorch笔记(3)--张量的创建(下) ...
最新文章
- CNN 常用网络结构解析 1x1 卷积运算 示意图
- 制造业采购审批流程设计示例
- 【C++ 语言】pthread_mutex_t 互斥锁
- Spread for Windows Forms 7新功能使用指南
- boost::geometry::concepts::check用法的测试程序
- 工具资源系列之给mac装个虚拟机
- 【Java】对Web Service的理解
- [转]OpenCL 教学(一)
- Java-进阶:Java-File--IO—1
- C++11 多线程 基础
- docker php 一键部署_Java开发提升十倍生产力:Idea远程一键部署springboot到Docker
- 今年圣诞,麋鹿第一次请假
- 微服务开发的 10 个最佳实践
- 数据结构中三表合一的实现
- 计算机都要学python吗_大学计算机要不要学python?
- return 1 或 return 0 的作用
- 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思
- NetLogo的下载安装过程
- 玩客云刷linuxARMBIAN当服务器过程记录
- 201571030135/201571030137《小学四则运算练习软件》结对项目报告