python打卡day37@浙大疏锦行
知识点回顾:
- 过拟合的判断:测试集和训练集同步打印指标
- 模型的保存和加载
- 仅保存权重
- 保存权重和模型
- 保存全部信息checkpoint,还包含训练状态
- 早停策略
作业:对信贷数据集训练后保存权重,加载权重后继续训练50轮,并采取早停策略
一、首先实现模型训练和保存权重
import torch
from model import CreditRiskModel # 假设这是您的模型类# ... 数据加载代码 ...def train_model():model = CreditRiskModel(input_size=30) # 根据实际特征数调整criterion = nn.BCELoss()optimizer = torch.optim.Adam(model.parameters())# 训练循环for epoch in range(100):# ... 训练代码 ...# 保存权重if epoch % 10 == 0:torch.save(model.state_dict(), f'weights/epoch_{epoch}.pth')# 最终保存torch.save(model.state_dict(), 'weights/final_weights.pth')
二、加载权重并继续训练50轮
from train import train_model # 导入之前的训练函数
from model import CreditRiskModeldef load_and_resume():# 初始化模型model = CreditRiskModel(input_size=30)# 加载保存的权重checkpoint = torch.load('weights/final_weights.pth')model.load_state_dict(checkpoint)# 继续训练50轮optimizer = torch.optim.Adam(model.parameters())for epoch in range(50):# ... 训练代码 ...
三、实现早停策略
class EarlyStopping:def __init__(self, patience=5, delta=0):self.patience = patienceself.delta = deltaself.counter = 0self.best_score = Noneself.early_stop = Falsedef __call__(self, val_loss):score = -val_lossif self.best_score is None:self.best_score = scoreelif score < self.best_score + self.delta:self.counter += 1if self.counter >= self.patience:self.early_stop = Trueelse:self.best_score = scoreself.counter = 0
四、整合到训练代码中
from utils.early_stopping import EarlyStoppingdef train_model():# ... 之前的初始化代码 ...early_stopping = EarlyStopping(patience=5)for epoch in range(100):# ... 训练代码 ...val_loss = validate(model) # 假设有验证函数# 早停检查early_stopping(val_loss)if early_stopping.early_stop:print(f"Early stopping triggered at epoch {epoch}")break# 保存最佳模型if early_stopping.counter == 0:torch.save(model.state_dict(), 'weights/best_weights.pth')
①权重保存方式:
# 仅保存权重
torch.save(model.state_dict(), 'model_weights.pth')# 保存整个模型
torch.save(model, 'full_model.pth')# 保存checkpoint(包含优化器状态等)
torch.save({'epoch': epoch,'model_state_dict': model.state_dict(),'optimizer_state_dict': optimizer.state_dict(),'loss': loss,
}, 'checkpoint.ckpt')
②加载方式对应:
# 加载权重
model.load_state_dict(torch.load('model_weights.pth'))# 加载整个模型
model = torch.load('full_model.pth')# 加载checkpoint
checkpoint = torch.load('checkpoint.ckpt')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])