本文整理汇总了Python中allennlp.common.Params.from_file方法的典型用法代码示例。如果您正苦于以下问题:Python Params.from_file方法的具体用法?Python Params.from_file怎么用?Python Params.from_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.common.Params
的用法示例。
在下文中一共展示了Params.from_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_up_model
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [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_train_can_fine_tune_model_from_archive
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def test_train_can_fine_tune_model_from_archive(self):
params = Params.from_file(
self.FIXTURES_ROOT / "basic_classifier" / "experiment_from_archive.jsonnet"
)
train_loop = TrainModel.from_params(
params=params, serialization_dir=self.TEST_DIR, local_rank=0, batch_weight_key=""
)
train_loop.run()
model = Model.from_archive(
self.FIXTURES_ROOT / "basic_classifier" / "serialization" / "model.tar.gz"
)
# This is checking that the vocabulary actually got extended. The data that we're using for
# training is different from the data we used to produce the model archive, and we set
# parameters such that the vocab should have been extended.
assert train_loop.model.vocab.get_vocab_size() > model.vocab.get_vocab_size()
示例3: test_transferring_of_modules_ensures_type_consistency
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [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_mismatching_dimensions_throws_configuration_error
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [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"))
示例5: set_up_model
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [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)
示例6: set_up_model
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [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)
示例7: train_model_from_file
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def train_model_from_file(parameter_filename: str,
serialization_dir: str,
overrides: str = "",
file_friendly_logging: bool = False,
recover: bool = False,
force: bool = False) -> Model:
"""
A wrapper around :func:`train_model` which loads the params from a file.
Parameters
----------
parameter_filename : ``str``
A json parameter file specifying an AllenNLP experiment.
serialization_dir : ``str``
The directory in which to save results and logs. We just pass this along to
:func:`train_model`.
overrides : ``str``
A JSON string that we will use to override values in the input parameter file.
file_friendly_logging : ``bool``, optional (default=False)
If ``True``, we make our output more friendly to saved model files. We just pass this
along to :func:`train_model`.
recover : ``bool`, optional (default=False)
If ``True``, we will try to recover a training run from an existing serialization
directory. This is only intended for use when something actually crashed during the middle
of a run. For continuing training a model on new data, see the ``fine-tune`` command.
force : ``bool``, optional (default=False)
If ``True``, we will overwrite the serialization directory if it already exists.
"""
# Load the experiment config from a file and pass it to ``train_model``.
params = Params.from_file(parameter_filename, overrides)
return train_model(params, serialization_dir, file_friendly_logging, recover, force)
示例8: find_learning_rate_from_args
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def find_learning_rate_from_args(args: argparse.Namespace) -> None:
"""
Start learning rate finder for given args
"""
params = Params.from_file(args.param_path, args.overrides)
find_learning_rate_model(
params,
args.serialization_dir,
start_lr=args.start_lr,
end_lr=args.end_lr,
num_batches=args.num_batches,
linear_steps=args.linear,
stopping_factor=args.stopping_factor,
force=args.force,
)
示例9: test_train_model_can_instantiate_from_params
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def test_train_model_can_instantiate_from_params(self):
params = Params.from_file(self.FIXTURES_ROOT / "simple_tagger" / "experiment.json")
# Can instantiate from base class params
TrainModel.from_params(
params=params, serialization_dir=self.TEST_DIR, local_rank=0, batch_weight_key=""
)
示例10: train_fixture_gpu
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def train_fixture_gpu(config_prefix: str) -> None:
config_file = config_prefix + "experiment.json"
serialization_dir = config_prefix + "serialization"
params = Params.from_file(config_file)
params["trainer"]["cuda_device"] = 0
# train this one to a tempdir
tempdir = tempfile.gettempdir()
train_model(params, tempdir)
# now copy back the weights and and archived model
shutil.copy(os.path.join(tempdir, "best.th"), os.path.join(serialization_dir, "best_gpu.th"))
shutil.copy(
os.path.join(tempdir, "model.tar.gz"), os.path.join(serialization_dir, "model_gpu.tar.gz")
)
示例11: cache_vocab
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def cache_vocab(params: Params, vocab_config_path: str = None):
"""
Caches the vocabulary given in the Params to the filesystem. Useful for large datasets that are run repeatedly.
:param params: the AllenNLP Params
:param vocab_config_path: an optional config path for constructing the vocab
"""
if "vocabulary" not in params or "directory_path" not in params["vocabulary"]:
return
vocab_path = params["vocabulary"]["directory_path"]
if os.path.exists(vocab_path):
if os.listdir(vocab_path):
return
# Remove empty vocabulary directory to make AllenNLP happy
try:
os.rmdir(vocab_path)
except OSError:
pass
vocab_config_path = vocab_config_path if vocab_config_path else VOCAB_CONFIG_PATH
params = merge_configs([params, Params.from_file(vocab_config_path)])
params["vocabulary"].pop("directory_path", None)
make_vocab_from_params(params, os.path.split(vocab_path)[0])
示例12: fine_tune_model_from_file_paths
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def fine_tune_model_from_file_paths(model_archive_path ,
config_file ,
serialization_dir ,
overrides = u"",
extend_vocab = False,
file_friendly_logging = False) :
u"""
A wrapper around :func:`fine_tune_model` which loads the model archive from a file.
Parameters
----------
model_archive_path : ``str``
Path to a saved model archive that is the result of running the ``train`` command.
config_file : ``str``
A configuration file specifying how to continue training. The format is identical to the
configuration file for the ``train`` command, but any contents in the ``model`` section is
ignored (as we are using the provided model archive instead).
serialization_dir : ``str``
The directory in which to save results and logs. We just pass this along to
:func:`fine_tune_model`.
overrides : ``str``
A JSON string that we will use to override values in the input parameter file.
file_friendly_logging : ``bool``, optional (default=False)
If ``True``, we make our output more friendly to saved model files. We just pass this
along to :func:`fine_tune_model`.
"""
# We don't need to pass in `cuda_device` here, because the trainer will call `model.cuda()` if
# necessary.
archive = load_archive(model_archive_path)
params = Params.from_file(config_file, overrides)
return fine_tune_model(model=archive.model,
params=params,
serialization_dir=serialization_dir,
extend_vocab=extend_vocab,
file_friendly_logging=file_friendly_logging)
示例13: train_model_from_file
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def train_model_from_file(parameter_filename ,
serialization_dir ,
overrides = u"",
file_friendly_logging = False,
recover = False) :
u"""
A wrapper around :func:`train_model` which loads the params from a file.
Parameters
----------
param_path : ``str``
A json parameter file specifying an AllenNLP experiment.
serialization_dir : ``str``
The directory in which to save results and logs. We just pass this along to
:func:`train_model`.
overrides : ``str``
A JSON string that we will use to override values in the input parameter file.
file_friendly_logging : ``bool``, optional (default=False)
If ``True``, we make our output more friendly to saved model files. We just pass this
along to :func:`train_model`.
recover : ``bool`, optional (default=False)
If ``True``, we will try to recover a training run from an existing serialization
directory. This is only intended for use when something actually crashed during the middle
of a run. For continuing training a model on new data, see the ``fine-tune`` command.
"""
# Load the experiment config from a file and pass it to ``train_model``.
params = Params.from_file(parameter_filename, overrides)
return train_model(params, serialization_dir, file_friendly_logging, recover)
示例14: test_file_archiving
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def test_file_archiving(self):
# This happens to be a good place to test auxiliary file archiving.
# Train the model
params = Params.from_file(self.FIXTURES_ROOT / u'elmo' / u'config' / u'characters_token_embedder.json')
serialization_dir = os.path.join(self.TEST_DIR, u'serialization')
train_model(params, serialization_dir)
# Inspect the archive
archive_file = os.path.join(serialization_dir, u'model.tar.gz')
unarchive_dir = os.path.join(self.TEST_DIR, u'unarchive')
with tarfile.open(archive_file, u'r:gz') as archive:
archive.extractall(unarchive_dir)
# It should contain `files_to_archive.json`
fta_file = os.path.join(unarchive_dir, u'files_to_archive.json')
assert os.path.exists(fta_file)
# Which should properly contain { flattened_key -> original_filename }
with open(fta_file) as fta:
files_to_archive = json.loads(fta.read())
assert files_to_archive == {
u'model.text_field_embedder.token_embedders.elmo.options_file':
unicode(pathlib.Path(u'allennlp') / u'tests' / u'fixtures' / u'elmo' / u'options.json'),
u'model.text_field_embedder.token_embedders.elmo.weight_file':
unicode(pathlib.Path(u'allennlp') / u'tests' / u'fixtures' / u'elmo' / u'lm_weights.hdf5'),
}
# Check that the unarchived contents of those files match the original contents.
for key, original_filename in list(files_to_archive.items()):
new_filename = os.path.join(unarchive_dir, u"fta", key)
assert filecmp.cmp(original_filename, new_filename)
示例15: test_forward_with_epoch_num_changes_cost_weight
# 需要导入模块: from allennlp.common import Params [as 别名]
# 或者: from allennlp.common.Params import from_file [as 别名]
def test_forward_with_epoch_num_changes_cost_weight(self):
# Redefining model. We do not want this to change the state of ``self.model``.
params = Params.from_file(self.param_file)
model = Model.from_params(vocab=self.vocab, params=params[u'model'])
# Initial cost weight, before forward is called.
assert model._checklist_cost_weight == 0.8
iterator = EpochTrackingBucketIterator(sorting_keys=[[u'sentence', u'num_tokens']])
cost_weights = []
for epoch_data in iterator(self.dataset, num_epochs=4):
model.forward(**epoch_data)
cost_weights.append(model._checklist_cost_weight)
# The config file has ``wait_num_epochs`` set to 0, so the model starts decreasing the cost
# weight at epoch 0 itself.
assert_almost_equal(cost_weights, [0.72, 0.648, 0.5832, 0.52488])