在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中捕捉详细异常信息的代码示例
Sep 18 Python
python实现udp数据报传输的方法
Sep 26 Python
Python实现的RSS阅读器实例
Jul 25 Python
Python cookbook(数据结构与算法)字典相关计算问题示例
Feb 18 Python
Python高级用法总结
May 26 Python
python实现统计文本中单词出现的频率详解
May 20 Python
pytorch 模型可视化的例子
Aug 17 Python
Python基于Tensor FLow的图像处理操作详解
Jan 15 Python
Tensorflow轻松实现XOR运算的方式
Feb 03 Python
在 Pycharm 安装使用black的方法详解
Apr 02 Python
PyCharm2020最新激活码+激活码补丁(亲测最新版PyCharm2020.2激活成功)
Nov 25 Python
python 写一个水果忍者游戏
Jan 13 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
用文本文件实现的动态实时发布新闻的程序
2006/10/09 PHP
PHP 的 __FILE__ 常量
2007/01/15 PHP
在Mac上编译安装PHP7的开发环境
2015/07/28 PHP
PHP获取指定时间段之间的 年,月,天,时,分,秒
2016/06/05 PHP
分享5个非常有用的Laravel Blade指令
2018/05/30 PHP
php原生数据库分页的代码实例
2019/02/18 PHP
Yii 使用intervention/image拓展实现图像处理功能
2019/06/22 PHP
Nigma vs Alliance BO5 第一场2.14
2021/03/10 DOTA
jQuery UI AutoComplete 自动完成使用小记
2010/08/21 Javascript
js获取IP地址的方法小结
2014/07/01 Javascript
jQuery实现多按钮单击变色
2014/11/27 Javascript
jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
2015/09/19 Javascript
基于jquery实现下拉框美化特效
2016/02/02 Javascript
基于JavaScript实现表单密码的隐藏和显示出来
2016/03/02 Javascript
Bootstrap精简教程中秋大放送
2016/09/15 Javascript
js定时器实例分享
2016/12/20 Javascript
JavaScript数据结构之双向链表和双向循环链表的实现
2017/11/28 Javascript
jquery如何实现点击空白处隐藏元素
2017/12/05 jQuery
Vue波纹按钮组件制作
2018/04/30 Javascript
Vue中的作用域CSS和CSS模块的区别
2018/10/09 Javascript
详解ES6系列之私有变量的实现
2018/11/21 Javascript
9102年webpack4搭建vue项目的方法步骤
2019/02/20 Javascript
python中wx将图标显示在右下角的脚本代码
2013/03/08 Python
python发送伪造的arp请求
2014/01/09 Python
解决Django中多条件查询的问题
2019/07/18 Python
自定义Django_rest_framework_jwt登陆错误返回的解决
2020/10/18 Python
Melijoe英国官网:法国儿童时尚网站
2016/11/18 全球购物
英国在线滑雪板和冲浪商店:The Board Basement
2020/01/11 全球购物
超市总经理岗位职责
2014/02/02 职场文书
监察建议书范文
2014/03/12 职场文书
服务标兵事迹材料
2014/05/04 职场文书
公务员个人总结
2015/02/12 职场文书
党支部工作总结2015
2015/04/01 职场文书
2015年村计划生育工作总结
2015/04/28 职场文书
千万级用户系统SQL调优实战分享
2022/03/03 MySQL
利用Python实时获取steam特惠游戏数据
2022/06/25 Python