张量属性¶
torch.d类型¶
- 类 Torch 的 Torch 类DTYPE¶
数据类型 |
DTYPE |
传统构造函数 |
---|---|---|
32 位浮点 |
|
|
64 位浮点 |
|
|
64 位复杂 |
|
|
128 位复杂 |
|
|
16 位浮点 1 |
|
|
16 位浮点 2 |
|
|
8 位整数 (无符号) |
|
|
8 位整数 (有符号) |
|
|
16 位整数(有符号) |
|
|
32 位整数(有符号) |
|
|
64 位整数 (有符号) |
|
|
布尔 |
|
|
- 1
有时称为 binary16:使用 1 个符号、5 个指数和 10 significand 位。当精度很重要时很有用。
- 2
有时也称为大脑浮点:使用 1 个符号、8 个指数和 7 significand 位。当 range 很重要时很有用,因为它具有相同的 指数位数为
float32
要确定 a 是否为浮点数据类型,可以使用该属性,该属性
将返回数据类型是否为浮点数据类型。
True
要确定 a 是否为复杂数据类型,可以使用该属性,该属性
返回数据类型是否为复杂数据类型。
True
当算术运算的输入(add、sub、div、mul)的 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 类装置¶
它包含设备类型(最常见的是“cpu”或
“cuda”,但也可能是“mps”、“xpu”、“xla”或“meta”)和可选
device ordinal (设备类型) 的序号。如果设备序号不存在,则此对象将始终表示
设备类型的当前设备,even after
被调用;例如,
a
构造的 device 等价于 X 所在的位置
的结果
。
'cuda'
'cuda:X'
的设备可通过属性进行访问
。
通过字符串:
>>> 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.strided
torch.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.sparse。torch.sparse_coo
torch.memory_format¶
- 类 Torch 的 Torch 类memory_format¶
可能的值为:
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