pytorch基础学习(四) 数据处理(一)

本篇主要介绍pytorch中tensor的attributes,操作注意事项,tensor的基本操作如:查看tensor的shape,rank和元素个数;改变tensor的shape甚至rank.

1. tensor的attributes

tensor有以下几个常用的attributes,首先看一段代码:

import torch
t = torch.Tensor()
print(type(t))  # <class 'torch.Tensor'>
print(t.dtype)  # torch.float32
print(t.device)  # cpu
print(t.layout)  # torch.strided

其输出为:

output:
<class 'torch.Tensor'>
torch.float32
cpu
torch.strided

对于这个输出的解释如下:

  • pytorch中tensor的数据类型为<class 'torch.Tensor'>
  • t.dtype表示tensor中元素的数据类型. 由上一节我们知道,使用torch.Tensor()构造的tensor是浮点型的,因此输出为torch.float32
  • t.device表示tensor所在的设备(CPU或GPU),默认是CPU,如何将一个tensor写在GPU上,下文会提到.
  • t.layout表示tensor在内存中的储存方式,有torch.stried和torch.sparse_coo两种,默认为torch.stried,这是一种紧凑的存储方式,使用t.stried()会返回一个list,表示每一维度从一个元素转到下一个元素所需要的内存.

2. tensor操作的注意事项

  1. 在旧版本的pytorch中,两个tensor进行运算,必须是相同的类型,但是在新版的pytorch中已经可以运算,类如:
t1 = torch.tensor([1, 2, 3])
t2 = torch.tensor([1., 2., 3.])
print(t1.dtype)
print(t2.dtype)
t = t1 + t2
print(t)  
output:
torch.int64
torch.float32
tensor([2., 4., 6.])

可以看到int64类型的tensor和float32类型的tensor进行了运算,输出的类型是float32类型的tensor,这在旧版中,就会报错error: t1, t2 must be the same dtype.

  1. 无论新旧版本的pytorch,两个tensor进行运算,必须在相同的设备上,类如:
t1 = torch.tensor([1, 2, 3])
t2 = t1.cuda()
print(t1.device)   
print(t2.device)
t = t1 + t2 
output:
cpu
cuda:0
RuntimeError: expected device cpu but got device cuda:0

这样是会报错的.
同时我们也可以看到,通过t1.cuda(),新生成的tensor的设备就从CPU变成了0号GPU.

3. tensor的基本操作

1. 查看tensor的shape,rank和元素个数

t = torch.tensor([
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]
], dtype=torch.float32)
print(t.shape)
print('rank of tensor: %d' % len(t.shape))
print('number of elements: %d' % torch.tensor(t.shape).prod())
print('number of elements: %d' % t.numel())
output:
torch.Size([3, 4])
rank of tensor: 2
number of elements: 12
number of elements: 12

最后两行,都可以得到tensor的元素个数.

2.改变tensor的shape甚至rank

  1. reshape,用法如下 :
t = torch.tensor([
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]
], dtype=torch.float32)
print(t.reshape(1, 12))
print(t.reshape(2, 2, 3))
print(t.reshape(2, -1))
output:
tensor([[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.]])
tensor([[[1., 1., 1.],
         [1., 2., 2.]],

        [[2., 2., 3.],
         [3., 3., 3.]]])
tensor([[1., 1., 1., 1., 2., 2.],
        [2., 2., 3., 3., 3., 3.]])

reshape的参数就是需要改变的shape,需要注意的是,reshape前后一定要保持元素数是一样的,否则会报错.
可能读者会注意到,最后一个reshape的参数是(2, -1),-1也可以作为reshape的参数,意思就是这个 axis的长度由其它axis的长度等于决定. 在这个例子中,t一种由12个元素,第一个aixs的长度是2. 因此第二个aixs的长度就是6,最终的shape就是(2, 6).
由-1的作用我们可以看出,-1只能有一个,否则会因出现歧义而报错,如t.reshape(2, -1, -1),这就不行,后面两个axes的长度可以是(1,6), (6,1), (2,3), (3,2),机器无法决定.

  1. squeeze与unsqueeze
    这两个操作在网络搭建中经常使用
    squeeze: 移除所有长度是1的axes
    unsqueeze(dim): 在特定的维度增加一个长度为1的轴,因此tensor的rank会增加1
    ,还是上面的tensor,我们举2个栗子吧:
print(t.reshape(2, 1, 6).squeeze().shape)
output:
torch.Size([2, 6])

可以看到,reshape可以将t的shape变为(2,1,6),经过squeeze的操作,第二个axes的长度为1被删去,因此经过squeeze的shape就变为(2,6).
再看看unsqueeze:

print(t.reshape(2, 1, 6).squeeze().unsqueeze(dim=1).shape)
output:
torch.Size([2, 1, 6])

由上面可以知道,经过t.reshape(2, 1, 6).squeeze(),shape此时为(2,6),后面由进行了unsqueeze(dim=1)操作,在第2维(第1维是dim=0)增加一个长度为1的轴,从而shape就变成了(2, 1, 6),这在网络输入图像时候非常有用,batch为1时候,只需要在每个样本的最前面使用unsqueeze增加一个轴就可以啦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。