當前位置: 首頁>>代碼示例>>Python>>正文


Python _jsonnet.evaluate_file方法代碼示例

本文整理匯總了Python中_jsonnet.evaluate_file方法的典型用法代碼示例。如果您正苦於以下問題:Python _jsonnet.evaluate_file方法的具體用法?Python _jsonnet.evaluate_file怎麽用?Python _jsonnet.evaluate_file使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在_jsonnet的用法示例。


在下文中一共展示了_jsonnet.evaluate_file方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_dump_best_config

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def test_dump_best_config() -> None:
    with tempfile.TemporaryDirectory() as tmp_dir:

        def objective(trial: optuna.Trial) -> float:
            trial.suggest_uniform("DROPOUT", dropout, dropout)
            executor = optuna.integration.AllenNLPExecutor(trial, input_config_file, tmp_dir)
            return executor.run()

        dropout = 0.5
        input_config_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "example.jsonnet"
        )
        output_config_file = os.path.join(tmp_dir, "result.json")

        study = optuna.create_study(direction="maximize")
        study.optimize(objective, n_trials=1)

        optuna.integration.allennlp.dump_best_config(input_config_file, output_config_file, study)
        best_config = json.loads(_jsonnet.evaluate_file(output_config_file))
        model_config = best_config["model"]
        target_config = model_config["text_field_embedder"]["token_embedders"]["token_characters"]
        assert target_config["dropout"] == dropout 
開發者ID:optuna,項目名稱:optuna,代碼行數:24,代碼來源:test_allennlp.py

示例2: dump_best_config

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def dump_best_config(input_config_file: str, output_config_file: str, study: optuna.Study) -> None:
    """Save JSON config file after updating with parameters from the best trial in the study.

    Args:
        input_config_file:
            Input Jsonnet config file used with
            :class:`~optuna.integration.AllenNLPExecutor`.
        output_config_file:
            Output JSON config file.
        study:
            Instance of :class:`~optuna.study.Study`.
            Note that :func:`~optuna.study.Study.optimize` must have been called.

    """
    _imports.check()

    best_params = study.best_params
    for key, value in best_params.items():
        best_params[key] = str(value)
    best_config = json.loads(_jsonnet.evaluate_file(input_config_file, ext_vars=best_params))
    best_config = allennlp.common.params.infer_and_cast(best_config)

    with open(output_config_file, "w") as f:
        json.dump(best_config, f, indent=4) 
開發者ID:optuna,項目名稱:optuna,代碼行數:26,代碼來源:allennlp.py

示例3: _build_params

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def _build_params(self) -> Dict[str, Any]:
        """Create a dict of params for AllenNLP.

        _build_params is based on allentune's train_func.
        For more detail, please refer to
        https://github.com/allenai/allentune/blob/master/allentune/modules/allennlp_runner.py#L34-L65

        """
        params = self._environment_variables()
        params.update({key: str(value) for key, value in self._params.items()})

        allennlp_params = json.loads(_jsonnet.evaluate_file(self._config_file, ext_vars=params))
        # allennlp_params contains a list of string or string as value values.
        # Some params couldn't be casted correctly and
        # infer_and_cast converts them into desired values.
        return allennlp.common.params.infer_and_cast(allennlp_params) 
開發者ID:optuna,項目名稱:optuna,代碼行數:18,代碼來源:allennlp.py

示例4: jsonnet_load_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def jsonnet_load_file(file_name):
    """
    Parses jsonnet file into jsonnet
    :param file_name: Jsonnet file
    :return:
    """
    json_parse = json.loads(_jsonnet.evaluate_file(file_name))

    return json_parse 
開發者ID:allenai,項目名稱:OpenBookQA,代碼行數:11,代碼來源:jsonnet.py

示例5: evaluate_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def evaluate_file(filename: str, **_kwargs) -> str:
        logger.warning(
            f"error loading _jsonnet (this is expected on Windows), treating {filename} as plain json"
        )
        with open(filename, "r") as evaluation_file:
            return evaluation_file.read() 
開發者ID:allenai,項目名稱:allennlp,代碼行數:8,代碼來源:params.py

示例6: from_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def from_file(
        cls, params_file: str, params_overrides: str = "", ext_vars: dict = None
    ) -> "Params":
        """
        Load a `Params` object from a configuration file.

        # Parameters

        params_file: `str`

            The path to the configuration file to load.

        params_overrides: `str`, optional

            A dict of overrides that can be applied to final object.
            e.g. {"model.embedding_dim": 10}

        ext_vars: `dict`, optional

            Our config files are Jsonnet, which allows specifying external variables
            for later substitution. Typically we substitute these using environment
            variables; however, you can also specify them here, in which case they
            take priority over environment variables.
            e.g. {"HOME_DIR": "/Users/allennlp/home"}
        """
        if ext_vars is None:
            ext_vars = {}

        # redirect to cache, if necessary
        params_file = cached_path(params_file)
        ext_vars = {**_environment_variables(), **ext_vars}

        file_dict = json.loads(evaluate_file(params_file, ext_vars=ext_vars))

        overrides_dict = parse_overrides(params_overrides)
        param_dict = with_fallback(preferred=overrides_dict, fallback=file_dict)

        return cls(param_dict) 
開發者ID:allenai,項目名稱:allennlp,代碼行數:40,代碼來源:params.py

示例7: evaluate

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def evaluate(self, *args, **kwargs):
    kwargs['import_callback'] = self._import_callback
    kwargs['native_callbacks'] = self._native_callbacks
    return json.loads(_jsonnet.evaluate_file(*args, **kwargs)) 
開發者ID:google,項目名稱:kasane,代碼行數:6,代碼來源:jsonnet.py

示例8: evaluate_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def evaluate_file(filename     , **_kwargs)       :
        logger.warning("_jsonnet not loaded, treating {filename} as json")
        with open(filename, u'r') as evaluation_file:
            return evaluation_file.read() 
開發者ID:plasticityai,項目名稱:magnitude,代碼行數:6,代碼來源:params.py

示例9: from_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def from_file(params_file     , params_overrides      = u"")            :
        u"""
        Load a `Params` object from a configuration file.
        """
        # redirect to cache, if necessary
        params_file = cached_path(params_file)
        ext_vars = dict(os.environ)

        file_dict = json.loads(evaluate_file(params_file, ext_vars=ext_vars))

        overrides_dict = parse_overrides(params_overrides)
        param_dict = with_fallback(preferred=overrides_dict, fallback=file_dict)

        return Params(param_dict) 
開發者ID:plasticityai,項目名稱:magnitude,代碼行數:16,代碼來源:params.py

示例10: evaluate_file

# 需要導入模塊: import _jsonnet [as 別名]
# 或者: from _jsonnet import evaluate_file [as 別名]
def evaluate_file(filename: str, **_kwargs) -> str:
        logger.warning(f"_jsonnet not loaded, treating {filename} as json")
        with open(filename, 'r') as evaluation_file:
            return evaluation_file.read() 
開發者ID:uwdata,項目名稱:errudite,代碼行數:6,代碼來源:params.py


注:本文中的_jsonnet.evaluate_file方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。