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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 优雅的操作张量维度(rearrange)和便携式矩阵乘法(einsum ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1、rearrange

2、repeat

3、reduce

4、附錄

4.1?對應圖像塊切片

4.2 嵌入到pytorch層中

?4.3?torch.einsum?多維線性表達式的方法


????????einops是一個簡潔優雅操作張量的庫,并且支持對numpypytorchtensorflow中的張量進行操作,該庫最大的優點是函數的使用邏輯清晰明了,其中中常用的三個函數分別是rearrangerepeatreduce。

  • rearrange: 用于對張量的維度進行重新變換排序,可用于替換pytorch中的reshape,view,transpose和permute等操作
  • repeat: 用于對張量的某一個維度進行復制,可用于替換pytorch中的repeat
  • reduce: 類似于tensorflow中的reduce操作,可以用于求平均值,最大最小值的同時壓縮張量維度。分別有’max’,‘min’,‘sum’,‘mean’,‘prod’。

1、rearrange

import torch from einops import rearrangeimages = torch.randn((32,30,40,3)) # (32, 30, 40, 3) print(rearrange(images, 'b h w c -> b h w c').shape)# (960, 40, 3) print(rearrange(images, 'b h w c -> (b h) w c').shape)# (30, 1280, 3) print(rearrange(images, 'b h w c -> h (b w) c').shape)# (32, 3, 30, 40) print(rearrange(images, 'b h w c -> b c h w').shape)# (32, 3600) print(rearrange(images, 'b h w c -> b (c h w)').shape)# --------------------------------------------- # 這里(h h1) (w w1)就相當于h與w變為原來的1/h1,1/w1倍# (128, 15, 20, 3) print(rearrange(images, 'b (h h1) (w w1) c -> (b h1 w1) h w c', h1=2, w1=2).shape)# (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、repeat

import torch from einops import repeatimage = torch.randn((30,40))# 整體復制 (30, 40, 3) print(repeat(image, 'h w -> h w c', c=3).shape)# 按行復制 (60, 40) print(repeat(image, 'h w -> (repeat h) w', repeat=2).shape)# 按列復制 (30, 120) 注意:(repeat w)與(w repeat)結果是不同的 print(repeat(image, 'h w -> h (repeat w)', repeat=3).shape)# (60, 80) print(repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)

3、reduce

import torch from einops import reducex = torch.randn(3, 5, 5) # (5, 5) print(reduce(x, 'c h w -> h w', 'max').shape)x = torch.randn(1, 3, 6, 6) # (1, 3, 3, 3) 注意:如果不是整除會報錯 y1 = reduce(x, 'b c (h h1) (w w1) -> b c h w', 'max', h1=2, w1=2) print(y1.shape)# Adaptive max-pooling:(1, 3, 3, 2) print(reduce(x, 'b c (h h1) (w w1) -> b c h1 w1', 'max', h1=3, w1=2).shape)# Global average pooling:(1, 3) print(reduce(x, 'b c h w -> b c', 'mean').shape)

4、附錄

4.1?對應圖像塊切片

import torch from einops import rearrangeimage = torch.randn(1, 3, 10, 10) # (1, 4, 75) # rearrange(image, 'b c (h h1) (w w1) -> b (h w) (h1 w1 c)', h1=p, w1=p) print(rearrange(image, 'b c (h h1) (w w1) -> b (h w) (h1 w1 c)', h1=5, w1=5).shape)

4.2 嵌入到pytorch層中

import torch.nn as nn from einops.layers.torch import Rearrangemodel = nn.Sequential(nn.Conv2d(3, 6, kernel_size=5),nn.MaxPool2d(kernel_size=2),nn.Conv2d(6, 16, kernel_size=5),nn.MaxPool2d(kernel_size=2),# flatteningRearrange('b c h w -> b (c h w)'), nn.Linear(16*5*5, 120), nn.ReLU(),nn.Linear(120, 10), )

?4.3?torch.einsum?多維線性表達式的方法

import torcha = torch.randn((1,1,3,2)) b = torch.randn((1,1,1,2))# 或 torch.einsum('b h i d, b h j d -> b h i j', [a,b]) # 相當于 torch.matmul(a,b.transpose(2,3)) c = torch.einsum('b h i d, b h j d -> b h i j', a, b) print(c.shape)

總結

以上是生活随笔為你收集整理的优雅的操作张量维度(rearrange)和便携式矩阵乘法(einsum )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。