本文源码基于版本1.0,交互界面基于0.4.1

import torch

按照指定轴上的坐标进行过滤

index_select()

沿着某tensor的一个轴dim筛选若干个坐标

>>> x = torch.randn(3, 4) # 目标矩阵

>>> x

tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],

[-0.4664, 0.2647, -0.1228, -1.1068],

[-1.1734, -0.6571, 0.7230, -0.6004]])

>>> indices = torch.tensor([0, 2]) # 在轴上筛选坐标

>>> torch.index_select(x, dim=0, indices) # 指定筛选对象、轴、筛选坐标

tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],

[-1.1734, -0.6571, 0.7230, -0.6004]])

>>> torch.index_select(x, dim=1, indices)

tensor([[ 0.1427, -0.5414],

[-0.4664, -0.1228],

[-1.1734, 0.7230]])

where()

用于将两个broadcastable的tensor组合成新的tensor,类似于c++中的三元操作符“?:”

>>> x = torch.randn(3, 2)

>>> y = torch.ones(3, 2)

>>> torch.where(x > 0, x, y)

tensor([[1.4013, 1.0000],

[1.0000, 0.9267],

[1.0000, 0.4302]])

>>> x

tensor([[ 1.4013, -0.9960],

[-0.3715, 0.9267],

[-0.7163, 0.4302]])

指定条件返回01-tensor

>>> x = torch.arange(5)

>>> x

tensor([0, 1, 2, 3, 4])

>>> torch.gt(x,1) # 大于

tensor([0, 0, 1, 1, 1], dtype=torch.uint8)

>>> x>1 # 大于

tensor([0, 0, 1, 1, 1], dtype=torch.uint8)

>>> torch.ne(x,1) # 不等于

tensor([1, 0, 1, 1, 1], dtype=torch.uint8)

>>> x!=1 # 不等于

tensor([1, 0, 1, 1, 1], dtype=torch.uint8)

>>> torch.lt(x,3) # 小于

tensor([1, 1, 1, 0, 0], dtype=torch.uint8)

>>> x<3 # 小于

tensor([1, 1, 1, 0, 0], dtype=torch.uint8)

>>> torch.eq(x,3) # 等于

tensor([0, 0, 0, 1, 0], dtype=torch.uint8)

>>> x==3 # 等于

tensor([0, 0, 0, 1, 0], dtype=torch.uint8)

返回索引

>>> x = torch.arange(5)

>>> x # 1维

tensor([0, 1, 2, 3, 4])

>>> torch.nonzero(x)

tensor([[1],

[2],

[3],

[4]])

>>> x = torch.Tensor([[0.6, 0.0, 0.0, 0.0],[0.0, 0.4, 0.0, 0.0],[0.0, 0.0, 1.2, 0.0],[0.0, 0.0, 0.0,-0.4]])

>>> x # 2维

tensor([[ 0.6000, 0.0000, 0.0000, 0.0000],

[ 0.0000, 0.4000, 0.0000, 0.0000],

[ 0.0000, 0.0000, 1.2000, 0.0000],

[ 0.0000, 0.0000, 0.0000, -0.4000]])

>>> torch.nonzero(x)

tensor([[0, 0],

[1, 1],

[2, 2],

[3, 3]])

借助nonzero()我们可以返回符合某一条件的index(https://stackoverflow.com/questions/47863001/how-pytorch-tensor-get-the-index-of-specific-value)

>>> x=torch.arange(12).view(3,4)

>>> x

tensor([[ 0, 1, 2, 3],

[ 4, 5, 6, 7],

[ 8, 9, 10, 11]])

>>> (x>4).nonzero()

tensor([[1, 1],

[1, 2],

[1, 3],

[2, 0],

[2, 1],

[2, 2],

[2, 3]])

以上这篇在PyTorch中Tensor的查找和筛选例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

本文标题: 在PyTorch中Tensor的查找和筛选例子

本文地址: http://www.cppcns.com/jiaoben/python/268885.html

pytorch tensor查找0_在PyTorch中Tensor的查找和筛选例子相关推荐

  1. 编写类A2, 定义方法find, 实现查找某字符串数组中的元素查找,并返回索引,如果找不到,返回-1

    编写类A2, 定义方法find, 实现查找某字符串数组中的元素查找,并返回索引,如果找不到,返回-1 思路: 方法的返回值类型: int 方法名 : find 方法的形参 (String , Stri ...

  2. 【Groovy】集合遍历 ( 使用集合的 find 方法查找集合元素 | 闭包中使用 == 作为查找匹配条件 | 闭包中使用 is 作为查找匹配条件 | 闭包使用 true 作为条件 | 代码示例 )

    文章目录 一.使用集合的 find 方法查找集合元素 1.闭包中使用 == 作为查找匹配条件 2.闭包中使用 is 作为查找匹配条件 3.闭包中使用 true 作为查找匹配条件 二.完整代码示例 一. ...

  3. Java版二叉树的前序遍历查找、中序遍历查找和后序遍历查找

    文章收藏的好句子:任何挫折,如果无法彻底击败你,那一定会使你更强. 目录 1.二叉树的节点查找 1.1 前序遍历查找 1.2 中序遍历查找 1.3 后序遍历查找 1.二叉树的节点查找  1.1 前序遍 ...

  4. pyTorch中tensor运算

    文章目录 PyTorch的简介 PyTorch中主要的包 PyTorch的安装 使用GPU的原因 使数据在GPU上运行 什么使Tensor(张量) 一些术语介绍 Tensor的属性介绍(Rank,ax ...

  5. PyTorch中tensor介绍

          PyTorch中的张量(Tensor)如同数组和矩阵一样,是一种特殊的数据结构.在PyTorch中,神经网络的输入.输出以及网络的参数等数据,都是使用张量来进行描述.       torc ...

  6. Pytorch中tensor.view().permute().contiguous()函数理解

    Pytorch中tensor.view().permute().contiguous()函数理解 yolov3中有一行这样的代码,在此记录一下三个函数的含义 # 例子中batch_size为整型,le ...

  7. Pytorch中tensor.expand()和tensor.expand_as()函数

    Pytorch中tensor.expand函数 Tensor.expand()函数详解 Tensor.expand_as()函数 Tensor.expand()函数详解 函数语法: # 官方解释: D ...

  8. Pytorch中tensor维度和torch.max()函数中dim参数的理解

    Pytorch中tensor维度和torch.max()函数中dim参数的理解 维度 参考了 https://blog.csdn.net/qq_41375609/article/details/106 ...

  9. pytorch中tensor、backward一些总结

    目录 说明 Tensor Tensor的创建 Tensor(张量)基本数据类型与常用属性 Tensor的自动微分 设置不可积分计算 pytorch 计算图 backward一些细节 该文章解决问题如下 ...

最新文章

  1. linux 修改hba参数,更改Raid卡和HBA卡在linux下的启动顺序
  2. 【HDFS】hdfs与fsck结合使用
  3. 2015年最好的员工心态培养 -- 我们需要把简单的事情做到极致
  4. IDEA集成MAVEN 报错
  5. cadence SPB17.4 - 保存和恢复颜色配置
  6. Solidworks CAM入门教程,简单生成雕刻机刀路,经验分享
  7. stata输出相关系数表到word
  8. SQL查询前几条数据的方法
  9. PHP在线运行,在线编译
  10. excel2013 最大行数列数
  11. 【Idea】Process finished with exit code 0 是什么意思?
  12. Win11聚焦锁屏壁纸不更新了?Win11锁屏聚焦不更换解决教程
  13. 华山全敏还是全劲_一梦江湖华山加点_一梦江湖华山加点推荐2020_攻略
  14. Node.js stream模块(三)背压机制
  15. 山东哈斯精密机械有限公司
  16. 家族关系查询系统程序设计算法思路_数据结构课程设计家族关系查询系统要点...
  17. django框架基于Python实现的作业查重系统
  18. 一文入门Go静态单赋值(SSA)
  19. IEEE SPL文章接收后通讯作者注册IEEE账户并验证文章
  20. 回文字符串与动态规划

热门文章

  1. Android开发小问题集
  2. c语言sleep_编程代码:用C语言来实现下雪效果,这个冬天,雪花很美
  3. 以短带长进军网综,西瓜视频能否干过“优爱腾”?
  4. ASP.NET Core 2 学习笔记(三)中间件
  5. 码教授告诉你面试不要骄傲自负,也不妄自菲薄
  6. 在CentOS6.8下安装Docker
  7. Python中的join()函数的用法
  8. php wordpress 开源,PHP 遭弃用!WordPress.com 开源并转用 Javascript
  9. 64位Win10安装Pytorch
  10. (转载)机器学习知识点(十一)隐马尔可夫模型