❖PyTorch中的随机相关的API

uniform distribution

>>> help(torch.rand)

    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:`sizes`.
    
    Args:
        sizes (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.
        {out}
        {dtype}
        {layout}
        {device}
        {requires_grad}
    
    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]])

normal distribution

>>> help(torch.randn)

fault_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]])

torch.randperm

torch.randperm(n, out=None) → LongTensor

给定参数n,返回一个从0n -1的随机整数排列。

随机种子

torch.manual_seed

>>> torch.manual_seed(5)
<torch._C.Generator object at 0x111d589b0>
>>> torch.initial_seed()
5

torch.normal

>>> help(torch.normal)
 mean (float, optional): the mean for all distributions
 std (Tensor): the tensor of per-element standard deviations
 out (Tensor, optional): the output tensor
 
 Example::
 
 >>> torch.normal(mean=0.5, std=torch.arange(1., 6.))
 tensor([-1.2793, -1.0732, -2.0687, 5.1177, -1.2303])
 
 .. function:: normal(mean, std=1.0, out=None) -> Tensor
 
 Similar to the function above, but the standard-deviations are shared among
 all drawn elements.
 
 Args:
 mean (Tensor): the tensor of per-element means
 std (float, optional): the standard deviation for all distributions
 out (Tensor, optional): the output tensor
 
 Example::
 
 >>> torch.normal(mean=torch.arange(1., 6.))
 tensor([ 1.1552, 2.6148, 2.6535, 5.8318, 4.2361])

    原文作者:胡今朝
    原文地址: https://zhuanlan.zhihu.com/p/52334890
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞