本文整理匯總了Python中allennlp.models.Model.from_params方法的典型用法代碼示例。如果您正苦於以下問題:Python Model.from_params方法的具體用法?Python Model.from_params怎麽用?Python Model.from_params使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類allennlp.models.Model
的用法示例。
在下文中一共展示了Model.from_params方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: set_up_model
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def set_up_model(self, param_file, dataset_file):
self.param_file = param_file
params = Params.from_file(self.param_file)
reader = DatasetReader.from_params(params["dataset_reader"])
# The dataset reader might be lazy, but a lazy list here breaks some of our tests.
instances = reader.read(str(dataset_file))
# Use parameters for vocabulary if they are present in the config file, so that choices like
# "non_padded_namespaces", "min_count" etc. can be set if needed.
if "vocabulary" in params:
vocab_params = params["vocabulary"]
vocab = Vocabulary.from_params(params=vocab_params, instances=instances)
else:
vocab = Vocabulary.from_instances(instances)
self.vocab = vocab
self.instances = instances
self.instances.index_with(vocab)
self.model = Model.from_params(vocab=self.vocab, params=params["model"])
# TODO(joelgrus) get rid of these
# (a lot of the model tests use them, so they'll have to be changed)
self.dataset = Batch(list(self.instances))
self.dataset.index_instances(self.vocab)
示例2: test_union_of_castable_types
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_union_of_castable_types(self):
class IntFloat(FromParams):
def __init__(self, a: Union[int, float]) -> None:
self.a = a
class FloatInt(FromParams):
def __init__(self, a: Union[float, int]) -> None:
self.a = a
float_param_str = '{"a": 1.0}'
int_param_str = '{"a": 1}'
import json
for expected_type, param_str in [(int, int_param_str), (float, float_param_str)]:
for cls in [IntFloat, FloatInt]:
c = cls.from_params(Params(json.loads(param_str)))
assert type(c.a) == expected_type
示例3: test_transferring_of_modules_ensures_type_consistency
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_transferring_of_modules_ensures_type_consistency(self):
model_archive = str(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
trained_model = load_archive(model_archive).model
config_file = str(self.FIXTURES_ROOT / "basic_classifier" / "experiment_seq2seq.jsonnet")
model_params = Params.from_file(config_file).pop("model").as_dict(quiet=True)
# Override only text_field_embedder and make it load Seq2SeqEncoder
model_params["text_field_embedder"] = {
"_pretrained": {
"archive_file": model_archive,
"module_path": "_seq2seq_encoder._module",
}
}
with pytest.raises(ConfigurationError):
Model.from_params(vocab=trained_model.vocab, params=Params(model_params))
示例4: test_lazy_construction_can_happen_multiple_times
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_lazy_construction_can_happen_multiple_times(self):
test_string = "this is a test"
extra_string = "extra string"
class ConstructedObject(FromParams):
def __init__(self, string: str, extra: str):
self.string = string
self.extra = extra
class Testing(FromParams):
def __init__(self, lazy_object: Lazy[ConstructedObject]):
first_time = lazy_object.construct(extra=extra_string)
second_time = lazy_object.construct(extra=extra_string)
assert first_time.string == test_string
assert first_time.extra == extra_string
assert second_time.string == test_string
assert second_time.extra == extra_string
Testing.from_params(Params({"lazy_object": {"string": test_string}}))
示例5: test_no_elmo_but_set_flags_throws_configuration_error
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_no_elmo_but_set_flags_throws_configuration_error(self):
params = Params.from_file(self.param_file)
# There is no elmo specified in self.param_file, but set
# use_input_elmo and use_integrator_output_elmo to True.
# use_input_elmo set to True
tmp_params = deepcopy(params)
tmp_params[u"model"][u"use_input_elmo"] = True
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
# use_integrator_output_elmo set to True
tmp_params = deepcopy(params)
tmp_params[u"model"][u"use_input_elmo"] = False
tmp_params[u"model"][u"use_integrator_output_elmo"] = True
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
# both use_input_elmo and use_integrator_output_elmo set to True
tmp_params = deepcopy(params)
tmp_params[u"model"][u"use_input_elmo"] = True
tmp_params[u"model"][u"use_integrator_output_elmo"] = True
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
示例6: test_elmo_num_repr_set_flags_mismatch_throws_configuration_error
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_elmo_num_repr_set_flags_mismatch_throws_configuration_error(self):
# pylint: disable=line-too-long
params = Params.from_file(self.FIXTURES_ROOT / u'biattentive_classification_network' / u'elmo_experiment.json')
# Elmo is specified in the model, with num_output_representations=2. Set
# only one flag to true.
tmp_params = deepcopy(params)
tmp_params[u"model"][u"use_input_elmo"] = False
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
tmp_params = deepcopy(params)
tmp_params[u"model"][u"use_input_elmo"] = True
tmp_params[u"model"][u"use_integrator_output_elmo"] = False
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
# set num_output_representations to 1, and set both flags to True.
tmp_params = deepcopy(params)
tmp_params[u"model"][u"elmo"][u"num_output_representations"] = 1
tmp_params[u"model"][u"use_input_elmo"] = True
tmp_params[u"model"][u"use_integrator_output_elmo"] = True
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=tmp_params.get(u"model"))
示例7: test_mismatching_dimensions_throws_configuration_error
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_mismatching_dimensions_throws_configuration_error(self):
params = Params.from_file(self.param_file)
# Make the phrase layer wrong - it should be 10 to match
# the embedding + char cnn dimensions.
params[u"model"][u"phrase_layer"][u"input_size"] = 12
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=params.pop(u"model"))
params = Params.from_file(self.param_file)
# Make the modeling layer input_dimension wrong - it should be 40 to match
# 4 * output_dim of the phrase_layer.
params[u"model"][u"phrase_layer"][u"input_size"] = 30
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=params.pop(u"model"))
params = Params.from_file(self.param_file)
# Make the modeling layer input_dimension wrong - it should be 70 to match
# 4 * phrase_layer.output_dim + 3 * modeling_layer.output_dim.
params[u"model"][u"span_end_encoder"][u"input_size"] = 50
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=params.pop(u"model"))
示例8: set_up_model
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def set_up_model(self, param_file, dataset_file):
# pylint: disable=attribute-defined-outside-init
self.param_file = param_file
params = Params.from_file(self.param_file)
reader = DatasetReader.from_params(params[u'dataset_reader'])
instances = reader.read(dataset_file)
# Use parameters for vocabulary if they are present in the config file, so that choices like
# "non_padded_namespaces", "min_count" etc. can be set if needed.
if u'vocabulary' in params:
vocab_params = params[u'vocabulary']
vocab = Vocabulary.from_params(params=vocab_params, instances=instances)
else:
vocab = Vocabulary.from_instances(instances)
self.vocab = vocab
self.instances = instances
self.model = Model.from_params(vocab=self.vocab, params=params[u'model'])
# TODO(joelgrus) get rid of these
# (a lot of the model tests use them, so they'll have to be changed)
self.dataset = Batch(self.instances)
self.dataset.index_instances(self.vocab)
示例9: set_up_model
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def set_up_model(self, param_file, dataset_file):
# pylint: disable=attribute-defined-outside-init
self.param_file = param_file
params = Params.from_file(self.param_file)
reader = DatasetReader.from_params(params['dataset_reader'])
# The dataset reader might be lazy, but a lazy list here breaks some of our tests.
instances = list(reader.read(str(dataset_file)))
# Use parameters for vocabulary if they are present in the config file, so that choices like
# "non_padded_namespaces", "min_count" etc. can be set if needed.
if 'vocabulary' in params:
vocab_params = params['vocabulary']
vocab = Vocabulary.from_params(params=vocab_params, instances=instances)
else:
vocab = Vocabulary.from_instances(instances)
self.vocab = vocab
self.instances = instances
self.model = Model.from_params(vocab=self.vocab, params=params['model'])
# TODO(joelgrus) get rid of these
# (a lot of the model tests use them, so they'll have to be changed)
self.dataset = Batch(self.instances)
self.dataset.index_instances(self.vocab)
示例10: from_params
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def from_params(cls, params: Params) -> 'WordSplitter':
params.assert_empty(cls.__name__)
return cls()
示例11: setup_method
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def setup_method(self):
super().setup_method()
params = Params(
{
"model": {
"type": "simple_tagger",
"text_field_embedder": {
"token_embedders": {"tokens": {"type": "embedding", "embedding_dim": 5}}
},
"encoder": {"type": "lstm", "input_size": 5, "hidden_size": 7, "num_layers": 2},
},
"dataset_reader": {"type": "sequence_tagging"},
"train_data_path": str(self.FIXTURES_ROOT / "data" / "sequence_tagging.tsv"),
"validation_data_path": str(self.FIXTURES_ROOT / "data" / "sequence_tagging.tsv"),
"data_loader": {"batch_size": 2},
"trainer": {"cuda_device": -1, "num_epochs": 2, "optimizer": "adam"},
}
)
all_datasets = datasets_from_params(params)
vocab = Vocabulary.from_params(
params.pop("vocabulary", {}),
instances=(instance for dataset in all_datasets.values() for instance in dataset),
)
model = Model.from_params(vocab=vocab, params=params.pop("model"))
train_data = all_datasets["train"]
train_data.index_with(vocab)
data_loader = DataLoader.from_params(dataset=train_data, params=params.pop("data_loader"))
trainer_params = params.pop("trainer")
serialization_dir = os.path.join(self.TEST_DIR, "test_search_learning_rate")
self.trainer = Trainer.from_params(
model=model,
serialization_dir=serialization_dir,
data_loader=data_loader,
train_data=train_data,
params=trainer_params,
validation_data=None,
validation_iterator=None,
)
示例12: test_mismatching_dimensions_throws_configuration_error
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_mismatching_dimensions_throws_configuration_error(self):
params = Params.from_file(self.param_file)
# Make the encoder wrong - it should be 2 to match
# the embedding dimension from the text_field_embedder.
params["model"]["encoder"]["input_size"] = 10
with pytest.raises(ConfigurationError):
Model.from_params(vocab=self.vocab, params=params.pop("model"))
示例13: setup_method
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def setup_method(self):
super().setup_method()
param_file = self.FIXTURES_ROOT / "simple_tagger" / "experiment_with_regularization.json"
self.set_up_model(param_file, self.FIXTURES_ROOT / "data" / "sequence_tagging.tsv")
params = Params.from_file(param_file)
self.reader = DatasetReader.from_params(params["dataset_reader"])
self.data_loader = DataLoader.from_params(
dataset=self.instances, params=params["data_loader"]
)
self.trainer = Trainer.from_params(
model=self.model,
data_loader=self.data_loader,
serialization_dir=self.TEST_DIR,
params=params.get("trainer"),
)
示例14: test_from_params
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_from_params(self):
my_class = MyClass.from_params(Params({"my_int": 10}), my_bool=True)
assert isinstance(my_class, MyClass)
assert my_class.my_int == 10
assert my_class.my_bool
示例15: test_no_constructor
# 需要導入模塊: from allennlp.models import Model [as 別名]
# 或者: from allennlp.models.Model import from_params [as 別名]
def test_no_constructor(self):
params = Params({"type": "just_spaces"})
Tokenizer.from_params(params)