在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使用Flask-SQLAlchemy连接数据库操作示例
Aug 31 Python
python中正则表达式 re.findall 用法
Oct 23 Python
python pandas消除空值和空格以及 Nan数据替换方法
Oct 30 Python
基于python实现的百度新歌榜、热歌榜下载器(附代码)
Aug 05 Python
使用PyTorch训练一个图像分类器实例
Jan 08 Python
Django调用百度AI接口实现人脸注册登录代码实例
Apr 23 Python
Pycharm如何导入python文件及解决报错问题
May 10 Python
Python dict的常用方法示例代码
Jun 23 Python
Python 高效编程技巧分享
Sep 10 Python
如何快速一次性卸载所有python包(第三方库)呢
Oct 20 Python
jupyter notebook快速入门及使用详解
Nov 13 Python
python 基于DDT实现数据驱动测试
Feb 18 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自动更新新闻DIY
2006/10/09 PHP
基于mysql的论坛(4)
2006/10/09 PHP
PHP中文件缓存转内存缓存的方法
2011/12/06 PHP
如何在smarty中增加类似foreach的功能自动加载数据
2013/06/26 PHP
ThinkPHP实现批量删除数据的代码实例
2014/07/02 PHP
使用JavaScript创建新样式表和新样式规则
2016/06/14 PHP
thinkPHP5框架闭包函数与子查询传参用法示例
2018/08/02 PHP
jQuery ready函数滥用分析
2011/02/16 Javascript
说明你的Javascript技术很烂的五个原因
2011/04/26 Javascript
window.event.keyCode兼容IE和Firefox实现js代码
2013/05/30 Javascript
js实现文章文字大小字号功能完整实例
2014/11/01 Javascript
Nodejs学习笔记之NET模块
2015/01/13 NodeJs
jquery实现简单实用的弹出层效果代码
2015/10/15 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
JS实现动态给标签控件添加事件的方法示例
2017/05/13 Javascript
利用Javascript开发一个二维周视图日历
2017/12/14 Javascript
使用webpack-dev-server处理跨域请求的方法
2018/04/18 Javascript
vue基础之事件v-onclick=&quot;函数&quot;用法示例
2019/03/11 Javascript
详解vue移动端项目代码拆分记录
2019/03/15 Javascript
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
详解vue中v-model和v-bind绑定数据的异同
2020/08/10 Javascript
Python实现的弹球小游戏示例
2017/08/01 Python
Python中跳台阶、变态跳台阶与矩形覆盖问题的解决方法
2018/05/19 Python
对numpy中二进制格式的数据存储与读取方法详解
2018/11/01 Python
python opencv进行图像拼接
2020/03/27 Python
加拿大女鞋品牌:ALDO
2016/11/13 全球购物
雅高酒店中国:Accorhotels.com China
2018/03/26 全球购物
娇韵诗香港官网:Clarins香港
2020/08/13 全球购物
软件工程专业推荐信
2013/10/28 职场文书
环保倡议书怎么写
2014/05/16 职场文书
大学生学习面向未来的赶考思想汇报
2014/09/12 职场文书
农村环境卫生倡议书
2015/04/29 职场文书
教师节主题班会方案
2015/08/17 职场文书
导游词之山东八仙过海景区
2019/11/11 职场文书
yyds什么意思?90后已经听不懂00后讲话了……
2022/02/03 杂记
CentOS7 minimal 最小化安装网络设置过程
2022/12/24 Servers