文章目录
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定义