【PyTorch】PyTorch之Tensors属性篇

文章目录


前言

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。

python 复制代码
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。

python 复制代码
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 上进行评论。

python 复制代码
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。

python 复制代码
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.

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

python 复制代码
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。

python 复制代码
# 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 架构上受支持。

python 复制代码
torch.set_flush_denormal(True)
torch.tensor([1e-323], dtype=torch.float64)
torch.set_flush_denormal(False)
torch.tensor([1e-323], dtype=torch.float64)
相关推荐
nightunderblackcat3 分钟前
新手向:Python网络编程,搭建简易HTTP服务器
网络·python·http
李昊哲小课6 分钟前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas
C嘎嘎嵌入式开发27 分钟前
python之set详谈
开发语言·python
whabc1001 小时前
和鲸社区深度学习基础训练营2025年关卡2(2)sklearn中的MLPClassifier
人工智能·深度学习·numpy
之歆1 小时前
Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
python·学习·正则表达式
往日情怀酿做酒 V17639296381 小时前
pytorch的介绍以及张量的创建
人工智能·pytorch·python
北辰alk1 小时前
如何实现AI多轮对话功能及解决对话记忆持久化问题
人工智能
智驱力人工智能1 小时前
极端高温下的智慧出行:危险检测与救援
人工智能·算法·安全·行为识别·智能巡航·高温预警·高温监测
Leo.yuan2 小时前
数据分析师如何构建自己的底层逻辑?
大数据·数据仓库·人工智能·数据挖掘·数据分析
笑稀了的野生俊2 小时前
ImportError: /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.32‘ not found
linux·人工智能·ubuntu·大模型·glibc·flash-attn