一、普通索引

示例

a = t.Tensor(4,5)
print(a)
print(a[0:1,:2])
print(a[0,:2]) # 注意和前一种索引出来的值相同,shape不同
print(a[[1,2]]) # 容器索引
 3.3845e+15  0.0000e+00  3.3846e+15  0.0000e+00  3.3845e+15
0.0000e+00 3.3845e+15 0.0000e+00 3.3418e+15 0.0000e+00
3.3845e+15 0.0000e+00 3.3846e+15 0.0000e+00 0.0000e+00
0.0000e+00 1.5035e+38 8.5479e-43 1.5134e-43 1.2612e-41
[torch.FloatTensor of size 4x5] 3.3845e+15 0.0000e+00
[torch.FloatTensor of size 1x2] 3.3845e+15
0.0000e+00
[torch.FloatTensor of size 2] 0.0000e+00 3.3845e+15 0.0000e+00 3.3418e+15 0.0000e+00
3.3845e+15 0.0000e+00 3.3846e+15 0.0000e+00 0.0000e+00
[torch.FloatTensor of size 2x5]

普通索引内存分析

普通索引后的结果和原Tensor的内存共享

print(a[a>1])
import copy
b = copy.deepcopy(a)
a[a>1]=10
print(a,b)
 3.3845e+15
3.3846e+15
3.3845e+15
3.3845e+15
3.3418e+15
3.3845e+15
3.3846e+15
1.5035e+38
[torch.FloatTensor of size 8] 10.0000 0.0000 10.0000 0.0000 10.0000
0.0000 10.0000 0.0000 10.0000 0.0000
10.0000 0.0000 10.0000 0.0000 0.0000
0.0000 10.0000 0.0000 0.0000 0.0000
[torch.FloatTensor of size 4x5] 3.3845e+15 0.0000e+00 3.3846e+15 0.0000e+00 3.3845e+15
0.0000e+00 3.3845e+15 0.0000e+00 3.3418e+15 0.0000e+00
3.3845e+15 0.0000e+00 3.3846e+15 0.0000e+00 0.0000e+00
0.0000e+00 1.5035e+38 8.5479e-43 1.5134e-43 1.2612e-41
[torch.FloatTensor of size 4x5]
array([[  1.00000000e+01,   0.00000000e+00,   1.00000000e+01,
0.00000000e+00, 1.00000000e+01],
[ 0.00000000e+00, 1.00000000e+01, 0.00000000e+00,
1.00000000e+01, 0.00000000e+00],
[ 1.00000000e+01, 0.00000000e+00, 1.00000000e+01,
0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+01, 8.54792063e-43,
1.51340234e-43, 1.26116862e-41]], dtype=float32)

索引函数gather介绍

方的介绍:
如果input是一个n维的tensor,size为
(x0,x1…,xi−1,xi,xi+1,…,xn−1),dim为i,然后index必须也为n维tensor,size为
(x0,x1,…,xi−1,y,xi+1,…,xn−1),其中y >= 1,最后输出的out与index的size是一样的。
意思就是按照一个指定的轴(维数)收集值
对于一个三维向量来说:

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2

参数:
input (Tensor) – 源tensor
dim (int) – 指定的轴数(维数)
index (LongTensor) – 需要聚集起来的数据的索引
out (Tensor, optional) – 目标tensor

简单来说,就是在Tensor(input)的众多维度中针对某一维度(dim参数),使用一维Tensor(index)进行索引,并对其他维度进行遍历。

a = t.arange(16).view(4,4)
index = t.LongTensor([[0,1,2,3]])
print(a)
print(index)
print(a.gather(0,index)) # 逆操作scatter_,注意是inplace的
b = t.zeros(4,4)
b.scatter_(0,index,a.gather(0,index))
print(b)
  0   1   2   3
4 5 6 7
8 9 10 11
12 13 14 15
[torch.FloatTensor of size 4x4] 0 1 2 3
[torch.LongTensor of size 1x4] 0 5 10 15
[torch.FloatTensor of size 1x4] 0 0 0 0
0 5 0 0
0 0 10 0
0 0 0 15
[torch.FloatTensor of size 4x4]

二、高阶索引

和普通索引不同,高阶索引前后一般不会共享内存,后面介绍Tensor内存结构时会提到。

x = t.arange(0,27).view(3,3,3)
print(x)
print(x[[1,2],[1,2],[2,0]]) # x[1,1,2]和x[2,2,0]
print(x[[2,1,0],[0],[0]]) # x[2,0,0]和x[1,0,0]和x[0,0,0]
(0 ,.,.) =
0 1 2
3 4 5
6 7 8 (1 ,.,.) =
9 10 11
12 13 14
15 16 17 (2 ,.,.) =
18 19 20
21 22 23
24 25 26
[torch.FloatTensor of size 3x3x3] 14
24
[torch.FloatTensor of size 2] 18
9
0
[torch.FloatTensor of size 3]

『PyTorch』第五弹_深入理解Tensor对象_中上:索引的更多相关文章

  1. 『PyTorch』第五弹_深入理解autograd_上:Variable属性方法

    在PyTorch中计算图的特点可总结如下: autograd根据用户对variable的操作构建其计算图.对变量的操作抽象为Function. 对于那些不是任何函数(Function)的输出,由用户创 ...

  2. 『PyTorch』第五弹_深入理解autograd_下:函数扩展&高阶导数

    一.封装新的PyTorch函数 继承Function类 forward:输入Variable->中间计算Tensor->输出Variable backward:均使用Variable 线性 ...

  3. 『PyTorch』第五弹_深入理解autograd_中:Variable梯度探究

    查看非叶节点梯度的两种方法 在反向传播过程中非叶子节点的导数计算完之后即被清空.若想查看这些变量的梯度,有两种方法: 使用autograd.grad函数 使用hook autograd.grad和ho ...

  4. 『PyTorch』第五弹_深入理解Tensor对象_中下:数学计算以及numpy比较_&_广播原理简介

    一.简单数学操作 1.逐元素操作 t.clamp(a,min=2,max=4)近似于tf.clip_by_value(A, min, max),修剪值域. a = t.arange(0,6).view ...

  5. 『PyTorch』第五弹_深入理解Tensor对象_下:从内存看Tensor

    Tensor存储结构如下, 如图所示,实际上很可能多个信息区对应于同一个存储区,也就是上一节我们说到的,初始化或者普通索引时经常会有这种情况. 一.几种共享内存的情况 view a = t.arang ...

  6. 『PyTorch』第五弹_深入理解Tensor对象_上:初始化以及尺寸调整

    一.创建Tensor 特殊方法: t.arange(1,6,2)t.linspace(1,10,3)t.randn(2,3) # 标准分布,*size t.randperm(5) # 随机排序,从0到 ...

  7. 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下

    『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上 # Author : Hellcat # Time : 2018/2/11 import torch as t import t ...

  8. 『PyTorch』第三弹重置_Variable对象

    『PyTorch』第三弹_自动求导 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Varibale包含三个属性: data ...

  9. 『PyTorch』第十弹_循环神经网络

    RNN基础: 『cs231n』作业3问题1选讲_通过代码理解RNN&图像标注训练 TensorFlow RNN: 『TensotFlow』基础RNN网络分类问题 『TensotFlow』基础R ...

随机推荐

  1. Linux服务器配置---tftpserver

    安装tftp-server 1.安装tftp-server [root@localhost weijie]# yum install -y tftp-server Loaded plugins: fa ...

  2. SVN版本服务器搭建

    windows:        https://blog.csdn.net/lu1024188315/article/details/74082227 SVN 的下载地址如下 http://torto ...

  3. 响应式瀑布流插件Grid-A-Licious

    Grid-A-Licious是一款遵守MIT协议的响应式瀑布流插件.该插件总代码行不超过400行,实现很巧妙,使用时也很流畅.实现原理也很简单,根据屏幕宽度和参数中设置的列宽度以及每项之间的间隔宽度, ...

  4. MySQL之表连接(内外连接和重命名的使用)

    #要多练练 1.连接查询根据连接方式分为 内连接 等值连接 非等值连接 自连接 外连接 左外连接(左连接) 右外连接(右连接) 当多张表进行连接查询,若没有任何条件进行限制,会 发生什么现象? 会出现 ...

  5. 了解微信小程序

    了解微信小程序 版权声明:未经博主授权,内容严禁转载分享! 微信小程序官方网址:https://mp.weixin.qq.com/cgi-bin/wx 某大神知乎专栏地址:七月在夏天 https:// ...

  6. 《将博客搬至51CTO》

    想把你的博客搬家到51CTO吗?想拥有一键式搬家的体验吗? 就算家大业大不好搬也没关系,我们帮你! 51CTO推出专业的搬家工具,用最短的时间.最快的速度,为你在这儿搭建一个温馨的家. 在这儿,你可以 ...

  7. jquery插件--问题类(新增&&删除)简易版

    HTML: <!doctype html> <head> <meta charset="utf-8" /> <script src=&qu ...

  8. linux下通过命令行上传文件到百度网盘

    一.环境: centos release 6.9 python 2.7.13 二.安装工具bypy sudo pip install bypy 三.使用bypy 3.1 授权 [root@ineedl ...

  9. 【ssh免登录】设置集群环境ssh免登录步骤

    1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...

  10. 51NOD 1087 1 10 100 1000

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1087 暴力大法 #include<bits/stdc++.h> ...