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)