import torch

torch.rand 0-1均匀分布

"""
Docstring:
rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

Returns a tensor filled with random numbers from a uniform distribution
on the interval :math:`[0, 1)`

The shape of the tensor is defined by the variable argument :attr:`size`.

Args:
    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.

Keyword args:
    out (Tensor, optional): the output tensor.
    dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
        Default: if ``None``, uses a global default (see :func:`torch.set_default_tensor_type`).
    layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.
        Default: ``torch.strided``.
    device (:class:`torch.device`, optional): the desired device of returned tensor.
        Default: if ``None``, uses the current device for the default tensor type
        (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU
        for CPU tensor types and the current CUDA device for CUDA tensor types.
    requires_grad (bool, optional): If autograd should record operations on the
        returned tensor. Default: ``False``.

Example::

    >>> torch.rand(4)
    tensor([ 0.5204,  0.2503,  0.3525,  0.5673])
    >>> torch.rand(2, 3)
    tensor([[ 0.8237,  0.5781,  0.6879],
            [ 0.3816,  0.7249,  0.0998]])
Type:      builtin_function_or_method
"""

# [0, 1)之间的均匀分布
pass
torch.rand([2, 3])
tensor([[0.7589, 0.2792, 0.2308],
        [0.1568, 0.1513, 0.7230]])
torch.empty([2, 3]).uniform_(0, 1)
tensor([[0.5647, 0.7497, 0.1139],
        [0.1436, 0.9677, 0.3738]])

torch.randn 标准正态分布

"""
Docstring:
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).

.. math::
    \text{out}_{i} \sim \mathcal{N}(0, 1)

The shape of the tensor is defined by the variable argument :attr:`size`.

Args:
    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.

Keyword args:
    out (Tensor, optional): the output tensor.
    dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
        Default: if ``None``, uses a global default (see :func:`torch.set_default_tensor_type`).
    layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.
        Default: ``torch.strided``.
    device (:class:`torch.device`, optional): the desired device of returned tensor.
        Default: if ``None``, uses the current device for the default tensor type
        (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU
        for CPU tensor types and the current CUDA device for CUDA tensor types.
    requires_grad (bool, optional): If autograd should record operations on the
        returned tensor. Default: ``False``.

Example::

    >>> torch.randn(4)
    tensor([-2.1436,  0.9966,  2.3426, -0.6366])
    >>> torch.randn(2, 3)
    tensor([[ 1.5954,  2.8929, -1.0923],
            [ 1.1719, -0.4709, -0.1996]])
Type:      builtin_function_or_method
"""

# 标准正态分布
torch.randn([2, 3])
tensor([[ 1.0449,  0.9979,  0.1418],
        [-2.3884, -0.8895,  0.9523]])

torch.randint 区间整数随机分布

"""
Docstring:
randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

Returns a tensor filled with random integers generated uniformly
between :attr:`low` (inclusive) and :attr:`high` (exclusive).

The shape of the tensor is defined by the variable argument :attr:`size`.

.. note:
    With the global dtype default (``torch.float32``), this function returns
    a tensor with dtype ``torch.int64``.

Args:
    low (int, optional): Lowest integer to be drawn from the distribution. Default: 0.
    high (int): One above the highest integer to be drawn from the distribution.
    size (tuple): a tuple defining the shape of the output tensor.

Keyword args:
    generator (:class:`torch.Generator`, optional): a pseudorandom number generator for sampling
    out (Tensor, optional): the output tensor.
    dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
        Default: if ``None``, uses a global default (see :func:`torch.set_default_tensor_type`).
    layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.
        Default: ``torch.strided``.
    device (:class:`torch.device`, optional): the desired device of returned tensor.
        Default: if ``None``, uses the current device for the default tensor type
        (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU
        for CPU tensor types and the current CUDA device for CUDA tensor types.
    requires_grad (bool, optional): If autograd should record operations on the
        returned tensor. Default: ``False``.

Example::

    >>> torch.randint(3, 5, (3,))
    tensor([4, 3, 4])


    >>> torch.randint(10, (2, 2))
    tensor([[0, 2],
            [5, 5]])


    >>> torch.randint(3, 10, (2, 2))
    tensor([[4, 5],
            [6, 7]])
Type:      builtin_function_or_method
"""
# [low, high)随机整数
torch.randint(low=0, high=10, size=[2, 3])
tensor([[3, 8, 8],
        [7, 9, 1]])

torch.bernoulli 从伯努利分布中提取二进制随机数(0或1)

Docstring:
bernoulli(input, *, generator=None, out=None) -> Tensor

Draws binary random numbers (0 or 1) from a Bernoulli distribution.

The :attr:input tensor should be a tensor containing probabilities
to be used for drawing the binary random number.
Hence, all values in :attr:input have to be in the range:
:math:0 \leq \text{input}_i \leq 1.

The :math:\text{i}^{th} element of the output tensor will draw a
value :math:1 according to the :math:\text{i}^{th} probability value given
in :attr:input.

.. math::
\text{out}{i} \sim \mathrm{Bernoulli}(p = \text{input})

The returned :attr:out tensor only has values 0 or 1 and is of the same
shape as :attr:input.

:attr:out can have integral dtype, but :attr:input must have floating
point dtype.

Args:
input (Tensor): the input tensor of probability values for the Bernoulli distribution

Keyword args:
generator (:class:torch.Generator, optional): a pseudorandom number generator for sampling
out (Tensor, optional): the output tensor.

Example::

>>> a = torch.empty(3, 3).uniform_(0, 1)  # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737,  0.0950,  0.3609],
        [ 0.7148,  0.0289,  0.2676],
        [ 0.9456,  0.8937,  0.7202]])
>>> torch.bernoulli(a)
tensor([[ 1.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  1.,  1.]])

>>> a = torch.ones(3, 3) # probability of drawing "1" is 1
>>> torch.bernoulli(a)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
>>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
>>> torch.bernoulli(a)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

Type: builtin_function_or_method

torch.ones(3,3).uniform_(0, 1)
tensor([[0.4097, 0.6395, 0.4441],
        [0.0644, 0.9323, 0.7877],
        [0.6428, 0.8769, 0.1398]])
torch.rand([3, 3]).uniform_(0,1)
tensor([[0.4031, 0.0478, 0.7641],
        [0.2450, 0.5245, 0.2092],
        [0.4752, 0.0585, 0.9410]])
torch.bernoulli(torch.full([1, 20], 0.8))
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.,
         0., 1.]])
a = torch.tensor([[1.0, 3.3, 6.0],[2,3,6.4]])
a
tensor([[1.0000, 3.3000, 6.0000],
        [2.0000, 3.0000, 6.4000]])
aa = a.numpy()
aa[0]
array([1. , 3.3, 6. ], dtype=float32)
aa[1]
array([2. , 3. , 6.4], dtype=float32)
aa[0] < aa[1]
array([ True, False,  True])
a[0] < a[1]
tensor([ True, False,  True])

Share on: TwitterFacebookEmail

Comments


Related Posts


Published

Category

Programming

Tags

Contact