示例#1
0
    def frozen(self, experiment: ExperimentBackend) -> 'MetaBlock':
        """
        save fitted models to the experiment

        Args:
            experiment:
                保存する対象となる environment
        Returns:
            myself
        """
        if not self._check_has_fitted_models():
            raise NotFittedError()
        dir_names = [
            self._get_fold_dir(i) for i in range(len(self._fitted_models))
        ]
        for name, model in zip(dir_names, self._fitted_models):
            with experiment.as_environment(name, style='nested') as fold_env:
                fold_env.save_as_python_object('model', model)
        experiment.mark('cv_dirs', dir_names)
        return self
示例#2
0
    def unzip(self, experiment: ExperimentBackend) -> 'MetaBlock':
        """load fitting models from experiment.

        Raises:
            NotFittedError
                there is no `cv_dirs` in marked object.
                fit が呼ばれた段階で, MetaBlock class は cv_dirs に cv ごとのディレクトリを保存しています.
                これが参照できない場合 fit が呼ばれていないと判断して `NotFittedError` を送出します

        Args:
            experiment:
                読み込み対象の experiment

        Returns:
            myself
        """
        if not self._check_has_models_in_exp(experiment):
            raise NotFittedError(
                '`cv_dirs` is not found in marked object. Must be call fit before `unzip`.'
            )
        mark = experiment.get_marked()
        output_dirs = mark.get('cv_dirs', None)  # type: List[str]
        if output_dirs is None:
            raise NotFittedError(
                '`cv_dirs` is not found in marked object. Must be call fit before `unzip`.'
            )

        models = []
        for out_dir in output_dirs:
            with experiment.as_environment(out_dir,
                                           style='nested') as fold_env:
                model = fold_env.load_object('model')
            models.append(model)

        self._fitted_models = models
        return self