Tensor索引操作
- #Tensor索引操作
- '''''
- Tensor支持与numpy.ndarray类似的索引操作,语法上也类似
- 如无特殊说明,索引出来的结果与原tensor共享内存,即修改一个,另一个会跟着修改
- '''
- import torch as t
- a = t.randn(3,4)
- '''''tensor([[ 0.1986, 0.1809, 1.4662, 0.6693],
- [-0.8837, -0.0196, -1.0380, 0.2927],
- [-1.1032, -0.2637, -1.4972, 1.8135]])'''
- print(a[0]) #第0行
- '''''tensor([0.1986, 0.1809, 1.4662, 0.6693])'''
- print(a[:,0]) #第0列
- '''''tensor([ 0.1986, -0.8837, -1.1032])'''
- print(a[0][2]) #第0行第2个元素,等价于a[0,2]
- '''''tensor(1.4662)'''
- print(a[0][-1]) #第0行最后一个元素
- '''''tensor(0.6693)'''
- print(a[:2,0:2]) #前两行,第0,1列
- '''''tensor([[ 0.1986, 0.1809],
- [-0.8837, -0.0196]])'''
- print(a[0:1,:2]) #第0行,前两列
- '''''tensor([[0.1986, 0.1809]])'''
- print(a[0,:2]) #注意两者的区别,形状不同
- '''''tensor([0.1986, 0.1809])'''
- print(a>1)
- '''''tensor([[0, 0, 1, 0],
- [0, 0, 0, 0],
- [0, 0, 0, 1]], dtype=torch.uint8)'''
- print(a[a>1]) #等价于a.masked_select(a>1),选择结果与原tensor不共享内存空间
- print(a.masked_select(a>1))
- '''''tensor([1.4662, 1.8135])
- tensor([1.4662, 1.8135])'''
- print(a[t.LongTensor([0,1])])
- '''''tensor([[ 0.1986, 0.1809, 1.4662, 0.6693],
- [-0.8837, -0.0196, -1.0380, 0.2927]])'''
- '''''
- 常用的选择函数
- index_select(input,dim,index) 在指定维度dim上选取,列如选择某些列、某些行
- masked_select(input,mask) 例子如上,a[a>0],使用ByteTensor进行选取
- non_zero(input) 非0元素的下标
- gather(input,dim,index) 根据index,在dim维度上选取数据,输出size与index一样
- gather是一个比较复杂的操作,对一个二维tensor,输出的每个元素如下:
- out[i][j] = input[index[i][j]][j] #dim = 0
- out[i][j] = input[i][index[i][j]] #dim = 1
- '''
- b = t.arange(0,16).view(4,4)
- '''''tensor([[ 0, 1, 2, 3],
- [ 4, 5, 6, 7],
- [ 8, 9, 10, 11],
- [12, 13, 14, 15]])'''
- index = t.LongTensor([[0,1,2,3]])
- print(b.gather(0,index)) #取对角线元素
- '''''tensor([[ 0, 5, 10, 15]])'''
- index = t.LongTensor([[3,2,1,0]]).t() #取反对角线上的元素
- print(b.gather(1,index))
- '''''tensor([[ 3],
- [ 6],
- [ 9],
- [12]])'''
- index = t.LongTensor([[3,2,1,0]]) #取反对角线的元素,与上面不同
- print(b.gather(0,index))
- '''''tensor([[12, 9, 6, 3]])'''
- index = t.LongTensor([[0,1,2,3],[3,2,1,0]]).t()
- print(b.gather(1,index))
- '''''tensor([[ 0, 3],
- [ 5, 6],
- [10, 9],
- [15, 12]])'''
- '''''
- 与gather相对应的逆操作是scatter_,gather把数据从input中按index取出,而
- scatter_是把取出的数据再放回去,scatter_函数时inplace操作
- out = input.gather(dim,index)
- out = Tensor()
- out.scatter_(dim,index)
- '''
- x = t.rand(2, 5)
- print(x)
- c = t.zeros(3, 5).scatter_(0, t.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
- print(c)
- 2018-10-23 20:30:30
Tensor索引操作的更多相关文章
- Pytorch Tensor 常用操作
https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...
- pytorch(03)tensor的操作
张量操作 一.张量的拼接 torch.cat() 功能:将张量按维度dim进行拼接,且[不会扩张张量的维度] tensors:张量序列 dim:要拼接的维度 torch.cat(tensors, di ...
- Mongodb学习笔记三(Mongodb索引操作及性能测试)
第三章 索引操作及性能测试 索引在大数据下的重要性就不多说了 下面测试中用到了mongodb的一个客户端工具Robomongo,大家可以在网上选择下载.官网下载地址:http://www.robomo ...
- Elasticsearch-PHP 索引操作(转)
索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...
- ElasticSearch+Kibana 索引操作
ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...
- Mysql之表的操作与索引操作
表的操作: 1.表的创建: create table if not exists table_name(字段定义); 例子: create table if not exists user(id in ...
- 3.Lucene3.x API分析,Director 索引操作目录,Document,分词器
1 Lucene卡发包结构分析 包名 功能 org.apache.lucene.analysis Analysis提供自带的各种Analyzer org.apache.lucene.colla ...
- SQL Server死锁诊断--同一行数据在不同索引操作下引起的死锁
死锁概述 对于数据库中出现的死锁,通俗地解释就是:不同Session(会话)持有一部分资源,并且同时相互排他性地申请对方持有的资源,然后双方都得不到自己想要的资源,从而造成的一种僵持的现象.当然,在任 ...
- 获取列表的索引操作:enumerate
通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...
随机推荐
- centos 7.2 安装mongodb 3.4.4免编译
/根目录下: 获取命令: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.4.tgz 解压命令: tar zvxf mon ...
- java程序设计习题总结
---恢复内容开始--- main()方法的参数名可以改变:main()方法的参数个数不可以改变. 当一个程序没有main()方法是,是可以编译通过的,但是不能给运行,因为找不到一个主函数入口. 标识 ...
- 2018-2019-2 20165325 《网络对抗技术》 Exp6 信息搜集与漏洞扫描
2018-2019-2 20165325 <网络对抗技术> Exp6 信息搜集与漏洞扫描 实验内容(概要) 1 各种搜索技巧的应用: 2 DNS IP注册信息的查询: 3 基本的扫描技术 ...
- 20175204 张湲祯 2018-2019-2《Java程序设计》第九周学习总结
20175204 张湲祯 2018-2019-2<Java程序设计>第九周学习总结 教材学习内容总结 -第十一章JDBC和MySQL数据库要点: 1.下载MySQL和客户端管理工具navi ...
- CVE-2018-19386:SolarWinds数据库性能分析器中反射的XSS
漏洞 在SolarWinds的11.1.457版中,"idcStateError.iwc"错误页面中存在Reflected Cross-Site Scripting漏洞,已经在版本 ...
- 014_IP专项研究监控
一.数据demo cat /proc/net/snmp Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagr ...
- D - WE POJ - 3273 (二分法)
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...
- 对oracle用户创建asm磁盘
--root用户执行vi /etc/sysctl.conf #Install oracle settingfs.aio-max-nr = 1048576fs.file-max = 6815744#ke ...
- 高可用Redis(十三):Redis缓存的使用和设计
1.缓存的受益和成本 1.1 受益 1.可以加速读写:Redis是基于内存的数据源,通过缓存加速数据读取速度 2.降低后端负载:后端服务器通过前端缓存降低负载,业务端使用Redis降低后端数据源的负载 ...
- 基于Vue2.x的小米商城移动端项目
初学vue已经有一段时间,为了检验自己的学习成果,决定做一个项目作为一个阶段性总结,项目花了差不多半个月时间,目前实现了7个页面,商城的主要功能基本实现,代码已经放到github上面. 这个项目把大部 ...