torch.randn()参数size与输出张量形状详解

torch.randn()参数size与输出张量形状详解

torch.randn(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution). out_i ∼ N(0,1), The shape of the tensor is defined by the variable argument size.

Parameters
size (int…) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

查看torch.randn()官方文档介绍,实际上torch.randn()返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。

好像并没有说清楚,参数size怎么作用的,这里给大家演示一下,注意size输入为一系列的正整数:
当size是1个数时,返回一个长度为4的正态分布张量

torch.randn(4)
tensor([ 1.3186, -0.8001, -0.2989,  0.6381])

当size是2个数时,返回一个2行3列的张量,最小元素的每一行服从正态分布

torch.randn(2, 3)
tensor([[ 1.3435, -1.2493, -0.8758],
        [ 1.1435, -1.5320, -0.9512]])

当size是3个数时,返回一个2行1列的张量,其中每个元素又是一个3行4列的张量,最小元素的每一行服从正态分布

torch.randn(2, 3, 4)
tensor([[[-2.0315,  0.1029,  0.4129, -0.3980],
         [-0.5129, -0.4984, -0.5934,  0.6140],
         [

你可能感兴趣的:(人工智能,深度学习,pytorch,神经网络)