einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式
一.rearrange和Rearrange,作用:從函數名稱也可以看出是對張量尺度進行重排,
區別:
1.einops.layers.torch中的Rearrange,用于搭建網絡結構時對張量進行“隱式”的處理
例如:
class PatchEmbedding(nn.Module):def __init__(self, in_channels: int = 3, patch_size: int = 16, emb_size: int = 768, img_size: int = 224):self.patch_size = patch_sizesuper().__init__()self.projection = nn.Sequential(# using a conv layer instead of a linear one -> performance gainsnn.Conv2d(in_channels, emb_size, kernel_size=patch_size, stride=patch_size),Rearrange('b e (h) (w) -> b (h w) e'),)這里的Rearrange('b e (h) (w) -> b (h w) e'),表示將4維張量轉換為3維,且原來的最后兩維合并為一維:(16,512,4,16)->(16,64,512)
這樣只要我們知道初始的張量維度就可以操作注釋來對其進行維度重排。
2.eniops中的rearrange,用于對張量‘顯示’的處理,是一個函數
例如:
rearrange(images, 'b h w c -> b (h w) c')將4維張量轉換為3維,同樣的,只要我們知道初始維度,就可以操作注釋對其進行重排
值得注意的是:這里的注釋給定以后就代表當前維度,不能更改,例如:
二.repeat:即將tensor中的某一維度進行重復,以擴充該維度數量
B = 16 cls_token = torch.randn(1, 1, emb_size) cls_tokens = repeat(cls_token, '() n e -> b n e', b=B)#維度為1的時候可用()代替將(1,1,emb_size)的張量處理為(B,1,emb_size)
R = 16 a = torch.randn(2,3,4) b = repeat(a, 'b n e -> (r b) n e', r = R) #(2R, 3, 4) c = repeat(a, 'b n e -> b (r n) e', r = R) #(2, 3R, 4)#錯誤用法: d = repeat(a, 'b n e -> c n e', c = 2R)#將(2,3,4)維張量處理為(2R, 3, 4)......
上面都是同緯度的擴充,我們看一個升維的擴充:
#將(2,3,4)維張量處理為(2, 3, 5, 4)......
這里我們同樣只須操作維度注釋即可完成相應的張量操作。
三.Reduce 和 reduce:
x = torch.randn(100, 32, 64) # perform max-reduction on the first axis: y0 = reduce(x, 't b c -> b c', 'max') #(32, 64)#指定h2,w2,相當于指定池化核的大小 x = torch.randn(10, 512, 30, 40) # 2d max-pooling with kernel size = 2 * 2 y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2) #(10, 512, 15, 20)# go back to the original height and width y2 = rearrange(y1, 'b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)', h2=2, w2=2) #(10, 128, 30, 40) #指定h1,w1,相當于指定池化后張量的大小 # 2d max-pooling to 12 * 16 grid: y3 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=12, w1=16) #(10, 512, 12, 16)# 2d average-pooling to 12 * 16 grid: y4 = (reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'mean', h1=12, w1=16) #(10, 512, 12, 16)# Global average pooling y5 = reduce(x, 'b c h w -> b c', 'mean') #(10, 512)Redece同理。
注意:這里我們以張量為例,einops同樣可以處理numpy下的數據
總結
以上是生活随笔為你收集整理的einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于pytorch中super的一点思考
- 下一篇: 记录之关于tensoflow中使用Ada