{"library":"pytorch-lightning","code":"import os\nfrom torch import optim, nn, utils, Tensor\nfrom torchvision.datasets import MNIST\nfrom torchvision.transforms import ToTensor\nimport lightning as L\n\n# 1. Define any number of nn.Modules (or use your current ones)\nencoder = nn.Sequential(nn.Linear(28 * 28, 64), nn.ReLU(), nn.Linear(64, 3))\ndecoder = nn.Sequential(nn.Linear(3, 64), nn.ReLU(), nn.Linear(64, 28 * 28))\n\n# 2. Define the LightningModule\nclass LitAutoEncoder(L.LightningModule):\n    def __init__(self, encoder, decoder):\n        super().__init__()\n        self.encoder = encoder\n        self.decoder = decoder\n\n    def training_step(self, batch, batch_idx):\n        x, _ = batch\n        x = x.view(x.size(0), -1)\n        z = self.encoder(x)\n        x_hat = self.decoder(z)\n        loss = nn.functional.mse_loss(x_hat, x)\n        self.log('train_loss', loss)\n        return loss\n\n    def configure_optimizers(self):\n        optimizer = optim.Adam(self.parameters(), lr=1e-3)\n        return optimizer\n\n# 3. Define a dataset\ndataset = MNIST(os.environ.get('DATASET_PATH', os.getcwd()), download=True, transform=ToTensor())\ntrain_dataloader = utils.data.DataLoader(dataset, batch_size=128)\n\n# 4. Train the model\nmodel = LitAutoEncoder(encoder, decoder)\ntrainer = L.Trainer(limit_train_batches=100, max_epochs=1)\ntrainer.fit(model, train_dataloader)\n\n# 5. Use the model (optional, example prediction step)\n# For inference, set model to eval mode and disable gradients\nmodel.eval()\nwith Tensor.no_grad():\n    sample_input, _ = dataset[0]\n    sample_input = sample_input.view(1, -1)\n    encoded_output = model.encoder(sample_input)\n    decoded_output = model.decoder(encoded_output)\n    print(f\"Original shape: {sample_input.shape}, Encoded shape: {encoded_output.shape}, Decoded shape: {decoded_output.shape}\")","lang":"python","description":"This quickstart demonstrates a minimal autoencoder training loop using `lightning`. It covers defining a `LightningModule`, setting up data loaders, and training with the `Trainer`. The code shows how Lightning automatically handles the training loop, backward passes, and optimizer steps, reducing boilerplate. A simple inference step is included to show how to use the trained model.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":-1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":-1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":-1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":-1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":-1}]}