借助 PyTorch 实现深度神经网络 - 线性回归 PyTorch 方法 - 第 3 周 | Coursera
随机梯度下降和数据加载器
在每个epoch中,使用一个样本进行更新的方法称为随机梯度下降,而使用所有样本进行更新的方法称为批量梯度下降。
随机梯度下降:
随机梯度下降的问题:近似损失快速波动
Pytorch实现:
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
# 定义数据集类
class Data(Dataset):
def __init__(self):
self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
self.y = 1 * self.x - 1 + 0.1 * torch.randn(self.x.size())
self.len = self.x.shape[0]
def __getitem__(self, index):
return self.x[index], self.y[index]
def __len__(self):
return self.len
# 初始化数据和数据加载器
dataset = Data()
trainloader = DataLoader(dataset=dataset, batch_size=1, shuffle=True)
# 定义模型参数
w = torch.tensor(-12.0, requires_grad=True)
b = torch.tensor(-10.0, requires_grad=True)
lr = 0.01 # 学习率
criterion = nn.MSELoss()
# 定义前向传播函数
def forward(x):
return w * x + b
# 训练模型并记录总损失
LOSS = []
def my_train_model(epochs):
for epoch in range(epochs):
total_loss = 0 # 每个epoch的总损失
for x, y in trainloader:
yhat = forward(x)
loss = criterion(yhat, y)
total_loss += loss.item()
loss.backward()
with torch.no_grad():
w -= lr * w.grad
b -= lr * b.grad
w.grad.zero_()
b.grad.zero_()
LOSS.append(total_loss)
print(f"Epoch {epoch + 1}/{epochs}, Loss: {total_loss}")
# 训练模型
my_train_model(10)
小批量梯度下降
允许处理更大的数据集(无法容纳在内存中)
总结:
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
# 定义数据集类
class Data(Dataset):
def __init__(self):
self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
self.y = 1 * self.x - 1 + 0.1 * torch.randn(self.x.size())
self.len = self.x.shape[0]
def __getitem__(self, index):
return self.x[index], self.y[index]
def __len__(self):
return self.len
# 初始化数据和数据加载器
dataset = Data()
trainloader = DataLoader(dataset=dataset, batch_size=10, shuffle=True)
# 定义模型参数
w = torch.tensor(-15.0, requires_grad=True)
b = torch.tensor(-10.0, requires_grad=True)
lr = 0.1 # 学习率
criterion = nn.MSELoss()
# 定义前向传播函数
def forward(x):
return w * x + b
# 训练模型并记录总损失
LOSS_MINI20 = []
def train_model_Mini20(epochs):
for epoch in range(epochs):
total_loss = 0 # 每个epoch的总损失
for x, y in trainloader:
yhat = forward(x)
loss = criterion(yhat, y)
total_loss += loss.item()
loss.backward()
with torch.no_grad():
w -= lr * w.grad
b -= lr * b.grad
w.grad.zero_()
b.grad.zero_()
LOSS_MINI20.append(total_loss)
print(f"Epoch {epoch + 1}/{epochs}, Loss: {total_loss}")
# 训练模型
train_model_Mini20(10)
Pytorch中的优化
替换criterion函数
训练、验证、测试
参数:w,b
超参数:lr、batch_size
使用验证数据集来决定超参数大小
用Pytorch实现:
有点网格搜索优化的感觉