一、组织张量的元素

(1)重排张量元素

本节介绍在不改变 张量元素个数 和 各元素的值的情况下改变张量的大小

torch.Tensor类的成员方法 reshape()

参数是多个int类型的值。

如果想要把一个张量的大小改成 s[0],s[1],s[2],s[3]....那就让s[0],s[1],s[2],s[3]....作为reshape() 方法的n个参数

使用 reshape()  在不改变元素个数和各元素的值的情况下改变张量大小

tc = torch.arange(12) #张量大小 (12,)
print('tc={}'.format(tc))
t322 = tc.reshape(3,2,2) #张量大小 (3,2,2)
print('t322={}'.format(t322))
t43 = t322.reshape(4,3) #张量大小(4,3)
print('t43={}'.format(t43))
tc=tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
t322=tensor([[[ 0, 1],
[ 2, 3]], [[ 4, 5],
[ 6, 7]], [[ 8, 9],
[10, 11]]])
t43=tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

在reshape() 参数里,1个维度的大小用 -1 代替。如果某个维度的大小用-1代替,那么该函数就会根据张量总元素的个数 和 其他各维度的元素个数 自动计算这个用 -1指定的维度大小

例如:

t =  torch.arange(24).reshape(2,-1,4)
print(t)
tensor([[[ 0,  1,  2,  3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]], [[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])

squeeze() 可以消除张量大小中大小为1的维度

t1 =  torch.arange(24).reshape(2,1,3,1,4)
print(t1.squeeze()) #消去一维后,大小为 (2,3,4)
tensor([[[ 0,  1,  2,  3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]], [[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])

unsqueeze()  增加一些大小为0的维度,增加的维度的位置由关键字参数 dims 指定

t2 =  torch.arange(24).reshape(2,3,4)
print(t2.unsqueeze(dim=2))
tensor([[[[ 0,  1,  2,  3]],

         [[ 4,  5,  6,  7]],

         [[ 8,  9, 10, 11]]],

        [[[12, 13, 14, 15]],

         [[16, 17, 18, 19]],

         [[20, 21, 22, 23]]]])

(2)张量的交换

张量的交换是将张量的各维度重新排列

permute () 交换

tt = torch.arange(24).reshape(1,2,3,4)   #大小 = (1,2,3,4)
print(tt.permute(dims=[2,0,1,3])) #大小 = (3,1,2,4) 即:交换原维度的第2 ..0 ..1..3。。。个维度

transpose() 转置      t()  二维张量装置

t12 = torch.tensor([[5.,-9.],])
print('t12={}'.format(t12))
t21 = t12.transpose(0,1)
print('t21={}'.format(t21))
t21 = t12.t()
print('t21={}'.format(t21))
t12=tensor([[ 5., -9.]])
t21=tensor([[ 5.],
[-9.]])
t21=tensor([[ 5.],
[-9.]])

(3)选取部分张量元素

 index_select() 

这个成员方法有两个参数 ———— 参数dim 是一个int 值 —— 表示对 哪个维度进行处理

参数index —— 是一个 torch.Tensor 实例,表示选取那个维度的哪些指标

例如: 调用index_select() 的torch.Tensor() 类实例的大小为(s[0]...s[x]..s[n-1]),dim = 1,index = (x,) 那么选取得到的张量大小 为(s[0]....s[x-1],x,s[x+1]...s[n-1])

t = torch.arange(24).reshape(2,3,4)  #大小 = {2,3,4}
index = torch.tensor([1,2]) #大小 (2,)
print(t.index_select(1,index)) #大小 = {2,2,4}
tensor([[[ 4,  5,  6,  7],
[ 8, 9, 10, 11]], [[16, 17, 18, 19],
[20, 21, 22, 23]]])

要对张量的某个维度进行选取 ———— 也可以用  [] ———— 需要给出在所有维度上的指标。在每个维度上,可以选取1个指标、多个指标【包括全部指标】。

t = torch.arange(12)
print(t)
print(t[3]) #选取1个元素,大小(),值为3
print(t[-5]) #选取1个元素,大小(),值为7
print(t[3:6]) #选取连续元素,大小(3,)
print(t[:6]) #选取连续元素,大小(6,)
print(t[3:]) #选取连续元素,大小(9,)
print(t[-5:]) #选取连续元素,大小(5,)
print(t[3:6:2]) #选取不连续元素,大小(2,)
print(t[3::2]) #选取不连续元素,大小(5,)
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
tensor(3)
tensor(7)
tensor([3, 4, 5])
tensor([0, 1, 2, 3, 4, 5])
tensor([ 3, 4, 5, 6, 7, 8, 9, 10, 11])
tensor([ 7, 8, 9, 10, 11])
tensor([3, 5])
tensor([ 3, 5, 7, 9, 11])

对于有多个维度的情况,可以用英文"," 分割各个维度的指标

t = torch.arange(12).reshape(3,4)
print(t)
print(t[2:,-2]) #大小(1,) 值为[10,]
print(t[0,:]) #大小(4,) 值为[0,1,2,3]
tensor([[ 0,  1,  2,  3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
tensor([10])
tensor([0, 1, 2, 3])

 masked_select() 

接受一个张量参数 mask,它的大小必须调和 用masked_selected()  方法的类实例相同,并且元素类型必须为 torch.int8

张量mask 里面的元素非0即1,表示是否要选择对应元素

masked_selected()  将张量mask 选定的那些值以一个一维张量的形式返回,一维张量的元素个数就是张量mask 中1的个数

t = torch.arange(12).reshape(3,4)
mask = torch.tensor([[1,0,0,1],[0,1,1,0],[0,0,1,0]],dtype=torch.uint8)
t.masked_select(mask) #大小(5,)
tensor([ 0,  3,  5,  6, 10])

take()

不再考虑张量的具体大小,而只考虑张量的元素总个数

take() 函数将张量个元素按照唯一的指标进行索引,相当于对经过 reshape(-1) 操作后的张量进行索引

t = torch.arange(12).reshape(3,4)
print(t)
indices = torch.tensor([2,5,6])
print(indices)
print(t.take(indices)) #大小(3,) 值为[2,5,6]
tensor([[ 0,  1,  2,  3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
tensor([2, 5, 6])
tensor([2, 5, 6])

(4)张量的扩展和拼接

repeat() 

可以将张量的内容进行重复,使张量的大小变大

(s[0],s[1]....s[n-1]) —— repeat() ———> (s[0]r[0],s[1]r[1]....s[n-1]r[n-1])

t12 = torch.tensor([[5.,-9.],])
print('t12={}'.format(t12)) #大小(1,2)
t34 = t12.repeat(3,2)
print(t34) #大小(3,4) 1*3 = 3 2*2 =4
t12=tensor([[ 5., -9.]])
tensor([[ 5., -9., 5., -9.],
[ 5., -9., 5., -9.],
[ 5., -9., 5., -9.]])

torch.cat() 

tp = torch.arange(12).reshape(3,4)
tn = -tp
tc0 = torch.cat([tp,tn],0)
print(tc0)
tc1 = torch.cat([tp,tp,tn,tn],1)
print(tc1)
tensor([[  0,   1,   2,   3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, -1, -2, -3],
[ -4, -5, -6, -7],
[ -8, -9, -10, -11]])
tensor([[ 0, 1, 2, 3, 0, 1, 2, 3, 0, -1, -2, -3, 0, -1,
-2, -3],
[ 4, 5, 6, 7, 4, 5, 6, 7, -4, -5, -6, -7, -4, -5,
-6, -7],
[ 8, 9, 10, 11, 8, 9, 10, 11, -8, -9, -10, -11, -8, -9,
-10, -11]])

torch.stack()

同样有 张量列表(或元组)和 维度两个参数

stack() 函数要求输入张量的大小完全相同,得到的张量的维度会比输入的张量的大小多1,并且多出的那个维度就是拼接的维度

tp = torch.arange(12).reshape(3,4)
tn = -tp
ts0 = torch.stack([tn,tp],0)
print(ts0)
ts1 = torch.stack([tp,tp,tn,tn],1)
print(ts1)
tensor([[[  0,  -1,  -2,  -3],
[ -4, -5, -6, -7],
[ -8, -9, -10, -11]], [[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]])
tensor([[[ 0, 1, 2, 3],
[ 0, 1, 2, 3],
[ 0, -1, -2, -3],
[ 0, -1, -2, -3]], [[ 4, 5, 6, 7],
[ 4, 5, 6, 7],
[ -4, -5, -6, -7],
[ -4, -5, -6, -7]], [[ 8, 9, 10, 11],
[ 8, 9, 10, 11],
[ -8, -9, -10, -11],
[ -8, -9, -10, -11]]])

Pytorch笔记 (3) 科学计算2的更多相关文章

  1. Pytorch笔记 (3) 科学计算1

    一.张量 标量 可以看作是  零维张量 向量 可以看作是  一维张量 矩阵 可以看作是  二维张量 继续扩展数据的维度,可以得到更高维度的张量 ————>  张量又称 多维数组 给定一个张量数据 ...

  2. python学习笔记(2):科学计算及数据可视化入门

    一.NumPy 1.NumPy:Numberical Python 2.高性能科学计算和数据分析的基础包 3.ndarray,多维数组(矩阵),具有矢量运算的能力,快速.节省空间 (1)ndarray ...

  3. Python 科学计算-介绍

    Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...

  4. Python科学计算(一)

    作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文件 http://git ...

  5. [Pytorch] pytorch笔记 <三>

    pytorch笔记 optimizer.zero_grad() 将梯度变为0,用于每个batch最开始,因为梯度在不同batch之间不是累加的,所以必须在每个batch开始的时候初始化累计梯度,重置为 ...

  6. Python科学计算三维可视化(整理完结)

    中国MOOC<Pyhton计算计算三维可视化>总结 课程url:here ,教师:黄天宇,嵩天 下文的图片和问题,答案都是从eclipse和上完课后总结的,转载请声明. Python数据三 ...

  7. Anaconda 用于科学计算的 Python 发行版

    用于科学计算的 Python 发行版: 1.Anaconda  https://www.continuum.io/    公司continuum.  有商业版本. Anaconda is the le ...

  8. 科学计算软件——Octave安装

    Octave是一个旨在提供与Matlab语法兼容的开放源代码科学计算及数值分析的工具,是Matlab商业软件的一个强有力的竞争产品. 参考:[ML:Octave Installation] Gener ...

  9. windows下安装python科学计算环境,numpy scipy scikit ,matplotlib等

    安装matplotlib: pip install matplotlib 背景: 目的:要用Python下的DBSCAN聚类算法. scikit-learn 是一个基于SciPy和Numpy的开源机器 ...

随机推荐

  1. Axure 日期函数详解

    时间函数详解 Now     根据计算机系统设定的日期和时间返回当前的日期和时间值.如:设置元件文本的值为:[[Now]]:输出:Mon Jan 08 2018 10:42:55 GMT+0800 ( ...

  2. vue 设置当前页背景色

    beforeRouteEnter(to, from, next) { // 添加背景色 document.querySelector('body').setAttribute('style', 'ba ...

  3. Git账号Window10系统配置密钥

    Git 拉取 推送 报错 . Window10系统 需要配置Git账号密钥.

  4. html5 和h5的区别

    html5 是公认的web开发的html规范,是一系列关于html的标准,它就好比是国家的法律,比如未成年不准进网吧,网吧要是允许未成年人进入,国家就要对网吧和未成年人进行处罚和教育.同样的,你写的h ...

  5. ASP.NET大文件上传断点续传解决方案

    HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...

  6. UVA 11346 Possibility

    #include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...

  7. vue-cli的基础构造

    1,项目目录 2, bulid 下文件及目录 3,config下文件及目录 接下来说说vue-cli项目中页面相关的主要文件^o^ 首先是index.html: 说明:一般只定义一个空的根节点,在ma ...

  8. Linux下kafka集群的搭建

    上一篇日志已经搭建好了zookeeper集群,详细请查看:http://www.cnblogs.com/lianliang/p/6533670.html,接下来继续搭建kafka的集群 1.首先下载k ...

  9. C#程序模拟登录批量获取各种邮件内容信息

    一般来说,如果现实中你有这样一种需求“假如你是褥羊毛的羊毛党,你某日发现了一个app有一个活动,通过邮箱注册账号激活可以领5元红包,而恰恰你手上又有一批邮箱可用,那么批量获取邮箱中的激活链接去激活则是 ...

  10. 记一次elastic-job使用

    当当的elastic-job定时任务 业务场景是定时从微信取accesstoken和jsticket,因为都只有7200秒的有效时间,所以设置了定时任务,定时将得到的数据存到redis缓存中 问题1: ...