2020-09-23 04:19:46 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-04-15 15:01:16 +00:00
|
|
|
"""
|
|
|
|
MNIST autoencoder example.
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2021-04-15 15:01:16 +00:00
|
|
|
To run:
|
|
|
|
python autoencoder.py --trainer.max_epochs=50
|
|
|
|
"""
|
2020-11-20 18:10:40 +00:00
|
|
|
|
2020-09-23 04:19:46 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn.functional as F
|
2020-11-20 18:10:40 +00:00
|
|
|
from torch import nn
|
2020-12-29 08:19:02 +00:00
|
|
|
from torch.utils.data import DataLoader, random_split
|
2020-09-23 21:58:03 +00:00
|
|
|
|
2020-11-20 18:10:40 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-04-13 16:33:32 +00:00
|
|
|
from pl_examples import _DATASETS_PATH, _TORCHVISION_MNIST_AVAILABLE, cli_lightning_logo
|
2021-04-15 15:01:16 +00:00
|
|
|
from pytorch_lightning.utilities.cli import LightningCLI
|
2021-04-13 16:33:32 +00:00
|
|
|
from pytorch_lightning.utilities.imports import _TORCHVISION_AVAILABLE
|
2020-11-20 18:10:40 +00:00
|
|
|
|
2021-03-11 11:19:48 +00:00
|
|
|
if _TORCHVISION_AVAILABLE:
|
2020-09-23 21:58:03 +00:00
|
|
|
from torchvision import transforms
|
2021-03-11 11:19:48 +00:00
|
|
|
if _TORCHVISION_MNIST_AVAILABLE:
|
|
|
|
from torchvision.datasets import MNIST
|
2020-11-20 18:10:40 +00:00
|
|
|
else:
|
2021-02-08 10:52:02 +00:00
|
|
|
from tests.helpers.datasets import MNIST
|
2020-09-23 04:19:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LitAutoEncoder(pl.LightningModule):
|
2020-12-17 10:13:48 +00:00
|
|
|
"""
|
|
|
|
>>> LitAutoEncoder() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
|
|
|
LitAutoEncoder(
|
|
|
|
(encoder): ...
|
|
|
|
(decoder): ...
|
|
|
|
)
|
|
|
|
"""
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2021-03-24 08:27:08 +00:00
|
|
|
def __init__(self, hidden_dim: int = 64):
|
2020-09-23 04:19:46 +00:00
|
|
|
super().__init__()
|
|
|
|
self.encoder = nn.Sequential(
|
2021-03-24 08:27:08 +00:00
|
|
|
nn.Linear(28 * 28, hidden_dim),
|
2020-09-23 04:19:46 +00:00
|
|
|
nn.ReLU(),
|
2021-03-24 08:27:08 +00:00
|
|
|
nn.Linear(hidden_dim, 3),
|
2020-09-23 04:19:46 +00:00
|
|
|
)
|
|
|
|
self.decoder = nn.Sequential(
|
2021-03-24 08:27:08 +00:00
|
|
|
nn.Linear(3, hidden_dim),
|
2020-09-23 04:19:46 +00:00
|
|
|
nn.ReLU(),
|
2021-03-24 08:27:08 +00:00
|
|
|
nn.Linear(hidden_dim, 28 * 28),
|
2020-09-23 04:19:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
# in lightning, forward defines the prediction/inference actions
|
|
|
|
embedding = self.encoder(x)
|
|
|
|
return embedding
|
|
|
|
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
z = self.encoder(x)
|
|
|
|
x_hat = self.decoder(z)
|
|
|
|
loss = F.mse_loss(x_hat, x)
|
2020-09-30 12:31:16 +00:00
|
|
|
return loss
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2021-02-16 19:31:07 +00:00
|
|
|
def validation_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
z = self.encoder(x)
|
|
|
|
x_hat = self.decoder(z)
|
|
|
|
loss = F.mse_loss(x_hat, x)
|
|
|
|
self.log('valid_loss', loss, on_step=True)
|
|
|
|
|
|
|
|
def test_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
z = self.encoder(x)
|
|
|
|
x_hat = self.decoder(z)
|
|
|
|
loss = F.mse_loss(x_hat, x)
|
|
|
|
self.log('test_loss', loss, on_step=True)
|
|
|
|
|
2020-09-23 04:19:46 +00:00
|
|
|
def configure_optimizers(self):
|
|
|
|
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
|
|
|
|
return optimizer
|
|
|
|
|
|
|
|
|
2021-04-15 15:01:16 +00:00
|
|
|
class MyDataModule(pl.LightningDataModule):
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
batch_size: int = 32,
|
|
|
|
):
|
|
|
|
super().__init__()
|
|
|
|
dataset = MNIST(_DATASETS_PATH, train=True, download=True, transform=transforms.ToTensor())
|
|
|
|
self.mnist_test = MNIST(_DATASETS_PATH, train=False, download=True, transform=transforms.ToTensor())
|
|
|
|
self.mnist_train, self.mnist_val = random_split(dataset, [55000, 5000])
|
|
|
|
self.batch_size = batch_size
|
|
|
|
|
|
|
|
def train_dataloader(self):
|
|
|
|
return DataLoader(self.mnist_train, batch_size=self.batch_size)
|
|
|
|
|
|
|
|
def val_dataloader(self):
|
|
|
|
return DataLoader(self.mnist_val, batch_size=self.batch_size)
|
|
|
|
|
|
|
|
def test_dataloader(self):
|
|
|
|
return DataLoader(self.mnist_test, batch_size=self.batch_size)
|
|
|
|
|
|
|
|
|
2020-09-23 04:19:46 +00:00
|
|
|
def cli_main():
|
2021-04-15 15:01:16 +00:00
|
|
|
cli = LightningCLI(LitAutoEncoder, MyDataModule, seed_everything_default=1234)
|
2021-05-13 10:18:03 +00:00
|
|
|
cli.trainer.test(cli.model, datamodule=cli.datamodule)
|
2020-09-23 04:19:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-12-17 09:21:00 +00:00
|
|
|
cli_lightning_logo()
|
2020-09-23 04:19:46 +00:00
|
|
|
cli_main()
|