Pytorch lightning install

Pytorch lightning install DEFAULT

# Lightning Module

import torch
from torch import nn
from torch.nn import functional as F
from import DataLoader
from import random_split
from torchvision.datasets import MNIST
from torchvision import transforms
import pytorch_lightning as pl




self.encoder = nn.Sequential(

nn.Linear(28 * 28, 64),
nn.Linear(64, 3))

self.decoder = nn.Sequential(

nn.Linear(3, 64),
nn.Linear(64, 28 * 28))

embedding = self.encoder(x)




optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)



(self, train_batch, batch_idx):

x, y = train_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('train_loss', loss)



(self, val_batch, batch_idx):

x, y = val_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('val_loss', loss)

# data

dataset = MNIST('', train=True, download=True, transform=transforms.ToTensor())
mnist_train, mnist_val = random_split(dataset, [55000, 5000])

train_loader = DataLoader(mnist_train, batch_size=32)
val_loader = DataLoader(mnist_val, batch_size=32)

# model

# training

trainer = pl.Trainer(gpus=4, precision=16, limit_train_batches=0.5), train_loader, val_loader)

*Codecov is > 90%+ but build delays may show less

PyTorch Lightning is just organized PyTorch

Lightning disentangles PyTorch code to decouple the science from the engineering. PT to PL

Lightning Design Philosophy

Lightning structures PyTorch code with these principles:

Lightning forces the following structure to your code which makes it reusable and shareable:

  • Research code (the LightningModule).
  • Engineering code (you delete, and is handled by the Trainer).
  • Non-essential research code (logging, etc... this goes in Callbacks).
  • Data (use PyTorch DataLoaders or organize them into a LightningDataModule).

Once you do this, you can train on multiple-GPUs, TPUs, CPUs and even in 16-bit precision without changing your code!

Get started with our 2 step guide

Continuous Integration

Lightning is rigorously tested across multiple GPUs, TPUs CPUs and against major Python and PyTorch versions.

How To Use

Step 0: Install

Simple installation from PyPI

pip install pytorch-lightning

Step 1: Add these imports

importosimporttorchfromtorchimportnnimporttorch.nn.functionalasFfromtorchvision.datasetsimportMNISTfromtorch.utils.dataimportDataLoader, random_splitfromtorchvisionimporttransformsimportpytorch_lightningaspl

Step 2: Define a LightningModule (nn.Module subclass)

A LightningModule defines a full system (ie: a GAN, autoencoder, BERT or a simple Image Classifier).

classLitAutoEncoder(pl.LightningModule): def__init__(self): super().__init__() self.encoder=nn.Sequential(nn.Linear(28*28, 128), nn.ReLU(), nn.Linear(128, 3)) self.decoder=nn.Sequential(nn.Linear(3, 128), nn.ReLU(), nn.Linear(128, 28*28)) defforward(self, x): # in lightning, forward defines the prediction/inference actionsembedding=self.encoder(x) returnembeddingdeftraining_step(self, batch, batch_idx): # training_step defines the train loop. It is independent of forwardx, y=batchx=x.view(x.size(0), -1) z=self.encoder(x) x_hat=self.decoder(z) loss=F.mse_loss(x_hat, x) self.log("train_loss", loss) returnlossdefconfigure_optimizers(self): optimizer=torch.optim.Adam(self.parameters(), lr=1e-3) returnoptimizer

Note: Training_step defines the training loop. Forward defines how the LightningModule behaves during inference/prediction.

Step 3: Train!

dataset=MNIST(os.getcwd(), download=True, transform=transforms.ToTensor()) train, val=random_split(dataset, [55000, 5000]) autoencoder=LitAutoEncoder() trainer=pl.Trainer(), DataLoader(train), DataLoader(val))

Advanced features

Lightning has over 40+ advanced features designed for professional AI research at scale.

Here are some examples:

Pro-level control of training loops (advanced users)

For complex/professional level work, you have optional full control of the training loop and optimizers.

classLitAutoEncoder(pl.LightningModule): def__init__(self): super().__init__() self.automatic_optimization=Falsedeftraining_step(self, batch, batch_idx): # access your optimizers with use_pl_optimizer=False. Default is Trueopt_a, opt_b=self.optimizers(use_pl_optimizer=True) loss_a= ... self.manual_backward(loss_a, opt_a) opt_a.step() opt_a.zero_grad() loss_b= ... self.manual_backward(loss_b, opt_b, retain_graph=True) self.manual_backward(loss_b, opt_b) opt_b.step() opt_b.zero_grad()

Advantages over unstructured PyTorch

  • Models become hardware agnostic
  • Code is clear to read because engineering code is abstracted away
  • Easier to reproduce
  • Make fewer mistakes because lightning handles the tricky engineering
  • Keeps all the flexibility (LightningModules are still PyTorch modules), but removes a ton of boilerplate
  • Lightning has dozens of integrations with popular machine learning tools.
  • Tested rigorously with every new PR. We test every combination of PyTorch and Python supported versions, every OS, multi GPUs and even TPUs.
  • Minimal running speed overhead (about 300 ms per epoch compared with pure PyTorch).


Hello world
Contrastive Learning
Reinforcement Learning
Classic ML


The lightning community is maintained by

  • 10+ core contributors who are all a mix of professional engineers, Research Scientists, and Ph.D. students from top AI labs.
  • 480+ active community contributors.

Want to help us build Lightning and reduce boilerplate for thousands of researchers? Learn how to make your first contribution here

Lightning is also part of the PyTorch ecosystem which requires projects to have solid testing, documentation and support.

Asking for help

If you have any questions please:

    1. Hgtv paint colors 2019
    2. Floating ice chest walmart
    3. Seth bike hacks bike
    4. Thunderbolt firmware update lenovo
    5. Keychain extension

    The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

    🐛 Bug

    Hey everyone,

    I am trying to train a model on the GPU workstation of our lab (that has 10 GPUs, of which 1 only is usually in use) using Lightning ad DDP. I have tried with several models (including the BoringModel) without success. In particular, I get a CUDA OOM error when DDP initializes. I tried BoringModel with the following configuration:

    And the output I get is the following:

    The script with the BoringModel I run on our workstation is in this gist.

    However, this doesn't happen on Colab using your BoringModel notebook (my version can be found here).

    I also tried to run locally the same notebook as Colab, and the result at the first attempt is the following:

    At the second attempt, though, it works, as expected (i.e. the model trains with no errors, even with multiple GPUs)! So in the script, I tried to do the following to attempt the fit twice as in the notebook:

    As a result, I get this stack trace:

    Expected behavior

    The models should train without issues.


    • CUDA:
      • GPU:
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
        • TITAN V
      • available: True
      • version: 10.1
    • Packages:
      • numpy: 1.19.2
      • pyTorch_debug: True
      • pyTorch_version: 1.7.0
      • pytorch-lightning: 1.0.6
      • tqdm: 4.52.0
    • System:
      • OS: Linux
      • architecture:
      • processor: x86_64
      • python: 3.8.5
      • version: #1 SMP Fri Oct 18 17:15:30 UTC 2019

    Additional context

    I tried installing torch, torchvision and pl with both Conda and PIP with fresh environments, and still no solution to this problem.

    This happens also if I select (free) GPUs manually by specifying them in the flag as a . Also interestingly, if I run this tutorial notebook by PyTorch that uses vanilla PyTorch DDP, I have no issues whatsoever. Final interesting fact, setting I have no issues.

    Thanks in advance!

    bug / fix help wanted DDP 
    Getting Started with PyTorch Lightning ⚡️

    Pytorch Lightning set up on Jetson Nano/Xavier NX

    I ended up fixing the problem. In short, the problem seemed to be that either the torch version that I possessed was not 1.4.0, or that I needed to use pip instead of pip3. For some reason, pip installed for both pip python 2 and pip3. I would invite other people to evaluate this further. This is a rundown of my documentation on the process:

    The general theme seems that we need to install pytorch lightning with the Pip package manager instead of pip3

    and/or that we need to have torch 1.4.0 with its corresponding torchvision in order to pass the torch>=1.4 requirement

    1. We need to retrieve the Pytorch Variant 1.4.0 and torchvision 0.5.0 (Torch needs to bne >=1.4 but it seems 1.4.0 may be necessary). Prior to this, we already possessed pip3 and a torch and torchvision installation corresponding to that.

    2. We will need both pip and pip3 to install pytorch lightning

    3. Once we have the pip package manager, we need to install torch 1.4.0 on out python 2 environment. We also need to edit the Requirements.txt within pytorch-lightning environment

    4. Install (The pip install seems to also install for pip3 manager)

    5. We can continue to verify with pip list

    It’s difficult to screencap these results using shutter with highlighted similarities, but your pip and pip3 list should look the same with the following and the same versions:

    Pytorch and Torchvision installation for Python 2 and 3:

    Below are pre-built PyTorch pip wheel installers for Python on Jetson Nano, Jetson TX1/TX2, and Jetson Xavier NX/AGX with JetPack 4.2 and newer. Download one of the PyTorch binaries from below for your version of JetPack, and see the installation instructions to run on your Jetson. These pip wheels are built for ARM aarch64 architecture, so run these commands on your Jetson (not on a host PC). PyTorch pip wheels

    Python 2 pip installation:

    Pytorch Lightning forum post (seems correct to a degree but non-working for us):

    Hi there, I’m currently trying to set up Pytorch Lightning on Jetson Nano/Jetson Xavier NX by building from source. So far, I have tried following this thread here: The requirements.txt has been changed and no longer has torchvision and scikit-learn as one of the requirements. However, it seems to seek a torch version>=1.4 as a result of torchmetrics>=0.2.0 (within requirements.txt). My Jetson even has torch 1.8.0 and torchvisio…

    Pytorch Lightning installation (A note article by USAEng on ptl installation):


    Lightning install pytorch

    Lightning in 2 steps¶

    In this guide we’ll show you how to organize your PyTorch code into Lightning in 2 steps.

    Organizing your code with PyTorch Lightning makes your code:

    • Keep all the flexibility (this is all pure PyTorch), but removes a ton of boilerplate

    • More readable by decoupling the research code from the engineering

    • Easier to reproduce

    • Less error-prone by automating most of the training loop and tricky engineering

    • Scalable to any hardware without changing your model

    Here’s a 3 minute conversion guide for PyTorch projects:

    Step 0: Install PyTorch Lightning¶

    You can install using pip

    pip install pytorch-lightning

    Or with conda (see how to install conda here):

    conda install pytorch-lightning -c conda-forge

    You could also use conda environments

    conda activate my_env pip install pytorch-lightning

    Import the following:


    Step 1: Define LightningModule¶

    classLitAutoEncoder(pl.LightningModule):def__init__(self):super().__init__()self.encoder=nn.Sequential(nn.Linear(28*28,64),nn.ReLU(),nn.Linear(64,3))self.decoder=nn.Sequential(nn.Linear(3,64),nn.ReLU(),nn.Linear(64,28*28))defforward(self,x):# in lightning, forward defines the prediction/inference actionsembedding=self.encoder(x)returnembeddingdeftraining_step(self,batch,batch_idx):# training_step defined the train loop.# It is independent of forwardx,y=batchx=x.view(x.size(0),-1)z=self.encoder(x)x_hat=self.decoder(z)loss=F.mse_loss(x_hat,x)# Logging to TensorBoard by defaultself.log("train_loss",loss)returnlossdefconfigure_optimizers(self):optimizer=torch.optim.Adam(self.parameters(),lr=1e-3)returnoptimizer


    A lightning module defines a system not a model.

    Examples of systems are:

    Under the hood a LightningModule is still just a that groups all research code into a single file to make it self-contained:

    • The Train loop

    • The Validation loop

    • The Test loop

    • The Model or system of Models

    • The Optimizer

    You can customize any part of training (such as the backward pass) by overriding any of the 20+ hooks found in Available Callback hooks



    In Lightning we separate training from inference. The training_step defines the full training loop. We encourage users to use the forward to define inference actions.

    For example, in this case we could define the autoencoder to act as an embedding extractor:


    Of course, nothing is stopping you from using forward from within the training_step.


    It really comes down to your application. We do, however, recommend that you keep both intents separate.

    • Use forward for inference (predicting).

    • Use training_step for training.

    More details in lightning module docs.

    Step 2: Fit with Lightning Trainer¶

    First, define the data however you want. Lightning just needs a for the train/val/test splits.


    Next, init the lightning module and the PyTorch Lightning , then call fit with both the data and model.

    # init modelautoencoder=LitAutoEncoder()# most basic trainer, uses good defaults (auto-tensorboard, checkpoints, logs, and more)# trainer = pl.Trainer(gpus=8) (if you have GPUs)trainer=pl.Trainer(),train_loader)

    The automates:


    If you prefer to manually manage optimizers you can use the Manual optimization mode (ie: RL, GANs, etc…).

    That’s it!

    These are the main 2 concepts you need to know in Lightning. All the other features of lightning are either features of the Trainer or LightningModule.

    Basic features¶

    Manual vs automatic optimization¶

    Automatic optimization¶

    With Lightning, you don’t need to worry about when to enable/disable grads, do a backward pass, or update optimizers as long as you return a loss with an attached graph from the training_step, Lightning will automate the optimization.


    Manual optimization¶

    However, for certain research like GANs, reinforcement learning, or something with multiple optimizers or an inner loop, you can turn off automatic optimization and fully control the training loop yourself.

    Turn off automatic optimization and you control the train loop!

    def__init__(self):self.automatic_optimization=Falsedeftraining_step(self,batch,batch_idx):# access your optimizers with use_pl_optimizer=False. Default is Trueopt_a,opt_b=self.optimizers(use_pl_optimizer=True)loss_a=self.generator(batch)opt_a.zero_grad()# use `manual_backward()` instead of `loss.backward` to automate half precision, etc...self.manual_backward(loss_a)opt_a.step()loss_b=self.discriminator(batch)opt_b.zero_grad()self.manual_backward(loss_b)opt_b.step()

    Predict or Deploy¶

    When you’re done training, you have 3 options to use your LightningModule for predictions.

    Option 1: Sub-models¶

    Pull out any model inside your system for predictions.

    # ----------------------------------# to use as embedding extractor# ----------------------------------autoencoder=LitAutoEncoder.load_from_checkpoint("path/to/checkpoint_file.ckpt")encoder_model=autoencoder.encoderencoder_model.eval()# ----------------------------------# to use as image generator# ----------------------------------decoder_model=autoencoder.decoderdecoder_model.eval()

    Option 2: Forward¶

    You can also add a forward method to do predictions however you want.

    # ----------------------------------# using the AE to extract embeddings# ----------------------------------classLitAutoEncoder(LightningModule):def__init__(self):super().__init__()self.encoder=nn.Sequential()defforward(self,x):embedding=self.encoder(x)returnembeddingautoencoder=LitAutoEncoder()autoencoder=autoencoder(torch.rand(1,28*28))
    # ----------------------------------# or using the AE to generate images# ----------------------------------classLitAutoEncoder(LightningModule):def__init__(self):super().__init__()self.decoder=nn.Sequential()defforward(self):z=torch.rand(1,3)image=self.decoder(z)image=image.view(1,1,28,28)returnimageautoencoder=LitAutoEncoder()image_sample=autoencoder()

    Option 3: Production¶

    For production systems, onnx or torchscript are much faster. Make sure you have added a forward method or trace only the sub-models you need.

    # ----------------------------------# torchscript# ----------------------------------autoencoder=LitAutoEncoder(),"")os.path.isfile("")
    # ----------------------------------# onnx# ----------------------------------withtempfile.NamedTemporaryFile(suffix=".onnx",delete=False)astmpfile:autoencoder=LitAutoEncoder()input_sample=torch.randn((1,28*28))autoencoder.to_onnx(,input_sample,export_params=True)os.path.isfile(

    Using CPUs/GPUs/TPUs¶

    It’s trivial to use CPUs, GPUs or TPUs in Lightning. There’s NO NEED to change your code, simply change the options.

    # train on CPUtrainer=Trainer()
    # train on 8 CPUstrainer=Trainer(num_processes=8)
    # train on 1024 CPUs across 128 machinestrainer=pl.Trainer(num_processes=8,num_nodes=128)
    # train on 1 GPUtrainer=pl.Trainer(gpus=1)
    # train on multiple GPUs across nodes (32 gpus here)trainer=pl.Trainer(gpus=4,num_nodes=8)
    # train on gpu 1, 3, 5 (3 gpus total)trainer=pl.Trainer(gpus=[1,3,5])
    # Multi GPU with mixed precisiontrainer=pl.Trainer(gpus=2,precision=16)
    # Train on TPUstrainer=pl.Trainer(tpu_cores=8)

    Without changing a SINGLE line of your code, you can now do the following with the above code:

    # train on TPUs using 16 bit precision# using only half the training data and checking validation every quarter of a training epochtrainer=pl.Trainer(tpu_cores=8,precision=16,limit_train_batches=0.5,val_check_interval=0.25)


    Lightning automatically saves your model. Once you’ve trained, you can load the checkpoints as follows:


    The above checkpoint contains all the arguments needed to init the model and set the state dict. If you prefer to do it manually, here’s the equivalent

    # load the ckptckpt=torch.load("path/to/checkpoint.ckpt")# equivalent to the abovemodel=LitModel()model.load_state_dict(ckpt["state_dict"])

    Data flow¶

    Each loop (training, validation, test) has three hooks you can implement:

    • x_step

    • x_step_end

    • x_epoch_end

    To illustrate how data flows, we’ll use the training loop (ie: x=training)


    The equivalent in Lightning is:


    In the event that you use DP or DDP2 distributed modes (ie: split a batch across GPUs), use the x_step_end to manually aggregate (or don’t implement it to let lightning auto-aggregate for you).

    forbatchindata:model_copies=copy_model_per_gpu(model,num_gpus)batch_split=split_batch_per_gpu(batch,num_gpus)gpu_outs=[]formodel,batch_partinzip(model_copies,batch_split):# LightningModule hookgpu_out=model.training_step(batch_part)gpu_outs.append(gpu_out)# LightningModule hookout=training_step_end(gpu_outs)

    The lightning equivalent is:



    The validation and test loops have the same structure.


    To log to Tensorboard, your favorite logger, and/or the progress bar, use the method which can be called from any method in the LightningModule.


    The method has a few options:

    • on_step (logs the metric at that step in training)

    • on_epoch (automatically accumulates and logs at the end of the epoch)

    • prog_bar (logs to the progress bar)

    • logger (logs to the logger like Tensorboard)

    Depending on where the log is called from, Lightning auto-determines the correct mode for you. But of course you can override the default behavior by manually setting the flags


    Setting on_epoch=True will accumulate your logged values over the full training epoch.



    The loss value shown in the progress bar is smoothed (averaged) over the last values, so it differs from the actual loss returned in the train/validation step.

    You can also use any method of your logger directly:


    Once your training starts, you can view the logs by using your favorite logger or booting up the Tensorboard logs:

    tensorboard --logdir ./lightning_logs


    Lightning automatically shows the loss value returned from in the progress bar. So, no need to explicitly log like this .

    Read more about loggers.

    Optional extensions¶


    A callback is an arbitrary self-contained program that can be executed at arbitrary parts of the training loop.

    Here’s an example adding a not-so-fancy learning rate decay rule:

    frompytorch_lightning.callbacksimportCallbackclassDecayLearningRate(Callback):def__init__(self):self.old_lrs=[]defon_train_start(self,trainer,pl_module):# track the initial learning ratesforopt_idx,optimizerinenumerate(trainer.optimizers):group=[param_group["lr"]forparam_groupinoptimizer.param_groups]self.old_lrs.append(group)defon_train_epoch_end(self,trainer,pl_module,outputs):foropt_idx,optimizerinenumerate(trainer.optimizers):old_lr_group=self.old_lrs[opt_idx]new_lr_group=[]forp_idx,param_groupinenumerate(optimizer.param_groups):old_lr=old_lr_group[p_idx]new_lr=old_lr*0.98new_lr_group.append(new_lr)param_group["lr"]=new_lrself.old_lrs[opt_idx]=new_lr_group# And pass the callback to the Trainerdecay_callback=DecayLearningRate()trainer=Trainer(callbacks=[decay_callback])

    Things you can do with a callback:

    • Send emails at some point in training

    • Grow the model

    • Update learning rates

    • Visualize gradients

    • You are only limited by your imagination

    Learn more about custom callbacks.


    DataLoaders and data processing code tends to end up scattered around. Make your data code reusable by organizing it into a .

    classMNISTDataModule(LightningDataModule):def__init__(self,batch_size=32):super().__init__()self.batch_size=batch_size# When doing distributed training, Datamodules have two optional arguments for# granular control over download/prepare/splitting data:# OPTIONAL, called only on 1 GPU/machinedefprepare_data(self):MNIST(os.getcwd(),train=True,download=True)MNIST(os.getcwd(),train=False,download=True)# OPTIONAL, called for every GPU/machine (assigning state is OK)defsetup(self,stage:Optional[str]=None):# transformstransform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.1307,),(0.3081,))])# split datasetifstagein(None,"fit"):mnist_train=MNIST(os.getcwd(),train=True,transform=transform)self.mnist_train,self.mnist_val=random_split(mnist_train,[55000,5000])ifstage==(None,"test"):self.mnist_test=MNIST(os.getcwd(),train=False,transform=transform)# return the dataloader for each splitdeftrain_dataloader(self):mnist_train=DataLoader(self.mnist_train,batch_size=self.batch_size)returnmnist_traindefval_dataloader(self):mnist_val=DataLoader(self.mnist_val,batch_size=self.batch_size)returnmnist_valdeftest_dataloader(self):mnist_test=DataLoader(self.mnist_test,batch_size=self.batch_size)returnmnist_test

    is designed to enable sharing and reusing data splits and transforms across different projects. It encapsulates all the steps needed to process data: downloading, tokenizing, processing etc.

    Now you can simply pass your to the :

    # init modelmodel=LitModel()# init datadm=MNISTDataModule()# traintrainer=pl.Trainer(),dm)# testtrainer.test(datamodule=dm)

    DataModules are specifically useful for building models based on data. Read more on datamodules.


    Lightning has many tools for debugging. Here is an example of just a few of them:

    # use only 10 train batches and 3 val batchestrainer=Trainer(limit_train_batches=10,limit_val_batches=3)
    # Automatically overfit the sane batch of your model for a sanity testtrainer=Trainer(overfit_batches=1)
    # unit test all the code- hits every line of your code once to see if you have bugs,# instead of waiting hours to crash on validationtrainer=Trainer(fast_dev_run=True)
    # train only 20% of an epochtrainer=Trainer(limit_train_batches=0.2)
    # run validation every 25% of a training epochtrainer=Trainer(val_check_interval=0.25)
    # Profile your code to find speed/memory bottlenecksTrainer(profiler="simple")

    Other cool features¶

    Once you define and train your first Lightning model, you might want to try other cool features like

    Or read our Guide to learn more!

    Grid AI¶

    Grid AI is our native solution for large scale training and tuning on the cloud.

    Get started for free with your GitHub or Google Account here.

    Episode 4: Implementing a PyTorch Trainer: PyTorch Lightning Trainer and callbacks under-the-hood

    PyTorchLightning / pytorch-lightning

    Posted by: robot 1 year, 5 months ago

    The lightweight PyTorch wrapper for ML researchers. Scale your models. Write less boilerplate

    Continuous Integration

    Simple installation from PyPI

    pip install pytorch-lightning



    MNIST on TPUs

    What is it?


    Lightning is a way to organize your PyTorch code to decouple the science code from the engineering. It's more of a PyTorch style-guide than a framework.

    In Lightning, you organize your code into 3 distinct categories:

    1. Research code (goes in the LightningModule).
    2. Engineering code (you delete, and is handled by the Trainer).
    3. Non-essential research code (logging, etc... this goes in Callbacks).

    Here's an example of how to refactor your research code into a LightningModule.

    PT to PL

    The rest of the code is automated by the Trainer! PT to PL

    Testing Rigour

    All the automated code by the Trainer is tested rigorously with every new PR.

    In fact, we also train a few models using a vanilla PyTorch loop and compare with the same model trained using the Trainer to make sure we achieve the EXACT same results. Check out the parity tests here.

    Overall, Lightning guarantees rigorously tested, correct, modern best practices for the automated parts.

    How flexible is it?

    As you see, you're just organizing your PyTorch code - there's no abstraction.

    And for the stuff that the Trainer abstracts out you can override any part you want to do things like implement your own distributed training, 16-bit precision, or even a custom backwards pass.

    For example, here you could do your own backward pass

    classLitModel(LightningModule): defoptimizer_step(self, current_epoch, batch_idx, optimizer, optimizer_idx, second_order_closure=None): optimizer.step() optimizer.zero_grad()

    For anything else you might need, we have an extensive callback system you can use to add arbitrary functionality not implemented by our team in the Trainer.

    Who is Lightning for?

    • Professional researchers
    • PhD students
    • Corporate production teams

    If you're just getting into deep learning, we recommend you learn PyTorch first! Once you've implemented a few models, come back and use all the advanced features of Lightning :)

    What does lightning control for me?

    Everything in Blue! This is how lightning separates the science (red) from the engineering (blue).


    How much effort is it to convert?

    If your code is not a huge mess you should be able to organize it into a LightningModule in less than 1 hour. If your code IS a mess, then you needed to clean up anyhow ;)

    Check out this step-by-step guide.

    Starting a new project?

    Use our seed-project aimed at reproducibility!

    Why do I want to use lightning?

    Although your research/production project might start simple, once you add things like GPU AND TPU training, 16-bit precision, etc, you end up spending more time engineering than researching. Lightning automates AND rigorously tests those parts for you.


    • 8 core contributors who are all a mix of professional engineers, Research Scientists, PhD students from top AI labs.
    • 100+ community contributors.

    Lightning is also part of the PyTorch ecosystem which requires projects to have solid testing, documentation and support.

    README Table of Contents

    Realistic example

    Here's how you would organize a realistic PyTorch project into Lightning.

    PT to PL

    The LightningModule defines a system such as seq-2-seq, GAN, etc... It can ALSO define a simple classifier.

    In summary, you:

    1. Define a LightningModule
    classLitSystem(pl.LightningModule): def__init__(self): super().__init__() # not the best model...self.l1 = torch.nn.Linear(28*28, 10) defforward(self, x): return torch.relu(self.l1(x.view(x.size(0), -1))) deftraining_step(self, batch, batch_idx): ...
    1. Fit it with a Trainer
    from pytorch_lightning import Trainer model = LitSystem() # most basic trainer, uses good defaults trainer = Trainer()

    Check out the COLAB demo here

    What types of research works?

    Anything! Remember, that this is just organized PyTorch code. The Training step defines the core complexity found in the training loop.

    Could be as complex as a seq2seq

    # define what happens for training heredeftraining_step(self, batch, batch_idx): x, y = batch # define your own forward and loss calculation hidden_states =self.encoder(x) # even as complex as a seq-2-seq + attn model# (this is just a toy, non-working example to illustrate) start_token ='<SOS>' last_hidden = torch.zeros(...) loss =0for step inrange(max_seq_len): attn_context =self.attention_nn(hidden_states, start_token) pred =self.decoder(start_token, attn_context, last_hidden) last_hidden = pred pred =self.predict_nn(pred) loss +=self.loss(last_hidden, y[step]) #toy example as well loss = loss / max_seq_len return {'loss': loss}

    Or as basic as CNN image classification

    # define what happens for validation heredefvalidation_step(self, batch, batch_idx): x, y = batch # or as basic as a CNN classification out =self(x) loss = my_loss(out, y) return {'loss': loss}

    And without changing a single line of code, you could run on CPUs

    trainer = Trainer(max_epochs=1)

    Or GPUs

    # 8 GPUs trainer = Trainer(max_epochs=1, gpus=8) # 256 GPUs trainer = Trainer(max_epochs=1, gpus=8, num_nodes=32)

    Or TPUs

    trainer = Trainer(num_tpu_cores=8)

    When you're done training, run the test accuracy


    Lightning has out-of-the-box integration with the popular logging/visualizing frameworks


    Lightning automates 40+ parts of DL/ML research

    • GPU training
    • Distributed GPU (cluster) training
    • TPU training
    • EarlyStopping
    • Logging/Visualizing
    • Checkpointing
    • Experiment management
    • Full list here


    Check out this awesome list of research papers and implementations done with Lightning.


    Check out our introduction guide to get started. Or jump straight into our tutorials.

    Asking for help

    Welcome to the Lightning community!

    If you have any questions, feel free to:

    1. read the docs.
    2. Search through the issues.
    3. Ask on stackoverflow with the tag pytorch-lightning.
    4. Join our slack.


    How do I use Lightning for rapid research?Here's a walk-through

    Why was Lightning created? Lightning has 3 goals in mind:

    1. Maximal flexibility while abstracting out the common boilerplate across research projects.
    2. Reproducibility. If all projects use the LightningModule template, it will be much much easier to understand what's going on and where to look! It will also mean every implementation follows a standard format.
    3. Democratizing PyTorch power user features. Distributed training? 16-bit? know you need them but don't want to take the time to implement? All good... these come built into Lightning.

    How does Lightning compare with Ignite and's a thorough comparison.

    Is this another library I have to learn? Nope! We use pure Pytorch everywhere and don't add unnecessary abstractions!

    Are there plans to support Python 2? Nope.

    Are there plans to support virtualenv? Nope. Please use anaconda or miniconda.

    Which PyTorch versions do you support?

    • PyTorch 1.1.0
      # install pytorch 1.1.0 using the official instructions# install test-tube which supports 1.1.0 pip install test-tube== # install latest Lightning version without upgrading deps pip install -U --no-deps pytorch-lightning
    • PyTorch 1.2.0, 1.3.0, Install via pip as normal

    Custom installation

    Bleeding edge

    If you can't wait for the next release, install the most up to date code with:

    • using GIT (locally clone whole repo with full history)
      pip install git+[email protected] --upgrade
    • using instant zip (last state of the repo without git history)
      pip install --upgrade

    Any release installation

    You can also install any past release from this repository:

    pip install --upgrade

    Lightning team


    Core Maintainers


    If you want to cite the framework feel free to use this (but only if you loved it ):

    @article{falcon2019pytorch, title={PyTorch Lightning}, author={Falcon, WA}, journal={GitHub. Note: https://github. com/williamFalcon/pytorch-lightning Cited by}, volume={3}, year={2019} }
    Share on TwitterShare on Facebook

    Recent Posts






    RSS/ Atom

    You will also like:


    320 321 322 323 324