Python allennlp.common.checks.ConfigurationError() Examples
The following are 30
code examples of allennlp.common.checks.ConfigurationError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
allennlp.common.checks
, or try the search function
.
Example #1
Source File: bag_of_word_counts_token_embedder.py From allennlp with Apache License 2.0 | 6 votes |
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
Example #2
Source File: util.py From OpenBookQA with Apache License 2.0 | 6 votes |
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
Example #3
Source File: train_test.py From allennlp with Apache License 2.0 | 6 votes |
def test_force_gpu(self): import copy params = copy.deepcopy(self.DEFAULT_PARAMS) params["trainer"]["batch_callbacks"] = ["training_device_logger"] params["trainer"]["cuda_device"] = 0 global _seen_training_devices _seen_training_devices.clear() if torch.cuda.device_count() == 0: with pytest.raises(ConfigurationError): train_model(params, serialization_dir=os.path.join(self.TEST_DIR, "test_force_gpu")) else: train_model(params, serialization_dir=os.path.join(self.TEST_DIR, "test_force_gpu")) assert len(_seen_training_devices) == 1 seen_training_device = next(iter(_seen_training_devices)) assert seen_training_device.type == "cuda"
Example #4
Source File: util.py From OpenBookQA with Apache License 2.0 | 6 votes |
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(',')])
Example #5
Source File: train_test.py From allennlp with Apache License 2.0 | 6 votes |
def test_distributed_raises_error_with_no_gpus(self): 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": SEQUENCE_TAGGING_DATA_PATH, "validation_data_path": SEQUENCE_TAGGING_DATA_PATH, "data_loader": {"batch_size": 2}, "trainer": {"num_epochs": 2, "optimizer": "adam"}, "distributed": {}, } ) with pytest.raises(ConfigurationError): train_model(params, serialization_dir=os.path.join(self.TEST_DIR, "test_train_model"))
Example #6
Source File: train_test.py From allennlp with Apache License 2.0 | 6 votes |
def test_error_is_throw_when_cuda_device_is_not_available(self): 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": "test_fixtures/data/sequence_tagging.tsv", "validation_data_path": "test_fixtures/data/sequence_tagging.tsv", "data_loader": {"batch_size": 2}, "trainer": { "num_epochs": 2, "cuda_device": torch.cuda.device_count(), "optimizer": "adam", }, } ) with pytest.raises(ConfigurationError, match="Experiment specified"): train_model(params, serialization_dir=os.path.join(self.TEST_DIR, "test_train_model"))
Example #7
Source File: from_params.py From allennlp with Apache License 2.0 | 6 votes |
def takes_kwargs(obj) -> bool: """ Checks whether a provided object takes in any positional arguments. Similar to takes_arg, we do this for both the __init__ function of the class or a function / method Otherwise, we raise an error """ if inspect.isclass(obj): signature = inspect.signature(obj.__init__) elif inspect.ismethod(obj) or inspect.isfunction(obj): signature = inspect.signature(obj) else: raise ConfigurationError(f"object {obj} is not callable") return any( p.kind == inspect.Parameter.VAR_KEYWORD # type: ignore for p in signature.parameters.values() )
Example #8
Source File: initializers.py From allennlp with Apache License 2.0 | 6 votes |
def __call__(self, tensor: torch.Tensor, parameter_name: str, **kwargs) -> None: # type: ignore # Select the new parameter name if it's being overridden if parameter_name in self.parameter_name_overrides: parameter_name = self.parameter_name_overrides[parameter_name] # If the size of the source and destination tensors are not the # same, then we need to raise an error source_weights = self.weights[parameter_name] if tensor.data.size() != source_weights.size(): raise ConfigurationError( "Incompatible sizes found for parameter %s. " "Found %s and %s" % (parameter_name, tensor.data.size(), source_weights.size()) ) # Copy the parameters from the source to the destination tensor.data[:] = source_weights[:]
Example #9
Source File: pytorch_seq2seq_wrapper.py From allennlp with Apache License 2.0 | 6 votes |
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
Example #10
Source File: util.py From allennlp with Apache License 2.0 | 6 votes |
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(","))
Example #11
Source File: util.py From allennlp with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: util.py From allennlp with Apache License 2.0 | 6 votes |
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)
Example #13
Source File: token_characters_indexer.py From allennlp with Apache License 2.0 | 6 votes |
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}
Example #14
Source File: model.py From allennlp with Apache License 2.0 | 6 votes |
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
Example #15
Source File: conll2003.py From allennlp with Apache License 2.0 | 6 votes |
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"
Example #16
Source File: bag_iterator.py From DISTRE with Apache License 2.0 | 6 votes |
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
Example #17
Source File: label_field.py From allennlp with Apache License 2.0 | 6 votes |
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)) )
Example #18
Source File: interleaving_dataset_reader.py From allennlp with Apache License 2.0 | 6 votes |
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")
Example #19
Source File: text_field.py From allennlp with Apache License 2.0 | 6 votes |
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
Example #20
Source File: trainer.py From allennlp with Apache License 2.0 | 5 votes |
def __init__( self, serialization_dir: str, cuda_device: Optional[Union[int, torch.device]] = None, distributed: bool = False, local_rank: int = 0, world_size: int = 1, ) -> None: if cuda_device is None: from torch import cuda if cuda.device_count() > 0: cuda_device = 0 else: cuda_device = -1 check_for_gpu(cuda_device) self._serialization_dir = serialization_dir if isinstance(cuda_device, list): raise ConfigurationError( "In allennlp 1.0, the Trainer can only be assigned a single `cuda_device`. " "Instead, we use torch's DistributedDataParallel at the command level, meaning " "our Trainer always uses a single GPU per process." ) if distributed and world_size <= 1: raise ConfigurationError( "Distributed training can be performed only with more than 1 device. Check " "`cuda_device` key in the experiment configuration." ) self.cuda_device = int_to_device(cuda_device) self._distributed = distributed self._rank = local_rank self._master = self._rank == 0 self._world_size = world_size
Example #21
Source File: learning_rate_scheduler.py From allennlp with Apache License 2.0 | 5 votes |
def step(self, metric: float = None) -> None: if metric is None: raise ConfigurationError( "This learning rate scheduler requires " "a validation metric to compute the schedule and therefore " "must be used with a validation dataset." ) self.lr_scheduler.step(metric)
Example #22
Source File: fbeta_measure.py From allennlp with Apache License 2.0 | 5 votes |
def __init__(self, beta: float = 1.0, average: str = None, labels: List[int] = None) -> None: average_options = {None, "micro", "macro", "weighted"} if average not in average_options: raise ConfigurationError(f"`average` has to be one of {average_options}.") if beta <= 0: raise ConfigurationError("`beta` should be >0 in the F-beta score.") if labels is not None and len(labels) == 0: raise ConfigurationError("`labels` cannot be an empty list.") self._beta = beta self._average = average self._labels = labels # statistics # the total number of true positive instances under each class # Shape: (num_classes, ) self._true_positive_sum: Union[None, torch.Tensor] = None # the total number of instances # Shape: (num_classes, ) self._total_sum: Union[None, torch.Tensor] = None # the total number of instances under each _predicted_ class, # including true positives and false positives # Shape: (num_classes, ) self._pred_sum: Union[None, torch.Tensor] = None # the total number of instances under each _true_ class, # including true positives and false negatives # Shape: (num_classes, ) self._true_sum: Union[None, torch.Tensor] = None
Example #23
Source File: util.py From allennlp with Apache License 2.0 | 5 votes |
def sort_batch_by_length(tensor: torch.Tensor, sequence_lengths: torch.Tensor): """ Sort a batch first tensor by some specified lengths. # Parameters tensor : `torch.FloatTensor`, required. A batch first Pytorch tensor. sequence_lengths : `torch.LongTensor`, required. A tensor representing the lengths of some dimension of the tensor which we want to sort by. # Returns sorted_tensor : `torch.FloatTensor` The original tensor sorted along the batch dimension with respect to sequence_lengths. sorted_sequence_lengths : `torch.LongTensor` The original sequence_lengths sorted by decreasing size. restoration_indices : `torch.LongTensor` Indices into the sorted_tensor such that `sorted_tensor.index_select(0, restoration_indices) == original_tensor` permutation_index : `torch.LongTensor` The indices used to sort the tensor. This is useful if you want to sort many tensors using the same ordering. """ if not isinstance(tensor, torch.Tensor) or not isinstance(sequence_lengths, torch.Tensor): raise ConfigurationError("Both the tensor and sequence lengths must be torch.Tensors.") sorted_sequence_lengths, permutation_index = sequence_lengths.sort(0, descending=True) sorted_tensor = tensor.index_select(0, permutation_index) index_range = torch.arange(0, len(sequence_lengths), device=sequence_lengths.device) # This is the equivalent of zipping with index, sorting by the original # sequence lengths and returning the now sorted indices. _, reverse_mapping = permutation_index.sort(0, descending=False) restoration_indices = index_range.index_select(0, reverse_mapping) return sorted_tensor, sorted_sequence_lengths, restoration_indices, permutation_index
Example #24
Source File: registrable.py From allennlp with Apache License 2.0 | 5 votes |
def list_available(cls) -> List[str]: """List default first if it exists""" keys = list(Registrable._registry[cls].keys()) default = cls.default_implementation if default is None: return keys elif default not in keys: raise ConfigurationError(f"Default implementation {default} is not registered") else: return [default] + [k for k in keys if k != default]
Example #25
Source File: index_field.py From allennlp with Apache License 2.0 | 5 votes |
def __init__(self, index: int, sequence_field: SequenceField) -> None: self.sequence_index = index self.sequence_field = sequence_field if not isinstance(index, int): raise ConfigurationError( "IndexFields must be passed integer indices. " "Found index: {} with type: {}.".format(index, type(index)) )
Example #26
Source File: sequence_label_field.py From allennlp with Apache License 2.0 | 5 votes |
def __init__( self, labels: Union[List[str], List[int]], sequence_field: SequenceField, label_namespace: str = "labels", ) -> None: self.labels = labels self.sequence_field = sequence_field self._label_namespace = label_namespace self._indexed_labels = None self._maybe_warn_for_namespace(label_namespace) if len(labels) != sequence_field.sequence_length(): raise ConfigurationError( "Label length and sequence length " "don't match: %d and %d" % (len(labels), sequence_field.sequence_length()) ) self._skip_indexing = False if all(isinstance(x, int) for x in labels): self._indexed_labels = labels self._skip_indexing = True elif not all(isinstance(x, str) for x in labels): raise ConfigurationError( "SequenceLabelFields must be passed either all " "strings or all ints. Found labels {} with " "types: {}.".format(labels, [type(x) for x in labels]) )
Example #27
Source File: train_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_dry_run_doesnt_overwrite_vocab(self): vocab_path = self.TEST_DIR / "vocabulary" os.mkdir(vocab_path) # Put something in the vocab directory with open(vocab_path / "test.txt", "a+") as open_file: open_file.write("test") # It should raise error if vocab dir is non-empty with pytest.raises(ConfigurationError): train_model(self.params, self.TEST_DIR, dry_run=True)
Example #28
Source File: pytorch_seq2seq_wrapper_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_wrapper_raises_if_batch_first_is_false(self): with pytest.raises(ConfigurationError): lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7) _ = PytorchSeq2SeqWrapper(lstm)
Example #29
Source File: maxout_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_init_checks_output_dims_consistency(self): with pytest.raises(ConfigurationError): Maxout(input_dim=2, num_layers=2, output_dims=[5, 4, 3], pool_sizes=4, dropout=0.0)
Example #30
Source File: util.py From allennlp with Apache License 2.0 | 5 votes |
def combine_tensors_and_multiply( combination: str, tensors: List[torch.Tensor], weights: torch.nn.Parameter ) -> torch.Tensor: """ Like [`combine_tensors`](./util.md#combine_tensors), but does a weighted (linear) multiplication while combining. This is a separate function from `combine_tensors` because we try to avoid instantiating large intermediate tensors during the combination, which is possible because we know that we're going to be multiplying by a weight vector in the end. # Parameters combination : `str` Same as in `combine_tensors` tensors : `List[torch.Tensor]` A list of tensors to combine, where the integers in the `combination` are (1-indexed) positions in this list of tensors. These tensors are all expected to have either three or four dimensions, with the final dimension being an embedding. If there are four dimensions, one of them must have length 1. weights : `torch.nn.Parameter` A vector of weights to use for the combinations. This should have shape (combined_dim,), as calculated by `get_combined_dim`. """ if len(tensors) > 9: raise ConfigurationError("Double-digit tensor lists not currently supported") combination = combination.replace("x", "1").replace("y", "2") pieces = combination.split(",") tensor_dims = [tensor.size(-1) for tensor in tensors] combination_dims = [_get_combination_dim(piece, tensor_dims) for piece in pieces] dims_so_far = 0 to_sum = [] for piece, combination_dim in zip(pieces, combination_dims): weight = weights[dims_so_far : (dims_so_far + combination_dim)] dims_so_far += combination_dim to_sum.append(_get_combination_and_multiply(piece, tensors, weight)) result = to_sum[0] for result_piece in to_sum[1:]: result = result + result_piece return result