2、TENSOR ATTRIBUTES

每个 torch.Tensor 都有一个 torch.dtype、torch.device 和 torch.layout。

torch.dtype

CLASStorch.dtype

torch.dtype 是表示 torch.Tensor 的数据类型的对象。 PyTorch 有十二种不同的数据类型:

Data type

dtype

Legacy Constructors

32-bit floating point

torch.float32 or torch.float

torch.*.FloatTensor

64-bit floating point

torch.float64 or torch.double

torch.*.DoubleTensor

64-bit complex

torch.complex64 or torch.cfloat

 

128-bit complex

torch.complex128 or torch.cdouble

 

16-bit floating point 1

torch.float16 or torch.half

torch.*.HalfTensor

16-bit floating point 2

torch.bfloat16

torch.*.BFloat16Tensor

8-bit integer (unsigned)

torch.uint8

torch.*.ByteTensor

8-bit integer (signed)

torch.int8

torch.*.CharTensor

16-bit integer (signed)

torch.int16 or torch.short

torch.*.ShortTensor

32-bit integer (signed)

torch.int32 or torch.int

torch.*.IntTensor

64-bit integer (signed)

torch.int64 or torch.long

torch.*.LongTensor

Boolean

torch.bool

torch.*.BoolTensor

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

2
有时称为脑浮点数:使用 1 个符号、8 个指数和 7 个有效位。 当范围很重要时很有用,因为它具有与 float32 相同数量的指数位。

要确定 torch.dtype 是否是浮点数据类型,可以使用属性 is_floating_point,如果数据类型是浮点数据类型,则返回 True。

要确定 torch.dtype 是否是复杂数据类型,可以使用属性 is_complex,如果数据类型是复杂数据类型,则返回 True。

当算术运算(add、sub、div、mul)的输入数据类型不同时,我们通过找到满足以下规则的最小数据类型来提升:

.如果标量操作数的类型比张量操作数(其中复杂 > 浮点 > 整数 > 布尔)属于更高的类别,我们将提升为具有足够大小以容纳该类别的所有标量操作数的类型。

.如果零维张量操作数的类别高于维度操作数,我们将提升为具有足够大小和类别的类型,以容纳该类别的所有零维张量操作数。

.如果没有更高类别的零维度操作数,我们将提升为具有足够大小和类别的类型以容纳所有维度的操作数。

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

丰富示例:

>>> 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

CLASStorch.device

torch.device 是一个对象,表示在其上分配或将分配 torch.Tensor 的设备。

torch.device 包含设备类型(“cpu”或“cuda”)和设备类型的可选设备序号。 如果设备序号不存在,则该对象将始终代表设备类型的当前设备,即使在调用 torch.cuda.set_device() 之后; 例如,使用设备 'cuda' 构造的 torch.Tensor 等效于 'cuda:X',其中 X 是 torch.cuda.current_device() 的结果。

可以通过 Tensor.device 属性访问 torch.Tensor 的设备。

torch.device 可以通过字符串或通过字符串和设备序数构造。

通过字符串:

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

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

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

通过字符串和设备序号:

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

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

函数中的 torch.device 参数通常可以替换为字符串。 这允许快速的代码原型设计。

>>> # 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')

出于遗留原因,可以通过单个设备序号构建设备,该序号被视为 cuda 设备。这与 Tensor.get_device() 匹配,它返回 cuda 张量的序数,并且不支持 cpu 张量。

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

采用设备的方法通常会接受(正确格式化的)字符串或(传统)整数设备序数,即以下都是等效的:

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

torch.layout

CLASStorch.layout

WARNING:torch.layout 类处于测试阶段,可能会发生变化。

torch.layout 是一个表示 torch.Tensor 内存布局的对象。 目前,我们支持 torch.strided(密集张量)并且对 torch.sparse_coo(稀疏 COO 张量)提供测试版支持。

torch.strided 代表密集张量,是最常用的内存布局。 每个跨步张量都有一个关联的 torch.Storage,它保存着它的数据。 这些张量提供了存储的多维、跨步视图。 步幅是一个整数列表:第 k 个步幅表示从一个元素到 Tensor 第 k 维中的下一个元素所需的内存跳跃。 这个概念使得高效地执行许多张量操作成为可能。

Example:

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

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

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

torch.memory_format

CLASStorch.memory_format

torch.memory_format 是一个对象,表示在其上分配或将分配 torch.Tensor 的内存格式。

可能的值为:

.torch.contiguous_format:张量正在或将在密集的非重叠内存中分配。 步幅由按递减顺序的值表示。

.torch.channels_last:张量正在或将在密集的非重叠内存中分配。步幅由 strides[0] > strides[2] > strides[3] > strides[1] == 1 aka NHWC order 中的值表示。

.torch.preserve_format:用于像 clone 这样的函数来保留输入张量的内存格式。 如果输入张量分配在密集的非重叠内存中,则输出张量步幅将从输入中复制。 否则输出步幅将遵循 torch.contiguous_format。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值