評價此頁

torch.full_like#

torch.full_like(input, fill_value, \*, dtype=None, layout=torch.strided, device=None, requires_grad=False, memory_format=torch.preserve_format) Tensor#

返回一個與 input 具有相同大小的張量,並用 fill_value 填充。 torch.full_like(input, fill_value) 等同於 torch.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device)

引數
  • input (Tensor) – input 的大小將決定輸出張量的大小。

  • fill_value – 用於填充輸出張量的數值。

關鍵字引數
  • dtype (torch.dtype, optional) – 返回的 Tensor 的所需資料型別。預設值:如果 None,則預設為 input 的 dtype。

  • layout (torch.layout, optional) – 返回的張量的所需佈局。預設值:如果 None,則預設為 input 的佈局。

  • device (torch.device, optional) – 返回的張量的所需裝置。預設值:如果 None,則預設為 input 的裝置。

  • requires_grad (bool, optional) – 如果 autograd 應記錄在返回的張量上的操作。預設值:False

  • memory_format (torch.memory_format, optional) – 返回的 Tensor 的所需記憶體格式。預設值:torch.preserve_format

示例

>>> x = torch.ones(2, 3)
>>> torch.full_like(x, 3.141592)
tensor([[ 3.1416,  3.1416,  3.1416],
        [ 3.1416,  3.1416,  3.1416]])
>>> torch.full_like(x, 7)
tensor([[7., 7., 7.],
        [7., 7., 7.]])
>>> torch.full_like(x, 0.5, dtype=torch.int32)
tensor([[0, 0, 0],
        [0, 0, 0]], dtype=torch.int32)
>>> y = torch.randn(3, 4, dtype=torch.float64)
>>> torch.full_like(y, -1.0)
tensor([[-1., -1., -1., -1.],
        [-1., -1., -1., -1.],
        [-1., -1., -1., -1.]], dtype=torch.float64)