当前位置: 首页>>代码示例>>Python>>正文


Python Params.pop_choice方法代码示例

本文整理汇总了Python中allennlp.common.Params.pop_choice方法的典型用法代码示例。如果您正苦于以下问题:Python Params.pop_choice方法的具体用法?Python Params.pop_choice怎么用?Python Params.pop_choice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在allennlp.common.Params的用法示例。


在下文中一共展示了Params.pop_choice方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'StanfordSentimentTreeBankDatasetReader':
     token_indexers = TokenIndexer.dict_from_params(params.pop('token_indexers', {}))
     use_subtrees = params.pop('use_subtrees', False)
     granularity = params.pop_choice('granularity', ["5-class", "3-class", "2-class"], True)
     lazy = params.pop('lazy', False)
     params.assert_empty(cls.__name__)
     return StanfordSentimentTreeBankDatasetReader(
             token_indexers=token_indexers,
             use_subtrees=use_subtrees,
             granularity=granularity,
             lazy=lazy)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:13,代码来源:stanford_sentiment_tree_bank.py

示例2: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'WordSplitter':
     choice = params.pop_choice('type', cls.list_available(), default_to_first_choice=True)
     return cls.by_name(choice).from_params(params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:5,代码来源:word_splitter.py

示例3: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, vocab: Vocabulary, params: Params) -> 'TokenEmbedder':
     choice = params.pop_choice('type', cls.list_available())
     return cls.by_name(choice).from_params(vocab, params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:5,代码来源:token_embedder.py

示例4: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'WordStemmer':
     choice = params.pop_choice('type', cls.list_available(), default_to_first_choice=True)
     params.assert_empty('WordStemmer')
     return cls.by_name(choice)()
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:6,代码来源:word_stemmer.py

示例5: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
    def from_params(cls, model_parameters: List, params: Params):  # type: ignore
        # pylint: disable=arguments-differ
        if isinstance(params, str):
            optimizer = params
            params = Params({})
        else:
            optimizer = params.pop_choice("type", Optimizer.list_available())

        # make the parameter groups if need
        groups = params.pop("parameter_groups", None)
        if groups:
            # The input to the optimizer is list of dict.
            # Each dict contains a "parameter group" and groups specific options,
            # e.g., {'params': [list of parameters], 'lr': 1e-3, ...}
            # Any config option not specified in the additional options (e.g.
            # for the default group) is inherited from the top level config.
            # see: http://pytorch.org/docs/0.3.0/optim.html?#per-parameter-options
            #
            # groups contains something like:
            #"parameter_groups": [
            #       [["regex1", "regex2"], {"lr": 1e-3}],
            #       [["regex3"], {"lr": 1e-4}]
            #]
            #(note that the allennlp config files require double quotes ", and will
            # fail (sometimes silently) with single quotes ').

            # This is typed as as Any since the dict values other then
            # the params key are passed to the Optimizer constructor and
            # can be any type it accepts.
            # In addition to any parameters that match group specific regex,
            # we also need a group for the remaining "default" group.
            # Those will be included in the last entry of parameter_groups.
            parameter_groups: Any = [{'params': []} for _ in range(len(groups) + 1)]
            # add the group specific kwargs
            for k in range(len(groups)): # pylint: disable=consider-using-enumerate
                parameter_groups[k].update(groups[k][1].as_dict())

            regex_use_counts: Dict[str, int] = {}
            parameter_group_names: List[set] = [set() for _ in range(len(groups) + 1)]
            for name, param in model_parameters:
                # Determine the group for this parameter.
                group_index = None
                for k, group_regexes in enumerate(groups):
                    for regex in group_regexes[0]:
                        if regex not in regex_use_counts:
                            regex_use_counts[regex] = 0
                        if re.search(regex, name):
                            if group_index is not None and group_index != k:
                                raise ValueError("{} was specified in two separate parameter groups".format(name))
                            group_index = k
                            regex_use_counts[regex] += 1

                if group_index is not None:
                    parameter_groups[group_index]['params'].append(param)
                    parameter_group_names[group_index].add(name)
                else:
                    # the default group
                    parameter_groups[-1]['params'].append(param)
                    parameter_group_names[-1].add(name)

            # log the parameter groups
            logger.info("Done constructing parameter groups.")
            for k in range(len(groups) + 1):
                group_options = {key: val for key, val in parameter_groups[k].items()
                                 if key != 'params'}
                logger.info("Group %s: %s, %s", k,
                            list(parameter_group_names[k]),
                            group_options)
            # check for unused regex
            for regex, count in regex_use_counts.items():
                if count == 0:
                    logger.warning("When constructing parameter groups, "
                                   " %s not match any parameter name", regex)

        else:
            parameter_groups = [param for name, param in model_parameters]

        # Log the number of parameters to optimize
        num_parameters = 0
        for parameter_group in parameter_groups:
            if isinstance(parameter_group, dict):
                num_parameters += sum(parameter.numel() for parameter in parameter_group["params"])
            else:
                num_parameters += parameter_group.numel()
        logger.info("Number of trainable parameters: %s", num_parameters)

        # By default we cast things that e.g. look like floats to floats before handing them
        # to the Optimizer constructor, but if you want to disable that behavior you could add a
        #       "infer_type_and_cast": false
        # key to your "trainer.optimizer" config.
        infer_type_and_cast = params.pop_bool("infer_type_and_cast", True)
        params_as_dict = params.as_dict(infer_type_and_cast=infer_type_and_cast)
        return Optimizer.by_name(optimizer)(parameter_groups, **params_as_dict) # type: ignore
开发者ID:apmoore1,项目名称:allennlp,代码行数:95,代码来源:optimizers.py

示例6: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'DatasetReader':
     """
     Static method that constructs the dataset reader described by ``params``.
     """
     choice = params.pop_choice('type', cls.list_available())
     return cls.by_name(choice).from_params(params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:8,代码来源:dataset_reader.py

示例7: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'TokenIndexer':  # type: ignore
     choice = params.pop_choice('type', cls.list_available(), default_to_first_choice=True)
     return cls.by_name(choice).from_params(params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:5,代码来源:token_indexer.py

示例8: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, vocab: Vocabulary, params: Params) -> 'TextFieldEmbedder':
     choice = params.pop_choice('type', cls.list_available(), default_to_first_choice=True)
     return cls.by_name(choice).from_params(vocab, params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:5,代码来源:text_field_embedder.py

示例9: from_params

# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import pop_choice [as 别名]
 def from_params(cls, params: Params) -> 'Seq2VecEncoder':
     choice = params.pop_choice('type', cls.list_available())
     return cls.by_name(choice).from_params(params)
开发者ID:Jordan-Sauchuk,项目名称:allennlp,代码行数:5,代码来源:seq2vec_encoder.py


注:本文中的allennlp.common.Params.pop_choice方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。