Category:PyTorch
Article From:https://www.cnblogs.com/logicalsky/p/9122437.html

torch.randn

  • torch.randn(*sizes, out=None) → Tensor(Tensor)
  • A tensor is returned, which contains a set of random numbers extracted from the standard normal distribution (mean 0, variance 1), whose shape is defined by variable parameter sizes. Parameters:

    • sizes (int…) – An integer sequence that defines the shape of the output
    • out (Tensor, optinal) – Result tensor

2-D”

>>> import torch
>>> torch.randn(2,3)
tensor([[-1.0413,  0.8792,  2.1381],
           [ 0.9541, -2.3019,  0.5490]])
>>>

three dimensional”

>>> torch.randn(2,2,3)
tensor([[[ 0.4200,  0.4624,  0.3099],
            [-0.1227,  0.2452,  0.9840]],

           [[-0.8800, -0.5937, -1.4465],
            [ 1.6523, -0.0170, -0.6393]]])
>>>
  • Three dimensions based on two-dimensional increase of one dimension, that is, (2, 2, 3) is a matrix containing 2 2 rows and three columns; the first number refers to the inclusion of several two-dimensional matrices.

>>> torch.randn(2,2,2,3)
tensor([[[[-1.1649, -1.1810, -0.3619],
             [-0.8433, -0.4411,  1.8187]],

            [[ 0.4896,  0.4773,  0.0032],
             [ 1.1269,  1.3638,  1.4495]]],

          [[[-0.1959,  0.5646,  0.7001],
            [ 0.6796,  0.1164,  1.6833]],

           [[-0.2674, -0.2411,  1.5875],
            [-0.2804,  1.4775,  0.2448]]]])
>>>
  • The four dimension adds one dimension, (2, 2, 2, 3) based on three dimensions, that is, 2 three-dimensional matrices.
>>> a = torch.randn(2,2,2,3)
>>> torch.numel(a)             # Calculate the number of elements in tensor, that is, the number of elements in the matrix.Twenty-four
  • If n dimensional, and so on, based on the upper one dimension increase one dimension calculation.

Leave a Reply

Your email address will not be published. Required fields are marked *