class_docstr: 保存最佳的 monitor 值最佳的模型，并在训练结束的时候重新加载模型，默认会在加载之后删除权重文件。仅在训练正常结束的时候才能加载
  最好的模型。
  
  :param monitor: 监控的 metric 值：
  
      * 为 ``None``
       将尝试使用 :class:`~fastNLP.core.controllers.Trainer` 中设置 `monitor` 值（如果有设置）；
      * 为 ``str``
       尝试直接使用该名称从 ``evaluation`` 结果中寻找，如果在 ``evaluation`` 结果中没有找到完全一致的名称，将
       使用 最长公共字符串算法 从 ``evaluation`` 结果中找到最匹配的那个作为 ``monitor`` ；
      * 为 :class:`Callable`
       接受参数为 ``evaluation`` 的结果(字典类型)，返回一个 ``float`` 值作为 ``monitor`` 的结果，如果当前结果中没有相关
       的 ``monitor`` 值请返回 ``None`` ；
  :param larger_better: 该 metric 值是否是越大越好；
  :param save_folder: 保存的文件夹，如果为空，则保存在内存中。不为空，则保存一份权重到文件中，当为多机训练，且本值不为空时，请确保
      不同的机器均可访问当该路径。当 ``model_save_fn`` 不为 None 时该值一定不能为空；
  :param only_state_dict: 是否只保存模型的参数。当 ``model_save_fn`` 不为空时，该值无效；
  :param model_save_fn: 保存 model 的函数，与 ``model_load_fn`` 必须同时不为空。本函数的输入为一个已经创建好的文件夹，没有输出，
      请在函数内完成对模型的保存；
  :param model_load_fn: 加载 model 的函数，与 ``model_save_fn`` 必须同时不为空。本函数的输入为一个已经创建好的文件夹，没有输出，
      请在函数内完成对模型的加载；
  :param delete_after_train: 在训练结束后是否删掉模型；
class_docstr_tok: ["\u4fdd\u5b58\u6700\u4f73\u7684","monitor","\u503c\u6700\u4f73\u7684\u6a21\u578b\uff0c\u5e76\u5728\u8bad\u7ec3\u7ed3\u675f\u7684\u65f6\u5019\u91cd\u65b0\u52a0\u8f7d\u6a21\u578b\uff0c\u9ed8\u8ba4\u4f1a\u5728\u52a0\u8f7d\u4e4b\u540e\u5220\u9664\u6743\u91cd\u6587\u4ef6\u3002\u4ec5\u5728\u8bad\u7ec3\u6b63\u5e38\u7ed3\u675f\u7684\u65f6\u5019\u624d\u80fd\u52a0\u8f7d","\u6700\u597d\u7684\u6a21\u578b\u3002",":","param","monitor",":","\u76d1\u63a7\u7684","metric","\u503c\uff1a","*","\u4e3a","``","None","``","\u5c06\u5c1d\u8bd5\u4f7f\u7528",":","class",":","`","~fastNLP.core.controllers.Trainer","`","\u4e2d\u8bbe\u7f6e","`","monitor","`","\u503c\uff08\u5982\u679c\u6709\u8bbe\u7f6e\uff09\uff1b","*","\u4e3a","``","str","``","\u5c1d\u8bd5\u76f4\u63a5\u4f7f\u7528\u8be5\u540d\u79f0\u4ece","``","evaluation","``","\u7ed3\u679c\u4e2d\u5bfb\u627e\uff0c\u5982\u679c\u5728","``","evaluation","``","\u7ed3\u679c\u4e2d\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u4e00\u81f4\u7684\u540d\u79f0\uff0c\u5c06","\u4f7f\u7528","\u6700\u957f\u516c\u5171\u5b57\u7b26\u4e32\u7b97\u6cd5","\u4ece","``","evaluation","``","\u7ed3\u679c\u4e2d\u627e\u5230\u6700\u5339\u914d\u7684\u90a3\u4e2a\u4f5c\u4e3a","``","monitor","``","\uff1b","*","\u4e3a",":","class",":","`","Callable","`","\u63a5\u53d7\u53c2\u6570\u4e3a","``","evaluation","``","\u7684\u7ed3\u679c","(","\u5b57\u5178\u7c7b\u578b",")","\uff0c\u8fd4\u56de\u4e00\u4e2a","``","float","``","\u503c\u4f5c\u4e3a","``","monitor","``","\u7684\u7ed3\u679c\uff0c\u5982\u679c\u5f53\u524d\u7ed3\u679c\u4e2d\u6ca1\u6709\u76f8\u5173","\u7684","``","monitor","``","\u503c\u8bf7\u8fd4\u56de","``","None","``","\uff1b",":","param","larger_better",":","\u8be5","metric","\u503c\u662f\u5426\u662f\u8d8a\u5927\u8d8a\u597d\uff1b",":","param","save_folder",":","\u4fdd\u5b58\u7684\u6587\u4ef6\u5939\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u4e0d\u4e3a\u7a7a\uff0c\u5219\u4fdd\u5b58\u4e00\u4efd\u6743\u91cd\u5230\u6587\u4ef6\u4e2d\uff0c\u5f53\u4e3a\u591a\u673a\u8bad\u7ec3\uff0c\u4e14\u672c\u503c\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u8bf7\u786e\u4fdd","\u4e0d\u540c\u7684\u673a\u5668\u5747\u53ef\u8bbf\u95ee\u5f53\u8be5\u8def\u5f84\u3002\u5f53","``","model_save_fn","``","\u4e0d\u4e3a","None","\u65f6\u8be5\u503c\u4e00\u5b9a\u4e0d\u80fd\u4e3a\u7a7a\uff1b",":","param","only_state_dict",":","\u662f\u5426\u53ea\u4fdd\u5b58\u6a21\u578b\u7684\u53c2\u6570\u3002\u5f53","``","model_save_fn","``","\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u8be5\u503c\u65e0\u6548\uff1b",":","param","model_save_fn",":","\u4fdd\u5b58","model","\u7684\u51fd\u6570\uff0c\u4e0e","``","model_load_fn","``","\u5fc5\u987b\u540c\u65f6\u4e0d\u4e3a\u7a7a\u3002\u672c\u51fd\u6570\u7684\u8f93\u5165\u4e3a\u4e00\u4e2a\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u6587\u4ef6\u5939\uff0c\u6ca1\u6709\u8f93\u51fa\uff0c","\u8bf7\u5728\u51fd\u6570\u5185\u5b8c\u6210\u5bf9\u6a21\u578b\u7684\u4fdd\u5b58\uff1b",":","param","model_load_fn",":","\u52a0\u8f7d","model","\u7684\u51fd\u6570\uff0c\u4e0e","``","model_save_fn","``","\u5fc5\u987b\u540c\u65f6\u4e0d\u4e3a\u7a7a\u3002\u672c\u51fd\u6570\u7684\u8f93\u5165\u4e3a\u4e00\u4e2a\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u6587\u4ef6\u5939\uff0c\u6ca1\u6709\u8f93\u51fa\uff0c","\u8bf7\u5728\u51fd\u6570\u5185\u5b8c\u6210\u5bf9\u6a21\u578b\u7684\u52a0\u8f7d\uff1b",":","param","delete_after_train",":","\u5728\u8bad\u7ec3\u7ed3\u675f\u540e\u662f\u5426\u5220\u6389\u6a21\u578b\uff1b"]
code: def __init__(
      self,
      monitor: Union[str, Callable] = None,
      larger_better: bool = True,
      only_state_dict: bool = True,
      save_folder: Optional[str] = None,
      model_save_fn: Optional[Callable] = None,
      model_load_fn: Optional[Callable] = None,
      delete_after_train: bool = True,
  ):
      super().__init__(
          monitor=monitor,
          larger_better=larger_better,
          must_have_monitor=True,
      )
      if model_load_fn is not None:
          assert callable(
              model_load_fn
          ), "`model_load_fn` must be a callable object."
          assert (
              model_save_fn is not None
          ), "`model_load_fn` and `model_save_fn` must be passed at the same time."
      if model_save_fn is not None:
          assert callable(
              model_save_fn
          ), "`model_save_fn` must be a callable object."
          assert (
              model_load_fn is not None
          ), "`model_load_fn` and `model_save_fn` must be passed at the same time."
  
      if model_save_fn is not None:
          assert (
              save_folder is not None
          ), "When passing `model_save_fn`, `save_folder` must be provided."
  
      if save_folder:
          if os.path.exists(save_folder):
              assert os.path.isdir(
                  save_folder
              ), f"`save_folder={save_folder}` must be a directory."
  
      self.save_folder = save_folder
      self.only_state_dict = only_state_dict
      self.model_save_fn = model_save_fn
      self.model_load_fn = model_load_fn
      self.delete_after_after = delete_after_train
      self.meta = {"epoch": -1, "batch": -1}
  
file_path: fastNLP/fastNLP/core/callbacks/load_best_model_callback.py
imports: import os
  from typing import Optional, Callable, Union
  from .has_monitor_callback import HasMonitorCallback
  from io import BytesIO
  import shutil
  from fastNLP.envs.env import FASTNLP_LAUNCH_TIME, FASTNLP_GLOBAL_RANK, FASTNLP_BACKEND_LAUNCH
  from fastNLP.core.log import logger
  from fastNLP.envs import all_rank_call_context
  from fastNLP.core.utils.exceptions import EarlyStopException
label_desc: Use image node_id 1 to create a new LoadBestModelCallback object from inherited base classes: HasMonitorCallback with example: obj = LoadBestModelCallback(monitor, larger_better, only_state_dict, save_folder, model_save_fn, model_load_fn, delete_after_train)