1 pytorch的概念
1.1 tensor张量
1.1.1 PyTorch的tensor与NumPy array相互转换
PyTorch的很多操作和numpy都是类似的,但是因为其能够在 GPU 上运行,所以比 NumPy 快很多。
import torch
import numpy as np
# 创建一个 numpy ndarray
numpy_tensor = np.random.randn(10, 20)
x = torch.randn(10, 20)
1.1.1.1 ndarray==>tensor
- torch.Tensor 强制类型转换
- torch.from_numpy创建函数
pytorch_tensor1 = torch.Tensor(numpy_tensor)
pytorch_tensor2 = torch.from_numpy(numpy_tensor)
print(type(pytorch_tensor2))
print(type(pytorch_tensor1))
<class 'torch.Tensor'>
<class 'torch.Tensor'>
1.1.1.2 tensor==>ndarray
# 如果 pytorch tensor 在 cpu 上
numpy_array = pytorch_tensor1.numpy()
# 如果 pytorch tensor 在 gpu 上
numpy_array = pytorch_tensor1.cpu().numpy()
1.1.2 PyTorch Tensor 使用 GPU 加速
# 第一种方式是定义 cuda 数据类型
dtype = torch.cuda.FloatTensor # 定义默认 GPU 的 数据类型
gpu_tensor = torch.randn(10, 20).type(dtype)
# 第二种方式更简单,推荐使用
gpu_tensor = torch.randn(10, 20).cuda() # 将 tensor 放在GPU 上
1.1.3 tensor的属性
# 可以通过下面两种方式得到 tensor 的大小
print(pytorch_tensor1.shape)
print(pytorch_tensor1.size())
# 得到 tensor 的数据类型
print(pytorch_tensor1.type())
## 维度
print(pytorch_tensor1.dim())
# 得到 tensor 的所有元素个数
print(pytorch_tensor1.numel())
1.1.4 tensor的数据类型变换
x = torch.randn(3, 2)
print(x)
x = x.type(torch.DoubleTensor)
print(x)
x_array = x.numpy()
print(x_array.dtype)
1.2 tensor的操作
1.2.1 squeeze和unsqueeze操作: 降维升维
print(torch.ones(2, 2))
#torch.Size([2, 2])
print(torch.ones(2, 2).size())
x = torch.ones(2, 2).unsqueeze(0)
torch.Size([1, 2, 2])
print(x)
print(x.size())
# 将 tensor 中所有的一维全部都去掉
x = x.squeeze()
print(x)
print(x.shape)
1.2.2 数值类型转换
# 将其转化为整形
x = x.long()
# x = x.type(torch.LongTensor)
print(x.type())
1.2.3 使用permute和transpose进行维度交换
x = torch.randn(3, 4, 5)
print(x.shape)
# 使用permute和transpose进行维度交换
x = x.permute(1, 0, 2) # permute 可以重新排列 tensor 的维度
print(x.shape)
x = x.transpose(0, 2) # transpose 交换 tensor 中的两个维度
print(x.shape)
1.2.4 使用 view 对 tensor 进行 reshape
x = torch.randn(3, 4, 5)
print(x.shape)
## 拉伸
x = x.view(-1, 5) # -1 表示任意的大小,5 表示第二维变成 5
print(x.shape)
x = x.view(3, 20) # 重新 reshape 成 (3, 20) 的大小
print(x.shape)
1.2.5 tensor的运算:相加
x = torch.zeros(3, 4)
y = torch.ones(3, 4)
# 两个 tensor 求和
z = x + y
print(z)
z = torch.add(x, y)
print(z)
1.2.6 tensor的inplace操作
pytorch中大多数的操作都支持 inplace 操作,也就是可以直接对 tensor 进行操作而不需要另外开辟内存空间。方式非常简单,一般都是在操作的符号后面加_
1.3 变量
- from torch.autograd import Variable
- Variable 是对 tensor 的封装,操作和 tensor 是一样的,
- 但是每个 Variabel都有三个属性,Variable 中的
.data
,梯度.grad
以及这个 Variable 是通过什么方式得到的.grad_fn
。
1.3.1 变量的梯度
# 通过下面这种方式导入 Variable
from torch.autograd import Variable
x_tensor = torch.randn(10, 5)
y_tensor = torch.randn(10, 5)
# 将 tensor 变成 Variable
# 默认 Variable 是不需要求梯度的,所以我们用这个方式申明需要对其进行求梯度
x = Variable(x_tensor, requires_grad=True)
2 梯度下降法Gradient Descent (GD)介绍
2.1 梯度下降法简介
2.1.1 梯度
比如一个一个函数 f ( x , y ) f(x, y) f(x,y),那么 f f f 的梯度就是
( ∂ f ∂ x , ∂ f ∂ y ) (\frac{\partial f}{\partial x},\ \frac{\partial f}{\partial y}) (∂x∂f, ∂y∂f)
可以称为 g r a d f ( x , y ) grad f(x, y) gradf(x,y) 或者 ∇ f ( x , y ) \nabla f(x, y) ∇f(x,y)。具体某一点 ( x 0 , y 0 ) (x_0,\ y_0) (x0, y