PyTorch Lightning is a lightweight PyTorch wrapper that takes care of much of the boilerplate code associated with training neural networks. It's designed to make your PyTorch code more organized, readable, and scalable without sacrificing flexibility.
Let's walk through a basic example of how to use PyTorch Lightning to train a simple neural network.
First, install PyTorch Lightning:
pip install pytorch-lightning
Now, let's create a basic neural network for MNIST digit classification:
import torch import torch.nn as nn import torch.nn.functional as F import pytorch_lightning as pl from torchvision import datasets, transforms class MNISTModel(pl.LightningModule): def __init__(self): super().__init__() self.layer_1 = nn.Linear(28 * 28, 128) self.layer_2 = nn.Linear(128, 10) def forward(self, x): x = x.view(x.size(0), -1) x = F.relu(self.layer_1(x)) x = self.layer_2(x) return x def training_step(self, batch, batch_idx): x, y = batch logits = self(x) loss = F.cross_entropy(logits, y) self.log('train_loss', loss) return loss def configure_optimizers(self): return torch.optim.Adam(self.parameters(), lr=0.001)
In this example, we've defined our model architecture, training step, and optimizer configuration all within a single class that inherits from pl.LightningModule
.
Lightning also provides a LightningDataModule
class to encapsulate all the steps needed to process data:
class MNISTDataModule(pl.LightningDataModule): def __init__(self, data_dir: str = "./data", batch_size: int = 32): super().__init__() self.data_dir = data_dir self.batch_size = batch_size self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]) def prepare_data(self): datasets.MNIST(self.data_dir, train=True, download=True) datasets.MNIST(self.data_dir, train=False, download=True) def setup(self, stage=None): self.mnist_train = datasets.MNIST(self.data_dir, train=True, transform=self.transform) self.mnist_test = datasets.MNIST(self.data_dir, train=False, transform=self.transform) def train_dataloader(self): return torch.utils.data.DataLoader(self.mnist_train, batch_size=self.batch_size) def test_dataloader(self): return torch.utils.data.DataLoader(self.mnist_test, batch_size=self.batch_size)
Now, let's put it all together and train our model:
model = MNISTModel() data = MNISTDataModule() trainer = pl.Trainer(max_epochs=5, gpus=1 if torch.cuda.is_available() else 0) trainer.fit(model, data)
That's it! With just these few lines, Lightning takes care of the entire training process, including moving data to the GPU if available.
PyTorch Lightning offers many advanced features that can significantly enhance your training pipeline:
Automatic Logging: Use self.log()
in your LightningModule
to automatically log metrics.
Early Stopping: Easily add early stopping with:
trainer = pl.Trainer(callbacks=[EarlyStopping(monitor='val_loss', patience=3)])
Checkpointing: Save and load model checkpoints:
trainer = pl.Trainer(callbacks=[ModelCheckpoint(monitor='val_loss')])
Mixed Precision Training: Enable with a single flag:
trainer = pl.Trainer(precision=16)
Multi-GPU Training: Just specify the number of GPUs:
trainer = pl.Trainer(gpus=4)
PyTorch Lightning offers a powerful, flexible framework for training neural networks that can significantly simplify your code and improve productivity. By abstracting away much of the boilerplate associated with training loops, it allows you to focus on what really matters: your model architecture and data processing.
25/09/2024 | Python
22/11/2024 | Python
26/10/2024 | Python
08/11/2024 | Python
15/11/2024 | Python
05/11/2024 | Python
15/11/2024 | Python
25/09/2024 | Python
25/09/2024 | Python
26/10/2024 | Python
05/10/2024 | Python
17/11/2024 | Python