Created
December 2, 2022 12:09
-
-
Save tchaton/9575abd7b323a70e85d455e6327bc299 to your computer and use it in GitHub Desktop.
Boring Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import torch | |
from torch.utils.data import DataLoader, Dataset | |
from pytorch_lightning import LightningModule, Trainer | |
class RandomDataset(Dataset): | |
def __init__(self, size, length): | |
self.len = length | |
self.data = torch.randn(length, size) | |
def __getitem__(self, index): | |
return self.data[index] | |
def __len__(self): | |
return self.len | |
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): | |
loss = self(batch).sum() | |
self.log("train_loss", loss) | |
return {"loss": loss} | |
def validation_step(self, batch, batch_idx): | |
loss = self(batch).sum() | |
self.log("valid_loss", loss) | |
def test_step(self, batch, batch_idx): | |
loss = self(batch).sum() | |
self.log("test_loss", loss) | |
def configure_optimizers(self): | |
return torch.optim.SGD(self.layer.parameters(), lr=0.1) | |
def run(): | |
train_data = DataLoader(RandomDataset(32, 64), batch_size=2) | |
val_data = DataLoader(RandomDataset(32, 64), batch_size=2) | |
test_data = DataLoader(RandomDataset(32, 64), batch_size=2) | |
model = BoringModel() | |
trainer = Trainer( | |
default_root_dir=os.getcwd(), | |
limit_train_batches=10, | |
limit_val_batches=10, | |
limit_test_batches=10, | |
num_sanity_val_steps=5, | |
max_epochs=100, | |
enable_model_summary=False, | |
) | |
trainer.fit(model, train_dataloaders=train_data, val_dataloaders=val_data) | |
trainer.test(model, dataloaders=test_data) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment