1.randn_like()
torch.randn_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
返回一个和输入大小相同的张量,其由均值为0、方差为1的标准正态分布填充。即
torch.randn_like(input)
等价于 torch.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
dtype(torch.dtype, optional)期望返回张量的类型,默认为input的类型
layout(torch.layout, optional)期望返回张量的分布,默认为input的分布
device(torch.device, optional)期望返回张量使用的设备,默认与input相同
requires_grad(bool, optional)如果需要在返回张量上保存梯度,默认为False
memory_format(torch.memory_format, optional)期望返回张量的保存类型,默认为torch.preserve_format
2.torch.layout
torch.layout
表示 torch.Tensor
的内存分布。当前PyTorch支持 torch.strided
和 torch.sparse_coo
(PyTorch1.8 测试支持)。
torch.strided
表示稠密张量,是最常见的内存分布方式。每一个张量和一个用来存放数据的 torch.Storage
向关联。
Example:
>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)
>>> x.t().stride()
(1, 5)
3.torch.memory_format
torch.memory_format
用来表示 torch.Tensor
储存格式
可取值:
torch.contiguous_format:张量会被分配在稠密不重叠的内存中。其中strides显示降序。
torch.channels_last:张量会被分配在稠密不重叠的内存中。其中strides排序为strides[0] > strides[2] > strides[3] > strides[1] == 1
torch.preserve_format:保留输入张量的内存格式,用在clone这样的函数中。如果输入张量是分配在稠密不重叠的内存中,那么输出张量的stride与输入相同。否则,输出的strides使用torch.contiguous_format
Original: https://blog.csdn.net/lzn025/article/details/114686880
Author: lzn1251
Title: PyTorch学习笔记(2)——randn_like()、layout、memory_format
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/711932/
转载文章受原作者版权保护。转载请注明原作者出处!