222/PyTorch-Lightning
/requirements.txt
# PyTorch-Lightning
Lightweight, modular, and extensible PyTorch library for training deep learning models, inspired by Keras.
Quickstart
Installation
```
pip install torch lightning
```
Basic Usage
```python
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import CSVLogger
from model import MyAwesomeModel
# Create a model instance
model = MyAwesomeModel()
# Create a trainer instance
trainer = Trainer(checkpoint_callback=ModelCheckpoint(),
logger=CSVLogger())
# Start training
trainer.fit(model)
```
Logging and Monitoring
Lightning automatically logs all relevant information, including hyperparameters, metrics, and models. You can easily view these logs in a variety of formats, including CSV and TensorBoard.
```python
# Customize the logger configuration
trainer = Trainer(logger=CSVLogger(save_dir="logs", version="my_version"))
# Log custom metrics
trainer.logger.log_metrics({"custom_metric": 0.95})
# Log model checkpoint
trainer.logger.log_model("my_model")
```
ModelCheckpoint Callback
The `ModelCheckpoint` callback is a powerful tool for saving and loading model checkpoints during training. You can configure it to save the best model based on a specific metric, and even load the best model from a checkpoint.
```python
# Save the best model based on the validation loss
trainer = Trainer(checkpoint_callback=ModelCheckpoint(
monitor="val_loss", mode="min"
))
# Load the best model from a checkpoint
trainer = Trainer(checkpoint_callback=ModelCheckpoint(
dirpath="path/to/checkpoint",
filename="best-checkpoint-{epoch:02d}",
save_top_k=1,
))
# Load the best model from a specific epoch
trainer = Trainer(checkpoint_callback=ModelCheckpoint(
dirpath="path/to/checkpoint",
filename="epoch={epoch}",
save_top_k=1,
save_last=True,
))
```
Callbacks
Lightning provides a variety of built-in callbacks, including:
- EarlyStopping: Automatically stops training when the validation loss no longer improves.
- LearningRateMonitor: Tracks the learning rate during training and logs it to the logger.
- ProgressBar: Displays a progress bar during training.
- ModelSummary: Provides a summary of the model's architecture and parameters.
- ModelCheckpoint: Saves model checkpoints during training.
- ModelSummary: Provides a summary of the model's architecture and parameters.
- OptimizerStateSaver: Saves the optimizer state during training.
Logging
Lightning automatically logs all relevant information, including hyperparameters, metrics, and models. You can easily view these logs in a variety of formats, including CSV and TensorBoard.
```python
# Customize the logger configuration
trainer = Trainer(logger=CSVLogger(save_dir="logs", version="my_version"))
# Log custom metrics
trainer.logger.log_metrics({"custom_metric": 0.95})
# Log model checkpoint
trainer.logger.log_model("my_model")
```
Visualization
Lightning provides a variety of visualization tools, including:
- TensorBoard: Automatically logs and visualizes all relevant information during training.
- ImageLogger: Logs images and other visualizations during training.
- PlotlyLogger: Logs plots and other visualizations during training.
- TensorBoardImageLogger: Logs images and other visualizations to TensorBoard.
Distributed Training
Lightning supports both local and distributed training. You can easily distribute training across multiple GPUs, multiple machines, or even across the cloud.
```python
# Local training
trainer = Trainer()
# Distributed training (local multi-GPU)
trainer = Trainer(gpus=2)
# Distributed training (multiple machines)
trainer = Trainer(tp_degree=2, tp_world_size=2)
```
Customization
Lightning is highly customizable, and you can easily extend the library to fit your specific needs. Here are some examples:
- Custom Callbacks: Create your own custom callbacks that perform specific tasks during training.
- Custom Loggers: Create your own custom loggers that log custom information during training.
- Custom Model Hooks: Create your own custom hooks that are called at specific points during training.
- Custom DataLoaders: Create your own custom data loaders that load data in a specific way.
- Custom Training Loop: Create your own custom training loop that trains your model in a specific way.
Contributing
Contributions are welcome! We encourage you to submit pull requests and help us improve the library. Please see the [Contributing Guide](CONTRIBUTING.md) for more information.
Acknowledgements
This library is inspired by Keras, a high-level API for deep learning. It was created by François Chollet and is available at <https://keras.io>. We appreciate the hard work and effort that went into Keras and are grateful for the valuable lessons learned from using it.
License
This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.
# PyTorch-Lightning
轻量级、模块化和可扩展的PyTorch库,旨在训练深度学习模型,灵感来源于Keras。
快速开始
安装
```bash
pip install torch lightning
```
基本用法
```python
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import CSVLogger
from model import MyAwesomeModel
# 创建模型实例
model = MyAwesomeModel()
# 创建一个训练器实例
trainer = Trainer(checkpoint_callback=ModelCheckpoint(),
logger=CSVLogger())