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])
本页目录