Pytorch张量数据结构

1.张量数据类型

张量的数据类型和numpy.array基本一一对应,但是不支持str类型,有如下几种形式

torch.float64 # 等同于(torch.double)
torch.float32 # 默认
torch.float16
torch.int64   # 等同于torch.long
torch.int32   # 默认
torch.int16
torch.int8
torch.uint8   # 二进制码,表示0-255
torch.bool

torch中的数据类型与python中的一样,会自己去推断数据的类型,而且数据都有的特定默认类型

​​​​在这里插入图片描述

当然,我们也可以制定它的数据类型

i=torch.tensor(1,dtype=torch.int64)
j=torch.tensor(2.0,dtype=torch.double)
a=torch.tensor(256,dtype=torch.uint8)

输出结果:

tensor(1)
tensor(2., dtype=torch.float64)
tensor(0, dtype=torch.uint8)

此外,还可以使用特定的构造函数
IntTensor,生成整数列表;FloatTensor、BoolTensor同理
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
还有不同类型的数据转换

i = torch.tensor(1); print(i,i.dtype)
x = i.float(); print(x,x.dtype) #调用 float方法转换成浮点类型
y = i.type(torch.float); print(y,y.dtype) #使用type函数转换成浮点类型
z = i.type_as(x);print(z,z.dtype) #使用type_as方法转换成某个Tensor相同类型
#输出:
tensor(1) torch.int64
tensor(1.) torch.float32
tensor(1.) torch.float32
tensor(1.) torch.float32

二、张量的维度

不同类型的数据可以用不同维度(dimension)的张量来表示。

标量为0维张量,向量为1维张量,矩阵为2维张量。

彩色图像有rgb三个通道,可以表示为3维张量。

视频还有时间维,可以表示为4维张量。

可以简单地总结为:有几层中括号,就是多少维的张量

标量
向量
矩阵
三维张量
四维张量
关于张量的操作有很多,包括对其shape的改变、dimension的增删和交换、Tensor的拼接分割和堆叠分解,下面只说一下对shape的操作,剩下的内容下篇博客再整理。

三、张量的尺寸

可以使用 shape属性或者 size()方法查看张量在每一维的长度.

可以使用view()方法改变张量的尺寸。

如果view()方法改变尺寸失败,可以使用reshape()方法.

scalar = torch.tensor(True)
print(scalar.size())
print(scalar.shape)
#输出:
torch.Size([])
torch.Size([])
vector = torch.tensor([1.0,2.0,3.0,4.0])
print(vector.size())
print(vector.shape)
#输出:
torch.Size([4])
torch.Size([4])
matrix = torch.tensor([[1.0,2.0],[3.0,4.0]])
print(matrix.size())
#输出:
torch.Size([2, 2])
#使用view改变尺寸示例
vector = torch.arange(0,12)
print(vector)
print(vector.shape)
#输出
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
torch.Size([12])

matrix34 = vector.view(3,4)
print(matrix34)
print(matrix34.shape)
#输出:
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
torch.Size([3, 4])

matrix43 = vector.view(4,-1) #-1表示该位置长度由程序自动推断
print(matrix43)
print(matrix43.shape)
#输出:
tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])
torch.Size([4, 3])

有些操作会让张量存储结构扭曲,直接使用view会失败,可以用reshape方法

matrix26 = torch.arange(0,12).view(2,6)
print(matrix26)
print(matrix26.shape)
#输出:
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]])
torch.Size([2, 6])

# 转置操作让张量存储结构扭曲
matrix62 = matrix26.t()
print(matrix62.is_contiguous())#查看Tensor变量在内存中的存储是否连续
#输出:False

#使用reshape的演示
#matrix34 = matrix62.view(3,4) #error!
matrix34 = matrix62.reshape(3,4) #等价于matrix34 = matrix62.contiguous().view(3,4)
print(matrix34)
#输出:
tensor([[ 0,  6,  1,  7],
        [ 2,  8,  3,  9],
        [ 4, 10,  5, 11]])


如果使用view(3,4)或reshape(3,4),得到的tensor并不是转置的效果,而是相当于将原tensor的元素按行取出,然后按行放入到新形状的tensor中。

a23=torch.arange(0,6).reshape(2,3)
print(a23)
print(a23.reshape(3,2))
#输出:
tensor([[0, 1, 2],
        [3, 4, 5]])
tensor([[0, 1],
        [2, 3],
        [4, 5]])

#使用方法permute进行矩阵转置
print(a23.permute(1,0))
#输出:
tensor([[0, 3],
        [1, 4],
        [2, 5]])

permute作用为调换Tensor的维度,参数为调换的维度。例如对于一个二维Tensor来说,调用tensor.permute(1,0)意为将1轴(列轴)与0轴(行轴)调换,相当于进行转置。

四、张量和numpy数组

可以用numpy方法从Tensor得到numpy数组,也可以用torch.from_numpy从numpy数组得到Tensor。

这两种方法关联的Tensor和numpy数组是共享数据内存的。

如果改变其中一个,另外一个的值也会发生改变。

如果有需要,可以用张量的clone方法拷贝张量,中断这种关联。
此外,还可以使用item方法从标量张量得到对应的Python数值
使用tolist方法从张量得到对应的Python数值列表。

下面是torch.from_numpy函数从numpy数组得到Tensor
numpy创建张量
注意tensor+1时的下划线的作用
**加粗样式
这种方法在对原始数据进行多种操作时尤其有用
在这里插入图片描述
在这里插入图片描述

OVER

参考:eat_pytorch_in_20_days_day_5

猜你喜欢

转载自blog.csdn.net/weixin_43427721/article/details/107140102