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


Python Model.from_params方法代码示例

本文整理汇总了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) 
开发者ID:allenai,项目名称:allennlp,代码行数:26,代码来源:model_test_case.py

示例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 
开发者ID:allenai,项目名称:allennlp,代码行数:19,代码来源:from_params_test.py

示例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)) 
开发者ID:allenai,项目名称:allennlp,代码行数:21,代码来源:from_params_test.py

示例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}})) 
开发者ID:allenai,项目名称:allennlp,代码行数:21,代码来源:from_params_test.py

示例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")) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:25,代码来源:biattentive_classification_network_test.py

示例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")) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:25,代码来源:biattentive_classification_network_test.py

示例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")) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:23,代码来源:bidaf_test.py

示例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) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:24,代码来源:model_test_case.py

示例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) 
开发者ID:allenai,项目名称:vampire,代码行数:25,代码来源:test_case.py

示例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() 
开发者ID:sheffieldnlp,项目名称:fever-naacl-2018,代码行数:5,代码来源:sentence_train.py

示例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,
        ) 
开发者ID:allenai,项目名称:allennlp,代码行数:42,代码来源:find_learning_rate_test.py

示例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")) 
开发者ID:allenai,项目名称:allennlp,代码行数:9,代码来源:simple_tagger_test.py

示例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"),
        ) 
开发者ID:allenai,项目名称:allennlp,代码行数:17,代码来源:simple_tagger_test.py

示例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 
开发者ID:allenai,项目名称:allennlp,代码行数:8,代码来源:from_params_test.py

示例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) 
开发者ID:allenai,项目名称:allennlp,代码行数:6,代码来源:from_params_test.py


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