<jupyter_start><jupyter_text>livelossplot example: PyTorch IgniteLast update: `livelossplot 0.5.0`. For code and documentation, see [livelossplot GitHub repository](https://github.com/stared/livelossplot).     NoteThe syntax changed, and from `0.5+` on it is):```{python}from livelossplot import PlotLossesIgnite```<jupyter_code>!pip install pytorch-ignite livelossplot --quiet
%matplotlib inline

from torch import nn
import torch.optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision.transforms import Compose, ToTensor, Normalize
from torchvision.datasets import MNIST

from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
from ignite.metrics import Accuracy, Loss
from ignite.contrib.handlers import ProgressBar

from livelossplot import PlotLossesIgnite 
TRAIN_BATCH_SIZE = 100
TEST_BATCH_SIZE = 100
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=-1)
def get_data_loaders(train_batch_size, val_batch_size):
    data_transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,))])

    train_loader = DataLoader(
        MNIST(download=True, root=".", transform=data_transform, train=True), batch_size=train_batch_size, shuffle=True
    )

    val_loader = DataLoader(
        MNIST(download=False, root=".", transform=data_transform, train=False), batch_size=val_batch_size, shuffle=False
    )
    return train_loader, val_loader
model = Net()
train_loader, val_loader = get_data_loaders(TRAIN_BATCH_SIZE, TEST_BATCH_SIZE)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.8)
loss = torch.nn.NLLLoss()
trainer = create_supervised_trainer(model, optimizer, loss)
metrics = metrics={
    'val_acc': Accuracy(),
    'val_loss': Loss(loss)
}
p_bar = ProgressBar()
p_bar.attach(trainer)
evaluator = create_supervised_evaluator(model, metrics=metrics)
@trainer.on(Events.EPOCH_COMPLETED)
def validate(trainer):
    evaluator.run(val_loader)
callback = PlotLossesIgnite()
callback.attach(evaluator)
trainer.run(train_loader, max_epochs=100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11893939393939394, 'fraction_numerical': 0.026136363636363635, 'mean_word_length': 4.753812636165577, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '506812', 'license': 'MIT', 'n_tokens_mistral': 974, 'n_tokens_neox': 924, 'n_words': 187, 'path': 'examples/pytorch-ignite.ipynb', 'repo_name': 'je-santos/livelossplot', 'size': '2640'}