評價此頁

torch.reshape#

torch.reshape(input, shape) Tensor#

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

有關何時可能返回檢視,請參閱 torch.Tensor.view()

單個維度可以為 -1,在這種情況下,它將根據其餘維度和 input 中的元素數量進行推斷。

引數
  • input (Tensor) – 要重塑的張量

  • 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])