2、TENSOR ATTRIBUTES

2024-08-26 23:48
文章标签 tensor attributes

本文主要是介绍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。

 

这篇关于2、TENSOR ATTRIBUTES的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110030

相关文章

Mindspore 初学教程 - 3. Tensor 张量

张量(Tensor)是一个可用来表示在一些矢量、标量和其他张量之间的线性关系的多线性函数,这些线性关系的基本例子有内积、外积、线性映射以及笛卡儿积。其坐标在  n n n 维空间内,有  n r n^{r} nr 个分量的一种量,其中每个分量都是坐标的函数,而在坐标变换时,这些分量也依照某些规则作线性变换。 r r r 称为该张量的秩或阶(与矩阵的秩和阶均无关系)。 张量是一种特殊的数据结构,

特征选择错误:The classifier does not expose coef_ or feature_importances_ attributes

在利用RFE进行特征筛选的时候出现问题,源代码如下: from sklearn.svm import SVRmodel_SVR = SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verb

七. 部署YOLOv8检测器-load-save-tensor

目录 前言0. 简述1. 案例运行2. 补充说明3. 代码分析3.1 main.cpp3.2 create_data.py 结语下载链接参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战课程》,链接。记录下个人学习笔记,仅供自己参考 本次课程我们来学习课程第六章—部署分类器,一起来学习利用 cnpy 库加载和保存 tensor 课程大纲可以看下面的思维导图

Pytorch:Tensor基本运算【add/sub/mul/div:加减乘除】【mm/matmul:矩阵相乘】【Pow/Sqrt/rsqrt:次方】【近似:floor...】【裁剪:clamp】

一、基本运算:加减乘除 1、乘法 1.1 a * b:element-wise 对应元素相乘 a * b:要求两个矩阵维度完全一致,即两个矩阵对应元素相乘,输出的维度也和原矩阵维度相同 1.2 torch.mul(a, b):element-wise 对应元素相乘 torch.mul(a, b):是矩阵a和b对应位相乘,a和b的维度必须相等,比如a的维度是(1, 2),b的维度是(1,

Pytorch:Tensor的高阶操作【where(按条件取元素)、gather(查表取元素)、scatter_(查表取元素)】【可并行计算,提高速度】

一、where:逐个元素按条件选取【并行计算,速度快】 torch.where(condition,x,y) #condition必须是tensor类型 condition的维度和x,y一致,用1和0分别表示该位置的取值 import torchcond = torch.tensor([[0.6, 0.7],[0.3, 0.6]])a = torch.tensor([[1., 1.],[

Pytorch:Tensor数组运算中的Broadcasting【广播机制】

简单来说,Broadcasting 可以这样理解:如果你有一个 m × n m×n m×n 的矩阵,让它加减乘除一个 1 × n 1×n 1×n 的矩阵,它会被复制 m m m 次,成为一个 m × n m×n m×n 的矩阵,然后再逐元素地进行加减乘除操作。 数组在进行矢量化运算时,要求数组的形状是相等的。当形状不相等的数组执行算术运算的时候,就会出现广播机制,该机制会对数组进行扩

tensorflow:超简单易懂 tensor list的使用 张量数组的使用 扩增 建立 append

构造张量数组: 最简单的方式: tensor_list=[tensor1,tensor2] 常用的方式(这个方式可以用于for循环) tensor_list=[]tensor_list.append(tensor1)tensor_list.append(tensor2) 张量数组的使用 批量处理张量数组里面的张量,之后将其存储到一个新的张量数组中 new_tensor_list

EXO:模型最终验证的地方;infer_tensor;step;MLXDynamicShardInferenceEngine

目录 EXO:模型最终验证的地方 EXO:infer_tensor  EXO:step  MXNet的 mx.array 类型是什么 NDArray优化了什么 1. 异步计算和内存优化 2. 高效的数学和线性代数运算 3. 稀疏数据支持 4. 自动化求导 举例说明 EXO:模型最终验证的地方 EXO:infer_tensor  这段代码定义了一个名为 in

Eigen::Tensor使用,定义高维矩阵

在实际项目中,需要存储大于等于三维的矩阵,而平常中我们使用Eigen::MatrixXd二维数据,这里我们使用Eigen::Tensor来定义 1.Using the Tensor module #include <unsupported/Eigen/CXX11/Tensor> 2.定义矩阵 2.一般矩阵 官方文档 // 定义一个2x3x4大小的矩阵Eigen::Tensor<f

tensor core实现矩阵乘法的详细解读

之前关于tensor core的介绍可以参考链接添加链接描述 基础的tensor core实现C=AB的代码可以参考下面这段内容: 上面代码的几个注意事项: 首先是加载mma.h头文件,这个是包含wmma模板类的头文件。 其次是设置的WMMA_M=16,WMMA_N=16,WMMA_K=8,这三个参数的表示的意思是,对于一个线程块内的一个warp来说,这个线程簇warp一次能处理的是[16,8]