src.eval

 1import numpy as np
 2import torch
 3from rich.progress import track
 4from torch import nn
 5from torch.utils.data import DataLoader
 6from torch.utils.tensorboard.writer import SummaryWriter
 7
 8
 9def eval_iteration(
10    model: nn.Module,
11    loss_fn: nn.Module,
12    dataloader_val: DataLoader,
13    global_step: int,
14    writer: SummaryWriter,
15) -> float:
16    """Perform evaluation on the model using the DataLoader.
17
18    Args:
19        model (nn.Module): Torch model.
20        loss_fn (nn.Module): Torch loss.
21        dataloader_val (DataLoader): A DataLoader that provides `(x, y)` where `x` is the input data and `y` is the true label.
22        global_step (int): The global step for tracking the evaluation.
23        writer (SummaryWriter): Tensorboard writer.
24
25    Returns:
26        float: Average evaluation loss.
27    """
28    # Initialization
29    eval_losses = []
30
31    # evaluation iteration
32    for x, y in track(dataloader_val, description="evaluation"):
33        # Turn off gradients when calculating loss
34        with torch.no_grad():
35            y_hat, _ = model(x)
36            loss = loss_fn(y_hat, y.long())
37            eval_losses.append(loss.item())
38
39    # Calculate the average loss and record it
40    avg_eval_loss = float(np.average(eval_losses))
41    writer.add_scalar(tag="Loss/eval", scalar_value=avg_eval_loss, global_step=global_step)
42
43    return avg_eval_loss
def eval_iteration( model: torch.nn.modules.module.Module, loss_fn: torch.nn.modules.module.Module, dataloader_val: torch.utils.data.dataloader.DataLoader, global_step: int, writer: torch.utils.tensorboard.writer.SummaryWriter) -> float:
10def eval_iteration(
11    model: nn.Module,
12    loss_fn: nn.Module,
13    dataloader_val: DataLoader,
14    global_step: int,
15    writer: SummaryWriter,
16) -> float:
17    """Perform evaluation on the model using the DataLoader.
18
19    Args:
20        model (nn.Module): Torch model.
21        loss_fn (nn.Module): Torch loss.
22        dataloader_val (DataLoader): A DataLoader that provides `(x, y)` where `x` is the input data and `y` is the true label.
23        global_step (int): The global step for tracking the evaluation.
24        writer (SummaryWriter): Tensorboard writer.
25
26    Returns:
27        float: Average evaluation loss.
28    """
29    # Initialization
30    eval_losses = []
31
32    # evaluation iteration
33    for x, y in track(dataloader_val, description="evaluation"):
34        # Turn off gradients when calculating loss
35        with torch.no_grad():
36            y_hat, _ = model(x)
37            loss = loss_fn(y_hat, y.long())
38            eval_losses.append(loss.item())
39
40    # Calculate the average loss and record it
41    avg_eval_loss = float(np.average(eval_losses))
42    writer.add_scalar(tag="Loss/eval", scalar_value=avg_eval_loss, global_step=global_step)
43
44    return avg_eval_loss

Perform evaluation on the model using the DataLoader.

Arguments:
  • model (nn.Module): Torch model.
  • loss_fn (nn.Module): Torch loss.
  • dataloader_val (DataLoader): A DataLoader that provides (x, y) where x is the input data and y is the true label.
  • global_step (int): The global step for tracking the evaluation.
  • writer (SummaryWriter): Tensorboard writer.
Returns:

float: Average evaluation loss.