在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 20 Python
Python双精度浮点数运算并分行显示操作示例
Jul 21 Python
python编程测试电脑开启最大线程数实例代码
Feb 09 Python
python中利用numpy.array()实现俩个数值列表的对应相加方法
Aug 26 Python
Python实现语音识别和语音合成功能
Sep 20 Python
python requests抓取one推送文字和图片代码实例
Nov 04 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
Jan 20 Python
TensorFlow实现checkpoint文件转换为pb文件
Feb 10 Python
Python查找不限层级Json数据中某个key或者value的路径方式
Feb 27 Python
Python递归函数特点及原理解析
Mar 04 Python
python编写俄罗斯方块
Mar 13 Python
python3操作redis实现List列表实例
Aug 04 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 Document 代码注释规范
2009/04/13 PHP
基于Zend的Config机制的应用分析
2013/05/02 PHP
php实现图片缩略图的方法
2016/03/29 PHP
JavaScript使用prototype定义对象类型
2007/02/07 Javascript
javascript开发技术大全 第4章 直接量与字符集
2011/07/03 Javascript
jquery调用wcf并展示出数据的方法
2011/07/07 Javascript
js鼠标滑过弹出层的定位IE6bug解决办法
2012/12/26 Javascript
了不起的node.js读书笔记之例程分析
2014/12/22 Javascript
jquery实现两个图片渐变切换效果的方法
2015/06/25 Javascript
jquery实现表单验证简单实例演示
2015/11/23 Javascript
js编写贪吃蛇的小游戏
2020/08/24 Javascript
vue.js实现表格合并示例代码
2016/11/30 Javascript
vuejs通过filterBy、orderBy实现搜索筛选、降序排序数据
2020/10/26 Javascript
将jquery.qqFace.js表情转换成微信的字符码
2017/12/01 jQuery
微信小程序bindtap事件与冒泡阻止详解
2019/08/08 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
JavaScript设计模式--简单工厂模式实例分析【XHR工厂案例】
2020/05/23 Javascript
JavaScript 如何在浏览器中使用摄像头
2020/12/02 Javascript
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
Django返回json数据用法示例
2016/09/18 Python
django中send_mail功能实现详解
2018/02/06 Python
对Python中gensim库word2vec的使用详解
2018/05/08 Python
python设定并获取socket超时时间的方法
2019/01/12 Python
Python中函数参数匹配模型详解
2019/06/09 Python
Python 寻找局部最高点的实现
2019/12/05 Python
appium+python adb常用命令分享
2020/03/06 Python
PyQt5 控件字体样式等设置的实现
2020/05/13 Python
python 制作磁力搜索工具
2021/03/04 Python
Css3+Js制作漂亮时钟(附源码)
2013/04/24 HTML / CSS
HTML5自定义mp3播放器源码
2020/01/06 HTML / CSS
美体小铺印度官网:The Body Shop印度
2019/10/17 全球购物
德国拖鞋网站:German Slippers
2019/11/08 全球购物
文明班级建设方案
2014/05/15 职场文书
2015年党员创先争优承诺书
2015/01/22 职场文书
Python编写可视化界面的全过程(Python+PyCharm+PyQt)
2021/05/17 Python
分析Java中Map的遍历性能问题
2021/06/26 Java/Android