从零开始搞定蒸馏模型

知识蒸馏的开山之作:Distilling the Knowledge in a Neural Network

什么是知识?

we tend to identify the knowledge in a trained model with the learned parameter values.
知识是从训练模型中学到的参数值。

什么是蒸馏?

which we call “distillation” to transfer the knowledge from the cumbersome model to a small model that is more suitable for deployment.
将知识从复杂的大模型(教师模型)迁移到适合部署的小模型(学生模型)。

知识蒸馏损失函数构成

在这里插入图片描述

Demo代码实现

利用MNIST数据集,从零实现一个蒸馏模型。

导入工具包

# 导入module
import torch
from torch import nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
from torchinfo import summary
from tqdm import tqdm

设置cuda环境

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 使用cuDNN 加速卷积运算
torch.backends.cudnn.benchmark = True

# 设置随机数种子,便于复现
torch.manual_seed(2022)

加载数据集

# 载入MNIST数据集
train_dataset = torchvision.datasets.MNIST(root='mnist/dataset/',
                                           train = True,
                                           transform = transforms.ToTensor(), 
                                           download = True)

test_dataset = torchvision.datasets.MNIST(root='mnist/dataset/',
                                           train = False,
                                           transform = transforms.ToTensor(), 
                                           download = True)

# 生成dataloader
train_loader = DataLoader(dataset = train_dataset, batch_size = 32, shuffle=True)
test_loader = DataLoader(dataset = test_dataset, batch_size = 32, shuffle=True)

从零训练教师模型

# 定义教师模型
class TeacherModel(nn.Module):
    def __init__(self, hidden_dim=1024, num_classes=10):
        super(TeacherModel, self).__init__()
        self.relu = nn.ReLU()
        self.fc1 = nn.Linear(784, hidden_dim)
        self.fc2 = nn.Linear(hidden_dim, hidden_dim)
        self.fc3 = nn.Linear(hidden_dim, num_classes)
        self.dropout = nn.Dropout(p=0.5)
        
    def forward(self, x):
        x = x.view(-1, 784)
        x = self.fc1(x)
        x = self.dropout(x)
        x = self.relu(x)

        x = self.fc2(x)
        x = self.dropout(x)
        x = self.relu(x)
        
        x = self.fc3(x)
        
        return x

model = TeacherModel()
model = model.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 1e-4)

summary(model)

可以看出,教师模型总参数量为:186w
在这里插入图片描述

# 训练教师模型
epochs = 10
for epoch in range(epochs):
    model.train()

    for data, targets in tqdm(train_loader):
        data = data.to(device)
        targets = targets.to(device)

        # 前向预测
        preds = model(data)
        loss = criterion(preds, targets)

        # 后向传播,优化权重
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    model.eval()
    num_correct, num_samples = 0, 0
    with torch.no_grad():
        for x, y in test_loader:
            x = x.to(device)
            y = y.to(device)

            preds = model(x)

            predictions = preds.max(1).indices
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)

        acc = (num_correct/num_samples).item()

    print('Epoch: {}\t Accuracy: {:.4f}'.format(epoch+1, acc))

知识蒸馏训练学生模型

# 定义学生模型
class StudentModel(nn.Module):
    def __init__(self, in_channels=1, hidden_dim=128, num_classes=10):
        super(StudentModel, self).__init__()
        self.relu = nn.ReLU()
        self.fc1 = nn.Linear(784, hidden_dim)
        self.fc2 = nn.Linear(hidden_dim, hidden_dim)
        self.fc3 = nn.Linear(hidden_dim, num_classes)
        self.dropout = nn.Dropout(p=0.5)
        
    def forward(self, x):
        x = x.view(-1, 784)
        x = self.fc1(x)
        # x = self.dropout(x)
        x = self.relu(x)

        x = self.fc2(x)
        # x = self.dropout(x)
        x = self.relu(x)
        
        x = self.fc3(x)
        
        return x
s_model = StudentModel(hidden_dim=10)
s_model = s_model.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(s_model.parameters(), lr = 1e-3)

summary(s_model)

学生模型总参数量为:8070,对模型进行极大地压缩。
在这里插入图片描述

# 准备预训练好的 教师模型
teacher_model.eval()

# 准备新的学生模型
s_model = StudentModel(hidden_dim=10)
s_model = s_model.to(device)
s_model.train()
# 蒸馏温度
temp = 7

# hard loss
hard_loss = nn.CrossEntropyLoss()
# hard loss权重 
alpha = 0.4


# soft loss
soft_loss = nn.KLDivLoss(reduction='batchmean')

optimizer = torch.optim.Adam(s_model.parameters(), lr = 1e-3)
# 蒸馏模型
epochs = 5
for epoch in range(epochs):
    s_model.train()

    for data, targets in tqdm(train_loader):
        data = data.to(device)
        targets = targets.to(device)

        # 教师模型 预测
        with torch.no_grad():
            teacher_preds = teacher_model(data)
        
        # 学生模型 预测
        student_preds = s_model(data)

        # hard loss
        student_loss = hard_loss(student_preds, targets)

        # soft loss
        distillation_loss = soft_loss(F.softmax(student_preds/temp, dim = 1),
                                      F.softmax(teacher_preds/temp, dim = 1))
        
        # 加权loss
        loss = alpha*student_loss + (1-alpha)*distillation_loss*(temp**2)

        # 后向传播,优化权重
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    s_model.eval()
    num_correct, num_samples = 0, 0
    with torch.no_grad():
        for x, y in test_loader:
            x = x.to(device)
            y = y.to(device)

            preds = s_model(x)

            predictions = preds.max(1).indices
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)

        acc = (num_correct/num_samples).item()

    s_model.train()
    print('Epoch: {}\t Accuracy: {:.4f}'.format(epoch+1, acc))
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
知识蒸馏是一种通过训练一个大型的、复杂的神经网络来提高小型、简单神经网络性能的技术。以下是构建一个简单的知识蒸馏模型的步骤: 1. 确定基础模型:选择一个简单的神经网络作为基础模型,该模型将被用于学习和预测目标任务。 2. 确定源模型:选择一个大型的、复杂的神经网络作为源模型,该模型将被用于提供知识和指导基础模型的学习。 3. 准备数据集:准备用于训练和测试模型的数据集,包括输入和输出。 4. 训练模型:使用数据集训练模型,直到其收敛并达到一个可以接受的性能水平。 5. 提取源模型的知识:使用源模型为基础模型提供知识的一种方法是提取源模型的中间层表示,这些中间层表示可以被认为是源模型的“知识”。 6. 训练基础模型:使用提取的中间层表示作为输入,使用目标任务的标签作为输出,使用数据集训练基础模型。 7. 比较基础模型和源模型:使用测试集评估基础模型和源模型的性能,并比较两者之间的性能差异。 8. 调整超参数:根据比较结果和基础模型的性能,调整超参数以提高基础模型的性能。 9. 部署基础模型:将基础模型用于实际应用程序中。 需要注意的是,以上步骤仅提供了构建知识蒸馏模型的基本框架,具体的实现细节和步骤可能会因不同的任务和数据集而有所变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NLP_wendi

谢谢您的支持。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值