在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 相关文章推荐
有关wxpython pyqt内存占用问题分析
Jun 09 Python
Python结巴中文分词工具使用过程中遇到的问题及解决方法
Apr 15 Python
python psutil库安装教程
Mar 19 Python
用pandas按列合并两个文件的实例
Apr 12 Python
python向已存在的excel中新增表,不覆盖原数据的实例
May 02 Python
Python程序包的构建和发布过程示例详解
Jun 09 Python
Python实现平行坐标图的两种方法小结
Jul 04 Python
python实现邮件自动发送
Aug 10 Python
python实现的批量分析xml标签中各个类别个数功能示例
Dec 30 Python
Numpy实现卷积神经网络(CNN)的示例
Oct 09 Python
python 爬取百度文库并下载(免费文章限定)
Dec 04 Python
Python内置类型集合set和frozenset的使用详解
Apr 26 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+dbfile开发小型留言本
2006/10/09 PHP
打造计数器DIY三步曲(上)
2006/10/09 PHP
UTF-8正则表达式如何匹配汉字
2015/08/03 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
2010/01/12 Javascript
js word表格动态添加代码
2010/06/07 Javascript
Javascript在IE下设置innerHTML时出现未知的运行时错误的解决方法
2011/01/12 Javascript
使用jQuery实现更改默认alert框体
2015/04/13 Javascript
浅析JavaScript中的事件机制
2015/06/04 Javascript
JavaScript修改作用域外变量的方法
2016/03/25 Javascript
JS实现的图片预览插件与用法示例【不上传图片】
2016/11/25 Javascript
Javascript中的prototype与继承
2017/02/06 Javascript
MUI  Scroll插件的使用详解
2017/04/13 Javascript
详解如何构建Promise队列实现异步函数顺序执行
2018/10/23 Javascript
JavaScript学习笔记之基于定时器实现图片无缝滚动功能详解
2019/01/09 Javascript
vue-cli3跨域配置的简单方法
2019/09/06 Javascript
vue 使用鼠标滚动加载数据的例子
2019/10/31 Javascript
[46:44]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第二场 3月7日
2021/03/11 DOTA
浅谈python socket函数中,send与sendall的区别与使用方法
2017/05/09 Python
Python使用tkinter库实现文本显示用户输入功能示例
2018/05/30 Python
Python 经典面试题 21 道【不可错过】
2018/09/21 Python
Django框架的中的setting.py文件说明详解
2018/10/15 Python
pyqt5与matplotlib的完美结合实例
2019/06/21 Python
Python实现代码统计工具
2019/09/19 Python
PyCharm 在Windows的有用快捷键详解
2020/04/07 Python
sklearn和keras的数据切分与交叉验证的实例详解
2020/06/19 Python
HTML5 canvas实现移动端上传头像拖拽裁剪效果
2016/03/14 HTML / CSS
人力资源总监工作说明
2014/03/03 职场文书
社区两委对照检查材料
2014/08/23 职场文书
2014党员民主评议个人思想剖析发言
2014/09/19 职场文书
国庆节标语大全
2014/10/08 职场文书
党的群众路线教育实践活动实施方案
2014/10/31 职场文书
学校实习推荐信
2015/03/27 职场文书
golang http使用踩过的坑与填坑指南
2021/04/27 Golang
Android开发之WECHAT微信小程序路由跳转的两种形式
2022/04/12 Java/Android
JS实现九宫格拼图游戏
2022/06/28 Javascript