【PyTorch】PyTorch之Tensors属性篇

2024-01-19 11:12
文章标签 属性 pytorch tensors

本文主要是介绍【PyTorch】PyTorch之Tensors属性篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、Tensors
    • 1、is_tensor
    • 2、is_storage
    • 3、is_complex
    • 4、is_conj
    • 5、is_floating_point
    • 6、is_nonzero
    • 7、set_default_dtype
    • 8、get_default_dtype
    • 9、set_default_device
    • 10、set_default_tensor_type
    • 11、torch.numel
    • 12、set_printoptions
    • 13、set_flush_denormal


前言

Torch包含用于多维tensors的数据结构,并定义了对这些tensor进行数学运算的操作。此外,还提供了许多工具用于张量的高效序列化和任意类型转换,以及其他实用的工具。
另外,它还有一个CUDA(Computer Unified Device Architecture)组件,使得能够再具有计算能力>=3.0的NVIDIA GPU上运行Tesnor计算。

一、Tensors

1、is_tensor

torch.is_tensor(obj)
Parameters:
obj (Object) – Object to test

如果obj是一个Pytorch Tensor,则返回True。该方法只是简单的做isinstance(obj, Tensor),推荐使用isinstance(obj, Tensor)都代替is_tensor。

x = torch.tensor([1, 2, 3])
print(torch.is_tensor(x)

2、is_storage

torch.is_storage(obj)
Parameters:
obj (Object) – Object to test

如果obj是一个Pytorch的储存对象,则返回True。

3、is_complex

torch.is_complex(input)
Parameters:
input (Tensor) – the input tensor.

如果input的数据类型是复数数据类型,则返回True。即,torch.complex64和torch.complex128。

4、is_conj

torch.is_conj(input)
Parameters:
input (Tensor) – the input tensor.

如果输入是一个共轭张量,即其共轭位被设置为True,则返回True。

5、is_floating_point

torch.is_floating_point(input)
Parameters:
input (Tensor) – the input tensor.

如果输入的数据类型是浮点数数据类型,即 torch.float64、torch.float32、torch.float16 和 torch.bfloat16 中的一种,则返回 True。

6、is_nonzero

torch.is_nonzero(input)
Parameters
input (Tensor) – the input tensor.

如果输入是一个经过类型转换后不等于零的单元素张量,即不等于 torch.tensor([0.])、torch.tensor([0]) 或 torch.tensor([False]),则返回 True。如果 torch.numel() != 1(即使在稀疏张量的情况下也是如此),则抛出 RuntimeError。

7、set_default_dtype

torch.set_default_dtype(d)
Parameters:
d (torch.dtype) – the floating point dtype to make the default. Either torch.float32 or torch.float64.

将数据设置成默认的浮点类型。支持 torch.float32 和 torch.float64。其他数据类型可能会被接受但不会有任何提示,不受支持且可能无法按预期工作。
在 PyTorch 初始化时,其默认浮点数数据类型是 torch.float32,set_default_dtype(torch.float64) 的目的是为了实现类似 NumPy 的类型推断。默认浮点数数据类型用于:

1、隐式确定默认复数数据类型。当默认浮点数类型为 float32 时,默认复数数据类型为 complex64,当默认浮点数类型为 float64 时,默认复数数据类型为 complex128。
2、推断使用 Python 浮点数或复数 Python 数字构建的张量的数据类型。
3、确定在布尔张量和整数张量以及Python浮点数和复数Python数字之间的类型提升的结果。

8、get_default_dtype

获取当前默认的浮点torch.dtype。

torch.get_default_dtype()  # initial default for floating point is torch.float32
torch.set_default_dtype(torch.float64)
torch.get_default_dtype()  # default is now changed to torch.float64
torch.set_default_tensor_type(torch.FloatTensor)  # setting tensor type also affects this
torch.get_default_dtype()  # changed to torch.float32, the dtype for torch.FloatTensor

9、set_default_device

torch.set_default_device(device)
Parameters:
device (device or string) – the device to set as default

将默认的 torch.Tensor 分配到设备上。这不会影响使用显式设备参数调用的工厂函数调用。工厂调用将被执行,就好像它们被传递了设备作为参数一样。

为了只在局部改变默认设备而不是全局设置它,可以使用 with torch.device(device):。

默认设备最初是 cpu。如果将默认张量设备设置为另一个设备(例如,cuda)而没有设备索引,张量将被分配到该设备类型的当前设备上,即使调用了 torch.cuda.set_device() 之后也是如此。
此函数会对每个 Python 调用 torch API(不仅仅是工厂函数)造成轻微的性能开销。如果这对您造成了问题,请在 https://github.com/pytorch/pytorch/issues/92701 上进行评论。

torch.tensor([1.2, 3]).device
torch.set_default_device('cuda')  # current device is 0
torch.tensor([1.2, 3]).device
torch.set_default_device('cuda:1')
torch.tensor([1.2, 3]).device

10、set_default_tensor_type

torch.set_default_tensor_type(t)
Parameters
t (type or string) – the floating point tensor type or its name

将默认的 torch.Tensor 类型设置为浮点数张量类型 t。该类型也将用作 torch.tensor() 中类型推断的默认浮点数类型。

默认的浮点数张量类型最初是 torch.FloatTensor。

torch.tensor([1.2, 3]).dtype    # initial default for floating point is torch.float32
torch.set_default_tensor_type(torch.DoubleTensor)
torch.tensor([1.2, 3]).dtype    # a new floating point tensor

11、torch.numel

torch.numel(input) → int
Parameters
input (Tensor) – the input tensor.

返回输入张量中元素的总数。

a = torch.randn(1, 2, 3, 4, 5)
torch.numel(a)
a = torch.zeros(4,4)
torch.numel(a)

12、set_printoptions

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)
Parameters
precision – Number of digits of precision for floating point output (default = 4).
threshold – Total number of array elements which trigger summarization rather than full repr (default = 1000).
edgeitems – Number of array items in summary at beginning and end of each dimension (default = 3).
linewidth – The number of characters per line for the purpose of inserting line breaks (default = 80). Thresholded matrices will ignore this parameter.
profile – Sane defaults for pretty printing. Can override with any of the above options. (any one of default, short, full)
sci_mode – Enable (True) or disable (False) scientific notation. If None (default) is specified, the value is defined by torch._tensor_str._Formatter. This value is automatically chosen by the framework.

打印内容参数设置。借鉴来自numpy。

# Limit the precision of elements
torch.set_printoptions(precision=2)
torch.tensor([1.12345])
# Limit the number of elements shown
torch.set_printoptions(threshold=5)
torch.arange(10)
# Restore defaults
torch.set_printoptions(profile='default')
torch.tensor([1.12345])
torch.arange(10)

13、set_flush_denormal

torch.set_flush_denormal(mode) → bool
Parameters
mode (bool) – Controls whether to enable flush denormal mode or not

禁用 CPU 上的非规格化浮点数。如果您的系统支持清除非规格化数并且成功配置了清除非规格化模式,则返回 True。set_flush_denormal() 仅在支持 SSE3 的 x86 架构上受支持。

torch.set_flush_denormal(True)
torch.tensor([1e-323], dtype=torch.float64)
torch.set_flush_denormal(False)
torch.tensor([1e-323], dtype=torch.float64)

这篇关于【PyTorch】PyTorch之Tensors属性篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

pytorch之torch.flatten()和torch.nn.Flatten()的用法

《pytorch之torch.flatten()和torch.nn.Flatten()的用法》:本文主要介绍pytorch之torch.flatten()和torch.nn.Flatten()的用... 目录torch.flatten()和torch.nn.Flatten()的用法下面举例说明总结torch

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

pytorch+torchvision+python版本对应及环境安装

《pytorch+torchvision+python版本对应及环境安装》本文主要介绍了pytorch+torchvision+python版本对应及环境安装,安装过程中需要注意Numpy版本的降级,... 目录一、版本对应二、安装命令(pip)1. 版本2. 安装全过程3. 命令相关解释参考文章一、版本对

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p