PyTorch - torch.ones_like、torch.zeros_like、 torch.full_like

PyTorch - torch.ones_like、 torch.zeros_like、 torch.full_like

flyfish

import torch
input = torch.rand(3, 4)
print(input)
# tensor([[0.5840, 0.8260, 0.7539, 0.2138],
#         [0.9743, 0.0964, 0.7610, 0.5746],
#         [0.6247, 0.3334, 0.6949, 0.9065]])

# 与input形状相同、元素全为1
a = torch.ones_like(input)
print(a)
# tensor([[1., 1., 1., 1.],
#         [1., 1., 1., 1.],
#         [1., 1., 1., 1.]])

#与input形状相同、元素全为0
b = torch.zeros_like(input)
print(b)
# tensor([[0., 0., 0., 0.],
#         [0., 0., 0., 0.],
#         [0., 0., 0., 0.]])

#与input形状相同、元素全为3
c = torch.full_like(input,3)
print(c)
# tensor([[3., 3., 3., 3.],
#         [3., 3., 3., 3.],
#         [3., 3., 3., 3.]])

print(torch.zeros(3,2))
# tensor([[0., 0.],
#         [0., 0.],
#         [0., 0.]])

print(torch.ones(3,2))
# tensor([[1., 1.],
#         [1., 1.],
#         [1., 1.]])
上一篇:Minor GC和Full GC触发条件


下一篇:CPU 字长(cpu word size)是什么意思?