快捷方式

torch.reshape

torch.reshape(input, shape) 張量

返回一個與 input 資料和元素數量相同,但具有指定形狀的張量。在可能的情況下,返回的張量將是 input 的一個檢視。否則,它將是一個副本。連續輸入和具有相容步長的輸入可以在不復制的情況下進行 reshape,但您不應依賴於複製或檢視行為。

請參閱 torch.Tensor.view(),瞭解何時可以返回檢視。

單個維度可以是 -1,這種情況下它會根據剩餘維度和 input 中的元素數量推斷得出。

引數
  • input (張量) – 要進行 reshape 的張量

  • shape (tuple of int) – 新的形狀

示例

>>> a = torch.arange(4.)
>>> torch.reshape(a, (2, 2))
tensor([[ 0.,  1.],
        [ 2.,  3.]])
>>> b = torch.tensor([[0, 1], [2, 3]])
>>> torch.reshape(b, (-1,))
tensor([ 0,  1,  2,  3])

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取適合初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源