本文整理汇总了Python中allennlp.models.archival.load_archive方法的典型用法代码示例。如果您正苦于以下问题:Python archival.load_archive方法的具体用法?Python archival.load_archive怎么用?Python archival.load_archive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.models.archival
的用法示例。
在下文中一共展示了archival.load_archive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def __init__(self,
archive_file=DEFAULT_ARCHIVE_FILE,
cuda_device=DEFAULT_CUDA_DEVICE,
model_file=None):
""" Constructor for NLU class. """
check_for_gpu(cuda_device)
if not os.path.isfile(archive_file):
if not model_file:
raise Exception("No model for JointNLU is specified!")
archive_file = cached_path(model_file)
archive = load_archive(archive_file,
cuda_device=cuda_device)
self.tokenizer = SpacyWordSplitter(language="en_core_web_sm")
dataset_reader_params = archive.config["dataset_reader"]
self.dataset_reader = DatasetReader.from_params(dataset_reader_params)
self.model = archive.model
self.model.eval()
示例2: __init__
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def __init__(self,
archive_file=DEFAULT_ARCHIVE_FILE,
cuda_device=DEFAULT_CUDA_DEVICE,
model_file=None):
""" Constructor for NLU class. """
SysPolicy.__init__(self)
check_for_gpu(cuda_device)
if not os.path.isfile(archive_file):
if not model_file:
raise Exception("No model for MILU is specified!")
archive_file = cached_path(model_file)
archive = load_archive(archive_file,
cuda_device=cuda_device)
dataset_reader_params = archive.config["dataset_reader"]
self.dataset_reader = DatasetReader.from_params(dataset_reader_params)
self.action_decoder = MultiWozVocabActionDecoder()
self.action_decoder.action_vocab = self.dataset_reader.action_vocab
self.state_encoder = self.dataset_reader.state_encoder
self.model = archive.model
self.model.eval()
示例3: test_archiving
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_archiving(self):
# copy params, since they'll get consumed during training
params_copy = copy.deepcopy(self.params.as_dict())
# `train_model` should create an archive
serialization_dir = self.TEST_DIR / "archive_test"
model = train_model(self.params, serialization_dir=serialization_dir)
archive_path = serialization_dir / "model.tar.gz"
# load from the archive
archive = load_archive(archive_path)
model2 = archive.model
assert_models_equal(model, model2)
# check that params are the same
params2 = archive.config
assert params2.as_dict() == params_copy
示例4: test_can_load_from_archive_model
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_can_load_from_archive_model(self):
serialization_dir = self.FIXTURES_ROOT / "basic_classifier" / "from_archive_serialization"
archive_path = serialization_dir / "model.tar.gz"
model = load_archive(archive_path).model
# We want to be sure that we don't just not crash, but also be sure that we loaded the right
# weights for the model. We'll do that by making sure that we didn't just load the model
# that's in the `archive_path` of the config file, which is this one.
base_model_path = self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
base_model = load_archive(base_model_path).model
base_model_params = dict(base_model.named_parameters())
for name, parameters in model.named_parameters():
if parameters.size() == base_model_params[name].size():
assert not (parameters == base_model_params[name]).all()
else:
# In this case, the parameters are definitely different, no need for the above
# check.
pass
示例5: test_simple_gradient_basic_text
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_simple_gradient_basic_text(self):
inputs = {"sentence": "It was the ending that I hated"}
archive = load_archive(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
predictor = Predictor.from_archive(archive, "text_classifier")
interpreter = SimpleGradient(predictor)
interpretation = interpreter.saliency_interpret_from_json(inputs)
assert interpretation is not None
assert "instance_1" in interpretation
assert "grad_input_1" in interpretation["instance_1"]
grad_input_1 = interpretation["instance_1"]["grad_input_1"]
assert len(grad_input_1) == 7 # 7 words in input
# two interpretations should be identical for gradient
repeat_interpretation = interpreter.saliency_interpret_from_json(inputs)
repeat_grad_input_1 = repeat_interpretation["instance_1"]["grad_input_1"]
for grad, repeat_grad in zip(grad_input_1, repeat_grad_input_1):
assert grad == approx(repeat_grad)
示例6: test_hotflip
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_hotflip(self):
inputs = {"sentence": "I always write unit tests for my code."}
archive = load_archive(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
predictor = Predictor.from_archive(archive)
hotflipper = Hotflip(predictor)
hotflipper.initialize()
attack = hotflipper.attack_from_json(inputs, "tokens", "grad_input_1")
assert attack is not None
assert "final" in attack
assert "original" in attack
assert "outputs" in attack
assert len(attack["final"][0]) == len(
attack["original"]
) # hotflip replaces words without removing
示例7: test_integrated_gradient
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_integrated_gradient(self):
inputs = {"sentence": "It was the ending that I hated"}
archive = load_archive(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
predictor = Predictor.from_archive(archive, "text_classifier")
interpreter = IntegratedGradient(predictor)
interpretation = interpreter.saliency_interpret_from_json(inputs)
assert interpretation is not None
assert "instance_1" in interpretation
assert "grad_input_1" in interpretation["instance_1"]
grad_input_1 = interpretation["instance_1"]["grad_input_1"]
assert len(grad_input_1) == 7 # 7 words in input
# two interpretations should be identical for integrated gradients
repeat_interpretation = interpreter.saliency_interpret_from_json(inputs)
repeat_grad_input_1 = repeat_interpretation["instance_1"]["grad_input_1"]
for grad, repeat_grad in zip(grad_input_1, repeat_grad_input_1):
assert grad == approx(repeat_grad)
示例8: test_transferring_of_modules_ensures_type_consistency
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [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))
示例9: test_loads_correct_dataset_reader
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_loads_correct_dataset_reader(self):
# This model has a different dataset reader configuration for train and validation. The
# parameter that differs is the token indexer's namespace.
archive = load_archive(
self.FIXTURES_ROOT / "simple_tagger_with_span_f1" / "serialization" / "model.tar.gz"
)
predictor = Predictor.from_archive(archive, "sentence_tagger")
assert predictor._dataset_reader._token_indexers["tokens"].namespace == "test_tokens"
predictor = Predictor.from_archive(
archive, "sentence_tagger", dataset_reader_to_load="train"
)
assert predictor._dataset_reader._token_indexers["tokens"].namespace == "tokens"
predictor = Predictor.from_archive(
archive, "sentence_tagger", dataset_reader_to_load="validation"
)
assert predictor._dataset_reader._token_indexers["tokens"].namespace == "test_tokens"
示例10: test_get_gradients
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_get_gradients(self):
inputs = {
"sentence": "I always write unit tests",
}
archive = load_archive(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
predictor = Predictor.from_archive(archive)
instance = predictor._json_to_instance(inputs)
outputs = predictor._model.forward_on_instance(instance)
labeled_instances = predictor.predictions_to_labeled_instances(instance, outputs)
for instance in labeled_instances:
grads = predictor.get_gradients([instance])[0]
assert "grad_input_1" in grads
assert grads["grad_input_1"] is not None
assert len(grads["grad_input_1"][0]) == 5 # 9 words in hypothesis
示例11: test_get_gradients_when_requires_grad_is_false
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_get_gradients_when_requires_grad_is_false(self):
inputs = {
"sentence": "I always write unit tests",
}
archive = load_archive(
self.FIXTURES_ROOT
/ "basic_classifier"
/ "embedding_with_trainable_is_false"
/ "model.tar.gz"
)
predictor = Predictor.from_archive(archive)
# ensure that requires_grad is initially False on the embedding layer
embedding_layer = util.find_embedding_layer(predictor._model)
assert not embedding_layer.weight.requires_grad
instance = predictor._json_to_instance(inputs)
outputs = predictor._model.forward_on_instance(instance)
labeled_instances = predictor.predictions_to_labeled_instances(instance, outputs)
# ensure that gradients are always present, despite requires_grad being false on the embedding layer
for instance in labeled_instances:
grads = predictor.get_gradients([instance])[0]
assert bool(grads)
# ensure that no side effects remain
assert not embedding_layer.weight.requires_grad
示例12: predict_model_with_archive
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def predict_model_with_archive(predictor: str, params: Params, archive: str,
input_file: str, output_file: str, batch_size: int = 1):
cuda_device = params["trainer"]["cuda_device"]
check_for_gpu(cuda_device)
archive = load_archive(archive,
cuda_device=cuda_device)
predictor = Predictor.from_archive(archive, predictor)
manager = _PredictManager(predictor,
input_file,
output_file,
batch_size,
print_to_console=False,
has_dataset_reader=True)
manager.run()
示例13: test_extra_files
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_extra_files(self):
serialization_dir = self.TEST_DIR / u'serialization'
# Train a model
train_model(self.params, serialization_dir=serialization_dir)
# Archive model, and also archive the training data
files_to_archive = {u"train_data_path": unicode(self.FIXTURES_ROOT / u'data' / u'sequence_tagging.tsv')}
archive_model(serialization_dir=serialization_dir, files_to_archive=files_to_archive)
archive = load_archive(serialization_dir / u'model.tar.gz')
params = archive.config
# The param in the data should have been replaced with a temporary path
# (which we don't know, but we know what it ends with).
assert params.get(u'train_data_path').endswith(u'/fta/train_data_path')
# The validation data path should be the same though.
assert params.get(u'validation_data_path') == unicode(self.FIXTURES_ROOT / u'data' / u'sequence_tagging.tsv')
示例14: test_initialize_weights_from_archive
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_initialize_weights_from_archive(self):
original_model_parameters = self.model.named_parameters()
original_model_weights = dict((name, parameter.data.clone().numpy())
for name, parameter in original_model_parameters)
# pylint: disable=line-too-long
mml_model_archive_file = (self.FIXTURES_ROOT / u"semantic_parsing" / u"nlvr_direct_semantic_parser" /
u"serialization" / u"model.tar.gz")
archive = load_archive(mml_model_archive_file)
archived_model_parameters = archive.model.named_parameters()
self.model._initialize_weights_from_archive(archive)
changed_model_parameters = dict(self.model.named_parameters())
for name, archived_parameter in archived_model_parameters:
archived_weight = archived_parameter.data.numpy()
original_weight = original_model_weights[name]
changed_weight = changed_model_parameters[name].data.numpy()
# We want to make sure that the weights in the original model have indeed been changed
# after a call to ``_initialize_weights_from_archive``.
with self.assertRaises(AssertionError, msg="{name} has not changed"):
assert_almost_equal(original_weight, changed_weight)
# This also includes the sentence token embedder. Those weights will be the same
# because the two models have the same vocabulary.
assert_almost_equal(archived_weight, changed_weight)
示例15: test_get_vocab_index_mapping
# 需要导入模块: from allennlp.models import archival [as 别名]
# 或者: from allennlp.models.archival import load_archive [as 别名]
def test_get_vocab_index_mapping(self):
# pylint: disable=line-too-long
mml_model_archive_file = (self.FIXTURES_ROOT / u"semantic_parsing" / u"nlvr_direct_semantic_parser" /
u"serialization" / u"model.tar.gz")
archive = load_archive(mml_model_archive_file)
mapping = self.model._get_vocab_index_mapping(archive.model.vocab)
expected_mapping = [(i, i) for i in range(16)]
assert mapping == expected_mapping
new_vocab = Vocabulary()
def copy_token_at_index(i):
token = self.vocab.get_token_from_index(i, u"tokens")
new_vocab.add_token_to_namespace(token, u"tokens")
copy_token_at_index(5)
copy_token_at_index(7)
copy_token_at_index(10)
mapping = self.model._get_vocab_index_mapping(new_vocab)
# Mapping of indices from model vocabulary to new vocabulary. 0 and 1 are padding and unk
# tokens.
assert mapping == [(0, 0), (1, 1), (5, 2), (7, 3), (10, 4)]