重點回顧:用 PyTorch Lightning 擴充的 AI 模型

Scalable AI Models with PyTorch Lightning

Sergiy Tkachuk

Director, GenAI Productivity

用 PyTorch Lightning 建立可擴充模型

  • 神經網路結構化
  • 有效訓練邏輯
  • 實作 LightningModule
import lightning.pytorch as pl
import torch.nn as nn

class ClassificationModel(pl.LightningModule):
    def __init__(self, input_dim,
                 hidden_dim, num_class):
          # Initialize parent class
        super().__init__()
        # First layer
        self.layer1 = nn.Linear(input_dim,
                                hidden_dim)
        # Activation function
        self.relu = nn.ReLU()
        # Output layer
        self.layer2 = nn.Linear(hidden_dim,
                                num_class)
Scalable AI Models with PyTorch Lightning

PyTorch Lightning 進階技巧

  • 資料管理與前處理
  • 驗證與測試方法
  • 最佳化 Lightning 回呼
from lightning.pytorch import Trainer
from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint

checkpoint = ModelCheckpoint(
    monitor='val_accuracy',
    save_top_k=2,
    mode='max')
early_stopping = EarlyStopping(
    monitor='val_accuracy',
    patience=5,
    mode='max')
Scalable AI Models with PyTorch Lightning

為可擴充性最佳化模型

  • 模型量化策略
  • 修剪模型以提升效率
  • 用 TorchScript 佈署
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def forward(self, x):
        return x * 2

scripted_model = torch.jit.script(SimpleModel())

torch.jit.save(scripted_mod,"model.pt") # Save the model
loaded_model=torch.jit.load("model.pt") # Load the model
Scalable AI Models with PyTorch Lightning

恭喜!

Scalable AI Models with PyTorch Lightning

Preparing Video For Download...