Python Pytorch one() 方法
PyTorch 是 Facebook 开发的开源机器学习库。它用于深度神经网络和自然语言处理目的。
函数torch.ones()
返回一个用标量值 1 填充的张量,其形状由变量参数大小定义。
Syntax: torch.ones(size, out=None)
Parameters:
size: a sequence of integers defining the shape of the output tensor
out (Tensor, optional): the output tensor
Return type: A tensor filled with scalar value 1, of same shape as size.
代码#1:
# Importing the PyTorch library
import torch
# Applying the ones function and
# storing the resulting tensor in 't'
a = torch.ones([3, 4])
print("a = ", a)
b = torch.ones([1, 5])
print("b = ", b)
c = torch.ones([5, 1])
print("c = ", c)
d = torch.ones([3, 3, 2])
print("d = ", d)
输出:
a = tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
b = tensor([[1., 1., 1., 1., 1.]])
c = tensor([[1.],
[1.],
[1.],
[1.],
[1.]])
d = tensor([[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]])