Registry / ai-ml / pytorch-ignite

pytorch-ignite

JSON →
library0.5.4pypypi✓ verified 87d ago

PyTorch-Ignite is a lightweight and user-friendly library designed to simplify training and evaluating neural networks with PyTorch. It provides a high-level API for setting up training loops, handling events, and integrating various experiment tracking tools. Currently at version 0.5.4, it maintains an active release cadence with frequent bug fixes and feature enhancements.

pip install pytorch-ignite
INSTALL
IMPORT
SIG · PYTORCH-IGNITE
P
pytorch-ignite
ai-mlpythonv0.5.4
Install
65.2s avg
Import
9928ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 74.9s
py 3.11
✕ build_error
✓ 69.95s
py 3.12
✕ build_error
✓ 58.95s
py 3.13
✕ build_error
✓ 57.03s
py 3.9
✕ build_error
✕ timeout
4787MB installed
● package 4787MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Engine
from ignite.engine import Engine
Events
from ignite.engine import Events
create_supervised_trainer
from ignite.engine import create_supervised_trainer
Accuracy
from ignite.metrics import Accuracy
from ignite.contrib.metrics import Accuracy
As of v0.5.0, `ignite.contrib.metrics` was moved to `ignite.metrics`.
ModelCheckpoint
from ignite.handlers import ModelCheckpoint
from ignite.contrib.handlers import ModelCheckpoint
As of v0.5.0, `ignite.contrib.handlers` was moved to `ignite.handlers`.

This quickstart demonstrates setting up a basic training loop with PyTorch-Ignite. It defines a simple PyTorch model, creates a trainer and evaluator using `create_supervised_trainer` and `create_supervised_evaluator`, attaches a handler to log results after each epoch, and runs the training process. The example includes dummy data for immediate execution.

import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset from ignite.engine import Engine, Events, create_supervised_trainer, create_supervised_evaluator from ignite.metrics import Accuracy, Loss # 1. Define a simple model, optimizer, loss function class SimpleModel(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(10, 2) def forward(self, x): return self.fc(x) model = SimpleModel() optimizer = optim.SGD(model.parameters(), lr=0.01) criterion = nn.CrossEntropyLoss() # 2. Create dummy data X = torch.randn(100, 10) y = torch.randint(0, 2, (100,)) dataset = TensorDataset(X, y) dataloader = DataLoader(dataset, batch_size=10) # 3. Create trainer and evaluator trainer = create_supervised_trainer(model, optimizer, criterion) evaluator = create_supervised_evaluator(model, criterion, metrics={'accuracy': Accuracy(), 'nll': Loss(criterion)}) # 4. Define handlers for events @trainer.on(Events.EPOCH_COMPLETED) def log_training_results(engine): evaluator.run(dataloader) metrics = evaluator.state.metrics print(f"Epoch {engine.state.epoch}/{engine.state.max_epochs} - Avg accuracy: {metrics['accuracy']:.2f}, Avg loss: {metrics['nll']:.2f}") # 5. Run the training trainer.run(dataloader, max_epochs=2) print("\nTraining complete.")
Debug
Known issues
breakingAll modules under `ignite.contrib.metrics` and `ignite.contrib.handlers` were moved directly to `ignite.metrics` and `ignite.handlers` respectively.
fix
Update all `from ignite.contrib.metrics import ...` to `from ignite.metrics import ...` and `from ignite.contrib.handlers import ...` to `from ignite.handlers import ...`.
affects: >=0.5.0
breakingThe `LRScheduler` handler was refactored to be attached to `Events.ITERATION_STARTED` and now requires an optimizer argument, changing its usage pattern significantly.
fix
Review the official documentation for `ignite.handlers.LRScheduler` to adapt to the new API. Instead of calling it, attach it to the `trainer` engine with the optimizer, e.g., `LRScheduler(optimizer, CosineAnnealingScheduler(lr_values=[1e-1, 1e-3], cycle_size=100)).attach(trainer, Events.ITERATION_STARTED)`.
affects: >=0.4.9
gotchaWhen using `ignite.distributed` (idist) for distributed training, ensure the distributed backend is properly initialized (e.g., `idist.initialize()`) before using `idist` utilities or distributed engines.
fix
Before any `idist` calls, ensure `from ignite.distributed import auto_model_and_optimizer_distributed, init_distributed, ...` and call `init_distributed()` or similar initialization routines appropriate for your distributed setup.
affects: All versions
gotchaEvent filtering with `every`, `once`, `before`, `after` can be powerful but also complex. Misunderstanding their interaction can lead to handlers not being triggered as expected.
fix
Always test event handler logic with simple examples. Refer to the official documentation on 'Event Filtering' for detailed explanations and examples of how `Events.X(every=N, once=M, before=Y, after=Z)` combinations work.
affects: All versions
Errors
Common errors & fixes
AttributeError: module 'ignite.contrib' has no attribute 'metrics'
Attempting to import metrics or handlers from the deprecated `ignite.contrib` module after PyTorch-Ignite v0.5.0.
fix
Change import statements. For example, `from ignite.contrib.metrics import Accuracy` should become `from ignite.metrics import Accuracy`.
TypeError: 'LRScheduler' object is not callable
Trying to call an `LRScheduler` instance directly (e.g., `lr_scheduler(engine)`) after PyTorch-Ignite v0.4.9, where its API changed to an attachable handler.
fix
Instead of calling, attach the `LRScheduler` instance to the trainer. Example: `LRScheduler(optimizer, lr_scheduler_function).attach(trainer, Events.ITERATION_STARTED)`.
ValueError: Distributed environment is not initialized.
Using `ignite.distributed` functionalities (e.g., `idist.spawn`, `idist.get_rank()`) without properly initializing the distributed backend first.
fix
Call `ignite.distributed.init_distributed()` or similar initialization function relevant to your setup (e.g., `torch.distributed.init_process_group` if managing manually) at the start of your script before any distributed operations.
RuntimeError: Expected all tensors to be on the same device, but found tensors on cuda:0 and cpu
A common PyTorch error that can occur in Ignite if models or data are not explicitly moved to the correct device (CPU/GPU) or if different parts of the pipeline are on mixed devices.
fix
Ensure your model and input data are consistently on the same device. Use `.to(device)` on models, tensors, and data loaders (via custom collate_fn) where `device = 'cuda' if torch.cuda.is_available() else 'cpu'`.
Upgrade
Version history
0.5.4latest on PyPI · released Mar 27, 2026
Audit
Dependencies
torchrequiredPyTorch-Ignite is built on top of PyTorch and requires a compatible version of PyTorch to function. It is usually assumed to be pre-installed.
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources