【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

相关文章

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Pytorch介绍与安装过程

《Pytorch介绍与安装过程》PyTorch因其直观的设计、卓越的灵活性以及强大的动态计算图功能,迅速在学术界和工业界获得了广泛认可,成为当前深度学习研究和开发的主流工具之一,本文给大家介绍Pyto... 目录1、Pytorch介绍1.1、核心理念1.2、核心组件与功能1.3、适用场景与优势总结1.4、优

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不