1. #Tensor索引操作
  2. '''''
  3. Tensor支持与numpy.ndarray类似的索引操作,语法上也类似
  4. 如无特殊说明,索引出来的结果与原tensor共享内存,即修改一个,另一个会跟着修改
  5. '''
  6. import torch as t
  7. a = t.randn(3,4)
  8. '''''tensor([[ 0.1986,  0.1809,  1.4662,  0.6693],
  9. [-0.8837, -0.0196, -1.0380,  0.2927],
  10. [-1.1032, -0.2637, -1.4972,  1.8135]])'''
  11. print(a[0])         #第0行
  12. '''''tensor([0.1986, 0.1809, 1.4662, 0.6693])'''
  13. print(a[:,0])       #第0列
  14. '''''tensor([ 0.1986, -0.8837, -1.1032])'''
  15. print(a[0][2])      #第0行第2个元素,等价于a[0,2]
  16. '''''tensor(1.4662)'''
  17. print(a[0][-1])     #第0行最后一个元素
  18. '''''tensor(0.6693)'''
  19. print(a[:2,0:2])    #前两行,第0,1列
  20. '''''tensor([[ 0.1986,  0.1809],
  21. [-0.8837, -0.0196]])'''
  22. print(a[0:1,:2])    #第0行,前两列
  23. '''''tensor([[0.1986, 0.1809]])'''
  24. print(a[0,:2])      #注意两者的区别,形状不同
  25. '''''tensor([0.1986, 0.1809])'''
  26. print(a>1)
  27. '''''tensor([[0, 0, 1, 0],
  28. [0, 0, 0, 0],
  29. [0, 0, 0, 1]], dtype=torch.uint8)'''
  30. print(a[a>1])        #等价于a.masked_select(a>1),选择结果与原tensor不共享内存空间
  31. print(a.masked_select(a>1))
  32. '''''tensor([1.4662, 1.8135])
  33. tensor([1.4662, 1.8135])'''
  34. print(a[t.LongTensor([0,1])])
  35. '''''tensor([[ 0.1986,  0.1809,  1.4662,  0.6693],
  36. [-0.8837, -0.0196, -1.0380,  0.2927]])'''
  37. '''''
  38. 常用的选择函数
  39. index_select(input,dim,index)   在指定维度dim上选取,列如选择某些列、某些行
  40. masked_select(input,mask)       例子如上,a[a>0],使用ByteTensor进行选取
  41. non_zero(input)                 非0元素的下标
  42. gather(input,dim,index)         根据index,在dim维度上选取数据,输出size与index一样
  43. gather是一个比较复杂的操作,对一个二维tensor,输出的每个元素如下:
  44. out[i][j] = input[index[i][j]][j]   #dim = 0
  45. out[i][j] = input[i][index[i][j]]   #dim = 1
  46. '''
  47. b = t.arange(0,16).view(4,4)
  48. '''''tensor([[ 0,  1,  2,  3],
  49. [ 4,  5,  6,  7],
  50. [ 8,  9, 10, 11],
  51. [12, 13, 14, 15]])'''
  52. index = t.LongTensor([[0,1,2,3]])
  53. print(b.gather(0,index))            #取对角线元素
  54. '''''tensor([[ 0,  5, 10, 15]])'''
  55. index = t.LongTensor([[3,2,1,0]]).t()       #取反对角线上的元素
  56. print(b.gather(1,index))
  57. '''''tensor([[ 3],
  58. [ 6],
  59. [ 9],
  60. [12]])'''
  61. index = t.LongTensor([[3,2,1,0]])           #取反对角线的元素,与上面不同
  62. print(b.gather(0,index))
  63. '''''tensor([[12,  9,  6,  3]])'''
  64. index = t.LongTensor([[0,1,2,3],[3,2,1,0]]).t()
  65. print(b.gather(1,index))
  66. '''''tensor([[ 0,  3],
  67. [ 5,  6],
  68. [10,  9],
  69. [15, 12]])'''
  70. '''''
  71. 与gather相对应的逆操作是scatter_,gather把数据从input中按index取出,而
  72. scatter_是把取出的数据再放回去,scatter_函数时inplace操作
  73. out = input.gather(dim,index)
  74. out = Tensor()
  75. out.scatter_(dim,index)
  76. '''
  77. x = t.rand(2, 5)
  78. print(x)
  79. c = t.zeros(3, 5).scatter_(0, t.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
  80. print(c)
  81. 2018-10-23 20:30:30

Tensor索引操作的更多相关文章

  1. Pytorch Tensor 常用操作

    https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...

  2. pytorch(03)tensor的操作

    张量操作 一.张量的拼接 torch.cat() 功能:将张量按维度dim进行拼接,且[不会扩张张量的维度] tensors:张量序列 dim:要拼接的维度 torch.cat(tensors, di ...

  3. Mongodb学习笔记三(Mongodb索引操作及性能测试)

    第三章 索引操作及性能测试 索引在大数据下的重要性就不多说了 下面测试中用到了mongodb的一个客户端工具Robomongo,大家可以在网上选择下载.官网下载地址:http://www.robomo ...

  4. Elasticsearch-PHP 索引操作(转)

    索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...

  5. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  6. Mysql之表的操作与索引操作

    表的操作: 1.表的创建: create table if not exists table_name(字段定义); 例子: create table if not exists user(id in ...

  7. 3.Lucene3.x API分析,Director 索引操作目录,Document,分词器

     1  Lucene卡发包结构分析 包名 功能 org.apache.lucene.analysis Analysis提供自带的各种Analyzer org.apache.lucene.colla ...

  8. SQL Server死锁诊断--同一行数据在不同索引操作下引起的死锁

    死锁概述 对于数据库中出现的死锁,通俗地解释就是:不同Session(会话)持有一部分资源,并且同时相互排他性地申请对方持有的资源,然后双方都得不到自己想要的资源,从而造成的一种僵持的现象.当然,在任 ...

  9. 获取列表的索引操作:enumerate

    通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...

随机推荐

  1. [Kubernetes]PV,PVC,StorageClass之间的关系详解

    在Kubernetes中,容器化一个应用比较麻烦的地方莫过于对其"状态"的管理,而最常见的"状态",莫过于存储状态. 在[Kubernetes]深入理解Stat ...

  2. 记一次较坑的eclipse安装AmaterasUML插件经历

    先是最开始以为只用下载AmaterasUML插件压缩包,解压之后把三个jar包放进eclipse-plugins目录之后重启就可以了,结果发现重启之后毫无反应orz 于是又去查资料,发现还需要安装GE ...

  3. 写一个python脚本监控在linux中的进程

    在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ...

  4. tp5.1入口文件隐藏

    修改.htaccess文件 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On Re ...

  5. 使用 Xtrabackup 部署 MySQL 从库

    数据库备份和恢复是数据库管理工作中非常重要的一个部分. 笔者常用的两个备份工具是mysqldump (逻辑备份)和 Xtrabackup (物理备份). 对于前者一般是用在凌晨的业务低峰期的定时任务中 ...

  6. 四 Memcache

    官网http://memcached.org 了解 cookies和session 安装memcached # yum install memcached  -y 安装nc和telnet 来管理mem ...

  7. cxf webservice请求https

    本地java请求https接口,不需要添加证书: 只需要修改配置文件applicationContext-soap-client.xml: <beans xmlns="http://w ...

  8. 微信H5支付开发(maven仓库版)

    官方文档:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1 开发之前确认开通了H5支付功能 一.安装微信sdk 二.创建config ...

  9. openv

    https://www.cnblogs.com/xishuai/p/centos-openvpn.html https://www.cnblogs.com/biaopei/category/11029 ...

  10. 【Linux】Ubuntu安装Mysql 8.0

    1.下载Mysql的安装配置,http://dev.mysql.com/downloads/repo/apt/ 2.执行配置配置文件 sudo dpkg -i mysql-apt-config_0.* ...