日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/4/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
from einops import rearrange, reduce, repeat from einops.layers.torch import Rearrange, Reduce

一.rearrange和Rearrange,作用:從函數(shù)名稱也可以看出是對張量尺度進(jìn)行重排,

區(qū)別:
1.einops.layers.torch中的Rearrange,用于搭建網(wǎng)絡(luò)結(jié)構(gòu)時(shí)對張量進(jìn)行“隱式”的處理

例如:

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維張量轉(zhuǎn)換為3維,且原來的最后兩維合并為一維:(16,512,4,16)->(16,64,512)
這樣只要我們知道初始的張量維度就可以操作注釋來對其進(jìn)行維度重排。

2.eniops中的rearrange,用于對張量‘顯示’的處理,是一個(gè)函數(shù)

例如:

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

將4維張量轉(zhuǎn)換為3維,同樣的,只要我們知道初始維度,就可以操作注釋對其進(jìn)行重排
值得注意的是:這里的注釋給定以后就代表當(dāng)前維度
不能更改,例如:

image = torch.randn(1,2,3,2) # torch.Size([1,2,3,2]) out = rearrange(image, 'b c h w -> b (c h w)', c=2,h=3,w=2) # torch.Size([1,12]) # h,w的值更改 err1 = rearrange(image, 'b c h w -> b (c h w)', c=2,h=2,w=3) # 報(bào)錯(cuò)

二.repeat:即將tensor中的某一維度進(jìn)行重復(fù),以擴(kuò)充該維度數(shù)量

B = 16 cls_token = torch.randn(1, 1, emb_size) cls_tokens = repeat(cls_token, '() n e -> b n e', b=B)#維度為1的時(shí)候可用()代替

將(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)#錯(cuò)誤用法: d = repeat(a, 'b n e -> c n e', c = 2R)

#將(2,3,4)維張量處理為(2R, 3, 4)......
上面都是同緯度的擴(kuò)充,我們看一個(gè)升維的擴(kuò)充:

R = 5 a = torch.randn(2, 3, 4) d = repeat(a,'b n e-> b n c e ', c = R)

#將(2,3,4)維張量處理為(2, 3, 5, 4)......

這里我們同樣只須操作維度注釋即可完成相應(yīng)的張量操作。

三.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,相當(dāng)于指定池化核的大小 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,相當(dāng)于指定池化后張量的大小 # 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下的數(shù)據(jù)

總結(jié)

以上是生活随笔為你收集整理的einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。