本文整理汇总了Python中allennlp.models.model.Model方法的典型用法代码示例。如果您正苦于以下问题:Python model.Model方法的具体用法?Python model.Model怎么用?Python model.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.models.model
的用法示例。
在下文中一共展示了model.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def __init__(self, model: Model, dataset_reader: DatasetReader) -> None:
super().__init__(model, dataset_reader)
# Due to a formatting issue, the word overlap model includes the newline character in the
# label string. So we first create a map from the stripped label to the model label and
# then use the model label to get the index of entailment class. It is designed in this
# manner to also automatically handle any new models that do not have this bug.
labels = {label.strip(): label
for label in self._model.vocab.get_index_to_token_vocabulary("labels").values()}
if "entails" in labels:
self._entailment_idx = self._model.vocab.get_token_index(labels["entails"], "labels")
elif "entailment" in labels:
self._entailment_idx = self._model.vocab.get_token_index(labels["entailment"], "labels")
else:
raise Exception("No label for entailment found in the label space: {}".format(
",".join(labels)))
示例2: evaluate
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def evaluate(model: Model,
instances: Iterable[Instance],
data_iterator: DataIterator,
output_file: str = None,
eval_type: str = None) -> Dict[str, Any]:
model.eval()
iterator = data_iterator(instances, num_epochs=1)
logger.info("Iterating over dataset")
generator_tqdm = Tqdm.tqdm(iterator, total=data_iterator.get_num_batches(instances))
with ExitStack() as stack:
if output_file is None:
file_handle = None
else:
file_handle = stack.enter_context(open(output_file, 'w'))
for batch in generator_tqdm:
model_output = model(**batch)
metrics = model.get_metrics()
if file_handle:
_persist_data(file_handle, batch.get("metadata"), model_output, eval_type)
description = ', '.join(["%s: %.2f" % (name, value) for name, value in metrics.items()]) + " ||"
generator_tqdm.set_description(description)
return model.get_metrics(reset=True)
示例3: evaluate
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def evaluate(model: Model,
instances: Iterable[Instance],
data_iterator: DataIterator,
output_file: str = None) -> Dict[str, Any]:
model.eval()
iterator = data_iterator(instances, num_epochs=1)
logger.info("Iterating over dataset")
generator_tqdm = Tqdm.tqdm(iterator, total=data_iterator.get_num_batches(instances))
with ExitStack() as stack:
if output_file is None:
file_handle = None
else:
file_handle = stack.enter_context(open(output_file, 'w'))
for batch in generator_tqdm:
model_output = model(**batch)
metrics = model.get_metrics()
if file_handle:
id2label = model.vocab.get_index_to_token_vocabulary("labels")
_persist_data(file_handle, batch.get("metadata"), model_output, id2label=id2label)
description = ', '.join(["%s: %.2f" % (name, value) for name, value in metrics.items()]) + " ||"
generator_tqdm.set_description(description)
return model.get_metrics()
示例4: enable_activation_logging
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def enable_activation_logging(self, model: Model) -> None:
if self._histogram_interval is not None:
# To log activation histograms to the forward pass, we register
# a hook on forward to capture the output tensors.
# This uses a closure to determine whether to log the activations,
# since we don't want them on every call.
for _, module in model.named_modules():
if not getattr(module, "should_log_activations", False):
# skip it
continue
def hook(module_, inputs, outputs):
log_prefix = "activation_histogram/{0}".format(module_.__class__)
if self.should_log_histograms_this_batch():
self.log_activation_histogram(outputs, log_prefix)
module.register_forward_hook(hook)
示例5: train_model_from_file
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [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)
示例6: evaluate
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def evaluate(model: Model,
instances: Iterable[Instance],
data_iterator: DataIterator,
cuda_device: int,
output_file: str = None,
eval_type: str = None) -> Dict[str, Any]:
model.eval()
iterator = data_iterator(instances, num_epochs=1)
logger.info("Iterating over dataset")
generator_tqdm = Tqdm.tqdm(iterator, total=data_iterator.get_num_batches(instances))
with ExitStack() as stack:
if output_file is None:
file_handle = None
else:
file_handle = stack.enter_context(open(output_file, 'w'))
for batch in generator_tqdm:
## made cuda compatible (if needed)
batch = move_to_device(batch, cuda_device)
model_output = model(**batch)
metrics = model.get_metrics()
if file_handle:
_persist_data(file_handle, batch.get("metadata"), model_output, eval_type)
description = ', '.join(["%s: %.2f" % (name, value) for name, value in metrics.items()]) + " ||"
generator_tqdm.set_description(description)
return model.get_metrics(reset=True)
示例7: enable_gradient_clipping
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def enable_gradient_clipping(model: Model, grad_clipping: Optional[float]) -> None:
if grad_clipping is not None:
for parameter in model.parameters():
if parameter.requires_grad:
parameter.register_hook(
lambda grad: nn_util.clamp_tensor(
grad, minimum=-grad_clipping, maximum=grad_clipping
)
)
示例8: rescale_gradients
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def rescale_gradients(model: Model, grad_norm: Optional[float] = None) -> Optional[float]:
"""
Performs gradient rescaling. Is a no-op if gradient rescaling is not enabled.
"""
if grad_norm:
parameters_to_clip = [p for p in model.parameters() if p.grad is not None]
return clip_grad_norm_(parameters_to_clip, grad_norm)
return None
示例9: get_metrics
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def get_metrics(
model: Model,
total_loss: float,
total_reg_loss: float,
num_batches: int,
reset: bool = False,
world_size: int = 1,
cuda_device: Union[int, torch.device] = torch.device("cpu"),
) -> Dict[str, float]:
"""
Gets the metrics but sets `"loss"` to
the total loss divided by the `num_batches` so that
the `"loss"` metric is "average loss per batch".
"""
metrics = model.get_metrics(reset=reset)
metrics["loss"] = float(total_loss / num_batches) if num_batches > 0 else 0.0
metrics["reg_loss"] = float(total_reg_loss / num_batches) if num_batches > 0 else 0.0
if world_size > 1:
# In distributed mode, average out all metrics across GPUs
aggregated_metrics = {}
for metric_name, metric_val in metrics.items():
metric_tensor = torch.tensor(metric_val).to(cuda_device)
dist.all_reduce(metric_tensor, op=dist.ReduceOp.SUM)
reduced_metric = metric_tensor.item() / world_size
aggregated_metrics[metric_name] = reduced_metric
return aggregated_metrics
else:
return metrics
示例10: log_batch
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def log_batch(
self,
model: Model,
optimizer: Optimizer,
batch_grad_norm: Optional[float],
metrics: Dict[str, float],
batch_group: List[List[TensorDict]],
param_updates: Optional[Dict[str, torch.Tensor]],
) -> None:
if self.should_log_this_batch():
self.log_parameter_and_gradient_statistics(model, batch_grad_norm)
self.log_learning_rates(model, optimizer)
self.add_train_scalar("loss/loss_train", metrics["loss"])
self.log_metrics({"epoch_metrics/" + k: v for k, v in metrics.items()})
if self.should_log_histograms_this_batch():
self.log_histograms(model)
self.log_gradient_updates(model, param_updates)
if self._batch_size_interval:
# We're assuming here that `log_batch` will get called every batch, and only every
# batch. This is true with our current usage of this code (version 1.0); if that
# assumption becomes wrong, this code will break.
batch_group_size = sum(training_util.get_batch_size(batch) for batch in batch_group)
self._batches_this_epoch += 1
self._cumulative_batch_group_size += batch_group_size
if (self._batches_this_epoch - 1) % self._batch_size_interval == 0:
average = self._cumulative_batch_group_size / self._batches_this_epoch
logger.info(f"current batch size: {batch_group_size} mean batch size: {average}")
self.add_train_scalar("current_batch_size", batch_group_size)
self.add_train_scalar("mean_batch_size", average)
示例11: log_parameter_and_gradient_statistics
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def log_parameter_and_gradient_statistics(self, model: Model, batch_grad_norm: float) -> None:
"""
Send the mean and std of all parameters and gradients to tensorboard, as well
as logging the average gradient norm.
"""
if self._should_log_parameter_statistics:
# Log parameter values to Tensorboard
for name, param in model.named_parameters():
if param.data.numel() > 0:
self.add_train_scalar("parameter_mean/" + name, param.data.mean())
if param.data.numel() > 1:
self.add_train_scalar("parameter_std/" + name, param.data.std())
if param.grad is not None:
if param.grad.is_sparse:
grad_data = param.grad.data._values()
else:
grad_data = param.grad.data
# skip empty gradients
if torch.prod(torch.tensor(grad_data.shape)).item() > 0:
self.add_train_scalar("gradient_mean/" + name, grad_data.mean())
if grad_data.numel() > 1:
self.add_train_scalar("gradient_std/" + name, grad_data.std())
else:
# no gradient for a parameter with sparse gradients
logger.info("No gradient for %s, skipping tensorboard logging.", name)
# norm of gradients
if batch_grad_norm is not None:
self.add_train_scalar("gradient_norm", batch_grad_norm)
示例12: log_histograms
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def log_histograms(self, model: Model) -> None:
"""
Send histograms of parameters to tensorboard.
"""
if not self._histogram_parameters:
# Avoiding calling this every batch. If we ever use two separate models with a single
# writer, this is wrong, but I doubt that will ever happen.
self._histogram_parameters = set(
model.get_parameters_for_histogram_tensorboard_logging()
)
for name, param in model.named_parameters():
if name in self._histogram_parameters:
self.add_train_histogram("parameter_histogram/" + name, param)
示例13: log_gradient_updates
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def log_gradient_updates(self, model: Model, param_updates: Dict[str, torch.Tensor]) -> None:
for name, param in model.named_parameters():
update_norm = torch.norm(param_updates[name].view(-1))
param_norm = torch.norm(param.view(-1)).cpu()
self.add_train_scalar(
"gradient_update/" + name,
update_norm / (param_norm + nn_util.tiny_value_of_dtype(param_norm.dtype)),
)
示例14: test_trainer_raises_on_model_with_no_loss_key
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def test_trainer_raises_on_model_with_no_loss_key(self):
class FakeModel(Model):
def forward(self, **kwargs):
return {}
with pytest.raises(RuntimeError):
trainer = GradientDescentTrainer(
FakeModel(None),
self.optimizer,
self.data_loader,
num_epochs=2,
serialization_dir=self.TEST_DIR,
)
trainer.train()
示例15: __init__
# 需要导入模块: from allennlp.models import model [as 别名]
# 或者: from allennlp.models.model import Model [as 别名]
def __init__(self, model: Model, dataset_reader: DatasetReader) -> None:
super().__init__(model, dataset_reader)
labels = self._model.vocab.get_index_to_token_vocabulary("labels").values()
#
if "entails" in labels:
self._entailment_idx = self._model.vocab.get_token_index("entails", "labels")
elif "entailment" in labels:
self._entailment_idx = self._model.vocab.get_token_index("entailment", "labels")
else:
raise Exception("No label for entailment found in the label space: {}".format(
",".join(labels)))