【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)
相关推荐
续亮~21 分钟前
智能体代理模式(Agent Agentic Patterns)深度解析
人工智能·ai·代理模式
田辛 | 田豆芽23 分钟前
【人工智能】大语言模型多义词解析技术揭秘——以“项目“歧义消解为例
人工智能·语言模型·自然语言处理
乌旭29 分钟前
AI芯片混战:GPU vs TPU vs NPU的算力与能效博弈
人工智能·pytorch·python·深度学习·机器学习·ai·ai编程
Jamence1 小时前
多模态大语言模型arxiv论文略读(十一)
人工智能·语言模型·自然语言处理
MinggeQingchun1 小时前
Python - 爬虫-网页抓取数据-库requests
爬虫·python·requests
weixin_457885821 小时前
DeepSeek与搜索引擎:AI生成内容如何突破“语义天花板”
人工智能·搜索引擎·ai·deepseek
拓端研究室TRL2 小时前
Python贝叶斯回归、强化学习分析医疗健康数据拟合截断删失数据与参数估计3实例
开发语言·人工智能·python·数据挖掘·回归
国科安芯2 小时前
高安全等级车规芯片在星载控制终端上的应用
人工智能·嵌入式硬件·物联网·架构·汽车
Direct_Yang2 小时前
如何使用 DeepSeek 帮助自己的工作?
人工智能