<jupyter_start><jupyter_text>Neptune + PyTorch Lightning  Install dependencies<jupyter_code>! pip install --quiet neptune-client>=0.11.0 pytorch-lightning>=1.5.0 scikit-learn scikit-plot==0.3.7 matplotlib==3.4.2<jupyter_output><empty_output><jupyter_text>Import libraries<jupyter_code>import os

import numpy as np
import torch
from sklearn.metrics import accuracy_score
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import MNIST

from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.loggers import NeptuneLogger<jupyter_output><empty_output><jupyter_text>Define hyper-parameters<jupyter_code>PARAMS = {
    "batch_size": 32,
    "lr": 0.007,
    "max_epochs": 15,
}<jupyter_output><empty_output><jupyter_text>Define LightningModule and DataLoader<jupyter_code># (neptune) define LightningModule with logging (self.log)
class MNISTModel(LightningModule):
    def __init__(self):
        super().__init__()
        self.l1 = torch.nn.Linear(28 * 28, 10)

    def forward(self, x):
        return torch.relu(self.l1(x.view(x.size(0), -1)))

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = F.cross_entropy(y_hat, y)
        self.log("metrics/batch/loss", loss, prog_bar=False)

        y_true = y.cpu().detach().numpy()
        y_pred = y_hat.argmax(axis=1).cpu().detach().numpy()
        acc = accuracy_score(y_true, y_pred)
        self.log("metrics/batch/acc", acc)

        return {"loss": loss,
                "y_true": y_true,
                "y_pred": y_pred}

    def training_epoch_end(self, outputs):
        loss = np.array([])
        y_true = np.array([])
        y_pred = np.array([])
        for results_dict in outputs:
            loss = np.append(loss, results_dict["loss"])
            y_true = np.append(y_true, results_dict["y_true"])
            y_pred = np.append(y_pred, results_dict["y_pred"])
        acc = accuracy_score(y_true, y_pred)
        self.log("metrics/epoch/loss", loss.mean())
        self.log("metrics/epoch/acc", acc)

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=PARAMS["lr"])

# init model
mnist_model = MNISTModel()

# init DataLoader from MNIST dataset
train_ds = MNIST(os.getcwd(), train=True, download=True, transform=transforms.ToTensor())
train_loader = DataLoader(train_ds, batch_size=PARAMS["batch_size"])<jupyter_output><empty_output><jupyter_text>Create NeptuneLogger<jupyter_code>neptune_logger = NeptuneLogger(
    api_key="ANONYMOUS",
    project="common/pytorch-lightning-integration",
    tags=["simple", "showcase"],
    log_model_checkpoints=False,
)<jupyter_output><empty_output><jupyter_text>Initialize a trainer and pass neptune_logger<jupyter_code># (neptune) initialize a trainer and pass neptune_logger
trainer = Trainer(
    logger=neptune_logger,
    max_epochs=PARAMS["max_epochs"],
)<jupyter_output><empty_output><jupyter_text>Log hyper-parameters to the Run<jupyter_code>neptune_logger.log_hyperparams(params=PARAMS)<jupyter_output><empty_output><jupyter_text>Train the model log metadata to the Neptune run<jupyter_code>trainer.fit(mnist_model, train_loader)<jupyter_output><empty_output><jupyter_text>Stop logging**Warning:**Once you are done logging, you should stop the run using the `stop()` method, which is method of the run object. Run object itself is part of the `neptune_logger`.This is needed only while logging from a notebook environment. While logging through a script, Neptune automatically stops tracking once the script has completed execution.<jupyter_code>neptune_logger.experiment.stop()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11453862660944206, 'fraction_numerical': 0.008583690987124463, 'mean_word_length': 4.710566615620214, 'pattern_counts': {'":': 6, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '625455', 'license': 'MIT', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1247, 'n_words': 257, 'path': 'integrations-and-supported-tools/pytorch-lightning/notebooks/Neptune_PyTorch_Lightning.ipynb', 'repo_name': 'neptune-ai/examples', 'size': '3728'}