1.Pytorch基础模块torch的API之Tensors实例详解

0. torch

torch包含多维张量的数据结构,并定义了对这些张量的数学运算。

import torch

1. Tensors

1.1 torch.is_tensor(obj)

判断obj是否为tensor,是返回True,否返回False

a = torch.randn(2,3) #定义一个标准正态分布(均值为0,方差为1)的随机数张量
b = 6
print(a)
print(torch.is_tensor(a))
print(torch.is_tensor(b))
tensor([[-0.7868,  1.1948, -0.6921],
        [ 0.7707,  0.4043,  0.5887]])
True
False
1.2 torch.is_storage(obj)

判断obj是否为storage,是返回True,否返回False

Pytorch中Tensor是头信息区,主要存储tensor的shape,size,stride等,真正的数据为连续数组,存放在Storage存储区

a = torch.rand(2,3) #定义一个在区间[0,1]的均匀分布随机数的张量
b = torch.FloatStorage([1,2,3,4,5,6]) #还有ByteStorage,ShortStorage,IntStorage,LongStorage,DoubleStorage
print(a)
print(a.storage())
print(torch.is_storage(a))
print(torch.is_storage(b))
tensor([[0.8862, 0.5079, 0.8108],
        [0.2631, 0.4686, 0.3212]])
 0.8861961960792542
 0.507926881313324
 0.8107842206954956
 0.2631419897079468
 0.4686075448989868
 0.3212276101112366
[torch.FloatStorage of size 6]
False
True
1.3 torch.is_complex(input)

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

a = torch.tensor([2, 4], dtype = torch.float32)
b = torch.tensor([3, 6], dtype = torch.float32)
c = torch.complex(a, b)
print(c)
print(torch.is_complex(c))
tensor([2.+3.j, 4.+6.j])
True

如果出现“module ‘torch’ has no attribute ‘complex’”,可能是因为torch版本太低,pytorch1.7以上才有这个接口

a = torch.tensor([2, 4], dtype = torch.float32)
b = torch.tensor([3, 6], dtype = torch.float32)
c = torch.complex(a, b)
print(c)
print(torch.is_complex(c))
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-34-f835d65fac63> in <module>
      1 a = torch.tensor([2, 4], dtype = torch.float32)
      2 b = torch.tensor([3, 6], dtype = torch.float32)
----> 3 c = torch.complex(a, b)
      4 print(c)
      5 print(c.dytpe)


AttributeError: module 'torch' has no attribute 'complex'
1.4 torch.is_floating_point(input)

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

a = torch.tensor([3, 6], dtype = torch.float64)
print(a)
print(torch.is_floating_point(a))
tensor([3., 6.])
True
1.5 torch.is_nonzero(input)

如果输入时类型转换后不等于零的单个元素张量,则返回True,即不等于torch.tensor([0.]),或torch.tensor([0])或troch.tensor([False])

print(torch.is_nonzero(torch.tensor([0.])))
print(torch.is_nonzero(torch.tensor([1.5])))
print(torch.is_nonzero(torch.tensor([False])))
print(torch.is_nonzero(torch.tensor([3])))
False
True
False
True
1.6 torch.set_default_dtype(d)

将默认浮点dtype设置为d,此d是:

1.在torch.tensor()中用于python浮点型的推断

2.用于推断python复数的类型推断,如果默认浮点dtype为torch.float64,则默认的complex dtype设置为torch.complex128,否则将其设置为torch.complex64

默认浮点dtype最初是float32

print(torch.tensor([1.2, 3]).dtype)
print(torch.tensor([1.2, 3j]).dtype)
torch.float64
torch.complex128
# 设置后再次输出
torch.set_default_dtype(torch.float64)
print(torch.tensor([1.2, 3]).dtype)
print(torch.tensor([1.2, 3j]).dtype)
torch.float64
torch.complex128
1.7 torch.get_default_dtype(d)

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

# 默认浮点为float32
torch.get_default_dtype() # 但此处因为1.6设置了浮点为float64,我如果重新启动jupyter就回复默认的float32
torch.float64
1.8 torch.set_default_tensor_type(t)

这个接口和1.6的有点相似,但这个更新,更强大一些,相同的是两者都只设置浮点数默认类型

1.9 torch.numal(input)

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

a = torch.randn(1, 2, 3, 4, 5, 6)
print(torch.numel(a))
b = torch.zeros(4, 4)
print(torch.numel(b))
720
16
1.10 torch.set_printoptions()

修改打印选项,有时候需要控制输出显示的参数

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)
# precision控制显示小数位数
a = torch.randn(3, 6)
print(a)
# 设置显示6位小数
torch.set_printoptions(precision = 6)
print(a)
tensor([[-1.1525,  0.9093,  2.0913,  0.0931, -0.3555,  1.3664],
        [-0.7510, -0.6092, -1.0060, -0.3187, -1.2517,  1.0981],
        [-2.2209, -1.5466, -0.8205, -0.2742, -0.3420,  0.2562]])
tensor([[-1.152488,  0.909250,  2.091264,  0.093103, -0.355520,  1.366420],
        [-0.750981, -0.609215, -1.005999, -0.318672, -1.251703,  1.098076],
        [-2.220941, -1.546623, -0.820527, -0.274219, -0.341989,  0.256163]])
# threshold控制显示tensor数目
a = torch.rand(300, 6)
torch.set_printoptions(threshold = 2000) # 默认为1000折叠,设置为2000会都显示
print(a)

在这里插入图片描述

# edgeitems设置折叠前后的行数
b = torch.rand(300, 6)
torch.set_printoptions(edgeitems = 4)
print(b)
tensor([[0.6685, 0.3716, 0.0439, 0.3024, 0.1559, 0.5953],
        [0.0443, 0.2895, 0.7828, 0.6393, 0.6875, 0.6370],
        [0.0904, 0.0420, 0.1196, 0.1175, 0.2010, 0.8222],
        [0.6564, 0.2175, 0.3224, 0.1519, 0.1214, 0.4407],
        ...,
        [0.6148, 0.6592, 0.2639, 0.9285, 0.4904, 0.0584],
        [0.2005, 0.3048, 0.8912, 0.4377, 0.3327, 0.2379],
        [0.7394, 0.0580, 0.9673, 0.4585, 0.7559, 0.7378],
        [0.4538, 0.1455, 0.5176, 0.7754, 0.3250, 0.8044]])

还有linewidth, profile, sci_mode参数可以设置…


reference:



须知少时凌云志,曾许人间第一流。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZPILOTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值