在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的setuptools框架下生成egg的教程
Apr 13 Python
python函数装饰器用法实例详解
Jun 04 Python
使用Python操作excel文件的实例代码
Oct 15 Python
Numpy掩码式数组详解
Apr 17 Python
python一行sql太长折成多行并且有多个参数的方法
Jul 19 Python
python 3.6.2 安装配置方法图文教程
Sep 18 Python
python打造爬虫代理池过程解析
Aug 15 Python
python3实现网页版raspberry pi(树莓派)小车控制
Feb 12 Python
python实现爱奇艺登陆密码RSA加密的方法示例详解
May 27 Python
Elasticsearch py客户端库安装及使用方法解析
Sep 14 Python
Python matplotlib多个子图绘制整合
Apr 13 Python
Python  lambda匿名函数和三元运算符
Apr 19 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性能优化的介绍
2013/06/20 PHP
ThinkPHP中html:list标签用法分析
2016/01/09 PHP
PHP常用函数之base64图片上传功能详解
2019/10/21 PHP
JavaScript高级程序设计(第3版)学习笔记8 js函数(中)
2012/10/11 Javascript
js 火狐下取本地路径实现思路
2013/04/02 Javascript
JS随机生成不重复数据的实例方法
2013/07/17 Javascript
IE中的File域无法清空使用jQuery重设File域
2014/04/24 Javascript
JS实现倒计时和文字滚动的效果实例
2014/10/29 Javascript
javascript中select下拉框的用法总结
2016/01/07 Javascript
BootStrap中按钮点击后被禁用按钮的最佳实现方法
2016/09/23 Javascript
postman+json+springmvc测试批量添加实例
2018/03/31 Javascript
node前端开发模板引擎Jade的入门
2018/05/11 Javascript
浅谈webpack SplitChunksPlugin实用指南
2018/09/17 Javascript
javascript实现支付宝滑块验证码效果
2020/07/24 Javascript
[51:44]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第二场
2018/04/04 DOTA
[02:49]2018DOTA2亚洲邀请赛主赛事决赛日战况回顾 Mineski鏖战5局夺得辉耀
2018/04/10 DOTA
[03:39]这就是刀塔,我们是冠军!燃情短片讲述我们的DOTA故事
2019/07/02 DOTA
Python的词法分析与语法分析
2013/05/18 Python
python实现查询苹果手机维修进度
2015/03/16 Python
在Django的视图中使用数据库查询的方法
2015/07/16 Python
Python金融数据可视化汇总
2017/11/17 Python
解决python 输出是省略号的问题
2018/04/19 Python
在Python中关于使用os模块遍历目录的实现方法
2019/01/03 Python
python 杀死自身进程的实现方法
2019/07/01 Python
python笔记_将循环内容在一行输出的方法
2019/08/08 Python
django框架ModelForm组件用法详解
2019/12/11 Python
使用Pyhton 分析酒店针孔摄像头
2020/03/04 Python
Python 实现使用空值进行赋值 None
2020/03/12 Python
4行Python代码生成图像验证码(2种)
2020/04/07 Python
2021年的Python 时间轴和即将推出的功能详解
2020/07/27 Python
HTML5 贪吃蛇游戏实现思路及源代码
2013/09/03 HTML / CSS
生物科学系大学生的自我评价
2013/12/20 职场文书
个人考核材料
2014/05/15 职场文书
技术负责人任命书
2014/06/05 职场文书
反四风个人对照检查材料
2014/09/26 职场文书
MySQL 5.7常见数据类型
2021/07/15 MySQL