深度学习Pytorch框架Tensor张量
点击上方“视学算法”,选择加"星标"或“置顶”
重磅干货,第一时间送达
作者 | 秦一@知乎(已授权)
来源 | https://zhuanlan.zhihu.com/p/399350505
编辑 | 极市平台
导读
本文主要介绍了Tensor的裁剪运算、索引与数据筛选、组合/拼接、切片、变形操作、填充操作和Tensor的频谱操作(傅里叶变换)。
1 Tensor的裁剪运算
对Tensor中的元素进行范围过滤
常用于梯度裁剪(gradient clipping),即在发生梯度离散或者梯度爆炸时对梯度的处理
torch.clamp(input, min, max, out=None) → Tensor:将输入
input
张量每个元素的夹紧到区间 [min,max],并返回结果到一个新张量。
2 Tensor的索引与数据筛选
torch.where(codition,x,y):按照条件从x和y中选出满足条件的元素组成新的tensor,输入参数condition:条件限制,如果满足条件,则选择a,否则选择b作为输出。
torch.gather(input,dim,index,out=None):在指定维度上按照索引赋值输出tensor
torch.inex_select(input,dim,index,out=None):按照指定索引赋值输出tensor
torch.masked_select(input,mask,out=None):按照mask输出tensor,输出为向量
torch.take(input,indices):将输入看成1D-tensor,按照索引得到输出tensor
torch.nonzero(input,out=None):输出非0元素的坐标
import torch
#torch.wherea = torch.rand(4, 4)
b = torch.rand(4, 4)print(a)
print(b)out = torch.where(a > 0.5, a, b)print(out)
print("torch.index_select")
a = torch.rand(4, 4)
print(a)
out = torch.index_select(a, dim=0,index=torch.tensor([0, 3, 2]))
#dim=0按列,index取的是行
print(out, out.shape)
print("torch.gather")
a = torch.linspace(1, 16, 16).view(4, 4)print(a)out = torch.gather(a, dim=0,index=torch.tensor([[0, 1, 1, 1],[0, 1, 2, 2],[0, 1, 3, 3]]))
print(out)
print(out.shape)
#注:从0开始,第0列的第0个,第一列的第1个,第二列的第1个,第三列的第1个,,,以此类推
#dim=0, out[i, j, k] = input[index[i, j, k], j, k]
#dim=1, out[i, j, k] = input[i, index[i, j, k], k]
#dim=2, out[i, j, k] = input[i, j, index[i, j, k]]
print("torch.masked_index")
a = torch.linspace(1, 16, 16).view(4, 4)
mask = torch.gt(a, 8)
print(a)
print(mask)
out = torch.masked_select(a, mask)
print(out)
print("torch.take")
a = torch.linspace(1, 16, 16).view(4, 4)b = torch.take(a, index=torch.tensor([0, 15, 13, 10]))print(b)
#torch.nonzero
print("torch.take")
a = torch.tensor([[0, 1, 2, 0], [2, 3, 0, 1]])
out = torch.nonzero(a)
print(out)
#稀疏表示
3 Tensor的组合/拼接
torch.cat(seq,dim=0,out=None):按照已经存在的维度进行拼接
torch.stack(seq,dim=0,out=None):沿着一个新维度对输入张量序列进行连接。序列中所有的张量都应该为相同形状。
print("torch.stack")
a = torch.linspace(1, 6, 6).view(2, 3)
b = torch.linspace(7, 12, 6).view(2, 3)
print(a, b)
out = torch.stack((a, b), dim=2)
print(out)
print(out.shape)print(out[:, :, 0])
print(out[:, :, 1])
4 Tensor的切片
torch.chunk(tensor,chunks,dim=0):按照某个维度平均分块(最后一个可能小于平均值)
torch.split(tensor,split_size_or_sections,dim=0):按照某个维度依照第二个参数给出的list或者int进行分割tensor
5 Tensor的变形操作
torch().reshape(input,shape)
torch().t(input):只针对2D tensor转置
torch().transpose(input,dim0,dim1):交换两个维度
torch().squeeze(input,dim=None,out=None):去除那些维度大小为1的维度
torch().unbind(tensor,dim=0):去除某个维度
torch().unsqueeze(input,dim,out=None):在指定位置添加维度,dim=-1在最后添加
torch().flip(input,dims):按照给定维度翻转张量
torch().rot90(input,k,dims):按照指定维度和旋转次数进行张量旋转
import torch
a = torch.rand(2, 3)
print(a)
out = torch.reshape(a, (3, 2))
print(out)
print(a)
print(torch.flip(a, dims=[2, 1]))print(a)
print(a.shape)
out = torch.rot90(a, -1, dims=[0, 2]) #顺时针旋转90°
print(out)
print(out.shape)
6 Tensor的填充操作
torch.full((2,3),3.14)
7 Tensor的频谱操作(傅里叶变换)
如果觉得有用,就请分享到朋友圈吧!
点个在看 paper不断!
深度学习Pytorch框架Tensor张量相关推荐
- 深度学习Pytorch框架
深度学习Pytorch框架 文章目录 深度学习Pytorch框架 前言 1. Pytorch命令之``nn.Sequential`` 2. Pytorch命令之``nn.Conv2d`` 3. Pyt ...
- 数据增强 transform_深度学习-Pytorch框架学习之数据处理篇
前言 数据是深度学习的核心,大部分论文里都会提到data-driven这个词,也就是数据驱动的意思.基本的模型搭建完成后,如何处理数据,如何将数据送给网络,如何做数据增强等等,对于提高网络的性能都十分 ...
- 深度学习tensorflow框架的张量
#-*-coding:utf-8-*- import tensorflow as tf #默认不进行转化 a=tf.constant(3.0,dtype=tf.float32) c=tf.consta ...
- torch的拼接函数_从零开始深度学习Pytorch笔记(13)—— torch.optim
前文传送门: 从零开始深度学习Pytorch笔记(1)--安装Pytorch 从零开始深度学习Pytorch笔记(2)--张量的创建(上) 从零开始深度学习Pytorch笔记(3)--张量的创建(下) ...
- 【完结】12大深度学习开源框架(caffe,tf,pytorch,mxnet等)快速入门项目
这是一篇总结文,给大家来捋清楚12大深度学习开源框架的快速入门,这是有三AI的GitHub项目,欢迎大家star/fork. https://github.com/longpeng2008/yousa ...
- 12大深度学习开源框架(caffe,tensorflow,pytorch,mxnet等)汇总详解
这是一篇总结文,给大家来捋清楚12大深度学习开源框架的快速入门,这是有三AI的GitHub项目,欢迎大家star/fork. https://github.com/longpeng2008/yousa ...
- 自制深度学习推理框架-第十一节-再探Tensor类并构建计算图的图关系
自制深度学习推理框架-第十一节-再探Tensor类并准备算子的输入输出 本课程介绍 我写了一个<从零自制深度学习推理框架>的课程,课程语言是 C++,课程主要讲解包括算子实现和框架设计的思 ...
- Karpathy更新深度学习开源框架排名:TensorFlow第一,PyTorch第二
上周,Keras作者.谷歌研究科学家François Chollet晒出一张图,他使用Google Search Index,展示了过去三个月,ArXiv上提到的深度学习框架排行,新智元也做了报道: ...
- 深度学习主流框架介绍(PyTorch、TensorFlow、Keras、Caffe、Theano、MXNET)
深度学习主流框架介绍(PyTorch.TensorFlow.Keras.Caffe.Theano.MXNET) 1.Theano Theano是最早的深度学习框架之一,由 Yoshua Bengio ...
最新文章
- python 生成器笔记
- vim 编辑器的快捷键
- vue项目中使用mock(二)
- python如何输入多行数据合并_python 实现将txt文件多行合并为一行并将中间的空格去掉方法...
- react native 学习
- 见习经理_第一天洗了整天碗
- 如何使用Jmeter对WebSocket进行压力测试
- CSDN免费快速获得积分和直接获取下载码的几个办法,亲测有效区
- 洛谷OJ:P3811 【模板】乘法逆元(线性递推求逆元)
- 【vue】vue中如何实现SPA 单页面应用_09
- 2021年中国DevOps现状调查报告发布!
- 老罗与西门子的公关战争
- 【建站教程】网站引用三方图片遇到简单防盗链referer的处理办法
- cognos使用自定义函数_将单点登录添加到您的IBM Cognos 8自定义Java身份验证提供程序
- 记录建hbase对应hive外表坑
- DNA核苷酸含量计算
- [PTA]习题11-1 输出月份英文名
- Python的整数与浮点数计算
- 第一部分 思科九年 一(6)
- 通用模型、全新框架,WavLM语音预训练模型全解