【PyTorch】PyTorch之Tensors属性篇

本文详细介绍了PyTorch库中的Tensors功能,包括检测对象是否为Tensor、处理复杂和浮点数据类型、张量非零检查、设置默认数据类型和设备、创建张量数量等,以及控制打印选项和处理非规范化浮点数的方法。
摘要由CSDN通过智能技术生成


前言

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)
  • 29
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值