PyTorch view和reshape的區別

2021-10-03 18:26:29 字數 281 閱讀 2410

相同之處

都可以用來重新調整 tensor 的形狀。

不同之處

view 函式只能用於 contiguous 後的 tensor 上,也就是只能用於記憶體中連續儲存的 tensor。如果對 tensor 呼叫過 transpose, permute 等操作的話會使該 tensor 在記憶體中變得不再連續,此時就不能再呼叫 view 函式。因此,需要先使用 contiguous 來返回乙個 contiguous copy。

reshape 則不需要依賴目標 tensor 是否在記憶體中是連續的。

pytorch view函式用法

在pytorch中view函式的作用為重構張量的維度,相當於numpy中resize 的功能 import torch t1 torch.tensor 1,2,3,4,5,6 result tt1.view 3,2 result tensor 1,2 3,4 5,6 建立乙個6維的向量,view 3...

android 從assets和res中讀取檔案

assets資料夾裡面的檔案都是保持原始的檔案格式,需要用assetmanager以位元組流的形式讀取檔案。1.先在activity裡面呼叫getassets 來獲取assetmanager引用。2.再用assetmanager的open string filename,int accessmode...

httpClient訪問http的rest介面

訪問http的rest介面 1 get方式訪問http的rest介面 以get的方式訪問http的rest介面 main string args throws exception2 post方式 json和表單 訪問http的rest介面 1 json的形式post資料 以json的形式提交資料 關...