目录

张量属性

每个都有 、 torch.Tensor

torch.d类型

Torch 的 Torch 类DTYPE

A 是表示 .PyTorch 有 12 种不同的数据类型:

数据类型

DTYPE

传统构造函数

32 位浮点

torch.float32torch.float

torch.*.FloatTensor

64 位浮点

torch.float64torch.double

torch.*.DoubleTensor

64 位复杂

torch.complex64torch.cfloat

128 位复杂

torch.complex128torch.cdouble

16 位浮点 1

torch.float16torch.half

torch.*.HalfTensor

16 位浮点 2

torch.bfloat16

torch.*.BFloat16Tensor

8 位整数 (无符号)

torch.uint8

torch.*.ByteTensor

8 位整数 (有符号)

torch.int8

torch.*.CharTensor

16 位整数(有符号)

torch.int16torch.short

torch.*.ShortTensor

32 位整数(有符号)

torch.int32torch.int

torch.*.IntTensor

64 位整数 (有符号)

torch.int64torch.long

torch.*.LongTensor

布尔

torch.bool

torch.*.BoolTensor

1

有时称为 binary16:使用 1 个符号、5 个指数和 10 significand 位。当精度很重要时很有用。

2

有时也称为大脑浮点:使用 1 个符号、8 个指数和 7 significand 位。当 range 很重要时很有用,因为它具有相同的 指数位数为float32

要确定 a 是否为浮点数据类型,可以使用该属性,该属性将返回数据类型是否为浮点数据类型。True

要确定 a 是否为复杂数据类型,可以使用该属性,该属性返回数据类型是否为复杂数据类型。True

当算术运算的输入(addsubdivmul)的 dtypes 不同时,我们提升 通过查找满足以下规则的最小 dtype :

  • 如果标量操作数的类型属于比张量操作数更高的类别 (其中 complex > 浮点型 > 整数> boolean),我们提升为具有足够大小的类型 该类别的所有标量操作数。

  • 如果零维张量操作数的类别高于有量纲操作数,则 我们提升为具有足够大小和类别的类型,以容纳 那个类别。

  • 如果没有更高类别的零维度操作数,我们将提升为具有足够大小的类型 和 category 来保存所有维度操作数。

浮点标量操作数具有 dtype torch.get_default_dtype() 和整数 非布尔标量操作数具有 DTYPE torch.int64。与 numpy 不同,我们不检查 值。量化类型和复杂类型 尚不支持。

促销示例:

>>> float_tensor = torch.ones(1, dtype=torch.float)
>>> double_tensor = torch.ones(1, dtype=torch.double)
>>> complex_float_tensor = torch.ones(1, dtype=torch.complex64)
>>> complex_double_tensor = torch.ones(1, dtype=torch.complex128)
>>> int_tensor = torch.ones(1, dtype=torch.int)
>>> long_tensor = torch.ones(1, dtype=torch.long)
>>> uint_tensor = torch.ones(1, dtype=torch.uint8)
>>> double_tensor = torch.ones(1, dtype=torch.double)
>>> bool_tensor = torch.ones(1, dtype=torch.bool)
# zero-dim tensors
>>> long_zerodim = torch.tensor(1, dtype=torch.long)
>>> int_zerodim = torch.tensor(1, dtype=torch.int)

>>> torch.add(5, 5).dtype
torch.int64
# 5 is an int64, but does not have higher category than int_tensor so is not considered.
>>> (int_tensor + 5).dtype
torch.int32
>>> (int_tensor + long_zerodim).dtype
torch.int32
>>> (long_tensor + int_tensor).dtype
torch.int64
>>> (bool_tensor + long_tensor).dtype
torch.int64
>>> (bool_tensor + uint_tensor).dtype
torch.uint8
>>> (float_tensor + double_tensor).dtype
torch.float64
>>> (complex_float_tensor + complex_double_tensor).dtype
torch.complex128
>>> (bool_tensor + int_tensor).dtype
torch.int32
# Since long is a different kind than float, result dtype only needs to be large enough
# to hold the float.
>>> torch.add(long_tensor, float_tensor).dtype
torch.float32
当指定算术运算的输出张量时,我们允许强制转换为其 dtype,但以下情况除外:
  • 整数输出张量不能接受浮点张量。

  • 布尔输出张量不能接受非布尔张量。

  • 非复数输出张量不能接受复数张量

选角示例:

# allowed:
>>> float_tensor *= float_tensor
>>> float_tensor *= int_tensor
>>> float_tensor *= uint_tensor
>>> float_tensor *= bool_tensor
>>> float_tensor *= double_tensor
>>> int_tensor *= long_tensor
>>> int_tensor *= uint_tensor
>>> uint_tensor *= int_tensor

# disallowed (RuntimeError: result type can't be cast to the desired output type):
>>> int_tensor *= float_tensor
>>> bool_tensor *= int_tensor
>>> bool_tensor *= uint_tensor
>>> float_tensor *= complex_float_tensor

torch.device

Torch 的 Torch 类装置

A 是一个对象,表示 a 所在的设备 或将被分配。

包含设备类型(最常见的是“cpu”或 “cuda”,但也可能是“mps”、“xpu”、“xla”“meta”)和可选 device ordinal (设备类型) 的序号。如果设备序号不存在,则此对象将始终表示 设备类型的当前设备,even after 被调用;例如, a 构造的 device 等价于 X 所在的位置 的结果'cuda''cuda:X'

的设备可通过属性进行访问

A 可以通过字符串或通过字符串和设备序号构造

通过字符串:

>>> torch.device('cuda:0')
device(type='cuda', index=0)

>>> torch.device('cpu')
device(type='cpu')

>>> torch.device('mps')
device(type='mps')

>>> torch.device('cuda')  # current cuda device
device(type='cuda')

通过 string 和 device 序号:

>>> torch.device('cuda', 0)
device(type='cuda', index=0)

>>> torch.device('mps', 0)
device(type='mps', index=0)

>>> torch.device('cpu', 0)
device(type='cpu', index=0)

设备对象还可以用作上下文管理器来更改默认值 device Tensor 分配在:

>>> with torch.device('cuda:1'):
...     r = torch.randn(2, 3)
>>> r.device
device(type='cuda', index=1)

如果将工厂函数传递给显式 非 None 设备参数。要全局更改默认设备,另请参阅

警告

此函数对每个 Python 都会施加轻微的性能成本 调用 torch API(不仅仅是工厂函数)。如果此 正在给您带来问题,请在 https://github.com/pytorch/pytorch/issues/92701 上发表评论

注意

函数中的参数通常可以用字符串替换。 这允许快速构建代码原型。

>>> # Example of a function that takes in a torch.device
>>> cuda1 = torch.device('cuda:1')
>>> torch.randn((2,3), device=cuda1)
>>> # You can substitute the torch.device with a string
>>> torch.randn((2,3), device='cuda:1')

注意

由于遗留原因,可以通过单个设备序号构造设备,该序号被处理 作为当前加速器类型。 这与 匹配 ,后者返回 device 的序号 张量,并且不支持 CPU 张量。

>>> torch.device(1)
device(type='cuda', index=1)

注意

采用 device 的方法通常会接受 (格式正确的) 字符串 或(旧版)整数设备序号,即以下内容都是等效的:

>>> torch.randn((2,3), device=torch.device('cuda:1'))
>>> torch.randn((2,3), device='cuda:1')
>>> torch.randn((2,3), device=1)  # legacy

注意

张量永远不会在设备之间自动移动,需要用户的显式调用。标量张量(使用 tensor.dim()==0)是此规则的唯一例外,它们会在需要时自动从 CPU 传输到 GPU,因为此操作可以“免费”完成。 例:

>>> # two scalars
>>> torch.ones(()) + torch.ones(()).cuda()  # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(()).cuda() + torch.ones(())  # OK, scalar auto-transferred from CPU to GPU
>>> # one scalar (CPU), one vector (GPU)
>>> torch.ones(()) + torch.ones(1).cuda()  # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(1).cuda() + torch.ones(())  # OK, scalar auto-transferred from CPU to GPU
>>> # one scalar (GPU), one vector (CPU)
>>> torch.ones(()).cuda() + torch.ones(1)  # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU
>>> torch.ones(1) + torch.ones(()).cuda()  # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU

torch.layout

Torch 的 Torch 类布局

警告

该课程目前处于测试阶段,可能会发生变化。torch.layout

A 是一个对象,表示 .目前,我们支持 (dense Tensors) 并且具有对(稀疏 COO 张量)的 beta 支持。torch.stridedtorch.sparse_coo

torch.strided表示密集的 Tensor,并且是 是最常用的。每个跨步张量都有一个关联的 ,用于保存其数据。这些张量提供 存储的多维跨步视图。步幅是一个整数列表:第 k 个步幅 表示从一个元素转到 next 在 Tensor 的第 k 维中。这个概念使之成为可能 高效执行许多 Tensor 操作。torch.Storage

例:

>>> x = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)

>>> x.t().stride()
(1, 5)

有关张量的更多信息,请参阅 torch.sparsetorch.sparse_coo

torch.memory_format

Torch 的 Torch 类memory_format

A 是一个对象,表示 a 所在的内存格式 或将被分配。

可能的值为:

  • torch.contiguous_format: Tensor 正在或将被分配到密集的非重叠内存中。步幅由降序中的值表示。

  • torch.channels_last: Tensor 正在或将被分配到密集的非重叠内存中。步幅由又名 NHWC 顺序的值表示。strides[0] > strides[2] > strides[3] > strides[1] == 1

  • torch.channels_last_3d: Tensor 正在或将被分配到密集的非重叠内存中。步幅由以 aka NDHWC 顺序表示的值表示。strides[0] > strides[2] > strides[3] > strides[4] > strides[1] == 1

  • torch.preserve_format: 在 clone 等函数中使用,以保留输入张量的内存格式。如果 input tensor 为 分配在密集的非重叠内存中,将从 Input 中复制 output Tensor strides。 否则,输出步幅将跟随torch.contiguous_format

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源