docstr: Saves a checkpoint of the model to self._serialization_dir.
  Is a no-op if self._serialization_dir is None.
  
  Parameters
  ----------
  epoch : Union[int, str], required.
      The epoch of training.  If the checkpoint is saved in the middle
      of an epoch, the parameter is a string with the epoch and timestamp.
  is_best: bool, optional (default = None)
      A flag which causes the model weights at the given epoch to
      be copied to a "best.th" file. The value of this flag should
      be based on some validation metric computed by your model.
docstr_tok: ["Saves","a","checkpoint","of","the","model","to","self._serialization_dir",".","Is","a","no-op","if","self._serialization_dir","is","None",".","Parameters","--","--","--","--","--","epoch",":","Union","[","int",",","str","]",",","required",".","The","epoch","of","training",".","If","the","checkpoint","is","saved","in","the","middle","of","an","epoch",",","the","parameter","is","a","string","with","the","epoch","and","timestamp",".","is_best",":","bool",",","optional","(","default","=","None",")","A","flag","which","causes","the","model","weights","at","the","given","epoch","to","be","copied","to","a","``","best.th","''","file",".","The","value","of","this","flag","should","be","based","on","some","validation","metric","computed","by","your","model","."]
code: def _save_checkpoint(self, epoch, val_metric_per_epoch, is_best=None):
      """
      Saves a checkpoint of the model to self._serialization_dir.
      Is a no-op if self._serialization_dir is None.
  
      Parameters
      ----------
      epoch : Union[int, str], required.
          The epoch of training.  If the checkpoint is saved in the middle
          of an epoch, the parameter is a string with the epoch and timestamp.
      is_best: bool, optional (default = None)
          A flag which causes the model weights at the given epoch to
          be copied to a "best.th" file. The value of this flag should
          be based on some validation metric computed by your model.
      """
      if self._serialization_dir is not None:
          model_path = os.path.join(
              self._serialization_dir,
              "model_state_epoch_{}.th".format(epoch),
          )
          model_state = self._model.state_dict()
          torch.save(model_state, model_path)
  
          training_state = {
              "epoch": epoch,
              "val_metric_per_epoch": val_metric_per_epoch,
              "optimizer": self._optimizer.state_dict(),
              "batch_num_total": self._batch_num_total,
          }
          training_path = os.path.join(
              self._serialization_dir,
              "training_state_epoch_{}.th".format(epoch),
          )
          torch.save(training_state, training_path)
          if is_best:
              logger.info(
                  "Best validation performance so far. "
                  "Copying weights to '%s/best.th'.",
                  self._serialization_dir,
              )
              shutil.copyfile(
                  model_path,
                  os.path.join(self._serialization_dir, "best.th"),
              )
  
          if (
              self._num_serialized_models_to_keep
              and self._num_serialized_models_to_keep >= 0
          ):
              self._serialized_paths.append(
                  [time.time(), model_path, training_path]
              )
              if (
                  len(self._serialized_paths)
                  > self._num_serialized_models_to_keep
              ):
                  paths_to_remove = self._serialized_paths.pop(0)
                  # Check to see if we should keep this checkpoint, if it has been longer
                  # then self._keep_serialized_model_every_num_seconds since the last
                  # kept checkpoint.
                  remove_path = True
                  if (
                      self._keep_serialized_model_every_num_seconds
                      is not None
                  ):
                      save_time = paths_to_remove[0]
                      time_since_checkpoint_kept = (
                          save_time
                          - self._last_permanent_saved_checkpoint_time
                      )
                      if (
                          time_since_checkpoint_kept
                          > self._keep_serialized_model_every_num_seconds
                      ):
                          # We want to keep this checkpoint.
                          remove_path = False
                          self._last_permanent_saved_checkpoint_time = (
                              save_time
                          )
                  if remove_path:
                      for fname in paths_to_remove[1:]:
                          os.remove(fname)
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import logging
  import os
  import shutil
  import time
  import re
  import datetime
  import traceback
  import torch
  import torch.optim.lr_scheduler
  from torch.nn.parallel import replicate, parallel_apply
  from torch.nn.parallel.scatter_gather import scatter_kwargs, gather
  from allennlp.common import Params
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.util import peak_memory_mb, gpu_memory_mb
  from allennlp.common.tqdm import Tqdm
  from allennlp.data.instance import Instance
  from allennlp.data.iterators.data_iterator import DataIterator
  from allennlp.models.model import Model
  from allennlp.nn import util
  from allennlp.training.learning_rate_schedulers import LearningRateScheduler
  from allennlp.training.optimizers import Optimizer
label_desc: Use image node_id 18 for calling the Trainer obj's underlying member method code with example usage: obj._save_checkpoint(epoch, val_metric_per_epoch, is_best) without return types