class_docstr: The Model class encapsulates a PyTorch network, a PyTorch optimizer, a loss function and
  metric functions. It allows the user to train a neural network without hand-coding the
  epoch/step logic.
  
  Args:
      network (torch.nn.Module): A PyTorch network.
      optimizer (Union[torch.optim.Optimizer, str, dict]): If torch.optim.Optimier, an initialized PyTorch.
          If str, should be the name of the optimizer in Pytorch (i.e. 'Adam' for torch.optim.Adam).
          If dict, should contain a key ``'optim'`` with the value be the name of the optimizer; other
          entries are passed to the optimizer as keyword arguments.
          (Default value = None)
      loss_function(Union[Callable, str]) It can be any PyTorch loss layer or custom loss function. It
          can also be a string with the same name as a PyTorch loss function (either the functional or
          object name). The loss function must have the signature ``loss_function(input, target)`` where
          ``input`` is the prediction of the network and ``target`` is the ground truth.
          (Default value = None)
      batch_metrics (list): List of functions with the same signature as a loss function or objects with the same
          signature as either :class:`~poutyne.Metric` or :class:`torchmetrics.Metric <torchmetrics.Metric>`. It can
          also be a string with the same name as a PyTorch loss function (either the functional or object name).
          Some metrics, such as  'accuracy' (or just 'acc'), are also available as strings. See :ref:`metrics` and
          the `TorchMetrics documentation <https://torchmetrics.readthedocs.io/en/latest/references/modules.html>`__
          for available metrics.
  
          Batch metric are computed on computed for each batch.
          (Default value = None)
  
          .. warning:: When using this argument, the metrics are computed for each batch. This can significantly slow
              down the compuations depending on the metrics used. This mostly happens on non-decomposable metrics
              such as :class:`torchmetrics.AUROC <torchmetrics.AUROC>` where an ordering of the elements is necessary
              to compute the metric. In such case, we advise to use them as epoch metrics instead.
      epoch_metrics (list): List of functions with the same signature as a loss function or objects with the same
          signature as either :class:`~poutyne.Metric` or :class:`torchmetrics.Metric <torchmetrics.Metric>`. It can
          also be a string with the same name as a PyTorch loss function (either the functional or object name).
          Some metrics, such as  'accuracy' (or just 'acc'), are also available as strings. See :ref:`metrics` and
          the `TorchMetrics documentation <https://torchmetrics.readthedocs.io/en/latest/references/modules.html>`__
          for available metrics.
  
          Epoch metrics are computed only at the end of the epoch.
          (Default value = None)
      device (Union[torch.torch.device, List[torch.torch.device]]): The device to which the network is
          sent or the list of device to which the network is sent. See :func:`~Model.to()` for details.
  
  Note:
      The name of each batch and epoch metric can be change by passing a tuple ``(name, metric)`` instead
      of simply the metric function or object, where ``name`` is the alternative name of the metric.
      Batch and epoch metrics can return multiple metrics (e.g. an epoch metric could return an F1-score
      with the associated precision and recall). See :ref:`multiple metrics at once` for more details.
  
  
  Attributes:
      network (torch.nn.Module): The associated PyTorch network.
      optimizer (torch.optim.Optimizer): The associated PyTorch optimizer.
      loss_function: The associated loss function.
      batch_metrics (list): The associated metric functions for every batch.
      epoch_metrics (list): The associated metric functions for every epoch.
  
  Examples:
      Using Numpy arrays (or tensors) dataset::
  
          from poutyne import Model
          import torch
          import numpy as np
          import torchmetrics
  
          num_features = 20
          num_classes = 5
  
          # Our training dataset with 800 samples.
          num_train_samples = 800
          train_x = np.random.randn(num_train_samples, num_features).astype('float32')
          train_y = np.random.randint(num_classes, size=num_train_samples).astype('int64')
  
          # Our validation dataset with 200 samples.
          num_valid_samples = 200
          valid_x = np.random.randn(num_valid_samples, num_features).astype('float32')
          valid_y = np.random.randint(num_classes, size=num_valid_samples).astype('int64')
  
          pytorch_network = torch.nn.Linear(num_features, num_classes) # Our network
  
          # We create and optimize our model
          model = Model(pytorch_network, 'sgd', 'cross_entropy',
                        batch_metrics=['accuracy'],
                        epoch_metrics=[torchmetrics.AUROC(num_classes=num_classes, task="multiclass")])
          model.fit(train_x, train_y,
                    validation_data=(valid_x, valid_y),
                    epochs=5,
                    batch_size=32)
  
      .. code-block:: none
  
          Epoch: 1/5 Train steps: 25 Val steps: 7 0.51s loss: 1.757784 acc: 20.750000 auroc: 0.494891
          val_loss: 1.756639 val_acc: 18.500000 val_auroc: 0.499404
          Epoch: 2/5 Train steps: 25 Val steps: 7 0.03s loss: 1.749623 acc: 20.375000 auroc: 0.496878
          val_loss: 1.748795 val_acc: 19.000000 val_auroc: 0.499723
          Epoch: 3/5 Train steps: 25 Val steps: 7 0.03s loss: 1.742070 acc: 20.250000 auroc: 0.499461
          val_loss: 1.741379 val_acc: 19.000000 val_auroc: 0.498577
          ...
  
      Using PyTorch DataLoader::
  
         import torch
         from torch.utils.data import DataLoader, TensorDataset
         from poutyne import Model
         import torchmetrics
  
         num_features = 20
         num_classes = 5
  
         # Our training dataset with 800 samples.
         num_train_samples = 800
         train_x = torch.rand(num_train_samples, num_features)
         train_y = torch.randint(num_classes, (num_train_samples,), dtype=torch.long)
         train_dataset = TensorDataset(train_x, train_y)
         train_generator = DataLoader(train_dataset, batch_size=32)
  
         # Our validation dataset with 200 samples.
         num_valid_samples = 200
         valid_x = torch.rand(num_valid_samples, num_features)
         valid_y = torch.randint(num_classes, (num_valid_samples,), dtype=torch.long)
         valid_dataset = TensorDataset(valid_x, valid_y)
         valid_generator = DataLoader(valid_dataset, batch_size=32)
  
         pytorch_network = torch.nn.Linear(num_features, num_classes)
  
         model = Model(pytorch_network, 'sgd', 'cross_entropy',
                       batch_metrics=['accuracy'],
                       epoch_metrics=[torchmetrics.AUROC(num_classes=num_classes, task="multiclass")])
         model.fit_generator(train_generator,
                             valid_generator,
                             epochs=5)
  
      .. code-block:: none
  
          Epoch: 1/5 Train steps: 25 Val steps: 7 0.07s loss: 1.614473 acc: 20.500000 auroc: 0.516850
          val_loss: 1.617141 val_acc: 21.500000 val_auroc: 0.522068
          Epoch: 2/5 Train steps: 25 Val steps: 7 0.03s loss: 1.614454 acc: 20.125000 auroc: 0.517618
          val_loss: 1.615585 val_acc: 22.000000 val_auroc: 0.521051
          Epoch: 3/5 Train steps: 25 Val steps: 7 0.03s loss: 1.613709 acc: 20.125000 auroc: 0.518307
          val_loss: 1.614440 val_acc: 22.000000 val_auroc: 0.520762
          ...
class_docstr_tok: ["The","Model","class","encapsulates","a","PyTorch","network",",","a","PyTorch","optimizer",",","a","loss","function","and","metric","functions",".","It","allows","the","user","to","train","a","neural","network","without","hand-coding","the","epoch\/step","logic",".","Args",":","network","(","torch.nn.Module",")",":","A","PyTorch","network",".","optimizer","(","Union","[","torch.optim.Optimizer",",","str",",","dict","]",")",":","If","torch.optim.Optimier",",","an","initialized","PyTorch",".","If","str",",","should","be","the","name","of","the","optimizer","in","Pytorch","(","i.e",".","'Adam","'","for","torch.optim.Adam",")",".","If","dict",",","should","contain","a","key","``","'optim","'","``","with","the","value","be","the","name","of","the","optimizer",";","other","entries","are","passed","to","the","optimizer","as","keyword","arguments",".","(","Default","value","=","None",")","loss_function","(","Union","[","Callable",",","str","]",")","It","can","be","any","PyTorch","loss","layer","or","custom","loss","function",".","It","can","also","be","a","string","with","the","same","name","as","a","PyTorch","loss","function","(","either","the","functional","or","object","name",")",".","The","loss","function","must","have","the","signature","``","loss_function","(","input",",","target",")","``","where","``","input","``","is","the","prediction","of","the","network","and","``","target","``","is","the","ground","truth",".","(","Default","value","=","None",")","batch_metrics","(","list",")",":","List","of","functions","with","the","same","signature","as","a","loss","function","or","objects","with","the","same","signature","as","either",":","class",":","`","~poutyne.Metric","`","or",":","class",":","`","torchmetrics.Metric","<","torchmetrics.Metric",">","`",".","It","can","also","be","a","string","with","the","same","name","as","a","PyTorch","loss","function","(","either","the","functional","or","object","name",")",".","Some","metrics",",","such","as","'accuracy","'","(","or","just","'acc","'",")",",","are","also","available","as","strings",".","See",":","ref",":","`","metrics","`","and","the","`","TorchMetrics","documentation","<","https",":","\/\/torchmetrics.readthedocs.io\/en\/latest\/references\/modules.html",">","`","__","for","available","metrics",".","Batch","metric","are","computed","on","computed","for","each","batch",".","(","Default","value","=","None",")","..","warning",":",":","When","using","this","argument",",","the","metrics","are","computed","for","each","batch",".","This","can","significantly","slow","down","the","compuations","depending","on","the","metrics","used",".","This","mostly","happens","on","non-decomposable","metrics","such","as",":","class",":","`","torchmetrics.AUROC","<","torchmetrics.AUROC",">","`","where","an","ordering","of","the","elements","is","necessary","to","compute","the","metric",".","In","such","case",",","we","advise","to","use","them","as","epoch","metrics","instead",".","epoch_metrics","(","list",")",":","List","of","functions","with","the","same","signature","as","a","loss","function","or","objects","with","the","same","signature","as","either",":","class",":","`","~poutyne.Metric","`","or",":","class",":","`","torchmetrics.Metric","<","torchmetrics.Metric",">","`",".","It","can","also","be","a","string","with","the","same","name","as","a","PyTorch","loss","function","(","either","the","functional","or","object","name",")",".","Some","metrics",",","such","as","'accuracy","'","(","or","just","'acc","'",")",",","are","also","available","as","strings",".","See",":","ref",":","`","metrics","`","and","the","`","TorchMetrics","documentation","<","https",":","\/\/torchmetrics.readthedocs.io\/en\/latest\/references\/modules.html",">","`","__","for","available","metrics",".","Epoch","metrics","are","computed","only","at","the","end","of","the","epoch",".","(","Default","value","=","None",")","device","(","Union","[","torch.torch.device",",","List","[","torch.torch.device","]","]",")",":","The","device","to","which","the","network","is","sent","or","the","list","of","device","to","which","the","network","is","sent",".","See",":","func",":","`","~Model.to","(",")","`","for","details",".","Note",":","The","name","of","each","batch","and","epoch","metric","can","be","change","by","passing","a","tuple","``","(","name",",","metric",")","``","instead","of","simply","the","metric","function","or","object",",","where","``","name","``","is","the","alternative","name","of","the","metric",".","Batch","and","epoch","metrics","can","return","multiple","metrics","(","e.g",".","an","epoch","metric","could","return","an","F1-score","with","the","associated","precision","and","recall",")",".","See",":","ref",":","`","multiple","metrics","at","once","`","for","more","details",".","Attributes",":","network","(","torch.nn.Module",")",":","The","associated","PyTorch","network",".","optimizer","(","torch.optim.Optimizer",")",":","The","associated","PyTorch","optimizer",".","loss_function",":","The","associated","loss","function",".","batch_metrics","(","list",")",":","The","associated","metric","functions","for","every","batch",".","epoch_metrics","(","list",")",":","The","associated","metric","functions","for","every","epoch",".","Examples",":","Using","Numpy","arrays","(","or","tensors",")","dataset",":",":","from","poutyne","import","Model","import","torch","import","numpy","as","np","import","torchmetrics","num_features","=","20","num_classes","=","5","#","Our","training","dataset","with","800","samples",".","num_train_samples","=","800","train_x","=","np.random.randn","(","num_train_samples",",","num_features",")",".astype","(","'float32","'",")","train_y","=","np.random.randint","(","num_classes",",","size=num_train_samples",")",".astype","(","'int64","'",")","#","Our","validation","dataset","with","200","samples",".","num_valid_samples","=","200","valid_x","=","np.random.randn","(","num_valid_samples",",","num_features",")",".astype","(","'float32","'",")","valid_y","=","np.random.randint","(","num_classes",",","size=num_valid_samples",")",".astype","(","'int64","'",")","pytorch_network","=","torch.nn.Linear","(","num_features",",","num_classes",")","#","Our","network","#","We","create","and","optimize","our","model","model","=","Model","(","pytorch_network",",","'sgd","'",",","'cross_entropy","'",",","batch_metrics=","[","'accuracy","'","]",",","epoch_metrics=","[","torchmetrics.AUROC","(","num_classes=num_classes",",","task=","''","multiclass","''",")","]",")","model.fit","(","train_x",",","train_y",",","validation_data=","(","valid_x",",","valid_y",")",",","epochs=5",",","batch_size=32",")","..","code-block",":",":","none","Epoch",":","1\/5","Train","steps",":","25","Val","steps",":","7","0.51s","loss",":","1.757784","acc",":","20.750000","auroc",":","0.494891","val_loss",":","1.756639","val_acc",":","18.500000","val_auroc",":","0.499404","Epoch",":","2\/5","Train","steps",":","25","Val","steps",":","7","0.03s","loss",":","1.749623","acc",":","20.375000","auroc",":","0.496878","val_loss",":","1.748795","val_acc",":","19.000000","val_auroc",":","0.499723","Epoch",":","3\/5","Train","steps",":","25","Val","steps",":","7","0.03s","loss",":","1.742070","acc",":","20.250000","auroc",":","0.499461","val_loss",":","1.741379","val_acc",":","19.000000","val_auroc",":","0.498577","...","Using","PyTorch","DataLoader",":",":","import","torch","from","torch.utils.data","import","DataLoader",",","TensorDataset","from","poutyne","import","Model","import","torchmetrics","num_features","=","20","num_classes","=","5","#","Our","training","dataset","with","800","samples",".","num_train_samples","=","800","train_x","=","torch.rand","(","num_train_samples",",","num_features",")","train_y","=","torch.randint","(","num_classes",",","(","num_train_samples",",",")",",","dtype=torch.long",")","train_dataset","=","TensorDataset","(","train_x",",","train_y",")","train_generator","=","DataLoader","(","train_dataset",",","batch_size=32",")","#","Our","validation","dataset","with","200","samples",".","num_valid_samples","=","200","valid_x","=","torch.rand","(","num_valid_samples",",","num_features",")","valid_y","=","torch.randint","(","num_classes",",","(","num_valid_samples",",",")",",","dtype=torch.long",")","valid_dataset","=","TensorDataset","(","valid_x",",","valid_y",")","valid_generator","=","DataLoader","(","valid_dataset",",","batch_size=32",")","pytorch_network","=","torch.nn.Linear","(","num_features",",","num_classes",")","model","=","Model","(","pytorch_network",",","'sgd","'",",","'cross_entropy","'",",","batch_metrics=","[","'accuracy","'","]",",","epoch_metrics=","[","torchmetrics.AUROC","(","num_classes=num_classes",",","task=","''","multiclass","''",")","]",")","model.fit_generator","(","train_generator",",","valid_generator",",","epochs=5",")","..","code-block",":",":","none","Epoch",":","1\/5","Train","steps",":","25","Val","steps",":","7","0.07s","loss",":","1.614473","acc",":","20.500000","auroc",":","0.516850","val_loss",":","1.617141","val_acc",":","21.500000","val_auroc",":","0.522068","Epoch",":","2\/5","Train","steps",":","25","Val","steps",":","7","0.03s","loss",":","1.614454","acc",":","20.125000","auroc",":","0.517618","val_loss",":","1.615585","val_acc",":","22.000000","val_auroc",":","0.521051","Epoch",":","3\/5","Train","steps",":","25","Val","steps",":","7","0.03s","loss",":","1.613709","acc",":","20.125000","auroc",":","0.518307","val_loss",":","1.614440","val_acc",":","22.000000","val_auroc",":","0.520762","..."]
code: def _format_truth_pred_return(
      self,
      init,
      pred_y,
      return_pred,
      true_y=None,
      return_ground_truth=False,
  ):
      # pylint: disable=too-many-arguments
      if return_pred:
          init += (pred_y,)
  
      if return_ground_truth:
          init += (true_y,)
  
      return init[0] if len(init) == 1 else init
  
imports: import contextlib
  import timeit
  from collections import defaultdict
  from typing import Any, Iterable, List, Mapping, Tuple, Union
  import numpy
  import torch
  import torch.nn
  import torch.optim
  from torch.nn.utils.rnn import PackedSequence
  from torch.utils.data import DataLoader
  from poutyne.framework.callbacks import Callback, CallbackList, ProgressionCallback
  from poutyne.framework.iterators import EpochIterator, StepIterator, _get_step_iterator
  from poutyne.framework.metrics import flatten_metric_names, get_callables_and_names, get_metric, rename_doubles
  from poutyne.framework.metrics.decomposable import convert_decomposable_metric_to_object
  from poutyne.framework.optimizers import get_optimizer
  from poutyne.utils import TensorDataset, _concat, get_batch_size, numpy_to_torch, torch_to, torch_to_numpy
label_desc: Use image node_id 19 for calling the Model obj's underlying member method code with example usage: obj._format_truth_pred_return(init, pred_y, return_pred, true_y, return_ground_truth) and returns: unknown