在PyTorch中Tensor的查找和筛选例子


Posted in Python onAugust 18, 2019

本文源码基于版本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的查找和筛选例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python根据出生日期返回年龄的方法
Mar 26 Python
在Python的gevent框架下执行异步的Solr查询的教程
Apr 16 Python
Python实现读取及写入csv文件的方法示例
Jan 12 Python
python计算日期之间的放假日期
Jun 05 Python
对python程序内存泄漏调试的记录
Jun 11 Python
python 基本数据类型占用内存空间大小的实例
Jun 12 Python
在Python中实现shuffle给列表洗牌
Nov 08 Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 Python
Django 配置多站点多域名的实现步骤
May 17 Python
关于numpy.where()函数 返回值的解释
Dec 06 Python
Python 实现Image和Ndarray互相转换
Feb 19 Python
Python如何把不同类型数据的json序列化
Apr 30 Python
对Pytorch神经网络初始化kaiming分布详解
Aug 18 #Python
pytorch中的embedding词向量的使用方法
Aug 18 #Python
Pytorch加载部分预训练模型的参数实例
Aug 18 #Python
在pytorch中查看可训练参数的例子
Aug 18 #Python
浅析PyTorch中nn.Module的使用
Aug 18 #Python
关于PyTorch 自动求导机制详解
Aug 18 #Python
pytorch神经网络之卷积层与全连接层参数的设置方法
Aug 18 #Python
You might like
php实现的数组转xml案例分析
2019/09/28 PHP
jquery中的 $(&quot;#jb51&quot;)与document.getElementById(&quot;jb51&quot;) 的区别
2011/07/26 Javascript
javaScript 动态访问JSon元素示例代码
2013/08/30 Javascript
js中一个函数获取另一个函数返回值问题探讨
2013/11/21 Javascript
js实现可得到不同颜色值的颜色选择器实例
2015/02/28 Javascript
jquery插件splitScren实现页面分屏切换模板特效
2015/06/16 Javascript
浅谈javascript中return语句
2015/07/15 Javascript
js游戏人物上下左右跑步效果代码分享
2015/08/28 Javascript
使用Function.apply()的参数数组化来提高 JavaScript程序性能的技巧
2015/12/23 Javascript
浅谈javascript中的call、apply、bind
2016/03/06 Javascript
基于vuejs+webpack的日期选择插件
2020/05/21 Javascript
微信小程序 MD5加密登录密码详解及实例代码
2017/01/12 Javascript
javascript将list转换成树状结构的实例
2017/09/08 Javascript
BootstrapTable加载按钮功能实例代码详解
2017/09/22 Javascript
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
微信小程序获取地理位置及经纬度授权代码实例
2019/09/18 Javascript
Vue两个版本的区别和使用方法(更深层次了解)
2020/02/16 Javascript
Vue的全局过滤器和私有过滤器的实现
2020/04/20 Javascript
js+canvas绘制图形验证码
2020/09/21 Javascript
玩转python爬虫之正则表达式
2016/02/17 Python
使用Puppeteer爬取微信文章的实现
2020/02/11 Python
使用python-Jenkins批量创建及修改jobs操作
2020/05/12 Python
浅谈Python 命令行参数argparse写入图片路径操作
2020/07/12 Python
python与idea的集成的实现
2020/11/20 Python
python os.listdir()乱码解决方案
2021/01/31 Python
what is the difference between ext2 and ext3
2013/11/03 面试题
校领导推荐信
2013/11/01 职场文书
远程教育心得体会
2014/01/03 职场文书
中年人生感言
2014/02/04 职场文书
工商治理实习生的自我评价分享
2014/02/20 职场文书
仓库主管岗位职责
2014/03/02 职场文书
禁止高声喧哗的标语
2014/06/11 职场文书
公务员年度个人总结
2015/02/12 职场文书
企业培训简报范文
2015/07/20 职场文书
加强党性修养心得体会
2016/01/21 职场文书
python批量更改目录名/文件名的方法
2021/04/18 Python