Python Pytorch full() 方法
PyTorch 是 Facebook 开发的开源机器学习库。它用于深度神经网络和自然语言处理目的。
函数torch.full()
返回一个用 fill_value 填充的尺寸大小的张量。
Syntax: torch.ones(size, fill_value, out=None)
Parameters:
size: a sequence of integers defining the shape of the output tensor
fill_value: the number to fill the output tensor with.
out (Tensor, optional): the output tensor
Return type: A tensor
代码#1:
# Importing the PyTorch library
import torch
# Applying the full function and
# storing the resulting tensor in 'a'
a = torch.full([3, 4], 3)
print("a = ", a)
b = torch.full([2, 5], 3.5)
print("b = ", b)
输出:
a = tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
b = tensor([[3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[3.5000, 3.5000, 3.5000, 3.5000, 3.5000]])