class_docstr: Callback to save the Elegy model or model weights at some frequency.
  
  `ModelCheckpoint` callback is used in conjunction with training using
  `model.fit()` to save a model or weights at some
  interval, so the model or weights can be loaded later to continue the training
  from the state saved.
  
  A few options this callback provides include:
  
  - Whether to only keep the model that has achieved the "best performance" so
      far, or whether to save the model at the end of every epoch regardless of
      performance.
  - Definition of 'best'; which quantity to monitor and whether it should be
      maximized or minimized.
  - The frequency it should save at. Currently, the callback supports saving at
      the end of every epoch, or after a fixed number of training batches.
  
  Example:
  
  ```python
  EPOCHS = 10
  checkpoint_path = '/tmp/checkpoint'
  model_checkpoint_callback = elegy.callbacks.ModelCheckpoint(
      path=checkpoint_path,
      monitor='val_acc',
      mode='max',
      save_best_only=True)
  
  # Model is saved at the end of every epoch, if it's the best seen
  # so far.
  model.fit(epochs=EPOCHS, callbacks=[model_checkpoint_callback])
  
  # The model status (that are considered the best) are loaded into the model.
  model.load(checkpoint_path)
  ```
class_docstr_tok: ["Callback","to","save","the","Elegy","model","or","model","weights","at","some","frequency",".","`","ModelCheckpoint","`","callback","is","used","in","conjunction","with","training","using","`","model.fit","(",")","`","to","save","a","model","or","weights","at","some","interval",",","so","the","model","or","weights","can","be","loaded","later","to","continue","the","training","from","the","state","saved",".","A","few","options","this","callback","provides","include",":","-","Whether","to","only","keep","the","model","that","has","achieved","the","``","best","performance","''","so","far",",","or","whether","to","save","the","model","at","the","end","of","every","epoch","regardless","of","performance",".","-","Definition","of","'best","'",";","which","quantity","to","monitor","and","whether","it","should","be","maximized","or","minimized",".","-","The","frequency","it","should","save","at",".","Currently",",","the","callback","supports","saving","at","the","end","of","every","epoch",",","or","after","a","fixed","number","of","training","batches",".","Example",":","``","`","python","EPOCHS","=","10","checkpoint_path","=","'\/tmp\/checkpoint'","model_checkpoint_callback","=","elegy.callbacks.ModelCheckpoint","(","path=checkpoint_path",",","monitor='val_acc","'",",","mode='max","'",",","save_best_only=True",")","#","Model","is","saved","at","the","end","of","every","epoch",",","if","it","'s","the","best","seen","#","so","far",".","model.fit","(","epochs=EPOCHS",",","callbacks=","[","model_checkpoint_callback","]",")","#","The","model","status","(","that","are","considered","the","best",")","are","loaded","into","the","model",".","model.load","(","checkpoint_path",")","``","`"]
docstr: Arguments:
      path: string, path to directory to save the model state. `path` can contain
          named formatting options, which will be filled the value of `epoch` and
          keys in `logs` (passed in `on_epoch_end`). For example: if `path` is
          `weights.{epoch:02d}-{val_loss:.2f}`, then the model checkpoints
          will be saved with the epoch number and the validation loss in the
          filename.
      monitor: quantity to monitor.
      verbose: verbosity mode, 0 or 1.
      save_best_only: if `save_best_only=True`, the latest best model according
          to the quantity monitored will not be overwritten.
          If `path` doesn't contain formatting options like `{epoch}` then
          `path` will be overwritten by each new better model.
      mode: one of {auto, min, max}. If `save_best_only=True`, the decision to
          overwrite the current save file is made based on either the maximization
          or the minimization of the monitored quantity. For `val_acc`, this
          should be `max`, for `val_loss` this should be `min`, etc. In `auto`
          mode, the direction is automatically inferred from the name of the
          monitored quantity.
      save_freq: `'epoch'` or integer. When using `'epoch'`, the callback saves
          the model after each epoch. When using integer, the callback saves the
          model at end of this many batches. Note that if the saving isn't aligned
          to epochs, the monitored metric may potentially be less reliable (it
          could reflect as little as 1 batch, since the metrics get reset every
          epoch). Defaults to `'epoch'`
      period: the number of epochs between which the model is saved. This only works
          if `save_freq` is 'epoch', otherwise the `save_freq` will override
          this period.
docstr_tok: ["Arguments",":","path",":","string",",","path","to","directory","to","save","the","model","state",".","`","path","`","can","contain","named","formatting","options",",","which","will","be","filled","the","value","of","`","epoch","`","and","keys","in","`","logs","`","(","passed","in","`","on_epoch_end","`",")",".","For","example",":","if","`","path","`","is","`","weights",".","{","epoch:02d","}","-","{","val_loss",":",".2f","}","`",",","then","the","model","checkpoints","will","be","saved","with","the","epoch","number","and","the","validation","loss","in","the","filename",".","monitor",":","quantity","to","monitor",".","verbose",":","verbosity","mode",",","0","or","1.","save_best_only",":","if","`","save_best_only=True","`",",","the","latest","best","model","according","to","the","quantity","monitored","will","not","be","overwritten",".","If","`","path","`","does","n't","contain","formatting","options","like","`","{","epoch","}","`","then","`","path","`","will","be","overwritten","by","each","new","better","model",".","mode",":","one","of","{","auto",",","min",",","max","}",".","If","`","save_best_only=True","`",",","the","decision","to","overwrite","the","current","save","file","is","made","based","on","either","the","maximization","or","the","minimization","of","the","monitored","quantity",".","For","`","val_acc","`",",","this","should","be","`","max","`",",","for","`","val_loss","`","this","should","be","`","min","`",",","etc",".","In","`","auto","`","mode",",","the","direction","is","automatically","inferred","from","the","name","of","the","monitored","quantity",".","save_freq",":","`","'epoch","'","`","or","integer",".","When","using","`","'epoch","'","`",",","the","callback","saves","the","model","after","each","epoch",".","When","using","integer",",","the","callback","saves","the","model","at","end","of","this","many","batches",".","Note","that","if","the","saving","is","n't","aligned","to","epochs",",","the","monitored","metric","may","potentially","be","less","reliable","(","it","could","reflect","as","little","as","1","batch",",","since","the","metrics","get","reset","every","epoch",")",".","Defaults","to","`","'epoch","'","`","period",":","the","number","of","epochs","between","which","the","model","is","saved",".","This","only","works","if","`","save_freq","`","is","'epoch","'",",","otherwise","the","`","save_freq","`","will","override","this","period","."]
code: def __init__(
      self,
      path: str,
      monitor: str = "val_loss",
      verbose: int = 0,
      save_best_only: bool = False,
      mode: str = "auto",
      save_freq: str = "epoch",
      period: int = 1,
  ):
      """
      Arguments:
          path: string, path to directory to save the model state. `path` can contain
              named formatting options, which will be filled the value of `epoch` and
              keys in `logs` (passed in `on_epoch_end`). For example: if `path` is
              `weights.{epoch:02d}-{val_loss:.2f}`, then the model checkpoints
              will be saved with the epoch number and the validation loss in the
              filename.
          monitor: quantity to monitor.
          verbose: verbosity mode, 0 or 1.
          save_best_only: if `save_best_only=True`, the latest best model according
              to the quantity monitored will not be overwritten.
              If `path` doesn't contain formatting options like `{epoch}` then
              `path` will be overwritten by each new better model.
          mode: one of {auto, min, max}. If `save_best_only=True`, the decision to
              overwrite the current save file is made based on either the maximization
              or the minimization of the monitored quantity. For `val_acc`, this
              should be `max`, for `val_loss` this should be `min`, etc. In `auto`
              mode, the direction is automatically inferred from the name of the
              monitored quantity.
          save_freq: `'epoch'` or integer. When using `'epoch'`, the callback saves
              the model after each epoch. When using integer, the callback saves the
              model at end of this many batches. Note that if the saving isn't aligned
              to epochs, the monitored metric may potentially be less reliable (it
              could reflect as little as 1 batch, since the metrics get reset every
              epoch). Defaults to `'epoch'`
          period: the number of epochs between which the model is saved. This only works
              if `save_freq` is 'epoch', otherwise the `save_freq` will override
              this period.
      """
      super(ModelCheckpoint, self).__init__()
      self.monitor = monitor
      self.verbose = verbose
      self.path = path
      self.save_best_only = save_best_only
      self.save_freq = save_freq
      self.period = period
      self.epochs_since_last_save = 0
      self._batches_seen_since_last_saving = 0
  
      if mode not in ["auto", "min", "max"]:
          logging.warning(
              "ModelCheckpoint mode %s is unknown, "
              "fallback to auto mode.",
              mode,
          )
          mode = "auto"
  
      if mode == "min":
          self.monitor_op = np.less
          self.best = np.Inf
      elif mode == "max":
          self.monitor_op = np.greater
          self.best = -np.Inf
      else:
          if "acc" in self.monitor or self.monitor.startswith(
              "fmeasure"
          ):
              self.monitor_op = np.greater
              self.best = -np.Inf
          else:
              self.monitor_op = np.less
              self.best = np.Inf
  
      if self.save_freq != "epoch" and not isinstance(
          self.save_freq, int
      ):
          raise ValueError(
              "Unrecognized save_freq: {}".format(self.save_freq)
          )
  
file_path: elegy/elegy/callbacks/model_checkpoint.py
imports: import logging
  import numpy
  from .callback import Callback
label_desc: Use image node_id 1 to create a new ModelCheckpoint object from inherited base classes: Callback with example: obj = ModelCheckpoint(path, monitor, verbose, save_best_only, mode, save_freq, period)