pytorch-lightning-sam-callback is an implementation of SAM using pytorch-lightning's Callback API. This project is motivated to integrate SAM with LightningModels without any modifications.
- SAM implementation
- Provided as pytorch-lightning's Callback API
- Mixed Precision Training is not supported
$ pip install pytorch-lightning-sam-callback
import torch
from torch.utils.data import DataLoader, Dataset
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning_sam_callback import SAM
class RandomDataset(Dataset):
def __init__(self, size, num_samples):
self.data = torch.randn(num_samples, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
class BoringModel(LightningModule):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2)
def forward(self, x):
return self.layer(x)
def training_step(self, batch, batch_idx):
return self(batch).mean()
def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=0.1)
model = BoringModel()
trainer = Trainer(max_epochs=3, callbacks=[SAM()])
trainer.fit(model, train_dataloaders=DataLoader(RandomDataset(32, 64), batch_size=2))