1.einops.rearrange==》重新指定维度

def rearrange(tensor, pattern, **axes_lengths):

einops.rearrange is a reader-friendly smart element reordering for multidimensional tensors. This operation includes functionality of transpose (axes permutation), reshape (view), squeeze, unsqueeze, stack, concatenate and other operations.

import numpy as np
from einops import rearrange, repeat# 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 :(32, 30, 40, 3)
print(rearrange(images, 'b h w c -> b h w c').shape)# concatenate images along height (vertical axis), 960 = 32 * 30  :(960, 40, 3)
print(rearrange(images, 'b h w c -> (b h) w c').shape)# concatenated images along horizontal axis, 1280 = 32 * 40  :(30, 1280, 3)
print(rearrange(images, 'b h w c -> h (b w) c').shape)# reordered axes to "b c h w" format for deep learning  :(32, 3, 30, 40)
print(rearrange(images, 'b h w c -> b c h w').shape)# flattened each image into a vector, 3600 = 30 * 40 * 3  :(32, 3600)
print(rearrange(images, 'b h w c -> b (c h w)').shape)# ======================================================================================================================
# 这里(h h1) (w w1)就相当于h与w变为原来的1/h1,1/w1倍# split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), 128 = 32 * 2 * 2  :(128, 15, 20, 3)
print(rearrange(images, 'b (h h1) (w w1) c -> (b h1 w1) h w c', h1=2, w1=2).shape)# space-to-depth operation  :(32, 15, 20, 12)
print(rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape)

2.einops.repeat==》重排和重复(增加)维度

einops.repeat allows reordering elements and repeating them in arbitrary combinations. This operation includes functionality of repeat, tile, broadcast functions.

import numpy as np
from einops import rearrange, repeat,reduce# a grayscale image (of shape height x width)
image = np.random.randn(30, 40)# change it to RGB format by repeating in each channel:(30, 40, 3)
print(repeat(image, 'h w -> h w c', c=3).shape)# repeat image 2 times along height (vertical axis):(60, 40)
print(repeat(image, 'h w -> (repeat h) w', repeat=2).shape)# repeat image 2 time along height and 3 times along width:(30, 120)
print(repeat(image, 'h w -> h (repeat w)', repeat=3).shape)# convert each pixel to a small square 2x2. Upsample image by 2x:(60, 80)
print(repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)# pixelate image first by downsampling by 2x, then upsampling:(30, 40)
downsampled = reduce(image, '(h h2) (w w2) -> h w', 'mean', h2=2, w2=2)
print(repeat(downsampled, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)

3.einops.reduce

einops.reduce provides combination of reordering and reduction using reader-friendly notation.

import numpy as np
from einops import rearrange,reducex = np.random.randn(100, 32, 64)
# perform max-reduction on the first axis:(32, 64)
print(reduce(x, 't b c -> b c', 'max').shape) # same as previous, but with clearer axes meaning:(32, 64)
print(reduce(x, 'time batch channel -> batch channel', 'max').shape)x = np.random.randn(10, 20, 30, 40)
# 2d max-pooling with kernel size = 2 * 2 for image processing:(10, 20, 15, 20)
y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2)
print(y1.shape)# if one wants to go back to the original height and width, depth-to-space trick can be applied:(10, 5, 30, 40)
y2 = rearrange(y1, 'b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)', h2=2, w2=2)
print(y2.shape)# Adaptive 2d max-pooling to 3 * 4 grid:(10, 20, 3, 4)
print(reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=3, w1=4).shape)# Global average pooling:(10, 20)
print(reduce(x, 'b c h w -> b c', 'mean').shape)

einops.rearrange、repeat、reduce==>对维度进行操作相关推荐

  1. einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式

    from einops import rearrange, reduce, repeat from einops.layers.torch import Rearrange, Reduce 一.rea ...

  2. einops库中rearrange,reduce和repeat的介绍

    用法介绍  einops是一个简洁优雅操作张量的库,并且支持对numpy,pytorch,tensorflow中的张量进行操作,该库最大的优点是函数的使用逻辑清晰明了,其中中常用的三个函数分别是rea ...

  3. 2 Spark入门reduce、reduceByKey的操作

    上一篇是讲map,map的主要作用就是替换.reduce的主要作用就是计算. package reduce;import org.apache.spark.api.java.JavaPairRDD; ...

  4. pandas一维度数据操作

    import pandas as pd # 一维度实例 # 使用列表实例化 data=pd.Series([0.25,0,75,0.5,1

  5. Numpy给数组增加维度的操作

    不说其他的,直接给个例子就懂了. a是一个一维数组 a = np.array([0.0, 10.0, 20.0, 30.0]) print(a.shape,a[:, np.newaxis],a[:, ...

  6. matlab圆阵列,matlab – 如何沿阵列的某个维度执行操作?

    我有一个包含5个3×4切片的3D数组,定义如下: rng(3372061); M = randi(100,3,4,5); 我想收集有关数组的一些统计信息: >每列中的最大值. >每行的平均 ...

  7. 优雅的操作张量维度(rearrange)和便携式矩阵乘法(einsum )

    目录 1.rearrange 2.repeat 3.reduce 4.附录 4.1 对应图像块切片 4.2 嵌入到pytorch层中 4.3 torch.einsum 多维线性表达式的方法 einop ...

  8. einops和einsum:直接操作张量的利器

    einops和einsum:直接操作张量的利器 einops和einsum是Vision Transformer的代码实现里出现的两个操作tensor维度和指定tensor计算的神器,在卷积神经网络里 ...

  9. 深度学习(8)TensorFlow基础操作四: 维度变换

    深度学习(8)TensorFlow基础操作四: 维度变换 1. View 2. 示例 3. Reshape操作可能会导致潜在的bug 4. tf.transpose 5. Squeeze VS Exp ...

最新文章

  1. LSTM如何解决梯度消失或爆炸的?
  2. org.dom4j.DocumentException: 20 Nested exception: 20
  3. 全球IPv4地址正式耗尽,你知道吗?
  4. 2015 DevOps状态调查报告
  5. mysql fetch rows_差异mysql_fetch_array()和mysql_fetch_rows()函数_mysql
  6. html获取url后面的参数_Golang Gin 实战(四)| URL查询参数的获取和原理分析
  7. 2.04 对字母数字的混合排序
  8. 对tensorflow 的BatchNormalization的坑的理解与测试
  9. 2.微服务设计 --- 演化式架构师
  10. Java判断是否为移动端
  11. SpreadJS V14.2.0 放假前Crack
  12. deeping linux安装安卓,Deepin 安装Android-studio
  13. R语言---ggplot绘图
  14. 微软2016校园招聘4月在线笔试1-Font Size
  15. 《创世纪语录》1、只要有梦想,什么都能够实现。3、做得越多机会越多。
  16. 数据分析笔记--对NBA球员数据的聚类分析(代码)
  17. 使用Qemu在Mac上安装虚拟机
  18. 天津大学计算机专业考研考什么,天津大学计算机专业考研难吗
  19. 瑞芯微RK PX30中文详解(带开源资料)简介
  20. MATLAB图像处理(直方图)

热门文章

  1. 数据结构基础(7) --循环队列的设计与实现
  2. RaySSH文件传输加速介绍!
  3. 【New Feature】阿里云OSS同城区域冗余存储重磅发布,提供云上同城容灾服务能力!...
  4. android类似QQ空间,微信朋友圈,微博主页源码
  5. Ubuntu之Docker安装
  6. tomcat启动报错APR问题
  7. 会计丑闻之后 东芝“迎来”第五次延交财报
  8. textarea内容有换行时存入数据库丢失问题的解决 (转载)
  9. 开发部考核管理制度 随想
  10. 层遇到select框时[收藏]