torch.index_select

index_select 只能处理两维矩阵,指定行或者列的索引,按行或者按列取出
# indices 只能是一维
x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [2, 2, 3, 4]])
print(x)
indices = torch.tensor([0, 2])
print(torch.index_select(x, 0, indices))
# 按行取
print(torch.index_select(x, 1, indices))
# 按列取
tensor([[1, 2, 3, 4],[5, 6, 7, 8],[2, 2, 3, 4]])
# 按行取
tensor([[1, 2, 3, 4],[2, 2, 3, 4]])
# 按列取
tensor([[1, 3],[5, 7],[2, 3]])

torch.gather

gather可处理多维张量, 按具体的索引取出对应位置的数(不是整行或者整列取)

x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [2, 2, 3, 4]])
print(x)
# 3*4indices = torch.tensor([[1], [2], [3]])
# 3*1
# 意思是取[0,1],[2,2],[3,3]索引位置的数
# 每行取一个,indices指定列标
print(torch.gather(x, 1, indices))
# 按照第二维度取数,indices第一个维度要和x一样indices = torch.tensor([[1, 2, 1, 0]])
# 1*4
# 意思是取[1,0],[2,1],[1,2],[0,3]索引位置的数
# 每列取一个,indices指定行标
print(torch.gather(x, 0, indices))
# 按照第一维度取数,indices第二个维度要和x一样
tensor([[1, 2, 3, 4],[5, 6, 7, 8],[2, 2, 3, 4]])
# 每行取一个,指定列标
tensor([[2],[7],[4]])
# 每列取一个,指定行标
tensor([[5, 2, 7, 4]])

torch.index_select与torch.gather相关推荐

  1. PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)

    1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...

  2. torch.Tensor和torch.tensor的区别

    torch.Tensor和torch.tensor的区别 2019-06-10 16:34:48 Vic_Hao 阅读数 4058更多 分类专栏: Pytorch 在Pytorch中,Tensor和t ...

  3. pytorch torch.Tensor.new_ones()(返回一个与size大小相同的用1填充的张量。 默认返回的Tensor具有与此张量相同的torch.dtype和torch.device)

    from https://pytorch.org/docs/1.1.0/tensors.html?highlight=new_ones#torch.Tensor.new_ones new_ones(s ...

  4. pytorch拼接函数:torch.stack()和torch.cat()--详解及例子

    原文链接: https://blog.csdn.net/xinjieyuan/article/details/105205326 https://blog.csdn.net/xinjieyuan/ar ...

  5. torch.unsqueeze()和torch.unsqueeze()

    参考:torch.squeeze() 和torch.unsqueeze()用法的通俗解释 import torch x = torch.tensor([[1, 2, 3],[1, 2, 3],[1, ...

  6. Pytorch中的 torch.as_tensor() 和 torch.from_numpy() 的区别

    之前我写过一篇文章,比较了 torch.Tensor() 和 torch.tensor() 的区别,而这两者都是深拷贝的方法,返回张量的同时,会在内存中创建一个额外的数据副本,与原数据不共享内存,所以 ...

  7. Pytorch中的 torch.Tensor() 和 torch.tensor() 的区别

    直接在搜索引擎里进行搜索,可以看到官方文档中两者对应的页面: 分别点击进去,第一个链接解释了什么是 torch.Tensor: torch.Tensor 是一个包含单一数据类型元素的多维矩阵(数组). ...

  8. 【pytorch】torch.range() 和 torch.arange() ==>以step为间隔输出从start到end的张量列表

     Parameters start (Number) – the starting value for the set of points. Default: 0. end (Number) – th ...

  9. torch.bmm() 与 torch.matmul()==>张量的相乘运算

    torch.bmm()强制规定维度和大小相同 torch.matmul()没有强制规定维度和大小,可以用利用广播机制进行不同维度的相乘操作 当进行操作的两个tensor都是3D时,两者等同. torc ...

最新文章

  1. python自动化功能测试_python、selenium自动化功能测试
  2. selenium 文件上传
  3. java servlet接口开发_servlet三种实现方式之一实现servlet接口
  4. python爬虫实例手机_python爬虫实例详解
  5. Python基础知识(1)简介
  6. 卖一头猪亏500元,“猪茅”也扛不住了?
  7. 特斯拉全球超级充电站已超过25000座 国内超过870座
  8. nodejs学习笔记-入门helloworld
  9. 使用Logisim软件实现一位全加器,四位串行加法器,四位并行加法器。
  10. [转载]JXTA技术与应用发展
  11. 信息学奥赛一本通答案大全
  12. 微信小程序js数组倒序reverse
  13. C++:[编程题]买帽子
  14. java-极光短信以及通知回调整合
  15. 【C语言练习】求名次、找凶手
  16. Linux无法终止进程,如何在Linux中终止进程
  17. WAMP和PHPStorm安装(Win10)
  18. 一文解析霍尔效应传感器
  19. 消防车从红色变为黄绿色 最初原因竟是这
  20. mysql找不到my.ini文件

热门文章

  1. 华为机试HJ72:百钱买百鸡问题
  2. 华为机试HJ3:明明的随机数
  3. js判断数组中重复元素并找出_javascript查找数组中重复元素的方法
  4. php.ini添加的变量读取,PHP5 在扩展里使用 INI 指令(直接添加和配合全局变量两种方式)...
  5. 通信原理眼图画法_光纤通信链路入侵与检测技术研究综述
  6. kylin版本_kylin多维数据分析(一)单机部署
  7. excel loc() python_python pandas df.loc[]的典型用法
  8. 月份对比_行业洞察 | 10月份行业概览amp;头部广告主盘点
  9. python turtle_Python:turtle画粽子
  10. c++重载、覆盖及隐藏规则