PyTorch 分别为 CPU tensor 和 GPU tensor 定义的张量基本类型
Pytorch里面处理的最基本的操作对象就是Tensor(张量),它表示的其实就是一个多维矩阵,并有矩阵相关的运算操作。
我们将标量称为 0D 张量(0维张量),向量称为 1D 张量(1维张量),矩阵称为 2D 张量(2维张量),依次类推。
在使用上和 numpy 是对应的,它和 numpy 唯一的不同就:pytorch 可以在 GPU 上运行,而 numpy 不可以。所以,我们也可以使用 Tensor 来代替 numpy 的使用。
数据类型 | CPU Tensor |
---|---|
8位整数(unsigned) | torch.ByteTensor |
8位整数(signed) | torch.CharTensor |
16位整型(signed) | torch.ShortTensor |
32位整型(signed) | torch.IntTensor |
64位整型(signed) | torch.LongTensor |
32位浮点型(signed) | torch.FloatTensor / torch.Tensor的默认类型 |
64位浮点型(signed) | torch.DoubleTensor |
import torch
v_int_tensor = torch.IntTensor([ # 定义一个 2 * 3的int型矩阵,从list中导入数据
[1, 2, 3],
[4, 5, 6]
])
v_float_tensor = torch.FloatTensor([ # 定义一个 1 * 3的float型矩阵,从list中导入数据
[3.13, 3.33, 7.21]
])
print(v_int_tensor)
print(v_float_tensor)
输出:
tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32) tensor([[3.1300, 3.3300, 7.2100]])
import torch
v_default_tensor = torch.Tensor([
[1, 2, 3],
[4, 5, 6]
])
v_int_tensor = torch.IntTensor([
[1, 2, 3],
[4, 5, 6]
])
v_float_tensor = torch.FloatTensor([
[3.13, 3.33, 7.21]
])
print(type(v_int_tensor)) #
print(v_float_tensor.type()) # torch.FloatTensor
print(v_default_tensor.type()) # torch.FloatTensor
instance函数检验:
import torch
v_default_tensor = torch.Tensor([
[1, 2, 3],
[4, 5, 6]
])
v_GPU_default_tensor = v_default_tensor.cuda(); # 将数据从CPU数据转换为GPU类型数据
print(isinstance(v_default_tensor,torch.FloatTensor)) # True
print(isinstance(v_default_tensor,torch.cuda.FloatTensor)) # False
print(isinstance(v_GPU_default_tensor,torch.FloatTensor)) # False
print(isinstance(v_GPU_default_tensor,torch.cuda.FloatTensor)) # True
0D 张量
:0D 张量仅包含一个数字,我们也可以称 0D 张量为标量,深度学习中loss函数的值通常为 0D 张量a = torch.tensor(1.) # print(a) # tensor(1.) print(a.size()) # torch.Size([]) --> 空的类型 print(a.shape) # torch.Size([]) print(a.dim()) # 0 b = torch.tensor(1.3) print(b) # tensor(1.3000) print(b.size()) # torch.Size([]) print(b.shape) # torch.Size([]) print(b.dim()) # 0 c = torch.tensor(3.3) print(c) # tensor(3.3000) print(c.size()) # torch.Size([]) print(c.shape) # torch.Size([]) print(len(c.shape)) # 0
PyTorch 提供了一个非常方便的内置函数item函数能够将 0D 张量转换为 Python 的基本数据类型
a = torch.tensor([1.0]) print(a.type()) # torch.FloatTensor b = a.item() print(type(b)) #
1D 张量
:1D 张量称为向量,在深度学习中阈值通常为向量的形式
- torch.tensor 接受的只能是数据的内容;
- torch.Tensor 接受的可以是数据的内容,也可以是数据的形状;
a = torch.tensor([1.1]) # []中是具体数据 print(a) # tensor([1.1000]) print(a.size()) # torch.Size([1]) print(a.dim()) # 1 b = torch.Tensor(1) # 随机生成dim为1 size为1的数据 print(b) # tensor([8.4490e-39]) print(b.size()) # torch.Size([1]) print(b.dim()) # 1 b2 = torch.Tensor(2) # 随机生成dim为1 size为2的数据 print(b2) # tensor([6.1290e+28, 1.8037e+28]) print(b2.size()) # torch.Size([2]) print(b2.dim()) # 1
2D 张量
:2D 张量称为矩阵,在深度学习中常用于向量数据。在前面介绍的手写数字识别问题中,我们将 (28 x 28) 的像素矩阵打平成 (784,) 的向量特征。为了方便矩阵计算以及提高效率,我们使用批梯度下降算法,即每次训练小批量的样本数据,因此我们通常会为 (784,) 的向量特征添加一个批量维batch_size,784 就是 features 特征,即 (batch_size, features)。a = torch.randn(2, 3) print(a.size()) # torch.Size([2, 3]) print(a.dim()) # 2 # 可以为size函数传入指定索引,来获取对应维度上的元素个数 print(a.size(0)) # 2 -> torch.randn(2, 3)中第一个元素2 print(a.size(1)) # 3 -> torch.randn(2, 3)中第二个元素3 v_default_tensor = torch.Tensor([ [1, 2, 3], [4, 5, 6] ]) print(v_default_tensor.dim()) # 2 print(v_default_tensor.size(0)) # 2 print(v_default_tensor.size(1)) # 3
3D 张量
:3D 张量通常用于时间序列的数据或者文本序列的数据,比如对于文本序列的数据,通常形状为 (batch_size, timesteps, features):
- batch_size:处理的文档数;
- timesteps:处理每篇文档的长度,由于每篇文档长度不一,需要人为指定一个长度阈值,超过长度阈值的文档进行裁剪,少于长度阈值的文档进行填充;
- features:词的表示,如果使用 one-hot 编码,则 features 为构成词典的大小。如果使用 Embedding 词嵌入,则 features 为设置词嵌入的维度;
a = torch.rand(1, 2, 3) print(a.size()) # torch.Size([1, 2, 3]) print(a.dim()) # 3
4D 张量
:4D 张量通常用于图像数据,形状为 (batch_size, height, width, channels) 或 (batch_size, channels, height, width),channel 的位置和具体使用的深度学习框架有关,在 TensorFlow 2.X 中图像形状为 (batch_size, height, width, channels),而在 PyTorch 中图像的形状为 (batch_size, channels, height, width),这点注意即可。a = torch.rand(2, 3, 28, 28) # 对应到现实意义:[照片个数,RGB三个通道,长,宽] print(a.size()) # torch.Size([2, 3, 28, 28]) print(a.dim()) # 4
5D 张量
:5D 张量通常用于视频数据,形状为 (batch_size, frames, height, width, channels) 或 (batch_size, frames, channels, height, width),channels 通道的位置和在图像中的一致,不同框架中可能表示 channels 通道维度不同,视频和图像数据相比仅仅是增加了 frames 帧数这一个维度。5D 以上的张量在深度学习中并不常见这里不再赘述。
data_numpy = numpy.ones(3)
print(data_numpy) # [1. 1. 1.]
data_tensor = torch.from_numpy(data_numpy)
print(data_tensor) # tensor([1., 1., 1.], dtype=torch.float64)
data_tensor = torch.randn((3, 2))
print(data_tensor)
data_numpy = data_tensor.numpy()
print(data_numpy)
```
输出:
tensor([[ 0.9307, 1.2856],
[-1.1095, -0.1586],
[-0.9531, -2.0985]])
[[ 0.93066823 1.2855971 ]
[-1.1095397 -0.15860502]
[-0.9531248 -2.0985377 ]]
```
a = torch.Tensor([
[1, 2],
[3, 4],
[5, 6]
])
print(a.numel()) # 6
a = torch.Tensor([
[1, 2],
])
print(a.type()) # torch.FloatTensor
torch.set_default_tensor_type(torch.DoubleTensor)
# tourch.IntTesnor会爆异常
b = torch.Tensor([
[1, 2],
])
print(b.type()) # torch.DoubleTensor
a = torch.rand(3, 3)
print(a)
'''
输出:
tensor([
[0.3535, 0.0184, 0.3210],
[0.8187, 0.0639, 0.1270],
[0.0299, 0.7948, 0.0381]
])
'''
a = torch.Tensor(3,3)
torch.rand_like(a)
print(a)
'''
输出:
tensor([[9.6429e-39, 8.4490e-39, 9.6429e-39],
[9.2755e-39, 1.0286e-38, 9.0919e-39],
[8.9082e-39, 9.2755e-39, 8.4490e-39]])
'''
a = torch.randint(1,10,[3,3])
print(a)
'''
输出:
tensor([[8, 7, 2],
[2, 9, 5],
[5, 9, 3]])
'''
a = torch.arange(1,10)
print(a)
b = torch.range(0, 10)
print(b)
c = torch.arange(1, 10, 3)
print(c)
'''
输出:
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
tensor([1, 4, 7])
UserWarning: torch.range is deprecated and will be removed in a future release
'''
a = torch.linspace(1,8, steps=4)
print(a)
'''
输出:
tensor([1.0000, 3.3333, 5.6667, 8.0000])
'''
a = torch.logspace(0,1,steps=10)
print(a)
'''
输出:
tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826,
3.5938, 4.6416, 5.9948, 7.7426, 10.0000])
'''
a = torch.ones(3, 3)
b = torch.zeros(3, 3)
c = torch.eye(3)
print(a)
print(b)
print(c)
'''
输出:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
'''
将参数形成的矩阵全部初始化为0
a = torch.ones(3, 3)
b = torch.zeros(3, 3)
c = torch.eye(3)
print(a)
print(b)
print(c)
'''
输出:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
'''