lightning/tests/models/debug.py

55 lines
1.6 KiB
Python
Raw Normal View History

2019-07-25 16:01:52 +00:00
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
import pytorch_lightning as pl
# from test_models import assert_ok_test_acc, load_model, \
# clear_save_dir, get_test_tube_logger, get_hparams, init_save_dir, \
# init_checkpoint_callback, reset_seed, set_random_master_port
2019-07-25 16:01:52 +00:00
2019-08-07 06:02:55 +00:00
class CoolModel(pl.LightningModule):
2019-07-25 16:01:52 +00:00
def __init(self):
super(CoolModel, self).__init__()
# not the best model...
self.l1 = torch.nn.Linear(28 * 28, 10)
def forward(self, x):
return torch.relu(self.l1(x))
def my_loss(self, y_hat, y):
return F.cross_entropy(y_hat, y)
def training_step(self, batch, batch_idx):
2019-07-25 16:01:52 +00:00
x, y = batch
y_hat = self.forward(x)
return {'training_loss': self.my_loss(y_hat, y)}
2019-07-25 16:01:52 +00:00
def validation_step(self, batch, batch_idx):
2019-07-25 16:01:52 +00:00
x, y = batch
y_hat = self.forward(x)
return {'val_loss': self.my_loss(y_hat, y)}
def validation_end(self, outputs):
avg_loss = torch.stack([x for x in outputs['val_loss']]).mean()
return avg_loss
def configure_optimizers(self):
return [torch.optim.Adam(self.parameters(), lr=0.02)]
2019-08-07 06:02:55 +00:00
@pl.data_loader
def train_dataloader(self):
2019-07-25 16:01:52 +00:00
return DataLoader(MNIST('path/to/save', train=True), batch_size=32)
2019-08-07 06:02:55 +00:00
@pl.data_loader
2019-07-25 16:01:52 +00:00
def val_dataloader(self):
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
2019-08-07 06:02:55 +00:00
@pl.data_loader
2019-07-25 16:01:52 +00:00
def test_dataloader(self):
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)