torch.randn用法以及小案例

一针见血:torch.randn是生成0~1的正太分布的随机数。

简单介绍一下torch.randn的用法,通过三个案例让大家一目了然。

1. torch.randn(5)

import torch

input = torch.randn(5)
print(input.shape)
print(input)

#torch.Size([5])
# tensor([ 0.0782, -0.3001, -0.2876,  0.5861, -0.7563])

2. torch.randn(2,5)

input = torch.randn(2,5)
print(input.shape)
print(input)

#torch.Size([2, 5])
# tensor([[ 0.8380, -1.2403, -1.2401, -0.3553,  1.0543],
#         [ 0.0560,  1.0026,  0.0155, -0.2781,  0.2533]])

3. torch.randn(3,2,5)

input = torch.randn(3,2,5)
print(input.shape)
print(input)

#torch.Size([3, 2, 5])
# tensor([[[-0.7493, -2.3638, -1.2277,  0.8436,  0.4134],
#          [-0.6572, -0.2461,  0.4799,  0.3729, -0.0833]],
# 
#         [[ 0.1289,  0.3243, -0.2497, -0.0351, -0.8498],
#          [ 1.0268,  0.1547,  0.1365,  0.2030,  0.2473]],
# 
#         [[-0.1078, -0.1764, -0.4895, -0.4100, -0.5980],
#          [-0.4417,  1.2105, -0.5508, -0.6169, -0.3140]]])

总结:torch.randn是生成0~1的正太分布的随机数。

你可能感兴趣的:(python小技巧,机器学习算法,自己编写元学习,深度学习,python,人工智能,开源)