在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获取豆瓣电影简介代码分享
Jan 16 Python
python批量修改文件名的实现代码
Sep 01 Python
python字符类型的一些方法小结
May 16 Python
python:pandas合并csv文件的方法(图书数据集成)
Apr 12 Python
详解python中Numpy的属性与创建矩阵
Sep 10 Python
python 实现selenium断言和验证的方法
Feb 13 Python
详解python中init方法和随机数方法
Mar 13 Python
python的依赖管理的实现
May 14 Python
Python如何使用argparse模块处理命令行参数
Dec 11 Python
python实现简单学生信息管理系统
Apr 09 Python
Python发起请求提示UnicodeEncodeError错误代码解决方法
Apr 21 Python
使用jupyter notebook运行python和R的步骤
Aug 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
全国FM电台频率大全 - 19 广东省
2020/03/11 无线电
PHP 文件系统详解
2012/09/13 PHP
PHP中Cookie的使用详解(简单易懂)
2017/04/28 PHP
PHP中递归的实现实例详解
2017/11/14 PHP
PHP安全之register_globals的on和off的区别
2020/07/23 PHP
Javascript 多物体运动的实现
2014/12/24 Javascript
AngularJs bootstrap搭载前台框架——准备工作
2016/09/01 Javascript
JS实现的手机端精简幻灯片效果
2016/09/05 Javascript
vuejs响应用户事件(如点击事件)
2017/03/14 Javascript
JS运动特效之完美运动框架实例分析
2018/01/24 Javascript
Vue-Router2.X多种路由实现方式总结
2018/02/09 Javascript
nginx部署访问vue-cli搭建的项目的方法
2018/02/12 Javascript
微信小程序:数据存储、传值、取值详解
2019/05/07 Javascript
关于引入vue.js 文件的知识点总结
2020/01/28 Javascript
[02:43]2014DOTA2国际邀请赛 官方Alliance战队纪录片
2014/07/14 DOTA
[03:30]完美盛典趣味短片 CSGO2019年度名场面
2019/12/07 DOTA
python制作爬虫爬取京东商品评论教程
2016/12/16 Python
python监控linux内存并写入mongodb(推荐)
2017/09/11 Python
Python实现的栈(Stack)
2018/01/26 Python
Python实现爬取马云的微博功能示例
2019/02/16 Python
python画图--输出指定像素点的颜色值方法
2019/07/03 Python
python实现ip代理池功能示例
2019/07/05 Python
python虚拟环境模块venv使用及示例
2020/03/04 Python
关于Kotlin中SAM转换的那些事
2020/09/15 Python
如何整合JQuery和Prototype
2014/01/31 面试题
90后毕业生的求职信范文
2013/09/21 职场文书
会计专业毕业生自我评价
2013/09/25 职场文书
高中军训感言500字
2014/02/24 职场文书
竞选学习委员演讲稿
2014/09/01 职场文书
党的群众路线教育实践活动个人对照检查材料(公安)
2014/11/05 职场文书
社区党务工作总结2015
2015/05/19 职场文书
2015年校务公开工作总结
2015/05/26 职场文书
环境卫生整治简报
2015/07/20 职场文书
python如何获取网络数据
2021/04/11 Python
tomcat正常启动但网页却无法访问的几种解决方法
2022/05/06 Servers
win server2012 r2服务器共享文件夹如何设置
2022/06/21 Servers