Python – Pytorch randn() 方法
PyTorch torch.randn()返回由可变参数大小定义的张量(定义输出张量形状的整数序列),其中包含来自标准正态分布的随机数。
Syntax: torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
Parameters:
- size: sequence of integers defining the size of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
- out: (optional) output tensor.
- dtype: (optional) data type of output tensor.
- layout: (optional) the desired layout of returned Tensor. Default value is torch.strided.
- 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: (optional) if set to true, autograd records operation on the output tensor.
Return: tensor filled with values from standard normal distribution.
让我们通过几个例子来看看这个概念:
示例 1:
Python
# import pytorch library
import torch
# create a tensor of size 2 x 4
input_var = torch.randn(2,4)
print (input_var)
Python3
# import Pytorch library
import torch
# create a 3-dimensional tensor
# of 4 x 5
input_var = torch.randn(3, 4, 5,
requires_grad = True)
print(input_var)
Python3
# import Pytorch library
import torch
# error occur
input_var = torch.randn(3.0, 4.0, 5.0,
requires_grad = True)
print(input_var)
输出:
tensor([[-1.4313, -0.3831, -0.8356, -1.5555],
[-1.2749, -1.1872, -0.4983, 0.1029]])
这将返回一个大小为 2 × 4 的张量,其中填充了来自标准正态分布的值,即均值为 0,方差为 1。
示例 2:
Python3
# import Pytorch library
import torch
# create a 3-dimensional tensor
# of 4 x 5
input_var = torch.randn(3, 4, 5,
requires_grad = True)
print(input_var)
输出:
tensor([[[-0.1097, 1.6845, 0.9375, -1.0515, 0.5767],
[ 0.1924, -0.7736, -0.7102, -0.2654, 0.3118],
[-0.5314, 0.1924, -1.1629, 0.2360, 0.8605],
[-0.8036, -0.0695, -0.6062, 1.4872, 0.5455]],
[[ 1.5699, -0.7190, 1.0925, 0.8463, -0.1906],
[-0.0763, -0.6819, -1.0517, -0.5087, -1.4451],
[-2.0127, 1.0061, 0.5723, -0.1336, -0.3821],
[ 0.0868, 1.1556, 0.3842, -0.4168, -1.4604]],
[[ 0.1368, -1.6240, -0.1875, -0.5964, 0.9352],
[ 0.4429, 0.2843, -1.2151, 1.3456, -0.4539],
[-0.4528, 1.9981, -1.2007, 0.0071, -0.0239],
[-0.1003, 0.7938, -0.0977, -1.4097, 0.1679]]], requires_grad=True)
这将返回一个大小为 3 × 4 × 5 的张量,其中填充了随机数,执行时还会记录梯度值。
示例 3:
Python3
# import Pytorch library
import torch
# error occur
input_var = torch.randn(3.0, 4.0, 5.0,
requires_grad = True)
print(input_var)
输出:
TypeError Traceback (most recent call last)
in
1 # import Pytorch library
2 import torch
—-> 3 input = torch.randn(3.0, 4.0, 5.0,requires_grad=True)
4 print( input )
TypeError: randn() received an invalid combination of arguments – got (float, float, float, requires_grad=bool), but expected one of:
* (tuple of ints size, *, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
* (tuple of ints size, *, torch.Generator generator, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
* (tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
* (tuple of ints size, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
size 参数不能采用浮点数,因此会产生错误。