einops.rearrange、repeat、reduce==>对维度进行操作

发布时间:2024-12-12 07:36

对电动工具进行深度清洁时,不要用湿手操作 #生活技巧# #居家生活技巧# #电器使用技巧# #电动工具维护保养#

首先,下面这两种写法都是一样的,里面的命名是你自己决定的,你只需要保证 ‘->’ 这个的前后的名字是对应的就行

print(reduce(x, 't b c -> b c', 'max').shape) 

print(reduce(x, 'time batch channel -> batch channel', 'max').shape)

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

images = [np.random.randn(30, 40, 3) for _ in range(32)]

print(rearrange(images, 'b h w c -> b h w c').shape)

print(rearrange(images, 'b h w c -> (b h) w c').shape)

print(rearrange(images, 'b h w c -> h (b w) c').shape)

print(rearrange(images, 'b h w c -> b c h w').shape)

print(rearrange(images, 'b h w c -> b (c h w)').shape)

print(rearrange(images, 'b (h h1) (w w1) c -> (b h1 w1) h w c', h1=2, w1=2).shape)

print(rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape)

使用rearrange增加一个维度: 

也可以使用rearrange增加一个维度:【直接加入一个1就可以了(这个1可以放在张量的任何位置,也就是可以在任何维度加入新的一维),类似于torch.unsqueeze(tensor, dim=0)操作】

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

image = np.random.randn(30, 40)

print(repeat(image, 'h w -> h w c', c=3).shape)

print(repeat(image, 'h w -> (repeat h) w', repeat=2).shape)

print(repeat(image, 'h w -> h (repeat w)', repeat=3).shape)

print(repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)

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,reduce

x = np.random.randn(100, 32, 64)

print(reduce(x, 't b c -> b c', 'max').shape)

print(reduce(x, 'time batch channel -> batch channel', 'max').shape)

x = np.random.randn(10, 20, 30, 40)

y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2)

print(y1.shape)

y2 = rearrange(y1, 'b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)', h2=2, w2=2)

print(y2.shape)

print(reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=3, w1=4).shape)

print(reduce(x, 'b c h w -> b c', 'mean').shape)

笔者有话说:

einops是一个用于操作张量的库,它的出现可以替代我们平时使用的reshape、view、transpose和permute等操作,相信接触过深度学习的同学一定对这些函数比较的熟悉。

einops相较于上面说的那些函数,最显著的区别就是逻辑更加的清晰,用网上的一句话来说的话,就是可以避免view、transpose等函数的神秘主义

这个和view()的区别是,

1、einops看起来更加的直观,你可以很清楚的看到维度的变化规则,

2、并且你甚至不用知道在变化维度的时候具体的变量名是什么,只需要知道它内部的维度代表什么即可,但是这样也需要你很深刻的理解维度的表示意义是什么
【例如:
x.view(batch, -1) #这里你需要知道batch这个上下文变量
rearrange(x, 'b h w' -> 'b (h w)')

3、并且这个支持的操作比view()要多一些,总的来说还是更推荐einops

https://www.cnblogs.com/c-chenbin/p/15375637.html

网址:einops.rearrange、repeat、reduce==>对维度进行操作 https://www.yuejiaxmz.com/news/view/450591

相关内容

Reduce Memory(内存优化工具) v1.7 中文绿色版
自动化运维?看看Python怎样完成自动任务调度⛵
如何对电脑进行维护 ▷➡️
如何进行家居清洁和维护?这种维护有哪些具体操作方法?
热量限制的低碳水化合物饮食和膳食纤维对非酒精性脂肪肝影响的研究进展
雅思写作大作文思路 压力的原因与解决方案 stress and how to reduce stress
如何更好的对电脑进行维护
设备操作管理制度
如何进行家居用品的拆卸与维护?这种操作如何保证安全性?
电脑操作系统维护技巧

随便看看