本文整理汇总了Python中allennlp.common.params.Params.pop_choice方法的典型用法代码示例。如果您正苦于以下问题:Python Params.pop_choice方法的具体用法?Python Params.pop_choice怎么用?Python Params.pop_choice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.common.params.Params
的用法示例。
在下文中一共展示了Params.pop_choice方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, optimizer: torch.optim.Optimizer, params: Params): # type: ignore
# pylint: disable=arguments-differ
scheduler = params.pop_choice("type", LearningRateScheduler.list_available())
schedulers = LearningRateScheduler.by_name(scheduler)(optimizer, **params.as_dict()) # type: ignore
if isinstance(schedulers, torch.optim.lr_scheduler.ReduceLROnPlateau):
return LearningRateWithMetricsWrapper(schedulers)
else:
return LearningRateWithoutMetricsWrapper(schedulers)
示例2: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls: Type[T], params: Params, **extras) -> T:
"""
This is the automatic implementation of `from_params`. Any class that subclasses `FromParams`
(or `Registrable`, which itself subclasses `FromParams`) gets this implementation for free.
If you want your class to be instantiated from params in the "obvious" way -- pop off parameters
and hand them to your constructor with the same names -- this provides that functionality.
If you need more complex logic in your from `from_params` method, you'll have to implement
your own method that overrides this one.
"""
# pylint: disable=protected-access
from allennlp.common.registrable import Registrable # import here to avoid circular imports
logger.info(f"instantiating class {cls} from params {getattr(params, 'params', params)} "
f"and extras {extras}")
if params is None:
return None
registered_subclasses = Registrable._registry.get(cls)
if registered_subclasses is not None:
# We know ``cls`` inherits from Registrable, so we'll use a cast to make mypy happy.
# We have to use a disable to make pylint happy.
# pylint: disable=no-member
as_registrable = cast(Type[Registrable], cls)
default_to_first_choice = as_registrable.default_implementation is not None
choice = params.pop_choice("type",
choices=as_registrable.list_available(),
default_to_first_choice=default_to_first_choice)
subclass = registered_subclasses[choice]
# We want to call subclass.from_params. It's possible that it's just the "free"
# implementation here, in which case it accepts `**extras` and we are not able
# to make any assumptions about what extra parameters it needs.
#
# It's also possible that it has a custom `from_params` method. In that case it
# won't accept any **extra parameters and we'll need to filter them out.
if not takes_arg(subclass.from_params, 'extras'):
# Necessarily subclass.from_params is a custom implementation, so we need to
# pass it only the args it's expecting.
extras = {k: v for k, v in extras.items() if takes_arg(subclass.from_params, k)}
return subclass.from_params(params=params, **extras)
else:
# This is not a base class, so convert our params and extras into a dict of kwargs.
if cls.__init__ == object.__init__:
# This class does not have an explicit constructor, so don't give it any kwargs.
# Without this logic, create_kwargs will look at object.__init__ and see that
# it takes *args and **kwargs and look for those.
kwargs: Dict[str, Any] = {}
else:
# This class has a constructor, so create kwargs for it.
kwargs = create_kwargs(cls, params, **extras)
return cls(**kwargs) # type: ignore
示例3: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, params: Params) -> 'Initializer': # type: ignore
# pylint: disable=arguments-differ
# Just a string - corresponds to the name of an initializer.
if isinstance(params, str):
return cls.by_name(params)()
else:
choice = params.pop_choice("type", cls.list_available())
return cls.by_name(choice).from_params(params)
示例4: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, params: Params) -> "SpanExtractor":
choice = params.pop_choice('type', cls.list_available())
return cls.by_name(choice).from_params(params)
示例5: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, params: Params, vocab: Optional[Vocabulary] = None):
metric_type = params.pop_choice("type", cls.list_available())
if vocab:
params["vocabulary"] = vocab
return cls.by_name(metric_type)(**params.as_dict()) # type: ignore
示例6: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, vocab: Vocabulary, params: Params) -> 'Model':
choice = params.pop_choice("type", cls.list_available())
model = cls.by_name(choice).from_params(vocab, params)
return model
示例7: from_params
# 需要导入模块: from allennlp.common.params import Params [as 别名]
# 或者: from allennlp.common.params.Params import pop_choice [as 别名]
def from_params(cls, optimizer: torch.optim.Optimizer, params: Params):
scheduler = params.pop_choice("type", LearningRateScheduler.list_available())
return LearningRateScheduler.by_name(scheduler)(optimizer, **params.as_dict()) # type: ignore