torch.full 实例

>>> torch.full((2, 3), 3.1416)
tensor([[ 3.1416,  3.1416,  3.1416],
        [ 3.1416,  3.1416,  3.1416]])

功能torch.full()返回一个大小为fill_value的张量的张量。

用法:torch.ones(size, fill_value, out=None)

参数

  • size:定义输出张量形状的整数序列
  • fill_value:用于填充输出张量的数字。
  • out (Tensor, optional):输出张量

返回类型:张量

代码1:

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]])

猜你喜欢

转载自blog.csdn.net/weixin_43135178/article/details/115213506