einops库中rearrange,reduce和repeat的介绍
用法介绍
einops是一个简洁优雅操作张量的库,并且支持对numpy,pytorch,tensorflow中的张量进行操作,该库最大的优点是函数的使用逻辑清晰明了,其中中常用的三个函数分别是rearrange,repeat,reduce。
- rearrange: 用于对张量的维度进行重新变换排序,可用于替换pytorch中的reshape,view,transpose和permute等操作
- repeat: 用于对张量的某一个维度进行复制,可用于替换pytorch中的repeat
- reduce: 类似于tensorflow中的reduce操作,可以用于求平均值,最大最小值的同时压缩张量维度
einops中rearrange,repeat,reduce的函数细节介绍如下所示
def rearrange(inputs, pattern, **axes_lengths) ⟶ \longrightarrow ⟶ transform_inputs
- inputs (tensor): 表示输入的张量
- pattern (str): 表示张量维度变换的映射关系
- **axes_lengths: 表示按照指定的规格形式进行变换
def repeat(inputs, pattern, **axes_lengths) ⟶ \longrightarrow ⟶ transform_inputs
- inputs (tensor): 表示输入的张量
- pattern (str): 表示张量按照某个维度复制的映射关系
- **axes_lengths: 表示按照指定的规格形式进行复制
def reduce(inputs, pattern, reduction, **axes_lengths) ⟶ \longrightarrow ⟶ transform_inputs
- inputs (tensor): 表示输入的张量
- pattern (str): 表示张量执行某种运算做操作后维度变换的映射关系
- reduction (str): 表示运算操作的类型,分别有’max’,‘min’,‘sum’,‘mean’,‘prod’
- **axes_lengths: 表示按照指定的规格形式进行运算操作
代码示例
einops库中rearrange函数的代码示例如下所示
# suppose we have a set of 32 images in "h w c" format (height-width-channel)
>>> images = [np.random.randn(30, 40, 3) for _ in range(32)]
# stack along first (batch) axis, output is a single array
>>> rearrange(images, 'b h w c -> b h w c').shape
(32, 30, 40, 3)
# concatenate images along height (vertical axis), 960 = 32 * 30
>>> rearrange(images, 'b h w c -> (b h) w c').shape
(960, 40, 3)
# concatenated images along horizontal axis, 1280 = 32 * 40
>>> rearrange(images, 'b h w c -> h (b w) c').shape
(30, 1280, 3)
# reordered axes to "b c h w" format for deep learning
>>> rearrange(images, 'b h w c -> b c h w').shape
(32, 3, 30, 40)
# flattened each image into a vector, 3600 = 30 * 40 * 3
>>> rearrange(images, 'b h w c -> b (c h w)').shape
(32, 3600)
# split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), 128 = 32 * 2 * 2
>>> rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', h1=2, w1=2).shape
(128, 15, 20, 3)
# space-to-depth operation
>>> rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape
(32, 15, 20, 12)
einops库中repeat函数的代码示例如下所示
# a grayscale image (of shape height x width)
>>> image = np.random.randn(30, 40)
# change it to RGB format by repeating in each channel
>>> repeat(image, 'h w -> h w c', c=3).shape
(30, 40, 3)
# repeat image 2 times along height (vertical axis)
>>> repeat(image, 'h w -> (repeat h) w', repeat=2).shape
(60, 40)
# repeat image 2 time along height and 3 times along width
>>> repeat(image, 'h w -> h (repeat w)', repeat=3).shape
(30, 120)
# convert each pixel to a small square 2x2. Upsample image by 2x
>>> repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape
(60, 80)
# pixelate image first by downsampling by 2x, then upsampling
>>> downsampled = reduce(image, '(h h2) (w w2) -> h w', 'mean', h2=2, w2=2)
>>> repeat(downsampled, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape
(30, 40)
einops库中reduce函数的代码示例如下所示
>>> x = np.random.randn(100, 32, 64)
# perform max-reduction on the first axis
>>> y = reduce(x, 't b c -> b c', 'max')
# same as previous, but with clearer axes meaning
>>> y = reduce(x, 'time batch channel -> batch channel', 'max')
>>> x = np.random.randn(10, 20, 30, 40)
# 2d max-pooling with kernel size = 2 * 2 for image processing
>>> y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2)
# if one wants to go back to the original height and width, depth-to-space trick can be applied
>>> y2 = rearrange(y1, 'b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)', h2=2, w2=2)
>>> assert parse_shape(x, 'b _ h w') == parse_shape(y2, 'b _ h w')
# Adaptive 2d max-pooling to 3 * 4 grid
>>> reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=3, w1=4).shape
(10, 20, 3, 4)
# Global average pooling
>>> reduce(x, 'b c h w -> b c', 'mean').shape
(10, 20)
# Subtracting mean over batch for each channel
>>> y = x - reduce(x, 'b c h w -> () c () ()', 'mean')
# Subtracting per-image mean for each channel
>>> y = x - reduce(x, 'b c h w -> b c () ()', 'mean')
einops库中rearrange,reduce和repeat的介绍相关推荐
- Lua字符串库中的几个重点函数介绍
在<Lua中的一些库>中也说到了,要对string库的模式匹配进行单独的讲解.对于字符串的处理,对于任何语言的学习来说,都是一个难点,而且也是一个必会的知识点.给你一个字符串,让你按照某种 ...
- einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式
from einops import rearrange, reduce, repeat from einops.layers.torch import Rearrange, Reduce 一.rea ...
- openresty开发系列16--lua中的控制结构if-else/repeat/for/while
openresty开发系列16--lua中的控制结构if-else/repeat/for/while一)条件 - 控制结构 if-else if-else 是我们熟知的一种控制结构.Lua 跟其他语言 ...
- PPQ库中KLD算法实现代码解析
PPQ量化工具库KLD算法解析 前言 PPQ算法实现 NVIDIA的PPT中KLD算法流程 KLD算法PPQ实现版本 PPQ与NVIDIA的区别: 前言 这是对PPQ库中KLD算法实现代码解析,关于P ...
- Dlib库中实现正脸人脸关键点(landmark)检测的测试代码
Dlib库中提供了正脸人脸关键点检测的接口,这里参考dlib/examples/face_landmark_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸关键点检测的 ...
- Dlib库中实现正脸人脸检测的测试代码
Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...
- CImg库中CImg,CImgList,CImgDisplay三个类的介绍
转自:http://www.cppprog.com/2009/0426/108.html 本文简单介绍了CImg库中的三个大类:CImg,CImgList,CImgDisplay.然后给出了让CImg ...
- C++标准库中各种排序归纳
一.简介 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.我们在编程过程中会经常接触到排序,比如游戏中的排行榜等.C++标准库中提供了各种不同的排序算法,这篇博 ...
- 备库中ORA-00600错误的简单修复
最近偶尔会接到一条短信,提示某个备库中出现了ORA-00600的错误.对于这个问题还真不能心存侥幸,自己带着疑问查看了一下, 这是一个一主两备的库,主库和其中的一个备库没有任何的ORA-00600的错 ...
最新文章
- android京东秒杀倒计时,js实现京东秒杀倒计时功能
- Makefile:160: recipe for target 'all' failed (Ubuntu 16.06 + Opencv3.2)解决办法
- Nginx 配置文件 nginx.conf 详解
- python输出奇数数字序位_python对输出的奇数偶数排序实例代码
- Java面试170题答案解析(1-20题)
- 作者:李涛(1975-),男,南京邮电大学计算机学院、软件学院院长,南京邮电大学大数据研究院院长。...
- mysql编译安装root密码_MySQL 5.7.11编译安装以及修改root密码小结
- 华北水利水电大学c语言实验报告八2020,2021年华北水利水电大学级C语言实验报告.doc...
- qpushbutton里面的文字怎么换行_ipad读PDF必备,OCR局部识别文字并快速提取,免费的buff你要不要?...
- Windows Azure 革新 – TFS集成(WAWS第2部分)
- js 内置对象之数组Array
- linux db2v9.7卸载,db2 卸载和安装
- utf-8和gbk的区别
- 用了三星Dex,我已经快一个月回家没开过电脑了
- 淘宝装修之模块之间有间隙(淘宝装修一)
- 如何制作个人商业网站
- Excel文档误删的4种恢复方法,1秒就可以还原所有内容,你用过吗
- 一本程序员黑话进阶指南!拿走不谢!
- codevs1034
- Activity理念(一个字头的诞生)
热门文章
- 「桌面好物」除了是无叶风扇,居然还可以用来...
- I.MX6U MMDC 控制器简介
- 7-3 抢红包 (25 分)
- JAVA-调用百度文字识别OCR进行身份证识别
- Keil中的code关键字
- OpenLDAP 最详细的概念教程
- 干涉测量技术的应用_百篇科普系列(71)—激光干涉的精密测量技术
- WheelCollider惯性漂移
- 华夏银行核心系统用Oracle,产品技术-H3C建设苏州华夏银行数据存储平台-新华三集团-H3C...
- Python 计算机视觉(十六)—— 图像和视频中的人脸识别