class_docstr: Wrapper created to ease the training/testing/loading.
  
  Args:
      model (nn.Module): The model to optimize.
      criterion (Callable): A loss function.
      replicate_in_memory (bool): Replicate in memory optional.
class_docstr_tok: ["Wrapper","created","to","ease","the","training\/testing\/loading",".","Args",":","model","(","nn.Module",")",":","The","model","to","optimize",".","criterion","(","Callable",")",":","A","loss","function",".","replicate_in_memory","(","bool",")",":","Replicate","in","memory","optional","."]
docstr: Train and test the model on both Dataset `train_dataset`, `test_dataset`.
  
  Args:
      train_dataset (Dataset): Dataset to train on.
      test_dataset (Dataset): Dataset to evaluate on.
      optimizer (Optimizer): Optimizer to use during training.
      batch_size (int): Batch size used.
      epoch (int): Number of epoch to train on.
      use_cuda (bool): Use Cuda or not.
      workers (int): Number of workers to use.
      collate_fn (Optional[Callable]): The collate function to use.
      regularizer (Optional[Callable]): The loss regularization for training.
      return_best_weights (bool): If True, will keep the best weights and return them.
      patience (Optional[int]): If provided, will use early stopping to stop after
                                  `patience` epoch without improvement.
      min_epoch_for_es (int): Epoch at which the early stopping starts.
      skip_epochs (int): Number of epochs to skip for test_on_dataset
  
  Returns:
      History and best weights if required.
docstr_tok: ["Train","and","test","the","model","on","both","Dataset","`","train_dataset","`",",","`","test_dataset","`",".","Args",":","train_dataset","(","Dataset",")",":","Dataset","to","train","on",".","test_dataset","(","Dataset",")",":","Dataset","to","evaluate","on",".","optimizer","(","Optimizer",")",":","Optimizer","to","use","during","training",".","batch_size","(","int",")",":","Batch","size","used",".","epoch","(","int",")",":","Number","of","epoch","to","train","on",".","use_cuda","(","bool",")",":","Use","Cuda","or","not",".","workers","(","int",")",":","Number","of","workers","to","use",".","collate_fn","(","Optional","[","Callable","]",")",":","The","collate","function","to","use",".","regularizer","(","Optional","[","Callable","]",")",":","The","loss","regularization","for","training",".","return_best_weights","(","bool",")",":","If","True",",","will","keep","the","best","weights","and","return","them",".","patience","(","Optional","[","int","]",")",":","If","provided",",","will","use","early","stopping","to","stop","after","`","patience","`","epoch","without","improvement",".","min_epoch_for_es","(","int",")",":","Epoch","at","which","the","early","stopping","starts",".","skip_epochs","(","int",")",":","Number","of","epochs","to","skip","for","test_on_dataset","Returns",":","History","and","best","weights","if","required","."]
code: def train_and_test_on_datasets(
      self,
      train_dataset: Dataset,
      test_dataset: Dataset,
      optimizer: Optimizer,
      batch_size: int,
      epoch: int,
      use_cuda: bool,
      workers: int = 4,
      collate_fn: Optional[Callable] = None,
      regularizer: Optional[Callable] = None,
      return_best_weights=False,
      patience=None,
      min_epoch_for_es=0,
      skip_epochs=1,
  ):
      """
      Train and test the model on both Dataset `train_dataset`, `test_dataset`.
  
      Args:
          train_dataset (Dataset): Dataset to train on.
          test_dataset (Dataset): Dataset to evaluate on.
          optimizer (Optimizer): Optimizer to use during training.
          batch_size (int): Batch size used.
          epoch (int): Number of epoch to train on.
          use_cuda (bool): Use Cuda or not.
          workers (int): Number of workers to use.
          collate_fn (Optional[Callable]): The collate function to use.
          regularizer (Optional[Callable]): The loss regularization for training.
          return_best_weights (bool): If True, will keep the best weights and return them.
          patience (Optional[int]): If provided, will use early stopping to stop after
                                      `patience` epoch without improvement.
          min_epoch_for_es (int): Epoch at which the early stopping starts.
          skip_epochs (int): Number of epochs to skip for test_on_dataset
  
      Returns:
          History and best weights if required.
      """
      best_weight = None
      best_loss = 1e10
      best_epoch = 0
      hist = []
      for e in range(epoch):
          _ = self.train_on_dataset(
              train_dataset,
              optimizer,
              batch_size,
              1,
              use_cuda,
              workers,
              collate_fn,
              regularizer,
          )
          if e % skip_epochs == 0:
              te_loss = self.test_on_dataset(
                  test_dataset,
                  batch_size,
                  use_cuda,
                  workers,
                  collate_fn,
              )
              hist.append(self.get_metrics())
              if te_loss < best_loss:
                  best_epoch = e
                  best_loss = te_loss
                  if return_best_weights:
                      best_weight = deepcopy(self.state_dict())
  
              if (
                  patience is not None
                  and (e - best_epoch) > patience
                  and (e > min_epoch_for_es)
              ):
                  # Early stopping
                  break
          else:
              hist.append(self.get_metrics("train"))
  
      if return_best_weights:
          return hist, best_weight
      else:
          return hist
  
imports: import sys
  from collections import defaultdict
  from collections.abc import Sequence
  from copy import deepcopy
  from typing import Callable, Optional
  import numpy
  import structlog
  import torch
  from torch.optim import Optimizer
  from torch.utils.data import DataLoader
  from torch.utils.data.dataloader import default_collate
  from tqdm import tqdm
  from baal.metrics.mixin import MetricMixin
  from baal.utils.array_utils import stack_in_memory
  from baal.active.dataset.base import Dataset
  from baal.utils.cuda_utils import to_cuda
  from baal.utils.iterutils import map_on_tensor
  from baal.utils.metrics import Loss
  from baal.utils.warnings import raise_warnings_cache_replicated
label_desc: Use image node_id 4 for calling the ModelWrapper obj's underlying member method code with example usage: obj.train_and_test_on_datasets(train_dataset, test_dataset, optimizer, batch_size, epoch, use_cuda, workers, collate_fn, regularizer, return_best_weights, patience, min_epoch_for_es, skip_epochs) and returns: hist,  best_weight, hist