clearml/examples/frameworks/pytorch-lightning/pytorch_lightning_example.py

96 lines
2.9 KiB
Python
Raw Normal View History

2023-05-11 12:59:11 +00:00
import sys
2020-12-23 22:30:32 +00:00
from argparse import ArgumentParser
2023-05-11 12:59:11 +00:00
2020-12-23 22:30:32 +00:00
import pytorch_lightning as pl
2023-05-11 12:59:11 +00:00
import torch
2020-12-23 22:30:32 +00:00
from torch.nn import functional as F
from torch.utils.data import DataLoader, random_split
from torchvision import transforms
2023-05-11 12:59:11 +00:00
from torchvision.datasets.mnist import MNIST
from clearml import Task
2020-12-23 22:30:32 +00:00
class LitClassifier(pl.LightningModule):
def __init__(self, hidden_dim=128, learning_rate=1e-3):
super().__init__()
self.save_hyperparameters()
self.l1 = torch.nn.Linear(28 * 28, self.hparams.hidden_dim)
self.l2 = torch.nn.Linear(self.hparams.hidden_dim, 10)
def forward(self, x):
x = x.view(x.size(0), -1)
x = torch.relu(self.l1(x))
x = torch.relu(self.l2(x))
return x
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.log('valid_loss', loss)
2023-05-11 12:59:11 +00:00
return loss
2020-12-23 22:30:32 +00:00
def test_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
2023-05-11 12:59:11 +00:00
return loss
2020-12-23 22:30:32 +00:00
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
@staticmethod
def add_model_specific_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument('--hidden_dim', type=int, default=128)
parser.add_argument('--learning_rate', type=float, default=0.0001)
return parser
if __name__ == '__main__':
pl.seed_everything(0)
parser = ArgumentParser()
parser.add_argument('--batch_size', default=32, type=int)
2023-05-11 12:59:11 +00:00
parser.add_argument('--max_epochs', default=3, type=int)
sys.argv.extend(['--max_epochs', '2'])
2020-12-23 22:30:32 +00:00
parser = LitClassifier.add_model_specific_args(parser)
args = parser.parse_args()
2023-07-25 09:19:24 +00:00
Task.init(project_name="examples", task_name="pytorch lightning MNIST")
2023-05-11 12:59:11 +00:00
2020-12-23 22:30:32 +00:00
# ------------
# data
# ------------
dataset = MNIST('', train=True, download=True, transform=transforms.ToTensor())
mnist_test = MNIST('', train=False, download=True, transform=transforms.ToTensor())
mnist_train, mnist_val = random_split(dataset, [55000, 5000])
2023-05-11 12:59:11 +00:00
train_loader = DataLoader(mnist_train, batch_size=args.batch_size)
val_loader = DataLoader(mnist_val, batch_size=args.batch_size)
test_loader = DataLoader(mnist_test, batch_size=args.batch_size)
2020-12-23 22:30:32 +00:00
# ------------
# model
# ------------
model = LitClassifier(args.hidden_dim, args.learning_rate)
# ------------
# training
# ------------
2023-05-11 12:59:11 +00:00
trainer = pl.Trainer(max_epochs=args.max_epochs)
2020-12-23 22:30:32 +00:00
trainer.fit(model, train_loader, val_loader)
# ------------
# testing
# ------------
trainer.test(dataloaders=test_loader)