深度学习--Pytorch使用
1. Pytorch基本语法
1.1 认识Pytorch
Pytorch的基本元素操作
- Tensors张量: 张量的概念类似于Numpy中的ndarray数据结构,最大的区别在于Tensor可以利用GPU的加速功能
- 我们使用Pytorch的时候,常规步骤是先将torch引用进来,如下所示:
import torch
- 创建矩阵的操作
创建一个没用初始化的矩阵:
x = torch.empty(5,3)
print(x)
输出结果
tensor([[9.2755e-39, 1.0837e-38, 8.4490e-39],
[1.1112e-38, 1.0194e-38, 9.0919e-39],
[4.5919e-39, 9.9184e-39, 9.0000e-39],
[8.9082e-39, 1.1112e-38, 9.0919e-39],
[1.0194e-38, 1.0286e-38, 1.0194e-38]])
创建一个有初始化的矩阵
x = torch.rand(5,3)
print(x)
输出结果
tensor([[0.7516, 0.3755, 0.7024],
[0.8107, 0.7110, 0.8737],
[0.3555, 0.7856, 0.2906],
[0.6110, 0.2330, 0.9692],
[0.7224, 0.5385, 0.2426]])
对比有无初始化的矩阵: 当声明一个未初始化的矩阵时,它本身不包含任何确切的值.当创建一个未初始化的矩阵时,分配给矩阵的内存中有什么数值就赋值给了这个矩阵,本质上是毫无意义的数据.
创建一个全零矩阵并可指定数据元素的类型为long
#指定数据元素类型
x = torch.zeros(5,3,dtype = torch.long)
print(x)
输出结果
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
直接通过数据创建张量
x = torch.tensor([2.5,3.5])
print(x)
输出结果
tensor([2.5000, 3.5000])
通过已有的一个张量创建相同尺寸的新张量
x = torch.ones(5,3,dtype=torch.double)
print(x)
#利用randn_like方法得到相同张量尺寸的一个新张量,并且采用随机初始化来对其进行赋值
y = torch.randn_like(x,dtype=torch.float)
print(y)
输出结果
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-0.3912, -0.5282, 0.3981],
[ 0.2406, 1.2102, -0.3314],
[ 0.6653, -0.7472, -1.1816],
[-0.4625, 0.6925, 0.0044],
[-1.9864, -0.3797, 0.9031]])
获取张量的形状
print(x.size())
print(y.size())
输出结果
torch.Size([5, 3])
torch.Size([5, 3])
- 注意:
- torch.size函数本质上返回的是一个元组,因此它支持一切元组的操作
a,b = x.size()
print('a=',a)
print('b=',b)
a= 5
b= 3
Pytorch基本运算操作
加法操作
- 第一种加法操作
x = torch.rand(5,3)
y = torch.rand(5,3)
print(x+y)
输出结果
tensor([[1.0298, 1.0839, 0.9679],
[1.0619, 1.5961, 1.7681],
[1.2386, 0.9616, 1.4837],
[1.7078, 0.7791, 0.7619],
[1.2851, 0.3282, 0.4210]])
- 第二种加法操作
print(torch.add(x,y))
结果同上
- 第三种加法
#提前设定一个空的张量
result = torch.empty(5,3)
#将空的张量作为加法的结果存储张量
x = torch.rand(5,3)
y = torch.rand(5,3)
torch.add(x,y,out=result)
print(result)
输出结果
tensor([[1.0639, 0.7863, 1.4445],
[1.1267, 1.6785, 1.0132],
[0.4953, 0.5477, 1.0618],
[0.3658, 1.0119, 1.1568],
[1.0322, 0.9772, 0.8682]])
- 第四种加法方式:in-place(原地置换)
#x+y的结果被就地存到了y种
x = torch.rand(5,3)
y = torch.rand(5,3)
print(y)
y.add_(x)
print(y)
输出结果
tensor([[0.5791, 0.0200, 0.0678],
[0.1055, 0.3206, 0.2485],
[0.4314, 0.9130, 0.2338],
[0.6478, 0.2670, 0.9803],
[0.8897, 0.4902, 0.9275]])
tensor([[0.6541, 0.4501, 0.4004],
[1.0115, 0.6334, 0.6620],
[1.2576, 1.7027, 0.4421],
[1.3807, 0.2868, 1.5334],
[1.6565, 0.6611, 1.1307]])
- 注意:
- 所有的in-place操作函数都有一个下划线的后缀
- 比如x.copy_(y),x.add_(y),都会直接改变x的值
Numpy操作张量
切片
x = torch.rand(5,3)
print(x)
print(x[:,1])
输出结果
tensor([[0.1027, 0.4800, 0.8575],
[0.6692, 0.4540, 0.9260],
[0.6039, 0.5626, 0.2958],
[0.3783, 0.1940, 0.9337],
[0.9120, 0.1436, 0.2570]])
tensor([0.4800, 0.4540, 0.5626, 0.1940, 0.1436])
改变张量的形状:torch.view()
x = torch.randn(4,4)
print(x)
#tensor.view()操作需要保证数据元素的总数量不变
y = x.view(16)
print(y)
# -1代表自动匹配个数
z = x.view(-1,8)
print(z)
print(x.size(),y.size(),z.size())
输出结果
tensor([[-0.2742, -0.9338, -1.0177, 0.7450],
[ 0.4704, -0.2740, 0.0730, 0.3134],
[ 2.1276, -1.1118, 0.6400, 0.6667],
[ 1.4148, -1.2484, -0.7334, 0.9639]])
tensor([-0.2742, -0.9338, -1.0177, 0.7450, 0.4704, -0.2740, 0.0730, 0.3134,
2.1276, -1.1118, 0.6400, 0.6667, 1.4148, -1.2484, -0.7334, 0.9639])
tensor([[-0.2742, -0.9338, -1.0177, 0.7450, 0.4704, -0.2740, 0.0730, 0.3134],
[ 2.1276, -1.1118, 0.6400, 0.6667, 1.4148, -1.2484, -0.7334, 0.9639]])
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
-
注意
-
torch.rand和torch.randn有什么区别? y = torch.rand(5,3) y=torch.randn(5,3)
一个均匀分布,一个是标准正态分布
-
如果张量种只有一个元素,可以用.item()将值取出,作为一个Python number
x = torch.randn(1)
print(x)
print(x.item())
输出结果
tensor([0.2764])
0.2764213979244232
关于Torch Tensor和Numpy array之间的相互转换
Torch.Tensor和Numpy array共享底层的内存空间,因此改变其中的一个值,另一个也会随之被改变
a = torch.ones(5)
print(a)
输出结果
tensor([1., 1., 1., 1., 1.])
将Torch Tensor转换为Numpy array
b = a.numpy()
print(b)
输出结果
[1. 1. 1. 1. 1.]
对其中一个进行加法操作
a.add_(1)
print(a)
print(b)
输出结果
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
将Numpy array转换为Torch Tensor
import numpy as np
import torch
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out=a)
print(a)
print(b)
输出结果
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
- 注意
- 所有在CPU上的Tensor,除了Char Tensor,都可以转换为Numpy array并可以反向转换
关于Cuda Tensor:Tensor可以用.to()方法来移动到任意设备上
#如果服务器上已经安装了GPU和CUDA
import torch
if torch.cuda.is_available():
#定义一个设备对象,这里指定成CUDA,即使用GPU
device = torch.device("cuda")
#直接在GPU上创建一个Tensor_y ,在CPU上创建Tensor_x
x = torch.randn(1)
y = torch.ones_like(x,device=device)
# 将x转移到GPU
x = x.to(device)
# x和y都在GPU上面,才能支持加法运算
z = x+y
# 此处的张量z在GPU上面
print(z)
# 也可以将z转移到CPU上面,并同时指定张量元素的数据类型
print(z.to("cpu",torch.double))
输出结果
tensor([1.3101], device='cuda:0')
tensor([1.3101], dtype=torch.float64)
小结
- 什么是Pytorch
- Pytorch是一个基于Numpy的科学计算包作为Numpy的替代者,向用户提供使用GPU强大功能的能力.
- 做为一款深度学习的平台,向用户提供最大的灵活性和速度
- Pytorch的基本元素操作
- 矩阵的初始化
- torch.empty()
- torch.rand(n,m)
- torch.zeros(n,m,dtype=torch.float)
- 其他若干操作:
- x.new_ones(n,m,dtype=torch,double)
- torch.randn_like(x,dtype=torch.float)
- x.size()
- 矩阵的初始化
- Pytorch的基本运算操作
- 加法操作
- x+y
- torch.add(x,y)
- torch.add(x,y,out=result)
- 若干其他操作
- x.view()
- x.item()
- 加法操作
- Torch Tensor和Numpy Array之间的相互转换
- 将Torch Tensor转换为Numpy Array
- b = a.numpy()
- 将Numpy Array转换为Torch Tensor
- b = torch.from_numpy(a)
- 注意:所有在CPU上的Tensor,除了Char Tensor,都可以转换为Numpy array并可以反向转换
- 将Torch Tensor转换为Numpy Array
- 任意的Tensor都可以用.to()方法来将其移动到任意设备上
- x.to(device)

浙公网安备 33010602011771号