本文整理匯總了Python中allennlp.common.checks.ConfigurationError方法的典型用法代碼示例。如果您正苦於以下問題:Python checks.ConfigurationError方法的具體用法?Python checks.ConfigurationError怎麽用?Python checks.ConfigurationError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類allennlp.common.checks
的用法示例。
在下文中一共展示了checks.ConfigurationError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def __init__(self,
sorting_keys: List[Tuple[str, str]],
padding_noise: float = 0.1,
biggest_batch_first: bool = False,
batch_size: int = 32,
instances_per_epoch: int = None,
max_instances_in_memory: int = None,
cache_instances: bool = False,
track_epoch: bool = False,
maximum_samples_per_batch: Tuple[str, int] = None) -> None:
if not sorting_keys:
raise ConfigurationError("BucketIterator requires sorting_keys to be specified")
super().__init__(cache_instances=cache_instances,
track_epoch=track_epoch,
batch_size=batch_size,
instances_per_epoch=instances_per_epoch,
max_instances_in_memory=max_instances_in_memory,
maximum_samples_per_batch=maximum_samples_per_batch)
self._sorting_keys = sorting_keys
self._padding_noise = padding_noise
self._biggest_batch_first = biggest_batch_first
示例2: get_combined_dim
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def get_combined_dim(combination: str, tensor_dims: List[int]) -> int:
"""
For use with :func:`combine_tensors`. This function computes the resultant dimension when
calling ``combine_tensors(combination, tensors)``, when the tensor dimension is known. This is
necessary for knowing the sizes of weight matrices when building models that use
``combine_tensors``.
Parameters
----------
combination : ``str``
A comma-separated list of combination pieces, like ``"1,2,1*2"``, specified identically to
``combination`` in :func:`combine_tensors`.
tensor_dims : ``List[int]``
A list of tensor dimensions, where each dimension is from the `last axis` of the tensors
that will be input to :func:`combine_tensors`.
"""
if len(tensor_dims) > 9:
raise ConfigurationError("Double-digit tensor lists not currently supported")
combination = combination.replace('x', '1').replace('y', '2')
return sum([_get_combination_dim(piece, tensor_dims) for piece in combination.split(',')])
示例3: _get_combination_dim
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def _get_combination_dim(combination: str, tensor_dims: List[int]) -> int:
if combination.isdigit():
index = int(combination) - 1
return tensor_dims[index]
else:
if "(" in combination:
# handles cases like combination="abs(x-y)"
if ")" not in combination:
raise ConfigurationError("Closing bracket was not found in {0}".format(combination))
combination = combination.replace(")", "").split("(")[-1]
if len(combination) != 3:
raise ConfigurationError("Invalid combination: " + combination)
first_tensor_dim = _get_combination_dim(combination[0], tensor_dims)
second_tensor_dim = _get_combination_dim(combination[2], tensor_dims)
operation = combination[1]
if first_tensor_dim != second_tensor_dim:
raise ConfigurationError("Tensor dims must match for operation \"{}\"".format(operation))
return first_tensor_dim
示例4: __init__
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def __init__(self, module: torch.nn.Module, stateful: bool = False) -> None:
super().__init__(stateful)
self._module = module
try:
if not self._module.batch_first:
raise ConfigurationError("Our encoder semantics assumes batch is always first!")
except AttributeError:
pass
try:
self._is_bidirectional = self._module.bidirectional
except AttributeError:
self._is_bidirectional = False
if self._is_bidirectional:
self._num_directions = 2
else:
self._num_directions = 1
示例5: __init__
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def __init__(
self,
vocab: Vocabulary,
vocab_namespace: str = "tokens",
projection_dim: int = None,
ignore_oov: bool = False,
) -> None:
super().__init__()
self.vocab = vocab
self.vocab_size = vocab.get_vocab_size(vocab_namespace)
if projection_dim:
self._projection = torch.nn.Linear(self.vocab_size, projection_dim)
else:
self._projection = None
self._ignore_oov = ignore_oov
oov_token = vocab._oov_token
self._oov_idx = vocab.get_token_to_index_vocabulary(vocab_namespace).get(oov_token)
if self._oov_idx is None:
raise ConfigurationError(
"OOV token does not exist in vocabulary namespace {}".format(vocab_namespace)
)
self.output_dim = projection_dim or self.vocab_size
示例6: tokens_to_indices
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def tokens_to_indices(
self, tokens: List[Token], vocabulary: Vocabulary
) -> Dict[str, List[List[int]]]:
indices: List[List[int]] = []
for token in itertools.chain(self._start_tokens, tokens, self._end_tokens):
token_indices: List[int] = []
if token.text is None:
raise ConfigurationError(
"TokenCharactersIndexer needs a tokenizer that retains text"
)
for character in self._character_tokenizer.tokenize(token.text):
if getattr(character, "text_id", None) is not None:
# `text_id` being set on the token means that we aren't using the vocab, we just
# use this id instead.
index = character.text_id
else:
index = vocabulary.get_token_index(character.text, self._namespace)
token_indices.append(index)
indices.append(token_indices)
return {"token_characters": indices}
示例7: print_statistics
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def print_statistics(self) -> None:
# Make sure if has been indexed first
sequence_field_lengths: Dict[str, List] = defaultdict(list)
for instance in self.instances:
if not instance.indexed:
raise ConfigurationError(
"Instances must be indexed with vocabulary "
"before asking to print dataset statistics."
)
for field, field_padding_lengths in instance.get_padding_lengths().items():
for key, value in field_padding_lengths.items():
sequence_field_lengths[f"{field}.{key}"].append(value)
print("\n\n----Dataset Statistics----\n")
for name, lengths in sequence_field_lengths.items():
print(f"Statistics for {name}:")
print(
f"\tLengths: Mean: {numpy.mean(lengths)}, Standard Dev: {numpy.std(lengths)}, "
f"Max: {numpy.max(lengths)}, Min: {numpy.min(lengths)}"
)
print("\n10 Random instances:")
for i in numpy.random.randint(len(self.instances), size=10):
print(f"Instance {i}:")
print(f"\t{self.instances[i]}")
示例8: __init__
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def __init__(
self,
token_indexers: Dict[str, TokenIndexer] = None,
tag_label: str = "ner",
feature_labels: Sequence[str] = (),
coding_scheme: str = "IOB1",
label_namespace: str = "labels",
**kwargs,
) -> None:
super().__init__(**kwargs)
self._token_indexers = token_indexers or {"tokens": SingleIdTokenIndexer()}
if tag_label is not None and tag_label not in self._VALID_LABELS:
raise ConfigurationError("unknown tag label type: {}".format(tag_label))
for label in feature_labels:
if label not in self._VALID_LABELS:
raise ConfigurationError("unknown feature label type: {}".format(label))
if coding_scheme not in ("IOB1", "BIOUL"):
raise ConfigurationError("unknown coding_scheme: {}".format(coding_scheme))
self.tag_label = tag_label
self.feature_labels = set(feature_labels)
self.coding_scheme = coding_scheme
self.label_namespace = label_namespace
self._original_coding_scheme = "IOB1"
示例9: _read
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def _read(self, file_path: str) -> Iterable[Instance]:
try:
file_paths = json.loads(file_path)
except json.JSONDecodeError:
raise ConfigurationError(
"the file_path for the InterleavingDatasetReader "
"needs to be a JSON-serialized dictionary {reader_name -> file_path}"
)
if file_paths.keys() != self._readers.keys():
raise ConfigurationError("mismatched keys")
# Load datasets
datasets = {key: reader.read(file_paths[key]) for key, reader in self._readers.items()}
if self._scheme == "round_robin":
yield from self._read_round_robin(datasets)
elif self._scheme == "all_at_once":
yield from self._read_all_at_once(datasets)
else:
raise RuntimeError("impossible to get here")
示例10: get_padding_lengths
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def get_padding_lengths(self) -> Dict[str, int]:
"""
The `TextField` has a list of `Tokens`, and each `Token` gets converted into arrays by
(potentially) several `TokenIndexers`. This method gets the max length (over tokens)
associated with each of these arrays.
"""
if self._indexed_tokens is None:
raise ConfigurationError(
"You must call .index(vocabulary) on a field before determining padding lengths."
)
padding_lengths = {}
for indexer_name, indexer in self._token_indexers.items():
indexer_lengths = indexer.get_padding_lengths(self._indexed_tokens[indexer_name])
for key, length in indexer_lengths.items():
padding_lengths[f"{indexer_name}___{key}"] = length
return padding_lengths
示例11: __init__
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def __init__(
self, label: Union[str, int], label_namespace: str = "labels", skip_indexing: bool = False
) -> None:
self.label = label
self._label_namespace = label_namespace
self._label_id = None
self._maybe_warn_for_namespace(label_namespace)
self._skip_indexing = skip_indexing
if skip_indexing:
if not isinstance(label, int):
raise ConfigurationError(
"In order to skip indexing, your labels must be integers. "
"Found label = {}".format(label)
)
self._label_id = label
elif not isinstance(label, str):
raise ConfigurationError(
"LabelFields must be passed a string label if skip_indexing=False. "
"Found label: {} with type: {}.".format(label, type(label))
)
示例12: _get_prediction_device
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def _get_prediction_device(self) -> int:
"""
This method checks the device of the model parameters to determine the cuda_device
this model should be run on for predictions. If there are no parameters, it returns -1.
# Returns
The cuda device this model should run on for predictions.
"""
devices = {util.get_device_of(param) for param in self.parameters()}
if len(devices) > 1:
devices_string = ", ".join(str(x) for x in devices)
raise ConfigurationError(f"Parameters have mismatching cuda_devices: {devices_string}")
elif len(devices) == 1:
return devices.pop()
else:
return -1
示例13: replace_masked_values
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def replace_masked_values(
tensor: torch.Tensor, mask: torch.BoolTensor, replace_with: float
) -> torch.Tensor:
"""
Replaces all masked values in `tensor` with `replace_with`. `mask` must be broadcastable
to the same shape as `tensor`. We require that `tensor.dim() == mask.dim()`, as otherwise we
won't know which dimensions of the mask to unsqueeze.
This just does `tensor.masked_fill()`, except the pytorch method fills in things with a mask
value of 1, where we want the opposite. You can do this in your own code with
`tensor.masked_fill(~mask, replace_with)`.
"""
if tensor.dim() != mask.dim():
raise ConfigurationError(
"tensor.dim() (%d) != mask.dim() (%d)" % (tensor.dim(), mask.dim())
)
return tensor.masked_fill(~mask, replace_with)
示例14: _get_combination
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def _get_combination(combination: str, tensors: List[torch.Tensor]) -> torch.Tensor:
if combination.isdigit():
index = int(combination) - 1
return tensors[index]
else:
if len(combination) != 3:
raise ConfigurationError("Invalid combination: " + combination)
first_tensor = _get_combination(combination[0], tensors)
second_tensor = _get_combination(combination[2], tensors)
operation = combination[1]
if operation == "*":
return first_tensor * second_tensor
elif operation == "/":
return first_tensor / second_tensor
elif operation == "+":
return first_tensor + second_tensor
elif operation == "-":
return first_tensor - second_tensor
else:
raise ConfigurationError("Invalid operation: " + operation)
示例15: get_combined_dim
# 需要導入模塊: from allennlp.common import checks [as 別名]
# 或者: from allennlp.common.checks import ConfigurationError [as 別名]
def get_combined_dim(combination: str, tensor_dims: List[int]) -> int:
"""
For use with [`combine_tensors`](./util.md#combine_tensors).
This function computes the resultant dimension when calling `combine_tensors(combination, tensors)`,
when the tensor dimension is known. This is necessary for knowing the sizes of weight matrices
when building models that use `combine_tensors`.
# Parameters
combination : `str`
A comma-separated list of combination pieces, like `"1,2,1*2"`, specified identically to
`combination` in `combine_tensors`.
tensor_dims : `List[int]`
A list of tensor dimensions, where each dimension is from the `last axis` of the tensors
that will be input to `combine_tensors`.
"""
if len(tensor_dims) > 9:
raise ConfigurationError("Double-digit tensor lists not currently supported")
combination = combination.replace("x", "1").replace("y", "2")
return sum(_get_combination_dim(piece, tensor_dims) for piece in combination.split(","))