pytorch torch.empty()函数(返回填充有未初始化数据的张量。 张量的形状由可变的参数大小定义)

本文介绍PyTorch中使用torch.empty()函数创建形状由sizes参数定义且包含未初始化数据的张量的方法。文章详细解释了函数的参数,包括sizes、out、dtype、layout、device、requires_grad和pin_memory,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from https://pytorch.org/docs/1.1.0/torch.html#torch.empty

torch.empty(*sizes, out=None, dtype=None, layout=torch.strided,
 device=None, requires_grad=False, pin_memory=False) → Tensor

Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument sizes.
返回填充有未初始化数据的张量。 张量的形状由可变的参数大小定义。

Parameters

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 (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()).
返回张量的所需数据类型。 
默认值:如果为None,则使用全局默认值(请参阅torch.set_default_tensor_type())。

layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
返回的Tensor的所需布局。 默认值:torch.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.
返回张量的所需设备。 
默认值:如果为None,则使用当前设备作为默认张量类型(请参见torch.set_default_tensor_type())。 
对于CPU张量类型,设备将是CPU;对于CUDA张量类型,设备将是当前CUDA设备。

requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
如果autograd应该在返回的张量上记录操作。 默认值:False。

pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.
如果设置,返回的张量将在固定的内存中分配。 
仅适用于CPU张量。 默认值:False

example

>>> torch.empty(2, 3)
tensor(1.00000e-08 *
       [[ 6.3984,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000]])
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值