class_docstr: Trainer extension to take snapshots.
  
  This extension serializes the given object and saves it to the output
  directory.
  
  This extension is called once per epoch by default. To take a
  snapshot at a different interval, a trigger object specifying the
  required interval can be passed along with this extension
  to the `extend()` method of the trainer.
  
  The default priority is -100, which is lower than that of most
  built-in extensions.
class_docstr_tok: ["Trainer","extension","to","take","snapshots",".","This","extension","serializes","the","given","object","and","saves","it","to","the","output","directory",".","This","extension","is","called","once","per","epoch","by","default",".","To","take","a","snapshot","at","a","different","interval",",","a","trigger","object","specifying","the","required","interval","can","be","passed","along","with","this","extension","to","the","`","extend","(",")","`","method","of","the","trainer",".","The","default","priority","is","-100",",","which","is","lower","than","that","of","most","built-in","extensions","."]
code: def initialize(self, trainer):
      target = trainer if self._target is None else self._target
      outdir = trainer.out
      if self.autoload:
          # If ``autoload`` is on, this code scans the ``outdir``
          # for potential snapshot files by matching the file names
          # from ``filename`` format, picks up the latest one in
          # terms of mtime, and tries to load it it the target or
          # trainer.
          filename = _find_latest_snapshot(self.filename, outdir)
          if filename is None:
              if chainer.is_debug():
                  print(
                      "No snapshot file that matches {} was found".format(
                          self.filename
                      )
                  )
          else:
              snapshot_file = os.path.join(outdir, filename)
              # As described above (at ``autoload`` option),
              # snapshot files to be autoloaded must be saved by
              # ``save_npz`` . In order to support general format,
              # we nned to first reconstruct the design of savefun
              # and loadfun.
              npz.load_npz(snapshot_file, target)
              if chainer.is_debug():
                  print("Snapshot loaded from", snapshot_file)
  
      if (
          hasattr(self.writer, "_add_cleanup_hook")
          and self.n_retains > 0
          and isinstance(self.filename, str)
      ):
          # This block sets a method to automatic cleanup of stale
          # snapshots, when ``n_retains`` argument is positive
          # number. When the given snapshot writer is Chainer's
          # built-in writer, a cleanup method that is to be
          # triggered right after creation of new snapshot file, is
          # injected here.
          def _cleanup():
              files = _find_stale_snapshots(
                  self.filename, outdir, self.n_retains
              )
              for file in files:
                  os.remove(os.path.join(outdir, file))
  
          self.writer._add_cleanup_hook(_cleanup)
  
imports: import os
  import warnings
  import chainer
  from chainer.serializers import npz
  from chainer.training import extension
  from chainer.training.extensions import snapshot_writers
  from chainer.utils import argument
label_desc: Use image node_id 2 for calling the _Snapshot obj's underlying member method code with example usage: obj.initialize(trainer) without return types