pytorch中reshape的使用小結(jié)
torch.reshape 是 PyTorch 中用于改變張量形狀的函數(shù)。它的作用是重新安排張量的維度,使其符合指定的形狀,但不會(huì)改變數(shù)據(jù)的順序。
Returns a tensor with the same data and number of elements as input, but with the specified shape.
基本語法
input_reshape = torch.reshape(input, shape)
或者:
input_reshape = input.reshape(shape)
input(tensor):輸入張量。shape(tuple of int):目標(biāo)形狀,可以是一個(gè)整數(shù)或一個(gè)整數(shù)元組,表示張量的新維度。
例子
import torch # 創(chuàng)建一個(gè)形狀為 (4, 3) 的張量 x = torch.arange(12).reshape(4, 3) print(x)
輸出:
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
你可以將這個(gè)張量重新調(diào)整成其他形狀,比如將它變成一個(gè) (2, 6) 的張量:
y = x.reshape(2, 6) print(y)
輸出:
tensor([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
特別的用法
使用1自動(dòng)推算維度:
如果你不確定某個(gè)維度應(yīng)該是多少,可以使用 -1 來自動(dòng)推算。例如,如果你只知道張量的總元素?cái)?shù)和某些維度,可以讓 PyTorch 自動(dòng)計(jì)算某個(gè)維度。
A single dimension may be -1, in which case it’s inferred from the remaining dimensions and the number of elements in
input.
z = x.reshape(2, -1) # PyTorch 會(huì)自動(dòng)推算第二維度的大小 print(z)
輸出:
tensor([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
torch.reshape(x, (-1,))將張量展平成一維(即向量)等效于x.flatten()
此時(shí) PyTorch 會(huì)自動(dòng)計(jì)算該維度的大小,計(jì)算公式為:
新維度大小 = 原始張量的總元素?cái)?shù)因?yàn)橐钩上蛄?,所以只有一個(gè)維度(表示有多少個(gè)數(shù)),所以只需要一個(gè)-1,后面空著就行
- 所以其實(shí)也可以寫成 (-1),即
torch.reshape(x, (-1))但推薦保留逗號(hào)
- 所以其實(shí)也可以寫成 (-1),即
注意:這跟 (1,-1)有區(qū)別,(-1,)是展平成一維向量,
而(1,-1)是1*n,這被視作二維的(一個(gè)維度是1個(gè)數(shù),另一個(gè)維度是n個(gè)數(shù))
例子:
x = torch.tensor([[0, 1],
[2, 3]])
x_vec = x.reshape(-1,)
print(x_vec)
x_matrix = x.reshape(1,-1)
print(x_matrix)
輸出:
tensor([0, 1, 2, 3]) tensor([[0, 1, 2, 3]])
總元素?cái)?shù):2 × 2 = 4
執(zhí)行 torch.reshape(x, (-1,)) 后:
→ 新形狀為 (4,)
→ 結(jié)果:tensor([0, 1, 2, 3])
執(zhí)行 torch.reshape(x, (1,-1)) 后:
→ 新形狀為 (1,4)
→ 結(jié)果:tensor([[0, 1, 2, 3]])
注意事項(xiàng)
保持元素?cái)?shù)量一致:
使用 reshape 時(shí),新形狀的元素數(shù)量必須和原形狀一致。例如,原來是 (4, 3),總共有 12 個(gè)元素,不能reshape成 (3, 5) 因?yàn)闀?huì)丟失元素。
# 錯(cuò)誤的 reshape # x.reshape(3, 5) # 會(huì)拋出錯(cuò)誤,無法reshape
總之,要接收返回的tensor
reshape不會(huì)修改原始張量,它會(huì)返回一個(gè)新的張量,除非原張量已經(jīng)在內(nèi)存中是以連續(xù)的方式存儲(chǔ)。
When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.
- 如果原張量的內(nèi)存布局不連續(xù),
reshape會(huì)返回一個(gè)新的內(nèi)存副本。 torch.view和torch.reshape在功能上相似,但torch.view要求張量是連續(xù)的,而torch.reshape會(huì)自動(dòng)處理不連續(xù)的張量。
到此這篇關(guān)于pytorch中reshape的使用小結(jié)的文章就介紹到這了,更多相關(guān)pytorch reshape內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)從web抓取文檔的方法
這篇文章主要介紹了python實(shí)現(xiàn)從web抓取文檔的方法,以抓取人人網(wǎng)頁面為例講述了完整的web文檔抓取方法,需要的朋友可以參考下2014-09-09
Python計(jì)算當(dāng)前日期是一年中的第幾天的方法詳解
在Python中,計(jì)算當(dāng)前日期是一年中的第幾天可以通過內(nèi)置的datetime模塊來實(shí)現(xiàn),本文將詳細(xì)介紹如何使用Python編寫代碼來完成這個(gè)任務(wù),需要的可以參考下2023-12-12
python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼
這篇文章主要介紹了python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼,需要的朋友可以參考下2018-05-05
python中用shutil.move移動(dòng)文件或目錄的方法實(shí)例
在python操作中大家對(duì)os,shutil,sys,等通用庫一定不陌生,下面這篇文章主要給大家介紹了關(guān)于python中用shutil.move移動(dòng)文件或目錄的相關(guān)資料,需要的朋友可以參考下2022-12-12

