torch.rand&torch.randn介绍

1.torch.rand介绍

从[0,1]均匀分布中随机抽取数据,返回张量

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

1. 1 descreption描述

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)
The shape of the tensor is defined by the variable argument size.
返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义

1.2 Keyword Arguments参数介绍

  • out (Tensor, optional) – the output tensor. 输出一个张量
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()). 返回张量所需的数据类型,可选
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.返回布局,默认为:strided
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. 返回张量的所需装置。Default:如果为None,则使用默认张量类型的当前设备(参见torch.set_default_tensor_type())。device将是用于CPU张量类型的CPU和用于CUDA张量类型的当前CUDA设备
  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.如果autograd应该记录对返回张量的操作。默认值:False。

1.3 example举例:

输入:

import torch
torch.rand(2,3)

输出:

tensor([[0.7464, 0.8926, 0.7526],
        [0.0360, 0.0133, 0.4858]])

注:从[0,1)均匀分布中随机抽取6个数据,组成一个2行3列的张量

2.torch.randn 介绍

返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义

2.1 descreption描述

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution)返回一个张量,其中包含均值为0,方差为1的正态分布的随机数(也称为标准正态分布)

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

2.2 Keyword Arguments参数介绍

  • out (Tensor, optional) – the output tensor.输出一个张量

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).返回张量所需的数据类型,可选

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.返回布局,默认为:strided

  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). 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.默认不记录梯度(FALSE),

2.3example 举例:

输入:

import torch
torch.randn(2,3)

输出:

tensor([[ 0.0816, -0.0611,  0.4068],
        [ 0.3514, -0.1440, -0.7806]])

注:返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义

  • 11
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值