Code
stringlengths 103
85.9k
| Summary
sequencelengths 0
94
|
---|---|
Please provide a description of the function:def read_dataset_schema(schema_path: str) -> Dict[str, List[TableColumn]]:
schema: Dict[str, List[TableColumn]] = defaultdict(list)
for i, line in enumerate(open(schema_path, "r")):
if i == 0:
header = [x.strip() for x in line.split(",")]
elif line[0] == "-":
continue
else:
data = {key: value for key, value in zip(header, [x.strip() for x in line.split(",")])}
table = data.get("Table Name", None) or data.get("Table")
column = data.get("Field Name", None) or data.get("Field")
is_primary_key = data.get("Primary Key") == "y"
schema[table.upper()].append(TableColumn(column.upper(), data["Type"], is_primary_key))
return {**schema} | [
"\n Reads a schema from the text2sql data, returning a dictionary\n mapping table names to their columns and respective types.\n This handles columns in an arbitrary order and also allows\n either ``{Table, Field}`` or ``{Table, Field} Name`` as headers,\n because both appear in the data. It also uppercases table and\n column names if they are not already uppercase.\n\n Parameters\n ----------\n schema_path : ``str``, required.\n The path to the csv schema.\n\n Returns\n -------\n A dictionary mapping table names to typed columns.\n "
] |
Please provide a description of the function:def process_sql_data(data: List[JsonDict],
use_all_sql: bool = False,
use_all_queries: bool = False,
remove_unneeded_aliases: bool = False,
schema: Dict[str, List[TableColumn]] = None) -> Iterable[SqlData]:
for example in data:
seen_sentences: Set[str] = set()
for sent_info in example['sentences']:
# Loop over the different sql statements with "equivalent" semantics
for sql in example["sql"]:
text_with_variables = sent_info['text'].strip().split()
text_vars = sent_info['variables']
query_tokens, tags = replace_variables(text_with_variables, text_vars)
if not use_all_queries:
key = " ".join(query_tokens)
if key in seen_sentences:
continue
else:
seen_sentences.add(key)
sql_tokens = clean_and_split_sql(sql)
if remove_unneeded_aliases:
sql_tokens = clean_unneeded_aliases(sql_tokens)
if schema is not None:
sql_tokens = resolve_primary_keys_in_schema(sql_tokens, schema)
sql_variables = {}
for variable in example['variables']:
sql_variables[variable['name']] = {'text': variable['example'], 'type': variable['type']}
sql_data = SqlData(text=query_tokens,
text_with_variables=text_with_variables,
variable_tags=tags,
sql=sql_tokens,
text_variables=text_vars,
sql_variables=sql_variables)
yield sql_data
# Some questions might have multiple equivalent SQL statements.
# By default, we just use the first one. TODO(Mark): Use the shortest?
if not use_all_sql:
break | [
"\n A utility function for reading in text2sql data. The blob is\n the result of loading the json from a file produced by the script\n ``scripts/reformat_text2sql_data.py``.\n\n Parameters\n ----------\n data : ``JsonDict``\n use_all_sql : ``bool``, optional (default = False)\n Whether to use all of the sql queries which have identical semantics,\n or whether to just use the first one.\n use_all_queries : ``bool``, (default = False)\n Whether or not to enforce query sentence uniqueness. If false,\n duplicated queries will occur in the dataset as separate instances,\n as for a given SQL query, not only are there multiple queries with\n the same template, but there are also duplicate queries.\n remove_unneeded_aliases : ``bool``, (default = False)\n The text2sql data by default creates alias names for `all` tables,\n regardless of whether the table is derived or if it is identical to\n the original (e.g SELECT TABLEalias0.COLUMN FROM TABLE AS TABLEalias0).\n This is not necessary and makes the action sequence and grammar manipulation\n much harder in a grammar based decoder. Note that this does not\n remove aliases which are legitimately required, such as when a new\n table is formed by performing operations on the original table.\n schema : ``Dict[str, List[TableColumn]]``, optional, (default = None)\n A schema to resolve primary keys against. Converts 'ID' column names\n to their actual name with respect to the Primary Key for the table\n in the schema.\n "
] |
Please provide a description of the function:def sort_and_run_forward(self,
module: Callable[[PackedSequence, Optional[RnnState]],
Tuple[Union[PackedSequence, torch.Tensor], RnnState]],
inputs: torch.Tensor,
mask: torch.Tensor,
hidden_state: Optional[RnnState] = None):
# In some circumstances you may have sequences of zero length. ``pack_padded_sequence``
# requires all sequence lengths to be > 0, so remove sequences of zero length before
# calling self._module, then fill with zeros.
# First count how many sequences are empty.
batch_size = mask.size(0)
num_valid = torch.sum(mask[:, 0]).int().item()
sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
sorted_inputs, sorted_sequence_lengths, restoration_indices, sorting_indices =\
sort_batch_by_length(inputs, sequence_lengths)
# Now create a PackedSequence with only the non-empty, sorted sequences.
packed_sequence_input = pack_padded_sequence(sorted_inputs[:num_valid, :, :],
sorted_sequence_lengths[:num_valid].data.tolist(),
batch_first=True)
# Prepare the initial states.
if not self.stateful:
if hidden_state is None:
initial_states = hidden_state
elif isinstance(hidden_state, tuple):
initial_states = [state.index_select(1, sorting_indices)[:, :num_valid, :].contiguous()
for state in hidden_state]
else:
initial_states = hidden_state.index_select(1, sorting_indices)[:, :num_valid, :].contiguous()
else:
initial_states = self._get_initial_states(batch_size, num_valid, sorting_indices)
# Actually call the module on the sorted PackedSequence.
module_output, final_states = module(packed_sequence_input, initial_states)
return module_output, final_states, restoration_indices | [
"\n This function exists because Pytorch RNNs require that their inputs be sorted\n before being passed as input. As all of our Seq2xxxEncoders use this functionality,\n it is provided in a base class. This method can be called on any module which\n takes as input a ``PackedSequence`` and some ``hidden_state``, which can either be a\n tuple of tensors or a tensor.\n\n As all of our Seq2xxxEncoders have different return types, we return `sorted`\n outputs from the module, which is called directly. Additionally, we return the\n indices into the batch dimension required to restore the tensor to it's correct,\n unsorted order and the number of valid batch elements (i.e the number of elements\n in the batch which are not completely masked). This un-sorting and re-padding\n of the module outputs is left to the subclasses because their outputs have different\n types and handling them smoothly here is difficult.\n\n Parameters\n ----------\n module : ``Callable[[PackedSequence, Optional[RnnState]],\n Tuple[Union[PackedSequence, torch.Tensor], RnnState]]``, required.\n A function to run on the inputs. In most cases, this is a ``torch.nn.Module``.\n inputs : ``torch.Tensor``, required.\n A tensor of shape ``(batch_size, sequence_length, embedding_size)`` representing\n the inputs to the Encoder.\n mask : ``torch.Tensor``, required.\n A tensor of shape ``(batch_size, sequence_length)``, representing masked and\n non-masked elements of the sequence for each element in the batch.\n hidden_state : ``Optional[RnnState]``, (default = None).\n A single tensor of shape (num_layers, batch_size, hidden_size) representing the\n state of an RNN with or a tuple of\n tensors of shapes (num_layers, batch_size, hidden_size) and\n (num_layers, batch_size, memory_size), representing the hidden state and memory\n state of an LSTM-like RNN.\n\n Returns\n -------\n module_output : ``Union[torch.Tensor, PackedSequence]``.\n A Tensor or PackedSequence representing the output of the Pytorch Module.\n The batch size dimension will be equal to ``num_valid``, as sequences of zero\n length are clipped off before the module is called, as Pytorch cannot handle\n zero length sequences.\n final_states : ``Optional[RnnState]``\n A Tensor representing the hidden state of the Pytorch Module. This can either\n be a single tensor of shape (num_layers, num_valid, hidden_size), for instance in\n the case of a GRU, or a tuple of tensors, such as those required for an LSTM.\n restoration_indices : ``torch.LongTensor``\n A tensor of shape ``(batch_size,)``, describing the re-indexing required to transform\n the outputs back to their original batch order.\n "
] |
Please provide a description of the function:def _get_initial_states(self,
batch_size: int,
num_valid: int,
sorting_indices: torch.LongTensor) -> Optional[RnnState]:
# We don't know the state sizes the first time calling forward,
# so we let the module define what it's initial hidden state looks like.
if self._states is None:
return None
# Otherwise, we have some previous states.
if batch_size > self._states[0].size(1):
# This batch is larger than the all previous states.
# If so, resize the states.
num_states_to_concat = batch_size - self._states[0].size(1)
resized_states = []
# state has shape (num_layers, batch_size, hidden_size)
for state in self._states:
# This _must_ be inside the loop because some
# RNNs have states with different last dimension sizes.
zeros = state.new_zeros(state.size(0),
num_states_to_concat,
state.size(2))
resized_states.append(torch.cat([state, zeros], 1))
self._states = tuple(resized_states)
correctly_shaped_states = self._states
elif batch_size < self._states[0].size(1):
# This batch is smaller than the previous one.
correctly_shaped_states = tuple(state[:, :batch_size, :] for state in self._states)
else:
correctly_shaped_states = self._states
# At this point, our states are of shape (num_layers, batch_size, hidden_size).
# However, the encoder uses sorted sequences and additionally removes elements
# of the batch which are fully padded. We need the states to match up to these
# sorted and filtered sequences, so we do that in the next two blocks before
# returning the state/s.
if len(self._states) == 1:
# GRUs only have a single state. This `unpacks` it from the
# tuple and returns the tensor directly.
correctly_shaped_state = correctly_shaped_states[0]
sorted_state = correctly_shaped_state.index_select(1, sorting_indices)
return sorted_state[:, :num_valid, :].contiguous()
else:
# LSTMs have a state tuple of (state, memory).
sorted_states = [state.index_select(1, sorting_indices)
for state in correctly_shaped_states]
return tuple(state[:, :num_valid, :].contiguous() for state in sorted_states) | [
"\n Returns an initial state for use in an RNN. Additionally, this method handles\n the batch size changing across calls by mutating the state to append initial states\n for new elements in the batch. Finally, it also handles sorting the states\n with respect to the sequence lengths of elements in the batch and removing rows\n which are completely padded. Importantly, this `mutates` the state if the\n current batch size is larger than when it was previously called.\n\n Parameters\n ----------\n batch_size : ``int``, required.\n The batch size can change size across calls to stateful RNNs, so we need\n to know if we need to expand or shrink the states before returning them.\n Expanded states will be set to zero.\n num_valid : ``int``, required.\n The batch may contain completely padded sequences which get removed before\n the sequence is passed through the encoder. We also need to clip these off\n of the state too.\n sorting_indices ``torch.LongTensor``, required.\n Pytorch RNNs take sequences sorted by length. When we return the states to be\n used for a given call to ``module.forward``, we need the states to match up to\n the sorted sequences, so before returning them, we sort the states using the\n same indices used to sort the sequences.\n\n Returns\n -------\n This method has a complex return type because it has to deal with the first time it\n is called, when it has no state, and the fact that types of RNN have heterogeneous\n states.\n\n If it is the first time the module has been called, it returns ``None``, regardless\n of the type of the ``Module``.\n\n Otherwise, for LSTMs, it returns a tuple of ``torch.Tensors`` with shape\n ``(num_layers, num_valid, state_size)`` and ``(num_layers, num_valid, memory_size)``\n respectively, or for GRUs, it returns a single ``torch.Tensor`` of shape\n ``(num_layers, num_valid, state_size)``.\n "
] |
Please provide a description of the function:def _update_states(self,
final_states: RnnStateStorage,
restoration_indices: torch.LongTensor) -> None:
# TODO(Mark): seems weird to sort here, but append zeros in the subclasses.
# which way around is best?
new_unsorted_states = [state.index_select(1, restoration_indices)
for state in final_states]
if self._states is None:
# We don't already have states, so just set the
# ones we receive to be the current state.
self._states = tuple(state.data for state in new_unsorted_states)
else:
# Now we've sorted the states back so that they correspond to the original
# indices, we need to figure out what states we need to update, because if we
# didn't use a state for a particular row, we want to preserve its state.
# Thankfully, the rows which are all zero in the state correspond exactly
# to those which aren't used, so we create masks of shape (new_batch_size,),
# denoting which states were used in the RNN computation.
current_state_batch_size = self._states[0].size(1)
new_state_batch_size = final_states[0].size(1)
# Masks for the unused states of shape (1, new_batch_size, 1)
used_new_rows_mask = [(state[0, :, :].sum(-1)
!= 0.0).float().view(1, new_state_batch_size, 1)
for state in new_unsorted_states]
new_states = []
if current_state_batch_size > new_state_batch_size:
# The new state is smaller than the old one,
# so just update the indices which we used.
for old_state, new_state, used_mask in zip(self._states,
new_unsorted_states,
used_new_rows_mask):
# zero out all rows in the previous state
# which _were_ used in the current state.
masked_old_state = old_state[:, :new_state_batch_size, :] * (1 - used_mask)
# The old state is larger, so update the relevant parts of it.
old_state[:, :new_state_batch_size, :] = new_state + masked_old_state
new_states.append(old_state.detach())
else:
# The states are the same size, so we just have to
# deal with the possibility that some rows weren't used.
new_states = []
for old_state, new_state, used_mask in zip(self._states,
new_unsorted_states,
used_new_rows_mask):
# zero out all rows which _were_ used in the current state.
masked_old_state = old_state * (1 - used_mask)
# The old state is larger, so update the relevant parts of it.
new_state += masked_old_state
new_states.append(new_state.detach())
# It looks like there should be another case handled here - when
# the current_state_batch_size < new_state_batch_size. However,
# this never happens, because the states themeselves are mutated
# by appending zeros when calling _get_inital_states, meaning that
# the new states are either of equal size, or smaller, in the case
# that there are some unused elements (zero-length) for the RNN computation.
self._states = tuple(new_states) | [
"\n After the RNN has run forward, the states need to be updated.\n This method just sets the state to the updated new state, performing\n several pieces of book-keeping along the way - namely, unsorting the\n states and ensuring that the states of completely padded sequences are\n not updated. Finally, it also detaches the state variable from the\n computational graph, such that the graph can be garbage collected after\n each batch iteration.\n\n Parameters\n ----------\n final_states : ``RnnStateStorage``, required.\n The hidden states returned as output from the RNN.\n restoration_indices : ``torch.LongTensor``, required.\n The indices that invert the sorting used in ``sort_and_run_forward``\n to order the states with respect to the lengths of the sequences in\n the batch.\n "
] |
Please provide a description of the function:def construct_prefix_tree(targets: Union[torch.Tensor, List[List[List[int]]]],
target_mask: Optional[torch.Tensor] = None) -> List[Dict[Tuple[int, ...], Set[int]]]:
batched_allowed_transitions: List[Dict[Tuple[int, ...], Set[int]]] = []
if not isinstance(targets, list):
assert targets.dim() == 3, "targets tensor needs to be batched!"
targets = targets.detach().cpu().numpy().tolist()
if target_mask is not None:
target_mask = target_mask.detach().cpu().numpy().tolist()
else:
target_mask = [None for _ in targets]
for instance_targets, instance_mask in zip(targets, target_mask):
allowed_transitions: Dict[Tuple[int, ...], Set[int]] = defaultdict(set)
for i, target_sequence in enumerate(instance_targets):
history: Tuple[int, ...] = ()
for j, action in enumerate(target_sequence):
if instance_mask and instance_mask[i][j] == 0:
break
allowed_transitions[history].add(action)
history = history + (action,)
batched_allowed_transitions.append(allowed_transitions)
return batched_allowed_transitions | [
"\n Takes a list of valid target action sequences and creates a mapping from all possible\n (valid) action prefixes to allowed actions given that prefix. While the method is called\n ``construct_prefix_tree``, we're actually returning a map that has as keys the paths to\n `all internal nodes of the trie`, and as values all of the outgoing edges from that node.\n\n ``targets`` is assumed to be a tensor of shape ``(batch_size, num_valid_sequences,\n sequence_length)``. If the mask is not ``None``, it is assumed to have the same shape, and\n we will ignore any value in ``targets`` that has a value of ``0`` in the corresponding\n position in the mask. We assume that the mask has the format 1*0* for each item in\n ``targets`` - that is, once we see our first zero, we stop processing that target.\n\n For example, if ``targets`` is the following tensor: ``[[1, 2, 3], [1, 4, 5]]``, the return\n value will be: ``{(): set([1]), (1,): set([2, 4]), (1, 2): set([3]), (1, 4): set([5])}``.\n\n This could be used, e.g., to do an efficient constrained beam search, or to efficiently\n evaluate the probability of all of the target sequences.\n "
] |
Please provide a description of the function:def to_value(original_string, corenlp_value=None):
if isinstance(original_string, Value):
# Already a Value
return original_string
if not corenlp_value:
corenlp_value = original_string
# Number?
amount = NumberValue.parse(corenlp_value)
if amount is not None:
return NumberValue(amount, original_string)
# Date?
ymd = DateValue.parse(corenlp_value)
if ymd is not None:
if ymd[1] == ymd[2] == -1:
return NumberValue(ymd[0], original_string)
else:
return DateValue(ymd[0], ymd[1], ymd[2], original_string)
# String.
return StringValue(original_string) | [
"Convert the string to Value object.\n\n Args:\n original_string (basestring): Original string\n corenlp_value (basestring): Optional value returned from CoreNLP\n Returns:\n Value\n "
] |
Please provide a description of the function:def to_value_list(original_strings, corenlp_values=None):
assert isinstance(original_strings, (list, tuple, set))
if corenlp_values is not None:
assert isinstance(corenlp_values, (list, tuple, set))
assert len(original_strings) == len(corenlp_values)
return list(set(to_value(x, y) for (x, y)
in zip(original_strings, corenlp_values)))
else:
return list(set(to_value(x) for x in original_strings)) | [
"Convert a list of strings to a list of Values\n\n Args:\n original_strings (list[basestring])\n corenlp_values (list[basestring or None])\n Returns:\n list[Value]\n "
] |
Please provide a description of the function:def check_denotation(target_values, predicted_values):
# Check size
if len(target_values) != len(predicted_values):
return False
# Check items
for target in target_values:
if not any(target.match(pred) for pred in predicted_values):
return False
return True | [
"Return True if the predicted denotation is correct.\n\n Args:\n target_values (list[Value])\n predicted_values (list[Value])\n Returns:\n bool\n "
] |
Please provide a description of the function:def parse(text):
try:
return int(text)
except ValueError:
try:
amount = float(text)
assert not isnan(amount) and not isinf(amount)
return amount
except (ValueError, AssertionError):
return None | [
"Try to parse into a number.\n\n Return:\n the number (int or float) if successful; otherwise None.\n "
] |
Please provide a description of the function:def parse(text):
try:
ymd = text.lower().split('-')
assert len(ymd) == 3
year = -1 if ymd[0] in ('xx', 'xxxx') else int(ymd[0])
month = -1 if ymd[1] == 'xx' else int(ymd[1])
day = -1 if ymd[2] == 'xx' else int(ymd[2])
assert not year == month == day == -1
assert month == -1 or 1 <= month <= 12
assert day == -1 or 1 <= day <= 31
return (year, month, day)
except (ValueError, AssertionError):
return None | [
"Try to parse into a date.\n\n Return:\n tuple (year, month, date) if successful; otherwise None.\n "
] |
Please provide a description of the function:def forward(self, # pylint: disable=arguments-differ
sequence_tensor: torch.FloatTensor,
span_indices: torch.LongTensor,
sequence_mask: torch.LongTensor = None,
span_indices_mask: torch.LongTensor = None):
raise NotImplementedError | [
"\n Given a sequence tensor, extract spans and return representations of\n them. Span representation can be computed in many different ways,\n such as concatenation of the start and end spans, attention over the\n vectors contained inside the span, etc.\n\n Parameters\n ----------\n sequence_tensor : ``torch.FloatTensor``, required.\n A tensor of shape (batch_size, sequence_length, embedding_size)\n representing an embedded sequence of words.\n span_indices : ``torch.LongTensor``, required.\n A tensor of shape ``(batch_size, num_spans, 2)``, where the last\n dimension represents the inclusive start and end indices of the\n span to be extracted from the ``sequence_tensor``.\n sequence_mask : ``torch.LongTensor``, optional (default = ``None``).\n A tensor of shape (batch_size, sequence_length) representing padded\n elements of the sequence.\n span_indices_mask : ``torch.LongTensor``, optional (default = ``None``).\n A tensor of shape (batch_size, num_spans) representing the valid\n spans in the ``indices`` tensor. This mask is optional because\n sometimes it's easier to worry about masking after calling this\n function, rather than passing a mask directly.\n\n Returns\n -------\n A tensor of shape ``(batch_size, num_spans, embedded_span_size)``,\n where ``embedded_span_size`` depends on the way spans are represented.\n "
] |
Please provide a description of the function:def main(serialization_directory: int,
device: int,
data: str,
prefix: str,
domain: str = None):
config = Params.from_file(os.path.join(serialization_directory, "config.json"))
if domain is not None:
# Hack to allow evaluation on different domains than the
# model was trained on.
config["dataset_reader"]["domain_identifier"] = domain
prefix = f"{domain}_{prefix}"
else:
config["dataset_reader"].pop("domain_identifier", None)
dataset_reader = DatasetReader.from_params(config['dataset_reader'])
evaluation_data_path = data if data else config['validation_data_path']
archive = load_archive(os.path.join(serialization_directory, "model.tar.gz"), cuda_device=device)
model = archive.model
model.eval()
prediction_file_path = os.path.join(serialization_directory, prefix + "_predictions.txt")
gold_file_path = os.path.join(serialization_directory, prefix + "_gold.txt")
prediction_file = open(prediction_file_path, "w+")
gold_file = open(gold_file_path, "w+")
# Load the evaluation data and index it.
print("reading evaluation data from {}".format(evaluation_data_path))
instances = dataset_reader.read(evaluation_data_path)
with torch.autograd.no_grad():
iterator = BasicIterator(batch_size=32)
iterator.index_with(model.vocab)
model_predictions = []
batches = iterator(instances, num_epochs=1, shuffle=False, cuda_device=device)
for batch in Tqdm.tqdm(batches):
result = model(**batch)
predictions = model.decode(result)
model_predictions.extend(predictions["tags"])
for instance, prediction in zip(instances, model_predictions):
fields = instance.fields
try:
# Most sentences have a verbal predicate, but not all.
verb_index = fields["verb_indicator"].labels.index(1)
except ValueError:
verb_index = None
gold_tags = fields["tags"].labels
sentence = [x.text for x in fields["tokens"].tokens]
write_to_conll_eval_file(prediction_file, gold_file,
verb_index, sentence, prediction, gold_tags)
prediction_file.close()
gold_file.close() | [
"\n serialization_directory : str, required.\n The directory containing the serialized weights.\n device: int, default = -1\n The device to run the evaluation on.\n data: str, default = None\n The data to evaluate on. By default, we use the validation data from\n the original experiment.\n prefix: str, default=\"\"\n The prefix to prepend to the generated gold and prediction files, to distinguish\n different models/data.\n domain: str, optional (default = None)\n If passed, filters the ontonotes evaluation/test dataset to only contain the\n specified domain. This overwrites the domain in the config file from the model,\n to allow evaluation on domains other than the one the model was trained on.\n "
] |
Please provide a description of the function:def decode(self,
initial_state: State,
transition_function: TransitionFunction,
supervision: SupervisionType) -> Dict[str, torch.Tensor]:
raise NotImplementedError | [
"\n Takes an initial state object, a means of transitioning from state to state, and a\n supervision signal, and uses the supervision to train the transition function to pick\n \"good\" states.\n\n This function should typically return a ``loss`` key during training, which the ``Model``\n will use as its loss.\n\n Parameters\n ----------\n initial_state : ``State``\n This is the initial state for decoding, typically initialized after running some kind\n of encoder on some inputs.\n transition_function : ``TransitionFunction``\n This is the transition function that scores all possible actions that can be taken in a\n given state, and returns a ranked list of next states at each step of decoding.\n supervision : ``SupervisionType``\n This is the supervision that is used to train the ``transition_function`` function to\n pick \"good\" states. You can use whatever kind of supervision you want (e.g., a single\n \"gold\" action sequence, a set of possible \"gold\" action sequences, a reward function,\n etc.). We use ``typing.Generics`` to make sure that our static type checker is happy\n with how you've matched the supervision that you provide in the model to the\n ``DecoderTrainer`` that you want to use.\n "
] |
Please provide a description of the function:def state_dict(self) -> Dict[str, Any]:
return {key: value for key, value in self.__dict__.items() if key != 'optimizer'} | [
"\n Returns the state of the scheduler as a ``dict``.\n "
] |
Please provide a description of the function:def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
self.__dict__.update(state_dict) | [
"\n Load the schedulers state.\n\n Parameters\n ----------\n state_dict : ``Dict[str, Any]``\n Scheduler state. Should be an object returned from a call to ``state_dict``.\n "
] |
Please provide a description of the function:def forward(self, # pylint: disable=arguments-differ
text_field_input: Dict[str, torch.Tensor],
num_wrapping_dims: int = 0) -> torch.Tensor:
raise NotImplementedError | [
"\n Parameters\n ----------\n text_field_input : ``Dict[str, torch.Tensor]``\n A dictionary that was the output of a call to ``TextField.as_tensor``. Each tensor in\n here is assumed to have a shape roughly similar to ``(batch_size, sequence_length)``\n (perhaps with an extra trailing dimension for the characters in each token).\n num_wrapping_dims : ``int``, optional (default=0)\n If you have a ``ListField[TextField]`` that created the ``text_field_input``, you'll\n end up with tensors of shape ``(batch_size, wrapping_dim1, wrapping_dim2, ...,\n sequence_length)``. This parameter tells us how many wrapping dimensions there are, so\n that we can correctly ``TimeDistribute`` the embedding of each named representation.\n "
] |
Please provide a description of the function:def ensemble(subresults: List[Dict[str, torch.Tensor]]) -> torch.Tensor:
# Choose the highest average confidence span.
span_start_probs = sum(subresult['span_start_probs'] for subresult in subresults) / len(subresults)
span_end_probs = sum(subresult['span_end_probs'] for subresult in subresults) / len(subresults)
return get_best_span(span_start_probs.log(), span_end_probs.log()) | [
"\n Identifies the best prediction given the results from the submodels.\n\n Parameters\n ----------\n subresults : List[Dict[str, torch.Tensor]]\n Results of each submodel.\n\n Returns\n -------\n The index of the best submodel.\n "
] |
Please provide a description of the function:def forward(self, # pylint: disable=arguments-differ
inputs: torch.Tensor,
mask: torch.LongTensor) -> torch.Tensor:
batch_size, total_sequence_length = mask.size()
stacked_sequence_output, final_states, restoration_indices = \
self.sort_and_run_forward(self._lstm_forward, inputs, mask)
num_layers, num_valid, returned_timesteps, encoder_dim = stacked_sequence_output.size()
# Add back invalid rows which were removed in the call to sort_and_run_forward.
if num_valid < batch_size:
zeros = stacked_sequence_output.new_zeros(num_layers,
batch_size - num_valid,
returned_timesteps,
encoder_dim)
stacked_sequence_output = torch.cat([stacked_sequence_output, zeros], 1)
# The states also need to have invalid rows added back.
new_states = []
for state in final_states:
state_dim = state.size(-1)
zeros = state.new_zeros(num_layers, batch_size - num_valid, state_dim)
new_states.append(torch.cat([state, zeros], 1))
final_states = new_states
# It's possible to need to pass sequences which are padded to longer than the
# max length of the sequence to a Seq2StackEncoder. However, packing and unpacking
# the sequences mean that the returned tensor won't include these dimensions, because
# the RNN did not need to process them. We add them back on in the form of zeros here.
sequence_length_difference = total_sequence_length - returned_timesteps
if sequence_length_difference > 0:
zeros = stacked_sequence_output.new_zeros(num_layers,
batch_size,
sequence_length_difference,
stacked_sequence_output[0].size(-1))
stacked_sequence_output = torch.cat([stacked_sequence_output, zeros], 2)
self._update_states(final_states, restoration_indices)
# Restore the original indices and return the sequence.
# Has shape (num_layers, batch_size, sequence_length, hidden_size)
return stacked_sequence_output.index_select(1, restoration_indices) | [
"\n Parameters\n ----------\n inputs : ``torch.Tensor``, required.\n A Tensor of shape ``(batch_size, sequence_length, hidden_size)``.\n mask : ``torch.LongTensor``, required.\n A binary mask of shape ``(batch_size, sequence_length)`` representing the\n non-padded elements in each sequence in the batch.\n\n Returns\n -------\n A ``torch.Tensor`` of shape (num_layers, batch_size, sequence_length, hidden_size),\n where the num_layers dimension represents the LSTM output from that layer.\n "
] |
Please provide a description of the function:def _lstm_forward(self,
inputs: PackedSequence,
initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None) -> \
Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
if initial_state is None:
hidden_states: List[Optional[Tuple[torch.Tensor,
torch.Tensor]]] = [None] * len(self.forward_layers)
elif initial_state[0].size()[0] != len(self.forward_layers):
raise ConfigurationError("Initial states were passed to forward() but the number of "
"initial states does not match the number of layers.")
else:
hidden_states = list(zip(initial_state[0].split(1, 0), initial_state[1].split(1, 0)))
inputs, batch_lengths = pad_packed_sequence(inputs, batch_first=True)
forward_output_sequence = inputs
backward_output_sequence = inputs
final_states = []
sequence_outputs = []
for layer_index, state in enumerate(hidden_states):
forward_layer = getattr(self, 'forward_layer_{}'.format(layer_index))
backward_layer = getattr(self, 'backward_layer_{}'.format(layer_index))
forward_cache = forward_output_sequence
backward_cache = backward_output_sequence
if state is not None:
forward_hidden_state, backward_hidden_state = state[0].split(self.hidden_size, 2)
forward_memory_state, backward_memory_state = state[1].split(self.cell_size, 2)
forward_state = (forward_hidden_state, forward_memory_state)
backward_state = (backward_hidden_state, backward_memory_state)
else:
forward_state = None
backward_state = None
forward_output_sequence, forward_state = forward_layer(forward_output_sequence,
batch_lengths,
forward_state)
backward_output_sequence, backward_state = backward_layer(backward_output_sequence,
batch_lengths,
backward_state)
# Skip connections, just adding the input to the output.
if layer_index != 0:
forward_output_sequence += forward_cache
backward_output_sequence += backward_cache
sequence_outputs.append(torch.cat([forward_output_sequence,
backward_output_sequence], -1))
# Append the state tuples in a list, so that we can return
# the final states for all the layers.
final_states.append((torch.cat([forward_state[0], backward_state[0]], -1),
torch.cat([forward_state[1], backward_state[1]], -1)))
stacked_sequence_outputs: torch.FloatTensor = torch.stack(sequence_outputs)
# Stack the hidden state and memory for each layer into 2 tensors of shape
# (num_layers, batch_size, hidden_size) and (num_layers, batch_size, cell_size)
# respectively.
final_hidden_states, final_memory_states = zip(*final_states)
final_state_tuple: Tuple[torch.FloatTensor,
torch.FloatTensor] = (torch.cat(final_hidden_states, 0),
torch.cat(final_memory_states, 0))
return stacked_sequence_outputs, final_state_tuple | [
"\n Parameters\n ----------\n inputs : ``PackedSequence``, required.\n A batch first ``PackedSequence`` to run the stacked LSTM over.\n initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None)\n A tuple (state, memory) representing the initial hidden state and memory\n of the LSTM, with shape (num_layers, batch_size, 2 * hidden_size) and\n (num_layers, batch_size, 2 * cell_size) respectively.\n\n Returns\n -------\n output_sequence : ``torch.FloatTensor``\n The encoded sequence of shape (num_layers, batch_size, sequence_length, hidden_size)\n final_states: ``Tuple[torch.FloatTensor, torch.FloatTensor]``\n The per-layer final (state, memory) states of the LSTM, with shape\n (num_layers, batch_size, 2 * hidden_size) and (num_layers, batch_size, 2 * cell_size)\n respectively. The last dimension is duplicated because it contains the state/memory\n for both the forward and backward layers.\n "
] |
Please provide a description of the function:def load_weights(self, weight_file: str) -> None:
requires_grad = self.requires_grad
with h5py.File(cached_path(weight_file), 'r') as fin:
for i_layer, lstms in enumerate(
zip(self.forward_layers, self.backward_layers)
):
for j_direction, lstm in enumerate(lstms):
# lstm is an instance of LSTMCellWithProjection
cell_size = lstm.cell_size
dataset = fin['RNN_%s' % j_direction]['RNN']['MultiRNNCell']['Cell%s' % i_layer
]['LSTMCell']
# tensorflow packs together both W and U matrices into one matrix,
# but pytorch maintains individual matrices. In addition, tensorflow
# packs the gates as input, memory, forget, output but pytorch
# uses input, forget, memory, output. So we need to modify the weights.
tf_weights = numpy.transpose(dataset['W_0'][...])
torch_weights = tf_weights.copy()
# split the W from U matrices
input_size = lstm.input_size
input_weights = torch_weights[:, :input_size]
recurrent_weights = torch_weights[:, input_size:]
tf_input_weights = tf_weights[:, :input_size]
tf_recurrent_weights = tf_weights[:, input_size:]
# handle the different gate order convention
for torch_w, tf_w in [[input_weights, tf_input_weights],
[recurrent_weights, tf_recurrent_weights]]:
torch_w[(1 * cell_size):(2 * cell_size), :] = tf_w[(2 * cell_size):(3 * cell_size), :]
torch_w[(2 * cell_size):(3 * cell_size), :] = tf_w[(1 * cell_size):(2 * cell_size), :]
lstm.input_linearity.weight.data.copy_(torch.FloatTensor(input_weights))
lstm.state_linearity.weight.data.copy_(torch.FloatTensor(recurrent_weights))
lstm.input_linearity.weight.requires_grad = requires_grad
lstm.state_linearity.weight.requires_grad = requires_grad
# the bias weights
tf_bias = dataset['B'][...]
# tensorflow adds 1.0 to forget gate bias instead of modifying the
# parameters...
tf_bias[(2 * cell_size):(3 * cell_size)] += 1
torch_bias = tf_bias.copy()
torch_bias[(1 * cell_size):(2 * cell_size)
] = tf_bias[(2 * cell_size):(3 * cell_size)]
torch_bias[(2 * cell_size):(3 * cell_size)
] = tf_bias[(1 * cell_size):(2 * cell_size)]
lstm.state_linearity.bias.data.copy_(torch.FloatTensor(torch_bias))
lstm.state_linearity.bias.requires_grad = requires_grad
# the projection weights
proj_weights = numpy.transpose(dataset['W_P_0'][...])
lstm.state_projection.weight.data.copy_(torch.FloatTensor(proj_weights))
lstm.state_projection.weight.requires_grad = requires_grad | [
"\n Load the pre-trained weights from the file.\n "
] |
Please provide a description of the function:def forward(self, # pylint: disable=arguments-differ
inputs: PackedSequence,
initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None) -> \
Tuple[Union[torch.Tensor, PackedSequence], Tuple[torch.Tensor, torch.Tensor]]:
if not initial_state:
hidden_states = [None] * len(self.lstm_layers)
elif initial_state[0].size()[0] != len(self.lstm_layers):
raise ConfigurationError("Initial states were passed to forward() but the number of "
"initial states does not match the number of layers.")
else:
hidden_states = list(zip(initial_state[0].split(1, 0),
initial_state[1].split(1, 0)))
output_sequence = inputs
final_states = []
for i, state in enumerate(hidden_states):
layer = getattr(self, 'layer_{}'.format(i))
# The state is duplicated to mirror the Pytorch API for LSTMs.
output_sequence, final_state = layer(output_sequence, state)
final_states.append(final_state)
final_hidden_state, final_cell_state = tuple(torch.cat(state_list, 0) for state_list in zip(*final_states))
return output_sequence, (final_hidden_state, final_cell_state) | [
"\n Parameters\n ----------\n inputs : ``PackedSequence``, required.\n A batch first ``PackedSequence`` to run the stacked LSTM over.\n initial_state : Tuple[torch.Tensor, torch.Tensor], optional, (default = None)\n A tuple (state, memory) representing the initial hidden state and memory\n of the LSTM. Each tensor has shape (1, batch_size, output_dimension).\n\n Returns\n -------\n output_sequence : PackedSequence\n The encoded sequence of shape (batch_size, sequence_length, hidden_size)\n final_states: Tuple[torch.Tensor, torch.Tensor]\n The per-layer final (state, memory) states of the LSTM, each with shape\n (num_layers, batch_size, hidden_size).\n "
] |
Please provide a description of the function:def substitute_any_type(type_: Type, basic_types: Set[BasicType]) -> List[Type]:
if type_ == ANY_TYPE:
return list(basic_types)
if isinstance(type_, BasicType):
return [type_]
# If we've made it this far, we have a ComplexType, and we can just call
# `type_.substitute_any_type()`.
return type_.substitute_any_type(basic_types) | [
"\n Takes a type and a set of basic types, and substitutes all instances of ANY_TYPE with all\n possible basic types and returns a list with all possible combinations. Note that this\n substitution is unconstrained. That is, If you have a type with placeholders, <#1,#1> for\n example, this may substitute the placeholders with different basic types. In that case, you'd\n want to use ``_substitute_placeholder_type`` instead.\n "
] |
Please provide a description of the function:def _get_complex_type_production(complex_type: ComplexType,
multi_match_mapping: Dict[Type, List[Type]]) -> List[Tuple[Type, str]]:
return_type = complex_type.return_type()
if isinstance(return_type, MultiMatchNamedBasicType):
return_types_matched = list(multi_match_mapping[return_type] if return_type in
multi_match_mapping else return_type.types_to_match)
else:
return_types_matched = [return_type]
arguments = complex_type.argument_types()
argument_types_matched = []
for argument_type in arguments:
if isinstance(argument_type, MultiMatchNamedBasicType):
matched_types = list(multi_match_mapping[argument_type] if argument_type in
multi_match_mapping else argument_type.types_to_match)
argument_types_matched.append(matched_types)
else:
argument_types_matched.append([argument_type])
complex_type_productions: List[Tuple[Type, str]] = []
for matched_return_type in return_types_matched:
for matched_arguments in itertools.product(*argument_types_matched):
complex_type_productions.append((matched_return_type,
_make_production_string(return_type,
[complex_type] + list(matched_arguments))))
return complex_type_productions | [
"\n Takes a complex type (without any placeholders), gets its return values, and returns productions\n (perhaps each with multiple arguments) that produce the return values. This method also takes\n care of ``MultiMatchNamedBasicTypes``. If one of the arguments or the return types is a multi\n match type, it gets all the substitutions of those types from ``multi_match_mapping`` and forms\n a list with all possible combinations of substitutions. If the complex type passed to this method\n has no ``MultiMatchNamedBasicTypes``, the returned list will contain a single tuple. For\n example, if the complex is type ``<a,<<b,c>,d>>``, and ``a`` is a multi match type that matches\n ``e`` and ``f``, this gives the following list of tuples: ``[('d', 'd -> [<a,<<b,c>,d>, e,\n <b,c>]), ('d', 'd -> [<a,<<b,c>,d>, f, <b,c>])]`` Note that we assume there will be no\n productions from the multi match type, and the list above does not contain ``('d', 'd ->\n [<a,<<b,c>,d>, a, <b,c>>]')``.\n "
] |
Please provide a description of the function:def get_valid_actions(name_mapping: Dict[str, str],
type_signatures: Dict[str, Type],
basic_types: Set[Type],
multi_match_mapping: Dict[Type, List[Type]] = None,
valid_starting_types: Set[Type] = None,
num_nested_lambdas: int = 0) -> Dict[str, List[str]]:
valid_actions: Dict[str, Set[str]] = defaultdict(set)
valid_starting_types = valid_starting_types or basic_types
for type_ in valid_starting_types:
valid_actions[str(START_TYPE)].add(_make_production_string(START_TYPE, type_))
complex_types = set()
for name, alias in name_mapping.items():
# Lambda functions and variables associated with them get produced in specific contexts. So
# we do not add them to ``valid_actions`` here, and let ``GrammarState`` deal with it.
# ``var`` is a special function that some languages (like LambdaDCS) use within lambda
# functions to indicate the use of a variable (eg.: ``(lambda x (fb:row.row.year (var x)))``)
# We do not have to produce this function outside the scope of lambda. Even within lambdas,
# it is a lot easier to not do it, and let the action sequence to logical form transformation
# logic add it to the output logical forms instead.
if name in ["lambda", "var", "x", "y", "z"]:
continue
name_type = type_signatures[alias]
# Type to terminal productions.
for substituted_type in substitute_any_type(name_type, basic_types):
valid_actions[str(substituted_type)].add(_make_production_string(substituted_type, name))
# Keeping track of complex types.
if isinstance(name_type, ComplexType) and name_type != ANY_TYPE:
complex_types.add(name_type)
for complex_type in complex_types:
for substituted_type in substitute_any_type(complex_type, basic_types):
for head, production in _get_complex_type_production(substituted_type,
multi_match_mapping or {}):
valid_actions[str(head)].add(production)
# We can produce complex types with a lambda expression, though we'll leave out
# placeholder types for now.
for i in range(num_nested_lambdas):
lambda_var = chr(ord('x') + i)
# We'll only allow lambdas to be functions that take and return basic types as their
# arguments, for now. Also, we're doing this for all possible complex types where
# the first and second types are basic types. So we may be overgenerating a bit.
for first_type in basic_types:
for second_type in basic_types:
key = ComplexType(first_type, second_type)
production_string = _make_production_string(key, ['lambda ' + lambda_var, second_type])
valid_actions[str(key)].add(production_string)
valid_action_strings = {key: sorted(value) for key, value in valid_actions.items()}
return valid_action_strings | [
"\n Generates all the valid actions starting from each non-terminal. For terminals of a specific\n type, we simply add a production from the type to the terminal. For all terminal `functions`,\n we additionally add a rule that allows their return type to be generated from an application of\n the function. For example, the function ``<e,<r,<d,r>>>``, which takes three arguments and\n returns an ``r`` would generate a the production rule ``r -> [<e,<r,<d,r>>>, e, r, d]``.\n\n For functions that do not contain ANY_TYPE or placeholder types, this is straight-forward.\n When there are ANY_TYPES or placeholders, we substitute the ANY_TYPE with all possible basic\n types, and then produce a similar rule. For example, the identity function, with type\n ``<#1,#1>`` and basic types ``e`` and ``r``, would produce the rules ``e -> [<#1,#1>, e]`` and\n ``r -> [<#1,#1>, r]``.\n\n We additionally add a valid action from the start symbol to all ``valid_starting_types``.\n\n Parameters\n ----------\n name_mapping : ``Dict[str, str]``\n The mapping of names that appear in your logical form languages to their aliases for NLTK.\n If you are getting all valid actions for a type declaration, this can be the\n ``COMMON_NAME_MAPPING``.\n type_signatures : ``Dict[str, Type]``\n The mapping from name aliases to their types. If you are getting all valid actions for a\n type declaration, this can be the ``COMMON_TYPE_SIGNATURE``.\n basic_types : ``Set[Type]``\n Set of all basic types in the type declaration.\n multi_match_mapping : ``Dict[Type, List[Type]]`` (optional)\n A mapping from `MultiMatchNamedBasicTypes` to the types they can match. This may be\n different from the type's ``types_to_match`` field based on the context. While building action\n sequences that lead to complex types with ``MultiMatchNamedBasicTypes``, if a type does not\n occur in this mapping, the default set of ``types_to_match`` for that type will be used.\n valid_starting_types : ``Set[Type]``, optional\n These are the valid starting types for your grammar; e.g., what types are we allowed to\n parse expressions into? We will add a \"START -> TYPE\" rule for each of these types. If\n this is ``None``, we default to using ``basic_types``.\n num_nested_lambdas : ``int`` (optional)\n Does the language used permit lambda expressions? And if so, how many nested lambdas do we\n need to worry about? We'll add rules like \"<r,d> -> ['lambda x', d]\" for all complex\n types, where the variable is determined by the number of nestings. We currently only\n permit up to three levels of nesting, just for ease of implementation.\n "
] |
Please provide a description of the function:def return_type(self) -> Type:
return_type = self.second
while isinstance(return_type, ComplexType):
return_type = return_type.second
return return_type | [
"\n Gives the final return type for this function. If the function takes a single argument,\n this is just ``self.second``. If the function takes multiple arguments and returns a basic\n type, this should be the final ``.second`` after following all complex types. That is the\n implementation here in the base class. If you have a higher-order function that returns a\n function itself, you need to override this method.\n "
] |
Please provide a description of the function:def argument_types(self) -> List[Type]:
arguments = [self.first]
remaining_type = self.second
while isinstance(remaining_type, ComplexType):
arguments.append(remaining_type.first)
remaining_type = remaining_type.second
return arguments | [
"\n Gives the types of all arguments to this function. For functions returning a basic type,\n we grab all ``.first`` types until ``.second`` is no longer a ``ComplexType``. That logic\n is implemented here in the base class. If you have a higher-order function that returns a\n function itself, you need to override this method.\n "
] |
Please provide a description of the function:def substitute_any_type(self, basic_types: Set[BasicType]) -> List[Type]:
substitutions = []
for first_type in substitute_any_type(self.first, basic_types):
for second_type in substitute_any_type(self.second, basic_types):
substitutions.append(self.__class__(first_type, second_type))
return substitutions | [
"\n Takes a set of ``BasicTypes`` and replaces any instances of ``ANY_TYPE`` inside this\n complex type with each of those basic types.\n "
] |
Please provide a description of the function:def resolve(self, other) -> Optional[Type]:
if not isinstance(other, NltkComplexType):
return None
other_first = other.first.resolve(other.second)
if not other_first:
return None
other_second = other.second.resolve(other_first)
if not other_second:
return None
return UnaryOpType(other_first, self._allowed_substitutions, self._signature) | [
"See ``PlaceholderType.resolve``"
] |
Please provide a description of the function:def resolve(self, other: Type) -> Optional[Type]:
if not isinstance(other, NltkComplexType):
return None
if not isinstance(other.second, NltkComplexType):
return None
other_first = other.first.resolve(other.second.first)
if other_first is None:
return None
other_first = other_first.resolve(other.second.second)
if not other_first:
return None
other_second = other.second.resolve(ComplexType(other_first, other_first))
if not other_second:
return None
return BinaryOpType(other_first, self._allowed_substitutions, self._signature) | [
"See ``PlaceholderType.resolve``"
] |
Please provide a description of the function:def _set_type(self, other_type: Type = ANY_TYPE, signature=None) -> None:
super(DynamicTypeApplicationExpression, self)._set_type(other_type, signature)
# TODO(pradeep): Assuming the mapping of "var" function is "V". Do something better.
if isinstance(self.argument, ApplicationExpression) and str(self.argument.function) == "V":
# pylint: disable=protected-access
self.argument.argument._set_type(self.function.type.first)
if str(self.argument) == "X" and str(self.function) != "V":
# pylint: disable=protected-access
self.argument._set_type(self.function.type.first) | [
"\n We override this method to do just one thing on top of ``ApplicationExpression._set_type``.\n In lambda expressions of the form /x F(x), where the function is F and the argument is x,\n we can use the type of F to infer the type of x. That is, if F is of type <a, b>, we can\n resolve the type of x against a. We do this as the additional step after setting the type\n of F(x).\n\n So why does NLTK not already do this? NLTK assumes all variables (x) are of type entity\n (e). So it does not have to resolve the type of x anymore. However, this would cause type\n inference failures in our case since x can bind to rows, numbers or cells, each of which\n has a different type. To deal with this issue, we made X of type ANY_TYPE. Also, LambdaDCS\n (and some other languages) contain a var function that indicate the usage of variables\n within lambda functions. We map var to V, and made it of type <#1, #1>. We cannot leave X\n as ANY_TYPE because that would propagate up the tree. We need to set its type when we have\n the information about F. Hence this method. Note that the language may or may not contain\n the var function. We deal with both cases below.\n "
] |
Please provide a description of the function:def log_parameter_and_gradient_statistics(self, # pylint: disable=invalid-name
model: Model,
batch_grad_norm: float) -> None:
if self._should_log_parameter_statistics:
# Log parameter values to Tensorboard
for name, param in model.named_parameters():
self.add_train_scalar("parameter_mean/" + name, param.data.mean())
self.add_train_scalar("parameter_std/" + name, param.data.std())
if param.grad is not None:
if param.grad.is_sparse:
# pylint: disable=protected-access
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: # pylint: disable=not-callable
self.add_train_scalar("gradient_mean/" + name, grad_data.mean())
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) | [
"\n Send the mean and std of all parameters and gradients to tensorboard, as well\n as logging the average gradient norm.\n "
] |
Please provide a description of the function:def log_learning_rates(self,
model: Model,
optimizer: torch.optim.Optimizer):
if self._should_log_learning_rate:
# optimizer stores lr info keyed by parameter tensor
# we want to log with parameter name
names = {param: name for name, param in model.named_parameters()}
for group in optimizer.param_groups:
if 'lr' not in group:
continue
rate = group['lr']
for param in group['params']:
# check whether params has requires grad or not
effective_rate = rate * float(param.requires_grad)
self.add_train_scalar("learning_rate/" + names[param], effective_rate) | [
"\n Send current parameter specific learning rates to tensorboard\n "
] |
Please provide a description of the function:def log_histograms(self, model: Model, histogram_parameters: Set[str]) -> None:
for name, param in model.named_parameters():
if name in histogram_parameters:
self.add_train_histogram("parameter_histogram/" + name, param) | [
"\n Send histograms of parameters to tensorboard.\n "
] |
Please provide a description of the function:def log_metrics(self,
train_metrics: dict,
val_metrics: dict = None,
epoch: int = None,
log_to_console: bool = False) -> None:
metric_names = set(train_metrics.keys())
if val_metrics is not None:
metric_names.update(val_metrics.keys())
val_metrics = val_metrics or {}
# For logging to the console
if log_to_console:
dual_message_template = "%s | %8.3f | %8.3f"
no_val_message_template = "%s | %8.3f | %8s"
no_train_message_template = "%s | %8s | %8.3f"
header_template = "%s | %-10s"
name_length = max([len(x) for x in metric_names])
logger.info(header_template, "Training".rjust(name_length + 13), "Validation")
for name in metric_names:
# Log to tensorboard
train_metric = train_metrics.get(name)
if train_metric is not None:
self.add_train_scalar(name, train_metric, timestep=epoch)
val_metric = val_metrics.get(name)
if val_metric is not None:
self.add_validation_scalar(name, val_metric, timestep=epoch)
# And maybe log to console
if log_to_console and val_metric is not None and train_metric is not None:
logger.info(dual_message_template, name.ljust(name_length), train_metric, val_metric)
elif log_to_console and val_metric is not None:
logger.info(no_train_message_template, name.ljust(name_length), "N/A", val_metric)
elif log_to_console and train_metric is not None:
logger.info(no_val_message_template, name.ljust(name_length), train_metric, "N/A") | [
"\n Sends all of the train metrics (and validation metrics, if provided) to tensorboard.\n "
] |
Please provide a description of the function:def get_explanation(logical_form: str,
world_extractions: JsonDict,
answer_index: int,
world: QuarelWorld) -> List[JsonDict]:
output = []
nl_world = {}
if world_extractions['world1'] != "N/A" and world_extractions['world1'] != ["N/A"]:
nl_world['world1'] = nl_world_string(world_extractions['world1'])
nl_world['world2'] = nl_world_string(world_extractions['world2'])
output.append({
"header": "Identified two worlds",
"content": [f'''world1 = {nl_world['world1']}''',
f'''world2 = {nl_world['world2']}''']
})
else:
nl_world['world1'] = 'world1'
nl_world['world2'] = 'world2'
parse = semparse_util.lisp_to_nested_expression(logical_form)
if parse[0] != "infer":
return None
setup = parse[1]
output.append({
"header": "The question is stating",
"content": nl_arg(setup, nl_world)
})
answers = parse[2:]
output.append({
"header": "The answer options are stating",
"content": ["A: " + " and ".join(nl_arg(answers[0], nl_world)),
"B: " + " and ".join(nl_arg(answers[1], nl_world))]
})
setup_core = setup
if setup[0] == 'and':
setup_core = setup[1]
s_attr = setup_core[0]
s_dir = world.qr_size[setup_core[1]]
s_world = nl_world[setup_core[2]]
a_attr = answers[answer_index][0]
qr_dir = world._get_qr_coeff(strip_entity_type(s_attr), strip_entity_type(a_attr)) # pylint: disable=protected-access
a_dir = s_dir * qr_dir
a_world = nl_world[answers[answer_index][2]]
content = [f'When {nl_attr(s_attr)} is {nl_dir(s_dir)} ' +
f'then {nl_attr(a_attr)} is {nl_dir(a_dir)} (for {s_world})']
if a_world != s_world:
content.append(f'''Therefore {nl_attr(a_attr)} is {nl_dir(-a_dir)} for {a_world}''')
content.append(f"Therefore {chr(65+answer_index)} is the correct answer")
output.append({
"header": "Theory used",
"content": content
})
return output | [
"\n Create explanation (as a list of header/content entries) for an answer\n "
] |
Please provide a description of the function:def align_entities(extracted: List[str],
literals: JsonDict,
stemmer: NltkPorterStemmer) -> List[str]:
literal_keys = list(literals.keys())
literal_values = list(literals.values())
overlaps = [get_stem_overlaps(extract, literal_values, stemmer) for extract in extracted]
worlds = []
for overlap in overlaps:
if overlap[0] > overlap[1]:
worlds.append(literal_keys[0])
elif overlap[0] < overlap[1]:
worlds.append(literal_keys[1])
else:
worlds.append(None)
return worlds | [
"\n Use stemming to attempt alignment between extracted world and given world literals.\n If more words align to one world vs the other, it's considered aligned.\n "
] |
Please provide a description of the function:def multi_perspective_match(vector1: torch.Tensor,
vector2: torch.Tensor,
weight: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
assert vector1.size(0) == vector2.size(0)
assert weight.size(1) == vector1.size(2) == vector1.size(2)
# (batch, seq_len, 1)
similarity_single = F.cosine_similarity(vector1, vector2, 2).unsqueeze(2)
# (1, 1, num_perspectives, hidden_size)
weight = weight.unsqueeze(0).unsqueeze(0)
# (batch, seq_len, num_perspectives, hidden_size)
vector1 = weight * vector1.unsqueeze(2)
vector2 = weight * vector2.unsqueeze(2)
similarity_multi = F.cosine_similarity(vector1, vector2, dim=3)
return similarity_single, similarity_multi | [
"\n Calculate multi-perspective cosine matching between time-steps of vectors\n of the same length.\n\n Parameters\n ----------\n vector1 : ``torch.Tensor``\n A tensor of shape ``(batch, seq_len, hidden_size)``\n vector2 : ``torch.Tensor``\n A tensor of shape ``(batch, seq_len or 1, hidden_size)``\n weight : ``torch.Tensor``\n A tensor of shape ``(num_perspectives, hidden_size)``\n\n Returns\n -------\n A tuple of two tensors consisting multi-perspective matching results.\n The first one is of the shape (batch, seq_len, 1), the second one is of shape\n (batch, seq_len, num_perspectives)\n "
] |
Please provide a description of the function:def multi_perspective_match_pairwise(vector1: torch.Tensor,
vector2: torch.Tensor,
weight: torch.Tensor,
eps: float = 1e-8) -> torch.Tensor:
num_perspectives = weight.size(0)
# (1, num_perspectives, 1, hidden_size)
weight = weight.unsqueeze(0).unsqueeze(2)
# (batch, num_perspectives, seq_len*, hidden_size)
vector1 = weight * vector1.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
vector2 = weight * vector2.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
# (batch, num_perspectives, seq_len*, 1)
vector1_norm = vector1.norm(p=2, dim=3, keepdim=True)
vector2_norm = vector2.norm(p=2, dim=3, keepdim=True)
# (batch, num_perspectives, seq_len1, seq_len2)
mul_result = torch.matmul(vector1, vector2.transpose(2, 3))
norm_value = vector1_norm * vector2_norm.transpose(2, 3)
# (batch, seq_len1, seq_len2, num_perspectives)
return (mul_result / norm_value.clamp(min=eps)).permute(0, 2, 3, 1) | [
"\n Calculate multi-perspective cosine matching between each time step of\n one vector and each time step of another vector.\n\n Parameters\n ----------\n vector1 : ``torch.Tensor``\n A tensor of shape ``(batch, seq_len1, hidden_size)``\n vector2 : ``torch.Tensor``\n A tensor of shape ``(batch, seq_len2, hidden_size)``\n weight : ``torch.Tensor``\n A tensor of shape ``(num_perspectives, hidden_size)``\n eps : ``float`` optional, (default = 1e-8)\n A small value to avoid zero division problem\n\n Returns\n -------\n A tensor of shape (batch, seq_len1, seq_len2, num_perspectives) consisting\n multi-perspective matching results\n "
] |
Please provide a description of the function:def forward(self,
context_1: torch.Tensor,
mask_1: torch.Tensor,
context_2: torch.Tensor,
mask_2: torch.Tensor) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:
# pylint: disable=arguments-differ
assert (not mask_2.requires_grad) and (not mask_1.requires_grad)
assert context_1.size(-1) == context_2.size(-1) == self.hidden_dim
# (batch,)
len_1 = get_lengths_from_binary_sequence_mask(mask_1)
len_2 = get_lengths_from_binary_sequence_mask(mask_2)
# (batch, seq_len*)
mask_1, mask_2 = mask_1.float(), mask_2.float()
# explicitly set masked weights to zero
# (batch_size, seq_len*, hidden_dim)
context_1 = context_1 * mask_1.unsqueeze(-1)
context_2 = context_2 * mask_2.unsqueeze(-1)
# array to keep the matching vectors for the two sentences
matching_vector_1: List[torch.Tensor] = []
matching_vector_2: List[torch.Tensor] = []
# Step 0. unweighted cosine
# First calculate the cosine similarities between each forward
# (or backward) contextual embedding and every forward (or backward)
# contextual embedding of the other sentence.
# (batch, seq_len1, seq_len2)
cosine_sim = F.cosine_similarity(context_1.unsqueeze(-2), context_2.unsqueeze(-3), dim=3)
# (batch, seq_len*, 1)
cosine_max_1 = masked_max(cosine_sim, mask_2.unsqueeze(-2), dim=2, keepdim=True)
cosine_mean_1 = masked_mean(cosine_sim, mask_2.unsqueeze(-2), dim=2, keepdim=True)
cosine_max_2 = masked_max(cosine_sim.permute(0, 2, 1), mask_1.unsqueeze(-2), dim=2, keepdim=True)
cosine_mean_2 = masked_mean(cosine_sim.permute(0, 2, 1), mask_1.unsqueeze(-2), dim=2, keepdim=True)
matching_vector_1.extend([cosine_max_1, cosine_mean_1])
matching_vector_2.extend([cosine_max_2, cosine_mean_2])
# Step 1. Full-Matching
# Each time step of forward (or backward) contextual embedding of one sentence
# is compared with the last time step of the forward (or backward)
# contextual embedding of the other sentence
if self.with_full_match:
# (batch, 1, hidden_dim)
if self.is_forward:
# (batch, 1, hidden_dim)
last_position_1 = (len_1 - 1).clamp(min=0)
last_position_1 = last_position_1.view(-1, 1, 1).expand(-1, 1, self.hidden_dim)
last_position_2 = (len_2 - 1).clamp(min=0)
last_position_2 = last_position_2.view(-1, 1, 1).expand(-1, 1, self.hidden_dim)
context_1_last = context_1.gather(1, last_position_1)
context_2_last = context_2.gather(1, last_position_2)
else:
context_1_last = context_1[:, 0:1, :]
context_2_last = context_2[:, 0:1, :]
# (batch, seq_len*, num_perspectives)
matching_vector_1_full = multi_perspective_match(context_1,
context_2_last,
self.full_match_weights)
matching_vector_2_full = multi_perspective_match(context_2,
context_1_last,
self.full_match_weights_reversed)
matching_vector_1.extend(matching_vector_1_full)
matching_vector_2.extend(matching_vector_2_full)
# Step 2. Maxpooling-Matching
# Each time step of forward (or backward) contextual embedding of one sentence
# is compared with every time step of the forward (or backward)
# contextual embedding of the other sentence, and only the max value of each
# dimension is retained.
if self.with_maxpool_match:
# (batch, seq_len1, seq_len2, num_perspectives)
matching_vector_max = multi_perspective_match_pairwise(context_1,
context_2,
self.maxpool_match_weights)
# (batch, seq_len*, num_perspectives)
matching_vector_1_max = masked_max(matching_vector_max,
mask_2.unsqueeze(-2).unsqueeze(-1),
dim=2)
matching_vector_1_mean = masked_mean(matching_vector_max,
mask_2.unsqueeze(-2).unsqueeze(-1),
dim=2)
matching_vector_2_max = masked_max(matching_vector_max.permute(0, 2, 1, 3),
mask_1.unsqueeze(-2).unsqueeze(-1),
dim=2)
matching_vector_2_mean = masked_mean(matching_vector_max.permute(0, 2, 1, 3),
mask_1.unsqueeze(-2).unsqueeze(-1),
dim=2)
matching_vector_1.extend([matching_vector_1_max, matching_vector_1_mean])
matching_vector_2.extend([matching_vector_2_max, matching_vector_2_mean])
# Step 3. Attentive-Matching
# Each forward (or backward) similarity is taken as the weight
# of the forward (or backward) contextual embedding, and calculate an
# attentive vector for the sentence by weighted summing all its
# contextual embeddings.
# Finally match each forward (or backward) contextual embedding
# with its corresponding attentive vector.
# (batch, seq_len1, seq_len2, hidden_dim)
att_2 = context_2.unsqueeze(-3) * cosine_sim.unsqueeze(-1)
# (batch, seq_len1, seq_len2, hidden_dim)
att_1 = context_1.unsqueeze(-2) * cosine_sim.unsqueeze(-1)
if self.with_attentive_match:
# (batch, seq_len*, hidden_dim)
att_mean_2 = masked_softmax(att_2.sum(dim=2), mask_1.unsqueeze(-1))
att_mean_1 = masked_softmax(att_1.sum(dim=1), mask_2.unsqueeze(-1))
# (batch, seq_len*, num_perspectives)
matching_vector_1_att_mean = multi_perspective_match(context_1,
att_mean_2,
self.attentive_match_weights)
matching_vector_2_att_mean = multi_perspective_match(context_2,
att_mean_1,
self.attentive_match_weights_reversed)
matching_vector_1.extend(matching_vector_1_att_mean)
matching_vector_2.extend(matching_vector_2_att_mean)
# Step 4. Max-Attentive-Matching
# Pick the contextual embeddings with the highest cosine similarity as the attentive
# vector, and match each forward (or backward) contextual embedding with its
# corresponding attentive vector.
if self.with_max_attentive_match:
# (batch, seq_len*, hidden_dim)
att_max_2 = masked_max(att_2, mask_2.unsqueeze(-2).unsqueeze(-1), dim=2)
att_max_1 = masked_max(att_1.permute(0, 2, 1, 3), mask_1.unsqueeze(-2).unsqueeze(-1), dim=2)
# (batch, seq_len*, num_perspectives)
matching_vector_1_att_max = multi_perspective_match(context_1,
att_max_2,
self.max_attentive_match_weights)
matching_vector_2_att_max = multi_perspective_match(context_2,
att_max_1,
self.max_attentive_match_weights_reversed)
matching_vector_1.extend(matching_vector_1_att_max)
matching_vector_2.extend(matching_vector_2_att_max)
return matching_vector_1, matching_vector_2 | [
"\n Given the forward (or backward) representations of sentence1 and sentence2, apply four bilateral\n matching functions between them in one direction.\n\n Parameters\n ----------\n context_1 : ``torch.Tensor``\n Tensor of shape (batch_size, seq_len1, hidden_dim) representing the encoding of the first sentence.\n mask_1 : ``torch.Tensor``\n Binary Tensor of shape (batch_size, seq_len1), indicating which\n positions in the first sentence are padding (0) and which are not (1).\n context_2 : ``torch.Tensor``\n Tensor of shape (batch_size, seq_len2, hidden_dim) representing the encoding of the second sentence.\n mask_2 : ``torch.Tensor``\n Binary Tensor of shape (batch_size, seq_len2), indicating which\n positions in the second sentence are padding (0) and which are not (1).\n\n Returns\n -------\n A tuple of matching vectors for the two sentences. Each of which is a list of\n matching vectors of shape (batch, seq_len, num_perspectives or 1)\n "
] |
Please provide a description of the function:def parse_example_line(lisp_string: str) -> Dict:
id_piece, rest = lisp_string.split(') (utterance "')
example_id = id_piece.split('(id ')[1]
question, rest = rest.split('") (context (graph tables.TableKnowledgeGraph ')
table_filename, rest = rest.split(')) (targetValue (list')
target_value_strings = rest.strip().split("(description")
target_values = []
for string in target_value_strings:
string = string.replace(")", "").replace('"', '').strip()
if string != "":
target_values.append(string)
return {'id': example_id,
'question': question,
'table_filename': table_filename,
'target_values': target_values} | [
"\n Training data in WikitableQuestions comes with examples in the form of lisp strings in the format:\n (example (id <example-id>)\n (utterance <question>)\n (context (graph tables.TableKnowledgeGraph <table-filename>))\n (targetValue (list (description <answer1>) (description <answer2>) ...)))\n\n We parse such strings and return the parsed information here.\n "
] |
Please provide a description of the function:def make_vocab_from_args(args: argparse.Namespace):
parameter_path = args.param_path
overrides = args.overrides
serialization_dir = args.serialization_dir
params = Params.from_file(parameter_path, overrides)
make_vocab_from_params(params, serialization_dir) | [
"\n Just converts from an ``argparse.Namespace`` object to params.\n "
] |
Please provide a description of the function:def execute(self, lf_raw: str) -> int:
# Remove "a:" prefixes from attributes (hack)
logical_form = re.sub(r"\(a:", r"(", lf_raw)
parse = semparse_util.lisp_to_nested_expression(logical_form)
if len(parse) < 2:
return -1
if parse[0] == 'infer':
args = [self._exec_and(arg) for arg in parse[1:]]
if None in args:
return -1
return self._exec_infer(*args)
return -1 | [
"\n Very basic model for executing friction logical forms. For now returns answer index (or\n -1 if no answer can be concluded)\n "
] |
Please provide a description of the function:def get_times_from_utterance(utterance: str,
char_offset_to_token_index: Dict[int, int],
indices_of_approximate_words: Set[int]) -> Dict[str, List[int]]:
pm_linking_dict = _time_regex_match(r'\d+pm',
utterance,
char_offset_to_token_index,
pm_map_match_to_query_value,
indices_of_approximate_words)
am_linking_dict = _time_regex_match(r'\d+am',
utterance,
char_offset_to_token_index,
am_map_match_to_query_value,
indices_of_approximate_words)
oclock_linking_dict = _time_regex_match(r"\d+ o'clock",
utterance,
char_offset_to_token_index,
lambda match: digit_to_query_time(match.rstrip(" o'clock")),
indices_of_approximate_words)
hours_linking_dict = _time_regex_match(r"\d+ hours",
utterance,
char_offset_to_token_index,
lambda match: [int(match.rstrip(" hours"))],
indices_of_approximate_words)
times_linking_dict: Dict[str, List[int]] = defaultdict(list)
linking_dicts = [pm_linking_dict, am_linking_dict, oclock_linking_dict, hours_linking_dict]
for linking_dict in linking_dicts:
for key, value in linking_dict.items():
times_linking_dict[key].extend(value)
return times_linking_dict | [
"\n Given an utterance, we get the numbers that correspond to times and convert them to\n values that may appear in the query. For example: convert ``7pm`` to ``1900``.\n "
] |
Please provide a description of the function:def get_date_from_utterance(tokenized_utterance: List[Token],
year: int = 1993) -> List[datetime]:
dates = []
utterance = ' '.join([token.text for token in tokenized_utterance])
year_result = re.findall(r'199[0-4]', utterance)
if year_result:
year = int(year_result[0])
trigrams = ngrams([token.text for token in tokenized_utterance], 3)
for month, tens, digit in trigrams:
# This will match something like ``september twenty first``.
day = ' '.join([tens, digit])
if month in MONTH_NUMBERS and day in DAY_NUMBERS:
try:
dates.append(datetime(year, MONTH_NUMBERS[month], DAY_NUMBERS[day]))
except ValueError:
print('invalid month day')
bigrams = ngrams([token.text for token in tokenized_utterance], 2)
for month, day in bigrams:
if month in MONTH_NUMBERS and day in DAY_NUMBERS:
# This will match something like ``september first``.
try:
dates.append(datetime(year, MONTH_NUMBERS[month], DAY_NUMBERS[day]))
except ValueError:
print('invalid month day')
fivegrams = ngrams([token.text for token in tokenized_utterance], 5)
for tens, digit, _, year_match, month in fivegrams:
# This will match something like ``twenty first of 1993 july``.
day = ' '.join([tens, digit])
if month in MONTH_NUMBERS and day in DAY_NUMBERS and year_match.isdigit():
try:
dates.append(datetime(int(year_match), MONTH_NUMBERS[month], DAY_NUMBERS[day]))
except ValueError:
print('invalid month day')
if month in MONTH_NUMBERS and digit in DAY_NUMBERS and year_match.isdigit():
try:
dates.append(datetime(int(year_match), MONTH_NUMBERS[month], DAY_NUMBERS[digit]))
except ValueError:
print('invalid month day')
return dates | [
"\n When the year is not explicitly mentioned in the utterance, the query assumes that\n it is 1993 so we do the same here. If there is no mention of the month or day then\n we do not return any dates from the utterance.\n "
] |
Please provide a description of the function:def get_numbers_from_utterance(utterance: str, tokenized_utterance: List[Token]) -> Dict[str, List[int]]:
# When we use a regex to find numbers or strings, we need a mapping from
# the character to which token triggered it.
char_offset_to_token_index = {token.idx : token_index
for token_index, token in enumerate(tokenized_utterance)}
# We want to look up later for each time whether it appears after a word
# such as "about" or "approximately".
indices_of_approximate_words = {index for index, token in enumerate(tokenized_utterance)
if token.text in APPROX_WORDS}
indices_of_words_preceding_time = {index for index, token in enumerate(tokenized_utterance)
if token.text in WORDS_PRECEDING_TIME}
indices_of_am_pm = {index for index, token in enumerate(tokenized_utterance)
if token.text in {'am', 'pm'}}
number_linking_dict: Dict[str, List[int]] = defaultdict(list)
for token_index, token in enumerate(tokenized_utterance):
if token.text.isdigit():
if token_index - 1 in indices_of_words_preceding_time and token_index + 1 not in indices_of_am_pm:
for time in digit_to_query_time(token.text):
number_linking_dict[str(time)].append(token_index)
times_linking_dict = get_times_from_utterance(utterance,
char_offset_to_token_index,
indices_of_approximate_words)
for key, value in times_linking_dict.items():
number_linking_dict[key].extend(value)
for index, token in enumerate(tokenized_utterance):
for number in NUMBER_TRIGGER_DICT.get(token.text, []):
if index - 1 in indices_of_approximate_words:
for approx_time in get_approximate_times([int(number)]):
number_linking_dict[str(approx_time)].append(index)
else:
number_linking_dict[number].append(index)
return number_linking_dict | [
"\n Given an utterance, this function finds all the numbers that are in the action space. Since we need to\n keep track of linking scores, we represent the numbers as a dictionary, where the keys are the string\n representation of the number and the values are lists of the token indices that triggers that number.\n "
] |
Please provide a description of the function:def digit_to_query_time(digit: str) -> List[int]:
if len(digit) > 2:
return [int(digit), int(digit) + TWELVE_TO_TWENTY_FOUR]
elif int(digit) % 12 == 0:
return [0, 1200, 2400]
return [int(digit) * HOUR_TO_TWENTY_FOUR,
(int(digit) * HOUR_TO_TWENTY_FOUR + TWELVE_TO_TWENTY_FOUR) % HOURS_IN_DAY] | [
"\n Given a digit in the utterance, return a list of the times that it corresponds to.\n "
] |
Please provide a description of the function:def get_approximate_times(times: List[int]) -> List[int]:
approximate_times = []
for time in times:
hour = int(time/HOUR_TO_TWENTY_FOUR) % 24
minute = time % HOUR_TO_TWENTY_FOUR
approximate_time = datetime.now()
approximate_time = approximate_time.replace(hour=hour, minute=minute)
start_time_range = approximate_time - timedelta(minutes=30)
end_time_range = approximate_time + timedelta(minutes=30)
approximate_times.extend([start_time_range.hour * HOUR_TO_TWENTY_FOUR + start_time_range.minute,
end_time_range.hour * HOUR_TO_TWENTY_FOUR + end_time_range.minute])
return approximate_times | [
"\n Given a list of times that follow a word such as ``about``,\n we return a list of times that could appear in the query as a result\n of this. For example if ``about 7pm`` appears in the utterance, then\n we also want to add ``1830`` and ``1930``.\n "
] |
Please provide a description of the function:def _time_regex_match(regex: str,
utterance: str,
char_offset_to_token_index: Dict[int, int],
map_match_to_query_value: Callable[[str], List[int]],
indices_of_approximate_words: Set[int]) -> Dict[str, List[int]]:
r
linking_scores_dict: Dict[str, List[int]] = defaultdict(list)
number_regex = re.compile(regex)
for match in number_regex.finditer(utterance):
query_values = map_match_to_query_value(match.group())
# If the time appears after a word like ``about`` then we also add
# the times that mark the start and end of the allowed range.
approximate_times = []
if char_offset_to_token_index.get(match.start(), 0) - 1 in indices_of_approximate_words:
approximate_times.extend(get_approximate_times(query_values))
query_values.extend(approximate_times)
if match.start() in char_offset_to_token_index:
for query_value in query_values:
linking_scores_dict[str(query_value)].extend([char_offset_to_token_index[match.start()],
char_offset_to_token_index[match.start()] + 1])
return linking_scores_dict | [
"\n Given a regex for matching times in the utterance, we want to convert the matches\n to the values that appear in the query and token indices they correspond to.\n\n ``char_offset_to_token_index`` is a dictionary that maps from the character offset to\n the token index, we use this to look up what token a regex match corresponds to.\n ``indices_of_approximate_words`` are the token indices of the words such as ``about`` or\n ``approximately``. We use this to check if a regex match is preceded by one of these words.\n If it is, we also want to add the times that define this approximate time range.\n\n ``map_match_to_query_value`` is a function that converts the regex matches to the\n values that appear in the query. For example, we may pass in a regex such as ``\\d+pm``\n that matches times such as ``7pm``. ``map_match_to_query_value`` would be a function that\n takes ``7pm`` as input and returns ``1900``.\n "
] |
Please provide a description of the function:def _evaluate_sql_query_subprocess(self, predicted_query: str, sql_query_labels: List[str]) -> int:
postprocessed_predicted_query = self.postprocess_query_sqlite(predicted_query)
try:
self._cursor.execute(postprocessed_predicted_query)
predicted_rows = self._cursor.fetchall()
except sqlite3.Error as error:
logger.warning(f'Error executing predicted: {error}')
exit(0)
# If predicted table matches any of the reference tables then it is counted as correct.
target_rows = None
for sql_query_label in sql_query_labels:
postprocessed_sql_query_label = self.postprocess_query_sqlite(sql_query_label)
try:
self._cursor.execute(postprocessed_sql_query_label)
target_rows = self._cursor.fetchall()
except sqlite3.Error as error:
logger.warning(f'Error executing predicted: {error}')
if predicted_rows == target_rows:
exit(1)
exit(0) | [
"\n We evaluate here whether the predicted query and the query label evaluate to the\n exact same table. This method is only called by the subprocess, so we just exit with\n 1 if it is correct and 0 otherwise.\n "
] |
Please provide a description of the function:def format_grammar_string(grammar_dictionary: Dict[str, List[str]]) -> str:
grammar_string = '\n'.join([f"{nonterminal} = {' / '.join(right_hand_side)}"
for nonterminal, right_hand_side in grammar_dictionary.items()])
return grammar_string.replace("\\", "\\\\") | [
"\n Formats a dictionary of production rules into the string format expected\n by the Parsimonious Grammar class.\n "
] |
Please provide a description of the function:def initialize_valid_actions(grammar: Grammar,
keywords_to_uppercase: List[str] = None) -> Dict[str, List[str]]:
valid_actions: Dict[str, Set[str]] = defaultdict(set)
for key in grammar:
rhs = grammar[key]
# Sequence represents a series of expressions that match pieces of the text in order.
# Eg. A -> B C
if isinstance(rhs, Sequence):
valid_actions[key].add(format_action(key, " ".join(rhs._unicode_members()), # pylint: disable=protected-access
keywords_to_uppercase=keywords_to_uppercase))
# OneOf represents a series of expressions, one of which matches the text.
# Eg. A -> B / C
elif isinstance(rhs, OneOf):
for option in rhs._unicode_members(): # pylint: disable=protected-access
valid_actions[key].add(format_action(key, option,
keywords_to_uppercase=keywords_to_uppercase))
# A string literal, eg. "A"
elif isinstance(rhs, Literal):
if rhs.literal != "":
valid_actions[key].add(format_action(key, repr(rhs.literal),
keywords_to_uppercase=keywords_to_uppercase))
else:
valid_actions[key] = set()
valid_action_strings = {key: sorted(value) for key, value in valid_actions.items()}
return valid_action_strings | [
"\n We initialize the valid actions with the global actions. These include the\n valid actions that result from the grammar and also those that result from\n the tables provided. The keys represent the nonterminals in the grammar\n and the values are lists of the valid actions of that nonterminal.\n "
] |
Please provide a description of the function:def format_action(nonterminal: str,
right_hand_side: str,
is_string: bool = False,
is_number: bool = False,
keywords_to_uppercase: List[str] = None) -> str:
keywords_to_uppercase = keywords_to_uppercase or []
if right_hand_side.upper() in keywords_to_uppercase:
right_hand_side = right_hand_side.upper()
if is_string:
return f'{nonterminal} -> ["\'{right_hand_side}\'"]'
elif is_number:
return f'{nonterminal} -> ["{right_hand_side}"]'
else:
right_hand_side = right_hand_side.lstrip("(").rstrip(")")
child_strings = [token for token in WHITESPACE_REGEX.split(right_hand_side) if token]
child_strings = [tok.upper() if tok.upper() in keywords_to_uppercase else tok for tok in child_strings]
return f"{nonterminal} -> [{', '.join(child_strings)}]" | [
"\n This function formats an action as it appears in models. It\n splits productions based on the special `ws` and `wsp` rules,\n which are used in grammars to denote whitespace, and then\n rejoins these tokens a formatted, comma separated list.\n Importantly, note that it `does not` split on spaces in\n the grammar string, because these might not correspond\n to spaces in the language the grammar recognises.\n\n Parameters\n ----------\n nonterminal : ``str``, required.\n The nonterminal in the action.\n right_hand_side : ``str``, required.\n The right hand side of the action\n (i.e the thing which is produced).\n is_string : ``bool``, optional (default = False).\n Whether the production produces a string.\n If it does, it is formatted as ``nonterminal -> ['string']``\n is_number : ``bool``, optional, (default = False).\n Whether the production produces a string.\n If it does, it is formatted as ``nonterminal -> ['number']``\n keywords_to_uppercase: ``List[str]``, optional, (default = None)\n Keywords in the grammar to uppercase. In the case of sql,\n this might be SELECT, MAX etc.\n "
] |
Please provide a description of the function:def add_action(self, node: Node) -> None:
if node.expr.name and node.expr.name not in ['ws', 'wsp']:
nonterminal = f'{node.expr.name} -> '
if isinstance(node.expr, Literal):
right_hand_side = f'["{node.text}"]'
else:
child_strings = []
for child in node.__iter__():
if child.expr.name in ['ws', 'wsp']:
continue
if child.expr.name != '':
child_strings.append(child.expr.name)
else:
child_right_side_string = child.expr._as_rhs().lstrip("(").rstrip(")") # pylint: disable=protected-access
child_right_side_list = [tok for tok in
WHITESPACE_REGEX.split(child_right_side_string) if tok]
child_right_side_list = [tok.upper() if tok.upper() in
self.keywords_to_uppercase else tok
for tok in child_right_side_list]
child_strings.extend(child_right_side_list)
right_hand_side = "[" + ", ".join(child_strings) + "]"
rule = nonterminal + right_hand_side
self.action_sequence = [rule] + self.action_sequence | [
"\n For each node, we accumulate the rules that generated its children in a list.\n "
] |
Please provide a description of the function:def visit(self, node):
method = getattr(self, 'visit_' + node.expr_name, self.generic_visit)
# Call that method, and show where in the tree it failed if it blows
# up.
try:
# Changing this to reverse here!
return method(node, [self.visit(child) for child in reversed(list(node))])
except (VisitationError, UndefinedLabel):
# Don't catch and re-wrap already-wrapped exceptions.
raise
except self.unwrapped_exceptions:
raise
except Exception: # pylint: disable=broad-except
# Catch any exception, and tack on a parse tree so it's easier to
# see where it went wrong.
exc_class, exc, traceback = exc_info()
reraise(VisitationError, VisitationError(exc, exc_class, node), traceback) | [
"\n See the ``NodeVisitor`` visit method. This just changes the order in which\n we visit nonterminals from right to left to left to right.\n "
] |
Please provide a description of the function:def forward(self,
input_ids: torch.LongTensor,
offsets: torch.LongTensor = None,
token_type_ids: torch.LongTensor = None) -> torch.Tensor:
# pylint: disable=arguments-differ
if token_type_ids is None:
token_type_ids = torch.zeros_like(input_ids)
input_mask = (input_ids != 0).long()
# input_ids may have extra dimensions, so we reshape down to 2-d
# before calling the BERT model and then reshape back at the end.
all_encoder_layers, _ = self.bert_model(input_ids=util.combine_initial_dims(input_ids),
token_type_ids=util.combine_initial_dims(token_type_ids),
attention_mask=util.combine_initial_dims(input_mask))
if self._scalar_mix is not None:
mix = self._scalar_mix(all_encoder_layers, input_mask)
else:
mix = all_encoder_layers[-1]
# At this point, mix is (batch_size * d1 * ... * dn, sequence_length, embedding_dim)
if offsets is None:
# Resize to (batch_size, d1, ..., dn, sequence_length, embedding_dim)
return util.uncombine_initial_dims(mix, input_ids.size())
else:
# offsets is (batch_size, d1, ..., dn, orig_sequence_length)
offsets2d = util.combine_initial_dims(offsets)
# now offsets is (batch_size * d1 * ... * dn, orig_sequence_length)
range_vector = util.get_range_vector(offsets2d.size(0),
device=util.get_device_of(mix)).unsqueeze(1)
# selected embeddings is also (batch_size * d1 * ... * dn, orig_sequence_length)
selected_embeddings = mix[range_vector, offsets2d]
return util.uncombine_initial_dims(selected_embeddings, offsets.size()) | [
"\n Parameters\n ----------\n input_ids : ``torch.LongTensor``\n The (batch_size, ..., max_sequence_length) tensor of wordpiece ids.\n offsets : ``torch.LongTensor``, optional\n The BERT embeddings are one per wordpiece. However it's possible/likely\n you might want one per original token. In that case, ``offsets``\n represents the indices of the desired wordpiece for each original token.\n Depending on how your token indexer is configured, this could be the\n position of the last wordpiece for each token, or it could be the position\n of the first wordpiece for each token.\n\n For example, if you had the sentence \"Definitely not\", and if the corresponding\n wordpieces were [\"Def\", \"##in\", \"##ite\", \"##ly\", \"not\"], then the input_ids\n would be 5 wordpiece ids, and the \"last wordpiece\" offsets would be [3, 4].\n If offsets are provided, the returned tensor will contain only the wordpiece\n embeddings at those positions, and (in particular) will contain one embedding\n per token. If offsets are not provided, the entire tensor of wordpiece embeddings\n will be returned.\n token_type_ids : ``torch.LongTensor``, optional\n If an input consists of two sentences (as in the BERT paper),\n tokens from the first sentence should have type 0 and tokens from\n the second sentence should have type 1. If you don't provide this\n (the default BertIndexer doesn't) then it's assumed to be all 0s.\n "
] |
Please provide a description of the function:def update_grammar_to_be_variable_free(grammar_dictionary: Dict[str, List[str]]):
# Tables in variable free grammars cannot be aliased, so we
# remove this functionality from the grammar.
grammar_dictionary["select_result"] = ['"*"', '(table_name ws ".*")', 'expr']
# Similarly, collapse the definition of a source table
# to not contain aliases and modify references to subqueries.
grammar_dictionary["single_source"] = ['table_name', '("(" ws query ws ")")']
del grammar_dictionary["source_subq"]
del grammar_dictionary["source_table"]
grammar_dictionary["expr"] = ['in_expr',
'(value wsp "LIKE" wsp string)',
'(value ws "BETWEEN" wsp value ws "AND" wsp value)',
'(value ws binaryop wsp expr)',
'(unaryop ws expr)',
'(col_ref ws "IS" ws "NOT" ws "NULL")',
'(col_ref ws "IS" ws "NULL")',
# This used to be source_subq - now
# we don't need aliases, we can colapse it to queries.
'("(" ws query ws ")")',
'value']
# Finally, remove the ability to reference an arbitrary name,
# because now we don't have aliased tables, we don't need
# to recognise new variables.
del grammar_dictionary["name"] | [
"\n SQL is a predominately variable free language in terms of simple usage, in the\n sense that most queries do not create references to variables which are not\n already static tables in a dataset. However, it is possible to do this via\n derived tables. If we don't require this functionality, we can tighten the\n grammar, because we don't need to support aliased tables.\n "
] |
Please provide a description of the function:def update_grammar_with_untyped_entities(grammar_dictionary: Dict[str, List[str]]) -> None:
grammar_dictionary["string_set_vals"] = ['(value ws "," ws string_set_vals)', 'value']
grammar_dictionary["value"].remove('string')
grammar_dictionary["value"].remove('number')
grammar_dictionary["limit"] = ['("LIMIT" ws "1")', '("LIMIT" ws value)']
grammar_dictionary["expr"][1] = '(value wsp "LIKE" wsp value)'
del grammar_dictionary["string"]
del grammar_dictionary["number"] | [
"\n Variables can be treated as numbers or strings if their type can be inferred -\n however, that can be difficult, so instead, we can just treat them all as values\n and be a bit looser on the typing we allow in our grammar. Here we just remove\n all references to number and string from the grammar, replacing them with value.\n "
] |
Please provide a description of the function:def _load(cls,
config: Params,
serialization_dir: str,
weights_file: str = None,
cuda_device: int = -1) -> 'Model':
model_params = config.get('model')
# The experiment config tells us how to _train_ a model, including where to get pre-trained
# embeddings from. We're now _loading_ the model, so those embeddings will already be
# stored in our weights. We don't need any pretrained weight file anymore, and we don't
# want the code to look for it, so we remove it from the parameters here.
remove_pretrained_embedding_params(model_params)
model = Model.from_params(vocab=None, params=model_params)
# Force model to cpu or gpu, as appropriate, to make sure that the embeddings are
# in sync with the weights
if cuda_device >= 0:
model.cuda(cuda_device)
else:
model.cpu()
return model | [
"\n Ensembles don't have vocabularies or weights of their own, so they override _load.\n "
] |
Please provide a description of the function:def text_standardize(text):
text = text.replace('—', '-')
text = text.replace('–', '-')
text = text.replace('―', '-')
text = text.replace('…', '...')
text = text.replace('´', "'")
text = re.sub(r'''(-+|~+|!+|"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)''', r' \1 ', text)
text = re.sub(r'\s*\n\s*', ' \n ', text)
text = re.sub(r'[^\S\n]+', ' ', text)
return text.strip() | [
"\n Apply text standardization following original implementation.\n "
] |
Please provide a description of the function:def main(prog: str = None,
subcommand_overrides: Dict[str, Subcommand] = {}) -> None:
# pylint: disable=dangerous-default-value
parser = ArgumentParserWithDefaults(description="Run AllenNLP", usage='%(prog)s', prog=prog)
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
subparsers = parser.add_subparsers(title='Commands', metavar='')
subcommands = {
# Default commands
"configure": Configure(),
"train": Train(),
"evaluate": Evaluate(),
"predict": Predict(),
"make-vocab": MakeVocab(),
"elmo": Elmo(),
"fine-tune": FineTune(),
"dry-run": DryRun(),
"test-install": TestInstall(),
"find-lr": FindLearningRate(),
"print-results": PrintResults(),
# Superseded by overrides
**subcommand_overrides
}
for name, subcommand in subcommands.items():
subparser = subcommand.add_subparser(name, subparsers)
# configure doesn't need include-package because it imports
# whatever classes it needs.
if name != "configure":
subparser.add_argument('--include-package',
type=str,
action='append',
default=[],
help='additional packages to include')
args = parser.parse_args()
# If a subparser is triggered, it adds its work as `args.func`.
# So if no such attribute has been added, no subparser was triggered,
# so give the user some help.
if 'func' in dir(args):
# Import any additional modules needed (to register custom classes).
for package_name in getattr(args, 'include_package', ()):
import_submodules(package_name)
args.func(args)
else:
parser.print_help() | [
"\n The :mod:`~allennlp.run` command only knows about the registered classes in the ``allennlp``\n codebase. In particular, once you start creating your own ``Model`` s and so forth, it won't\n work for them, unless you use the ``--include-package`` flag.\n "
] |
Please provide a description of the function:def get_padding_lengths(self) -> Dict[str, int]:
# Our basic outline: we will iterate over `TokenIndexers`, and aggregate lengths over tokens
# for each indexer separately. Then we will combine the results for each indexer into a single
# dictionary, resolving any (unlikely) key conflicts by taking a max.
lengths = []
if self._indexed_tokens is None:
raise ConfigurationError("You must call .index(vocabulary) on a "
"field before determining padding lengths.")
# Each indexer can return a different sequence length, and for indexers that return
# multiple arrays each can have a different length. We'll keep track of them here.
for indexer_name, indexer in self._token_indexers.items():
indexer_lengths = {}
for indexed_tokens_key in self._indexer_name_to_indexed_token[indexer_name]:
# This is a list of dicts, one for each token in the field.
token_lengths = [indexer.get_padding_lengths(token)
for token in self._indexed_tokens[indexed_tokens_key]]
if not token_lengths:
# This is a padding edge case and occurs when we want to pad a ListField of
# TextFields. In order to pad the list field, we need to be able to have an
# _empty_ TextField, but if this is the case, token_lengths will be an empty
# list, so we add the default empty padding dictionary to the list instead.
token_lengths = [{}]
# Iterate over the keys and find the maximum token length.
# It's fine to iterate over the keys of the first token since all tokens have the same keys.
for key in token_lengths[0]:
indexer_lengths[key] = max(x[key] if key in x else 0 for x in token_lengths)
lengths.append(indexer_lengths)
padding_lengths = {}
num_tokens = set()
for indexer_name, token_list in self._indexed_tokens.items():
padding_lengths[f"{indexer_name}_length"] = len(token_list)
num_tokens.add(len(token_list))
# We don't actually use this for padding anywhere, but we used to. We add this key back in
# so that older configs still work if they sorted by this key in a BucketIterator. Taking
# the max of all of these should be fine for that purpose.
padding_lengths['num_tokens'] = max(num_tokens)
# Get all keys which have been used for padding for each indexer and take the max if there are duplicates.
padding_keys = {key for d in lengths for key in d.keys()}
for padding_key in padding_keys:
padding_lengths[padding_key] = max(x[padding_key] if padding_key in x else 0 for x in lengths)
return padding_lengths | [
"\n The ``TextField`` has a list of ``Tokens``, and each ``Token`` gets converted into arrays by\n (potentially) several ``TokenIndexers``. This method gets the max length (over tokens)\n associated with each of these arrays.\n "
] |
Please provide a description of the function:def main(vocab_path: str,
elmo_config_path: str,
elmo_weights_path: str,
output_dir: str,
batch_size: int,
device: int,
use_custom_oov_token: bool = False):
# Load the vocabulary words and convert to char ids
with open(vocab_path, 'r') as vocab_file:
tokens = vocab_file.read().strip().split('\n')
# Insert the sentence boundary tokens which elmo uses at positions 1 and 2.
if tokens[0] != DEFAULT_OOV_TOKEN and not use_custom_oov_token:
raise ConfigurationError("ELMo embeddings require the use of a OOV token.")
tokens = [tokens[0]] + ["<S>", "</S>"] + tokens[1:]
indexer = ELMoTokenCharactersIndexer()
indices = indexer.tokens_to_indices([Token(token) for token in tokens], Vocabulary(), "indices")["indices"]
sentences = []
for k in range((len(indices) // 50) + 1):
sentences.append(indexer.pad_token_sequence(indices[(k * 50):((k + 1) * 50)],
desired_num_tokens=50,
padding_lengths={}))
last_batch_remainder = 50 - (len(indices) % 50)
if device != -1:
elmo_token_embedder = _ElmoCharacterEncoder(elmo_config_path,
elmo_weights_path).cuda(device)
else:
elmo_token_embedder = _ElmoCharacterEncoder(elmo_config_path,
elmo_weights_path)
all_embeddings = []
for i in range((len(sentences) // batch_size) + 1):
array = numpy.array(sentences[i * batch_size: (i + 1) * batch_size])
if device != -1:
batch = torch.from_numpy(array).cuda(device)
else:
batch = torch.from_numpy(array)
token_embedding = elmo_token_embedder(batch)['token_embedding'].data
# Reshape back to a list of words of shape (batch_size * 50, encoding_dim)
# We also need to remove the <S>, </S> tokens appended by the encoder.
per_word_embeddings = token_embedding[:, 1:-1, :].contiguous().view(-1, token_embedding.size(-1))
all_embeddings.append(per_word_embeddings)
# Remove the embeddings associated with padding in the last batch.
all_embeddings[-1] = all_embeddings[-1][:-last_batch_remainder, :]
embedding_weight = torch.cat(all_embeddings, 0).cpu().numpy()
# Write out the embedding in a glove format.
os.makedirs(output_dir, exist_ok=True)
with gzip.open(os.path.join(output_dir, "elmo_embeddings.txt.gz"), 'wb') as embeddings_file:
for i, word in enumerate(tokens):
string_array = " ".join([str(x) for x in list(embedding_weight[i, :])])
embeddings_file.write(f"{word} {string_array}\n".encode('utf-8'))
# Write out the new vocab with the <S> and </S> tokens.
_, vocab_file_name = os.path.split(vocab_path)
with open(os.path.join(output_dir, vocab_file_name), "w") as new_vocab_file:
for word in tokens:
new_vocab_file.write(f"{word}\n") | [
"\n Creates ELMo word representations from a vocabulary file. These\n word representations are _independent_ - they are the result of running\n the CNN and Highway layers of the ELMo model, but not the Bidirectional LSTM.\n ELMo requires 2 additional tokens: <S> and </S>. The first token\n in this file is assumed to be an unknown token.\n\n This script produces two artifacts: A new vocabulary file\n with the <S> and </S> tokens inserted and a glove formatted embedding\n file containing word : vector pairs, one per line, with all values\n separated by a space.\n "
] |
Please provide a description of the function:def sort_by_padding(instances: List[Instance],
sorting_keys: List[Tuple[str, str]], # pylint: disable=invalid-sequence-index
vocab: Vocabulary,
padding_noise: float = 0.0) -> List[Instance]:
instances_with_lengths = []
for instance in instances:
# Make sure instance is indexed before calling .get_padding
instance.index_fields(vocab)
padding_lengths = cast(Dict[str, Dict[str, float]], instance.get_padding_lengths())
if padding_noise > 0.0:
noisy_lengths = {}
for field_name, field_lengths in padding_lengths.items():
noisy_lengths[field_name] = add_noise_to_dict_values(field_lengths, padding_noise)
padding_lengths = noisy_lengths
instance_with_lengths = ([padding_lengths[field_name][padding_key]
for (field_name, padding_key) in sorting_keys],
instance)
instances_with_lengths.append(instance_with_lengths)
instances_with_lengths.sort(key=lambda x: x[0])
return [instance_with_lengths[-1] for instance_with_lengths in instances_with_lengths] | [
"\n Sorts the instances by their padding lengths, using the keys in\n ``sorting_keys`` (in the order in which they are provided). ``sorting_keys`` is a list of\n ``(field_name, padding_key)`` tuples.\n "
] |
Please provide a description of the function:def infer(self, setup: QuaRelType, answer_0: QuaRelType, answer_1: QuaRelType) -> int:
if self._check_quarels_compatible(setup, answer_0):
if self._check_quarels_compatible(setup, answer_1):
# Found two answers
return -2
else:
return 0
elif self._check_quarels_compatible(setup, answer_1):
return 1
else:
return -1 | [
"\n Take the question and check if it is compatible with either of the answer choices.\n "
] |
Please provide a description of the function:def make_app(predictor: Predictor,
field_names: List[str] = None,
static_dir: str = None,
sanitizer: Callable[[JsonDict], JsonDict] = None,
title: str = "AllenNLP Demo") -> Flask:
if static_dir is not None:
static_dir = os.path.abspath(static_dir)
if not os.path.exists(static_dir):
logger.error("app directory %s does not exist, aborting", static_dir)
sys.exit(-1)
elif static_dir is None and field_names is None:
print("Neither build_dir nor field_names passed. Demo won't render on this port.\n"
"You must use nodejs + react app to interact with the server.")
app = Flask(__name__) # pylint: disable=invalid-name
@app.errorhandler(ServerError)
def handle_invalid_usage(error: ServerError) -> Response: # pylint: disable=unused-variable
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
@app.route('/')
def index() -> Response: # pylint: disable=unused-variable
if static_dir is not None:
return send_file(os.path.join(static_dir, 'index.html'))
else:
html = _html(title, field_names)
return Response(response=html, status=200)
@app.route('/predict', methods=['POST', 'OPTIONS'])
def predict() -> Response: # pylint: disable=unused-variable
if request.method == "OPTIONS":
return Response(response="", status=200)
data = request.get_json()
prediction = predictor.predict_json(data)
if sanitizer is not None:
prediction = sanitizer(prediction)
log_blob = {"inputs": data, "outputs": prediction}
logger.info("prediction: %s", json.dumps(log_blob))
return jsonify(prediction)
@app.route('/predict_batch', methods=['POST', 'OPTIONS'])
def predict_batch() -> Response: # pylint: disable=unused-variable
if request.method == "OPTIONS":
return Response(response="", status=200)
data = request.get_json()
prediction = predictor.predict_batch_json(data)
if sanitizer is not None:
prediction = [sanitizer(p) for p in prediction]
return jsonify(prediction)
@app.route('/<path:path>')
def static_proxy(path: str) -> Response: # pylint: disable=unused-variable
if static_dir is not None:
return send_from_directory(static_dir, path)
else:
raise ServerError("static_dir not specified", 404)
return app | [
"\n Creates a Flask app that serves up the provided ``Predictor``\n along with a front-end for interacting with it.\n\n If you want to use the built-in bare-bones HTML, you must provide the\n field names for the inputs (which will be used both as labels\n and as the keys in the JSON that gets sent to the predictor).\n\n If you would rather create your own HTML, call it index.html\n and provide its directory as ``static_dir``. In that case you\n don't need to supply the field names -- that information should\n be implicit in your demo site. (Probably the easiest thing to do\n is just start with the bare-bones HTML and modify it.)\n\n In addition, if you want somehow transform the JSON prediction\n (e.g. by removing probabilities or logits)\n you can do that by passing in a ``sanitizer`` function.\n ",
"make a prediction using the specified model and return the results",
"make a prediction using the specified model and return the results"
] |
Please provide a description of the function:def _html(title: str, field_names: List[str]) -> str:
inputs = ''.join(_SINGLE_INPUT_TEMPLATE.substitute(field_name=field_name)
for field_name in field_names)
quoted_field_names = [f"'{field_name}'" for field_name in field_names]
quoted_field_list = f"[{','.join(quoted_field_names)}]"
return _PAGE_TEMPLATE.substitute(title=title,
css=_CSS,
inputs=inputs,
qfl=quoted_field_list) | [
"\n Returns bare bones HTML for serving up an input form with the\n specified fields that can render predictions from the configured model.\n "
] |
Please provide a description of the function:def get_valid_actions(self) -> Dict[str, Tuple[torch.Tensor, torch.Tensor, List[int]]]:
actions = self._valid_actions[self._nonterminal_stack[-1]]
context_actions = []
for type_, variable in self._lambda_stacks:
if self._nonterminal_stack[-1] == type_:
production_string = f"{type_} -> {variable}"
context_actions.append(self._context_actions[production_string])
if context_actions:
input_tensor, output_tensor, action_ids = actions['global']
new_inputs = [input_tensor] + [x[0] for x in context_actions]
input_tensor = torch.cat(new_inputs, dim=0)
new_outputs = [output_tensor] + [x[1] for x in context_actions]
output_tensor = torch.cat(new_outputs, dim=0)
new_action_ids = action_ids + [x[2] for x in context_actions]
# We can't just reassign to actions['global'], because that would modify the state of
# self._valid_actions. Instead, we need to construct a new actions dictionary.
new_actions = {**actions}
new_actions['global'] = (input_tensor, output_tensor, new_action_ids)
actions = new_actions
return actions | [
"\n Returns the valid actions in the current grammar state. See the class docstring for a\n description of what we're returning here.\n "
] |
Please provide a description of the function:def take_action(self, production_rule: str) -> 'LambdaGrammarStatelet':
left_side, right_side = production_rule.split(' -> ')
assert self._nonterminal_stack[-1] == left_side, (f"Tried to expand {self._nonterminal_stack[-1]}"
f"but got rule {left_side} -> {right_side}")
assert all(self._lambda_stacks[key][-1] == left_side for key in self._lambda_stacks)
new_stack = self._nonterminal_stack[:-1]
new_lambda_stacks = {key: self._lambda_stacks[key][:-1] for key in self._lambda_stacks}
productions = self._get_productions_from_string(right_side)
# Looking for lambda productions, but not for cells or columns with the word "lambda" in
# them.
if 'lambda' in productions[0] and 'fb:' not in productions[0]:
production = productions[0]
if production[0] == "'" and production[-1] == "'":
# The production rule with a lambda is typically "<t,d> -> ['lambda x', d]". We
# need to strip the quotes.
production = production[1:-1]
lambda_variable = production.split(' ')[1]
# The left side must be formatted as "<t,d>", where "t" is the type of the lambda
# variable, and "d" is the return type of the lambda function. We need to pull out the
# "t" here. TODO(mattg): this is pretty limiting, but I'm not sure how general we
# should make this.
if len(left_side) != 5:
raise NotImplementedError("Can't handle this type yet:", left_side)
lambda_type = left_side[1]
new_lambda_stacks[(lambda_type, lambda_variable)] = []
for production in reversed(productions):
if self._is_nonterminal(production):
new_stack.append(production)
for lambda_stack in new_lambda_stacks.values():
lambda_stack.append(production)
# If any of the lambda stacks have now become empty, we remove them from our dictionary.
new_lambda_stacks = {key: new_lambda_stacks[key]
for key in new_lambda_stacks if new_lambda_stacks[key]}
return LambdaGrammarStatelet(nonterminal_stack=new_stack,
lambda_stacks=new_lambda_stacks,
valid_actions=self._valid_actions,
context_actions=self._context_actions,
is_nonterminal=self._is_nonterminal) | [
"\n Takes an action in the current grammar state, returning a new grammar state with whatever\n updates are necessary. The production rule is assumed to be formatted as \"LHS -> RHS\".\n\n This will update the non-terminal stack and the context-dependent actions. Updating the\n non-terminal stack involves popping the non-terminal that was expanded off of the stack,\n then pushing on any non-terminals in the production rule back on the stack. We push the\n non-terminals on in `reverse` order, so that the first non-terminal in the production rule\n gets popped off the stack first.\n\n For example, if our current ``nonterminal_stack`` is ``[\"r\", \"<e,r>\", \"d\"]``, and\n ``action`` is ``d -> [<e,d>, e]``, the resulting stack will be ``[\"r\", \"<e,r>\", \"e\",\n \"<e,d>\"]``.\n "
] |
Please provide a description of the function:def decode_mst(energy: numpy.ndarray,
length: int,
has_labels: bool = True) -> Tuple[numpy.ndarray, numpy.ndarray]:
if has_labels and energy.ndim != 3:
raise ConfigurationError("The dimension of the energy array is not equal to 3.")
elif not has_labels and energy.ndim != 2:
raise ConfigurationError("The dimension of the energy array is not equal to 2.")
input_shape = energy.shape
max_length = input_shape[-1]
# Our energy matrix might have been batched -
# here we clip it to contain only non padded tokens.
if has_labels:
energy = energy[:, :length, :length]
# get best label for each edge.
label_id_matrix = energy.argmax(axis=0)
energy = energy.max(axis=0)
else:
energy = energy[:length, :length]
label_id_matrix = None
# get original score matrix
original_score_matrix = energy
# initialize score matrix to original score matrix
score_matrix = numpy.array(original_score_matrix, copy=True)
old_input = numpy.zeros([length, length], dtype=numpy.int32)
old_output = numpy.zeros([length, length], dtype=numpy.int32)
current_nodes = [True for _ in range(length)]
representatives: List[Set[int]] = []
for node1 in range(length):
original_score_matrix[node1, node1] = 0.0
score_matrix[node1, node1] = 0.0
representatives.append({node1})
for node2 in range(node1 + 1, length):
old_input[node1, node2] = node1
old_output[node1, node2] = node2
old_input[node2, node1] = node2
old_output[node2, node1] = node1
final_edges: Dict[int, int] = {}
# The main algorithm operates inplace.
chu_liu_edmonds(length, score_matrix, current_nodes,
final_edges, old_input, old_output, representatives)
heads = numpy.zeros([max_length], numpy.int32)
if has_labels:
head_type = numpy.ones([max_length], numpy.int32)
else:
head_type = None
for child, parent in final_edges.items():
heads[child] = parent
if has_labels:
head_type[child] = label_id_matrix[parent, child]
return heads, head_type | [
"\n Note: Counter to typical intuition, this function decodes the _maximum_\n spanning tree.\n\n Decode the optimal MST tree with the Chu-Liu-Edmonds algorithm for\n maximum spanning arborescences on graphs.\n\n Parameters\n ----------\n energy : ``numpy.ndarray``, required.\n A tensor with shape (num_labels, timesteps, timesteps)\n containing the energy of each edge. If has_labels is ``False``,\n the tensor should have shape (timesteps, timesteps) instead.\n length : ``int``, required.\n The length of this sequence, as the energy may have come\n from a padded batch.\n has_labels : ``bool``, optional, (default = True)\n Whether the graph has labels or not.\n "
] |
Please provide a description of the function:def chu_liu_edmonds(length: int,
score_matrix: numpy.ndarray,
current_nodes: List[bool],
final_edges: Dict[int, int],
old_input: numpy.ndarray,
old_output: numpy.ndarray,
representatives: List[Set[int]]):
# Set the initial graph to be the greedy best one.
parents = [-1]
for node1 in range(1, length):
parents.append(0)
if current_nodes[node1]:
max_score = score_matrix[0, node1]
for node2 in range(1, length):
if node2 == node1 or not current_nodes[node2]:
continue
new_score = score_matrix[node2, node1]
if new_score > max_score:
max_score = new_score
parents[node1] = node2
# Check if this solution has a cycle.
has_cycle, cycle = _find_cycle(parents, length, current_nodes)
# If there are no cycles, find all edges and return.
if not has_cycle:
final_edges[0] = -1
for node in range(1, length):
if not current_nodes[node]:
continue
parent = old_input[parents[node], node]
child = old_output[parents[node], node]
final_edges[child] = parent
return
# Otherwise, we have a cycle so we need to remove an edge.
# From here until the recursive call is the contraction stage of the algorithm.
cycle_weight = 0.0
# Find the weight of the cycle.
index = 0
for node in cycle:
index += 1
cycle_weight += score_matrix[parents[node], node]
# For each node in the graph, find the maximum weight incoming
# and outgoing edge into the cycle.
cycle_representative = cycle[0]
for node in range(length):
if not current_nodes[node] or node in cycle:
continue
in_edge_weight = float("-inf")
in_edge = -1
out_edge_weight = float("-inf")
out_edge = -1
for node_in_cycle in cycle:
if score_matrix[node_in_cycle, node] > in_edge_weight:
in_edge_weight = score_matrix[node_in_cycle, node]
in_edge = node_in_cycle
# Add the new edge score to the cycle weight
# and subtract the edge we're considering removing.
score = (cycle_weight +
score_matrix[node, node_in_cycle] -
score_matrix[parents[node_in_cycle], node_in_cycle])
if score > out_edge_weight:
out_edge_weight = score
out_edge = node_in_cycle
score_matrix[cycle_representative, node] = in_edge_weight
old_input[cycle_representative, node] = old_input[in_edge, node]
old_output[cycle_representative, node] = old_output[in_edge, node]
score_matrix[node, cycle_representative] = out_edge_weight
old_output[node, cycle_representative] = old_output[node, out_edge]
old_input[node, cycle_representative] = old_input[node, out_edge]
# For the next recursive iteration, we want to consider the cycle as a
# single node. Here we collapse the cycle into the first node in the
# cycle (first node is arbitrary), set all the other nodes not be
# considered in the next iteration. We also keep track of which
# representatives we are considering this iteration because we need
# them below to check if we're done.
considered_representatives: List[Set[int]] = []
for i, node_in_cycle in enumerate(cycle):
considered_representatives.append(set())
if i > 0:
# We need to consider at least one
# node in the cycle, arbitrarily choose
# the first.
current_nodes[node_in_cycle] = False
for node in representatives[node_in_cycle]:
considered_representatives[i].add(node)
if i > 0:
representatives[cycle_representative].add(node)
chu_liu_edmonds(length, score_matrix, current_nodes, final_edges, old_input, old_output, representatives)
# Expansion stage.
# check each node in cycle, if one of its representatives
# is a key in the final_edges, it is the one we need.
found = False
key_node = -1
for i, node in enumerate(cycle):
for cycle_rep in considered_representatives[i]:
if cycle_rep in final_edges:
key_node = node
found = True
break
if found:
break
previous = parents[key_node]
while previous != key_node:
child = old_output[parents[previous], previous]
parent = old_input[parents[previous], previous]
final_edges[child] = parent
previous = parents[previous] | [
"\n Applies the chu-liu-edmonds algorithm recursively\n to a graph with edge weights defined by score_matrix.\n\n Note that this function operates in place, so variables\n will be modified.\n\n Parameters\n ----------\n length : ``int``, required.\n The number of nodes.\n score_matrix : ``numpy.ndarray``, required.\n The score matrix representing the scores for pairs\n of nodes.\n current_nodes : ``List[bool]``, required.\n The nodes which are representatives in the graph.\n A representative at it's most basic represents a node,\n but as the algorithm progresses, individual nodes will\n represent collapsed cycles in the graph.\n final_edges: ``Dict[int, int]``, required.\n An empty dictionary which will be populated with the\n nodes which are connected in the maximum spanning tree.\n old_input: ``numpy.ndarray``, required.\n old_output: ``numpy.ndarray``, required.\n representatives : ``List[Set[int]]``, required.\n A list containing the nodes that a particular node\n is representing at this iteration in the graph.\n\n Returns\n -------\n Nothing - all variables are modified in place.\n\n "
] |
Please provide a description of the function:def assign_average_value(self) -> None:
for name, parameter in self._parameters:
self._backups[name].copy_(parameter.data)
parameter.data.copy_(self._shadows[name]) | [
"\n Replace all the parameter values with the averages.\n Save the current parameter values to restore later.\n "
] |
Please provide a description of the function:def restore(self) -> None:
for name, parameter in self._parameters:
parameter.data.copy_(self._backups[name]) | [
"\n Restore the backed-up (non-average) parameter values.\n "
] |
Please provide a description of the function:def forward(self, tensor_1: torch.Tensor, tensor_2: torch.Tensor) -> torch.Tensor:
# pylint: disable=arguments-differ
raise NotImplementedError | [
"\n Takes two tensors of the same shape, such as ``(batch_size, length_1, length_2,\n embedding_dim)``. Computes a (possibly parameterized) similarity on the final dimension\n and returns a tensor with one less dimension, such as ``(batch_size, length_1, length_2)``.\n "
] |
Please provide a description of the function:def _prune_beam(states: List[State],
beam_size: int,
sort_states: bool = False) -> List[State]:
states_by_batch_index: Dict[int, List[State]] = defaultdict(list)
for state in states:
assert len(state.batch_indices) == 1
batch_index = state.batch_indices[0]
states_by_batch_index[batch_index].append(state)
pruned_states = []
for _, instance_states in states_by_batch_index.items():
if sort_states:
scores = torch.cat([state.score[0].view(-1) for state in instance_states])
_, sorted_indices = scores.sort(-1, descending=True)
sorted_states = [instance_states[i] for i in sorted_indices.detach().cpu().numpy()]
instance_states = sorted_states
for state in instance_states[:beam_size]:
pruned_states.append(state)
return pruned_states | [
"\n This method can be used to prune the set of unfinished states on a beam or finished states\n at the end of search. In the former case, the states need not be sorted because the all come\n from the same decoding step, which does the sorting. However, if the states are finished and\n this method is called at the end of the search, they need to be sorted because they come\n from different decoding steps.\n "
] |
Please provide a description of the function:def _get_best_final_states(self, finished_states: List[StateType]) -> Dict[int, List[StateType]]:
batch_states: Dict[int, List[StateType]] = defaultdict(list)
for state in finished_states:
batch_states[state.batch_indices[0]].append(state)
best_states: Dict[int, List[StateType]] = {}
for batch_index, states in batch_states.items():
# The time this sort takes is pretty negligible, no particular need to optimize this
# yet. Maybe with a larger beam size...
finished_to_sort = [(-state.score[0].item(), state) for state in states]
finished_to_sort.sort(key=lambda x: x[0])
best_states[batch_index] = [state[1] for state in finished_to_sort[:self._beam_size]]
return best_states | [
"\n Returns the best finished states for each batch instance based on model scores. We return\n at most ``self._max_num_decoded_sequences`` number of sequences per instance.\n "
] |
Please provide a description of the function:def _read_pretrained_embeddings_file(file_uri: str,
embedding_dim: int,
vocab: Vocabulary,
namespace: str = "tokens") -> torch.FloatTensor:
file_ext = get_file_extension(file_uri)
if file_ext in ['.h5', '.hdf5']:
return _read_embeddings_from_hdf5(file_uri,
embedding_dim,
vocab, namespace)
return _read_embeddings_from_text_file(file_uri,
embedding_dim,
vocab, namespace) | [
"\n Returns and embedding matrix for the given vocabulary using the pretrained embeddings\n contained in the given file. Embeddings for tokens not found in the pretrained embedding file\n are randomly initialized using a normal distribution with mean and standard deviation equal to\n those of the pretrained embeddings.\n\n We support two file formats:\n\n * text format - utf-8 encoded text file with space separated fields: [word] [dim 1] [dim 2] ...\n The text file can eventually be compressed, and even resides in an archive with multiple files.\n If the file resides in an archive with other files, then ``embeddings_filename`` must\n be a URI \"(archive_uri)#file_path_inside_the_archive\"\n\n * hdf5 format - hdf5 file containing an embedding matrix in the form of a torch.Tensor.\n\n If the filename ends with '.hdf5' or '.h5' then we load from hdf5, otherwise we assume\n text format.\n\n Parameters\n ----------\n file_uri : str, required.\n It can be:\n\n * a file system path or a URL of an eventually compressed text file or a zip/tar archive\n containing a single file.\n\n * URI of the type ``(archive_path_or_url)#file_path_inside_archive`` if the text file\n is contained in a multi-file archive.\n\n vocab : Vocabulary, required.\n A Vocabulary object.\n namespace : str, (optional, default=tokens)\n The namespace of the vocabulary to find pretrained embeddings for.\n trainable : bool, (optional, default=True)\n Whether or not the embedding parameters should be optimized.\n\n Returns\n -------\n A weight matrix with embeddings initialized from the read file. The matrix has shape\n ``(vocab.get_vocab_size(namespace), embedding_dim)``, where the indices of words appearing in\n the pretrained embedding file are initialized to the pretrained embedding value.\n "
] |
Please provide a description of the function:def _read_embeddings_from_text_file(file_uri: str,
embedding_dim: int,
vocab: Vocabulary,
namespace: str = "tokens") -> torch.FloatTensor:
tokens_to_keep = set(vocab.get_index_to_token_vocabulary(namespace).values())
vocab_size = vocab.get_vocab_size(namespace)
embeddings = {}
# First we read the embeddings from the file, only keeping vectors for the words we need.
logger.info("Reading pretrained embeddings from file")
with EmbeddingsTextFile(file_uri) as embeddings_file:
for line in Tqdm.tqdm(embeddings_file):
token = line.split(' ', 1)[0]
if token in tokens_to_keep:
fields = line.rstrip().split(' ')
if len(fields) - 1 != embedding_dim:
# Sometimes there are funny unicode parsing problems that lead to different
# fields lengths (e.g., a word with a unicode space character that splits
# into more than one column). We skip those lines. Note that if you have
# some kind of long header, this could result in all of your lines getting
# skipped. It's hard to check for that here; you just have to look in the
# embedding_misses_file and at the model summary to make sure things look
# like they are supposed to.
logger.warning("Found line with wrong number of dimensions (expected: %d; actual: %d): %s",
embedding_dim, len(fields) - 1, line)
continue
vector = numpy.asarray(fields[1:], dtype='float32')
embeddings[token] = vector
if not embeddings:
raise ConfigurationError("No embeddings of correct dimension found; you probably "
"misspecified your embedding_dim parameter, or didn't "
"pre-populate your Vocabulary")
all_embeddings = numpy.asarray(list(embeddings.values()))
embeddings_mean = float(numpy.mean(all_embeddings))
embeddings_std = float(numpy.std(all_embeddings))
# Now we initialize the weight matrix for an embedding layer, starting with random vectors,
# then filling in the word vectors we just read.
logger.info("Initializing pre-trained embedding layer")
embedding_matrix = torch.FloatTensor(vocab_size, embedding_dim).normal_(embeddings_mean,
embeddings_std)
num_tokens_found = 0
index_to_token = vocab.get_index_to_token_vocabulary(namespace)
for i in range(vocab_size):
token = index_to_token[i]
# If we don't have a pre-trained vector for this word, we'll just leave this row alone,
# so the word has a random initialization.
if token in embeddings:
embedding_matrix[i] = torch.FloatTensor(embeddings[token])
num_tokens_found += 1
else:
logger.debug("Token %s was not found in the embedding file. Initialising randomly.", token)
logger.info("Pretrained embeddings were found for %d out of %d tokens",
num_tokens_found, vocab_size)
return embedding_matrix | [
"\n Read pre-trained word vectors from an eventually compressed text file, possibly contained\n inside an archive with multiple files. The text file is assumed to be utf-8 encoded with\n space-separated fields: [word] [dim 1] [dim 2] ...\n\n Lines that contain more numerical tokens than ``embedding_dim`` raise a warning and are skipped.\n\n The remainder of the docstring is identical to ``_read_pretrained_embeddings_file``.\n "
] |
Please provide a description of the function:def _read_embeddings_from_hdf5(embeddings_filename: str,
embedding_dim: int,
vocab: Vocabulary,
namespace: str = "tokens") -> torch.FloatTensor:
with h5py.File(embeddings_filename, 'r') as fin:
embeddings = fin['embedding'][...]
if list(embeddings.shape) != [vocab.get_vocab_size(namespace), embedding_dim]:
raise ConfigurationError(
"Read shape {0} embeddings from the file, but expected {1}".format(
list(embeddings.shape), [vocab.get_vocab_size(namespace), embedding_dim]))
return torch.FloatTensor(embeddings) | [
"\n Reads from a hdf5 formatted file. The embedding matrix is assumed to\n be keyed by 'embedding' and of size ``(num_tokens, embedding_dim)``.\n "
] |
Please provide a description of the function:def _get_num_tokens_from_first_line(line: str) -> Optional[int]:
fields = line.split(' ')
if 1 <= len(fields) <= 2:
try:
int_fields = [int(x) for x in fields]
except ValueError:
return None
else:
num_tokens = max(int_fields)
logger.info('Recognized a header line in the embedding file with number of tokens: %d',
num_tokens)
return num_tokens
return None | [
" This function takes in input a string and if it contains 1 or 2 integers, it assumes the\n largest one it the number of tokens. Returns None if the line doesn't match that pattern. "
] |
Please provide a description of the function:def _get_predicted_embedding_addition(self,
checklist_state: ChecklistStatelet,
action_ids: List[int],
action_embeddings: torch.Tensor) -> torch.Tensor:
# Our basic approach here will be to figure out which actions we want to bias, by doing
# some fancy indexing work, then multiply the action embeddings by a mask for those
# actions, and return the sum of the result.
# Shape: (num_terminal_actions, 1). This is 1 if we still want to predict something on the
# checklist, and 0 otherwise.
checklist_balance = checklist_state.get_balance().clamp(min=0)
# (num_terminal_actions, 1)
actions_in_agenda = checklist_state.terminal_actions
# (1, num_current_actions)
action_id_tensor = checklist_balance.new(action_ids).long().unsqueeze(0)
# Shape: (num_terminal_actions, num_current_actions). Will have a value of 1 if the
# terminal action i is our current action j, and a value of 0 otherwise. Because both sets
# of actions are free of duplicates, there will be at most one non-zero value per current
# action, and per terminal action.
current_agenda_actions = (actions_in_agenda == action_id_tensor).float()
# Shape: (num_current_actions,). With the inner multiplication, we remove any current
# agenda actions that are not in our checklist balance, then we sum over the terminal
# action dimension, which will have a sum of at most one. So this will be a 0/1 tensor,
# where a 1 means to encourage the current action in that position.
actions_to_encourage = torch.sum(current_agenda_actions * checklist_balance, dim=0)
# Shape: (action_embedding_dim,). This is the sum of the action embeddings that we want
# the model to prefer.
embedding_addition = torch.sum(action_embeddings * actions_to_encourage.unsqueeze(1),
dim=0,
keepdim=False)
if self._add_action_bias:
# If we're adding an action bias, the last dimension of the action embedding is a bias
# weight. We don't want this addition to affect the bias (TODO(mattg): or do we?), so
# we zero out that dimension here.
embedding_addition[-1] = 0
return embedding_addition | [
"\n Gets the embeddings of desired terminal actions yet to be produced by the decoder, and\n returns their sum for the decoder to add it to the predicted embedding to bias the\n prediction towards missing actions.\n "
] |
Please provide a description of the function:def _create_tensor_dicts(input_queue: Queue,
output_queue: Queue,
iterator: DataIterator,
shuffle: bool,
index: int) -> None:
def instances() -> Iterator[Instance]:
instance = input_queue.get()
while instance is not None:
yield instance
instance = input_queue.get()
for tensor_dict in iterator(instances(), num_epochs=1, shuffle=shuffle):
output_queue.put(tensor_dict)
output_queue.put(index) | [
"\n Pulls at most ``max_instances_in_memory`` from the input_queue,\n groups them into batches of size ``batch_size``, converts them\n to ``TensorDict`` s, and puts them on the ``output_queue``.\n "
] |
Please provide a description of the function:def _queuer(instances: Iterable[Instance],
input_queue: Queue,
num_workers: int,
num_epochs: Optional[int]) -> None:
epoch = 0
while num_epochs is None or epoch < num_epochs:
epoch += 1
for instance in instances:
input_queue.put(instance)
# Now put a None for each worker, since each needs to receive one
# to know that it's done.
for _ in range(num_workers):
input_queue.put(None) | [
"\n Reads Instances from the iterable and puts them in the input_queue.\n "
] |
Please provide a description of the function:def get_valid_actions(self) -> List[Dict[str, Tuple[torch.Tensor, torch.Tensor, List[int]]]]:
return [state.get_valid_actions() for state in self.grammar_state] | [
"\n Returns a list of valid actions for each element of the group.\n "
] |
Please provide a description of the function:def _worker(reader: DatasetReader,
input_queue: Queue,
output_queue: Queue,
index: int) -> None:
# Keep going until you get a file_path that's None.
while True:
file_path = input_queue.get()
if file_path is None:
# Put my index on the queue to signify that I'm finished
output_queue.put(index)
break
logger.info(f"reading instances from {file_path}")
for instance in reader.read(file_path):
output_queue.put(instance) | [
"\n A worker that pulls filenames off the input queue, uses the dataset reader\n to read them, and places the generated instances on the output queue.\n When there are no filenames left on the input queue, it puts its ``index``\n on the output queue and doesn't do anything else.\n "
] |
Please provide a description of the function:def allowed_transitions(constraint_type: str, labels: Dict[int, str]) -> List[Tuple[int, int]]:
num_labels = len(labels)
start_tag = num_labels
end_tag = num_labels + 1
labels_with_boundaries = list(labels.items()) + [(start_tag, "START"), (end_tag, "END")]
allowed = []
for from_label_index, from_label in labels_with_boundaries:
if from_label in ("START", "END"):
from_tag = from_label
from_entity = ""
else:
from_tag = from_label[0]
from_entity = from_label[1:]
for to_label_index, to_label in labels_with_boundaries:
if to_label in ("START", "END"):
to_tag = to_label
to_entity = ""
else:
to_tag = to_label[0]
to_entity = to_label[1:]
if is_transition_allowed(constraint_type, from_tag, from_entity,
to_tag, to_entity):
allowed.append((from_label_index, to_label_index))
return allowed | [
"\n Given labels and a constraint type, returns the allowed transitions. It will\n additionally include transitions for the start and end states, which are used\n by the conditional random field.\n\n Parameters\n ----------\n constraint_type : ``str``, required\n Indicates which constraint to apply. Current choices are\n \"BIO\", \"IOB1\", \"BIOUL\", and \"BMES\".\n labels : ``Dict[int, str]``, required\n A mapping {label_id -> label}. Most commonly this would be the value from\n Vocabulary.get_index_to_token_vocabulary()\n\n Returns\n -------\n ``List[Tuple[int, int]]``\n The allowed transitions (from_label_id, to_label_id).\n "
] |
Please provide a description of the function:def is_transition_allowed(constraint_type: str,
from_tag: str,
from_entity: str,
to_tag: str,
to_entity: str):
# pylint: disable=too-many-return-statements
if to_tag == "START" or from_tag == "END":
# Cannot transition into START or from END
return False
if constraint_type == "BIOUL":
if from_tag == "START":
return to_tag in ('O', 'B', 'U')
if to_tag == "END":
return from_tag in ('O', 'L', 'U')
return any([
# O can transition to O, B-* or U-*
# L-x can transition to O, B-*, or U-*
# U-x can transition to O, B-*, or U-*
from_tag in ('O', 'L', 'U') and to_tag in ('O', 'B', 'U'),
# B-x can only transition to I-x or L-x
# I-x can only transition to I-x or L-x
from_tag in ('B', 'I') and to_tag in ('I', 'L') and from_entity == to_entity
])
elif constraint_type == "BIO":
if from_tag == "START":
return to_tag in ('O', 'B')
if to_tag == "END":
return from_tag in ('O', 'B', 'I')
return any([
# Can always transition to O or B-x
to_tag in ('O', 'B'),
# Can only transition to I-x from B-x or I-x
to_tag == 'I' and from_tag in ('B', 'I') and from_entity == to_entity
])
elif constraint_type == "IOB1":
if from_tag == "START":
return to_tag in ('O', 'I')
if to_tag == "END":
return from_tag in ('O', 'B', 'I')
return any([
# Can always transition to O or I-x
to_tag in ('O', 'I'),
# Can only transition to B-x from B-x or I-x, where
# x is the same tag.
to_tag == 'B' and from_tag in ('B', 'I') and from_entity == to_entity
])
elif constraint_type == "BMES":
if from_tag == "START":
return to_tag in ('B', 'S')
if to_tag == "END":
return from_tag in ('E', 'S')
return any([
# Can only transition to B or S from E or S.
to_tag in ('B', 'S') and from_tag in ('E', 'S'),
# Can only transition to M-x from B-x, where
# x is the same tag.
to_tag == 'M' and from_tag in ('B', 'M') and from_entity == to_entity,
# Can only transition to E-x from B-x or M-x, where
# x is the same tag.
to_tag == 'E' and from_tag in ('B', 'M') and from_entity == to_entity,
])
else:
raise ConfigurationError(f"Unknown constraint type: {constraint_type}") | [
"\n Given a constraint type and strings ``from_tag`` and ``to_tag`` that\n represent the origin and destination of the transition, return whether\n the transition is allowed under the given constraint type.\n\n Parameters\n ----------\n constraint_type : ``str``, required\n Indicates which constraint to apply. Current choices are\n \"BIO\", \"IOB1\", \"BIOUL\", and \"BMES\".\n from_tag : ``str``, required\n The tag that the transition originates from. For example, if the\n label is ``I-PER``, the ``from_tag`` is ``I``.\n from_entity: ``str``, required\n The entity corresponding to the ``from_tag``. For example, if the\n label is ``I-PER``, the ``from_entity`` is ``PER``.\n to_tag : ``str``, required\n The tag that the transition leads to. For example, if the\n label is ``I-PER``, the ``to_tag`` is ``I``.\n to_entity: ``str``, required\n The entity corresponding to the ``to_tag``. For example, if the\n label is ``I-PER``, the ``to_entity`` is ``PER``.\n\n Returns\n -------\n ``bool``\n Whether the transition is allowed under the given ``constraint_type``.\n "
] |
Please provide a description of the function:def _input_likelihood(self, logits: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
batch_size, sequence_length, num_tags = logits.size()
# Transpose batch size and sequence dimensions
mask = mask.float().transpose(0, 1).contiguous()
logits = logits.transpose(0, 1).contiguous()
# Initial alpha is the (batch_size, num_tags) tensor of likelihoods combining the
# transitions to the initial states and the logits for the first timestep.
if self.include_start_end_transitions:
alpha = self.start_transitions.view(1, num_tags) + logits[0]
else:
alpha = logits[0]
# For each i we compute logits for the transitions from timestep i-1 to timestep i.
# We do so in a (batch_size, num_tags, num_tags) tensor where the axes are
# (instance, current_tag, next_tag)
for i in range(1, sequence_length):
# The emit scores are for time i ("next_tag") so we broadcast along the current_tag axis.
emit_scores = logits[i].view(batch_size, 1, num_tags)
# Transition scores are (current_tag, next_tag) so we broadcast along the instance axis.
transition_scores = self.transitions.view(1, num_tags, num_tags)
# Alpha is for the current_tag, so we broadcast along the next_tag axis.
broadcast_alpha = alpha.view(batch_size, num_tags, 1)
# Add all the scores together and logexp over the current_tag axis
inner = broadcast_alpha + emit_scores + transition_scores
# In valid positions (mask == 1) we want to take the logsumexp over the current_tag dimension
# of ``inner``. Otherwise (mask == 0) we want to retain the previous alpha.
alpha = (util.logsumexp(inner, 1) * mask[i].view(batch_size, 1) +
alpha * (1 - mask[i]).view(batch_size, 1))
# Every sequence needs to end with a transition to the stop_tag.
if self.include_start_end_transitions:
stops = alpha + self.end_transitions.view(1, num_tags)
else:
stops = alpha
# Finally we log_sum_exp along the num_tags dim, result is (batch_size,)
return util.logsumexp(stops) | [
"\n Computes the (batch_size,) denominator term for the log-likelihood, which is the\n sum of the likelihoods across all possible state sequences.\n "
] |
Please provide a description of the function:def _joint_likelihood(self,
logits: torch.Tensor,
tags: torch.Tensor,
mask: torch.LongTensor) -> torch.Tensor:
batch_size, sequence_length, _ = logits.data.shape
# Transpose batch size and sequence dimensions:
logits = logits.transpose(0, 1).contiguous()
mask = mask.float().transpose(0, 1).contiguous()
tags = tags.transpose(0, 1).contiguous()
# Start with the transition scores from start_tag to the first tag in each input
if self.include_start_end_transitions:
score = self.start_transitions.index_select(0, tags[0])
else:
score = 0.0
# Add up the scores for the observed transitions and all the inputs but the last
for i in range(sequence_length - 1):
# Each is shape (batch_size,)
current_tag, next_tag = tags[i], tags[i+1]
# The scores for transitioning from current_tag to next_tag
transition_score = self.transitions[current_tag.view(-1), next_tag.view(-1)]
# The score for using current_tag
emit_score = logits[i].gather(1, current_tag.view(batch_size, 1)).squeeze(1)
# Include transition score if next element is unmasked,
# input_score if this element is unmasked.
score = score + transition_score * mask[i + 1] + emit_score * mask[i]
# Transition from last state to "stop" state. To start with, we need to find the last tag
# for each instance.
last_tag_index = mask.sum(0).long() - 1
last_tags = tags.gather(0, last_tag_index.view(1, batch_size)).squeeze(0)
# Compute score of transitioning to `stop_tag` from each "last tag".
if self.include_start_end_transitions:
last_transition_score = self.end_transitions.index_select(0, last_tags)
else:
last_transition_score = 0.0
# Add the last input if it's not masked.
last_inputs = logits[-1] # (batch_size, num_tags)
last_input_score = last_inputs.gather(1, last_tags.view(-1, 1)) # (batch_size, 1)
last_input_score = last_input_score.squeeze() # (batch_size,)
score = score + last_transition_score + last_input_score * mask[-1]
return score | [
"\n Computes the numerator term for the log-likelihood, which is just score(inputs, tags)\n "
] |
Please provide a description of the function:def forward(self,
inputs: torch.Tensor,
tags: torch.Tensor,
mask: torch.ByteTensor = None) -> torch.Tensor:
# pylint: disable=arguments-differ
if mask is None:
mask = torch.ones(*tags.size(), dtype=torch.long)
log_denominator = self._input_likelihood(inputs, mask)
log_numerator = self._joint_likelihood(inputs, tags, mask)
return torch.sum(log_numerator - log_denominator) | [
"\n Computes the log likelihood.\n "
] |
Please provide a description of the function:def viterbi_tags(self,
logits: torch.Tensor,
mask: torch.Tensor) -> List[Tuple[List[int], float]]:
_, max_seq_length, num_tags = logits.size()
# Get the tensors out of the variables
logits, mask = logits.data, mask.data
# Augment transitions matrix with start and end transitions
start_tag = num_tags
end_tag = num_tags + 1
transitions = torch.Tensor(num_tags + 2, num_tags + 2).fill_(-10000.)
# Apply transition constraints
constrained_transitions = (
self.transitions * self._constraint_mask[:num_tags, :num_tags] +
-10000.0 * (1 - self._constraint_mask[:num_tags, :num_tags])
)
transitions[:num_tags, :num_tags] = constrained_transitions.data
if self.include_start_end_transitions:
transitions[start_tag, :num_tags] = (
self.start_transitions.detach() * self._constraint_mask[start_tag, :num_tags].data +
-10000.0 * (1 - self._constraint_mask[start_tag, :num_tags].detach())
)
transitions[:num_tags, end_tag] = (
self.end_transitions.detach() * self._constraint_mask[:num_tags, end_tag].data +
-10000.0 * (1 - self._constraint_mask[:num_tags, end_tag].detach())
)
else:
transitions[start_tag, :num_tags] = (-10000.0 *
(1 - self._constraint_mask[start_tag, :num_tags].detach()))
transitions[:num_tags, end_tag] = -10000.0 * (1 - self._constraint_mask[:num_tags, end_tag].detach())
best_paths = []
# Pad the max sequence length by 2 to account for start_tag + end_tag.
tag_sequence = torch.Tensor(max_seq_length + 2, num_tags + 2)
for prediction, prediction_mask in zip(logits, mask):
sequence_length = torch.sum(prediction_mask)
# Start with everything totally unlikely
tag_sequence.fill_(-10000.)
# At timestep 0 we must have the START_TAG
tag_sequence[0, start_tag] = 0.
# At steps 1, ..., sequence_length we just use the incoming prediction
tag_sequence[1:(sequence_length + 1), :num_tags] = prediction[:sequence_length]
# And at the last timestep we must have the END_TAG
tag_sequence[sequence_length + 1, end_tag] = 0.
# We pass the tags and the transitions to ``viterbi_decode``.
viterbi_path, viterbi_score = util.viterbi_decode(tag_sequence[:(sequence_length + 2)], transitions)
# Get rid of START and END sentinels and append.
viterbi_path = viterbi_path[1:-1]
best_paths.append((viterbi_path, viterbi_score.item()))
return best_paths | [
"\n Uses viterbi algorithm to find most likely tags for the given inputs.\n If constraints are applied, disallows all other transitions.\n "
] |
Please provide a description of the function:def search(self,
start_predictions: torch.Tensor,
start_state: StateType,
step: StepFunctionType) -> Tuple[torch.Tensor, torch.Tensor]:
batch_size = start_predictions.size()[0]
# List of (batch_size, beam_size) tensors. One for each time step. Does not
# include the start symbols, which are implicit.
predictions: List[torch.Tensor] = []
# List of (batch_size, beam_size) tensors. One for each time step. None for
# the first. Stores the index n for the parent prediction, i.e.
# predictions[t-1][i][n], that it came from.
backpointers: List[torch.Tensor] = []
# Calculate the first timestep. This is done outside the main loop
# because we are going from a single decoder input (the output from the
# encoder) to the top `beam_size` decoder outputs. On the other hand,
# within the main loop we are going from the `beam_size` elements of the
# beam to `beam_size`^2 candidates from which we will select the top
# `beam_size` elements for the next iteration.
# shape: (batch_size, num_classes)
start_class_log_probabilities, state = step(start_predictions, start_state)
num_classes = start_class_log_probabilities.size()[1]
# Make sure `per_node_beam_size` is not larger than `num_classes`.
if self.per_node_beam_size > num_classes:
raise ConfigurationError(f"Target vocab size ({num_classes:d}) too small "
f"relative to per_node_beam_size ({self.per_node_beam_size:d}).\n"
f"Please decrease beam_size or per_node_beam_size.")
# shape: (batch_size, beam_size), (batch_size, beam_size)
start_top_log_probabilities, start_predicted_classes = \
start_class_log_probabilities.topk(self.beam_size)
if self.beam_size == 1 and (start_predicted_classes == self._end_index).all():
warnings.warn("Empty sequences predicted. You may want to increase the beam size or ensure "
"your step function is working properly.",
RuntimeWarning)
return start_predicted_classes.unsqueeze(-1), start_top_log_probabilities
# The log probabilities for the last time step.
# shape: (batch_size, beam_size)
last_log_probabilities = start_top_log_probabilities
# shape: [(batch_size, beam_size)]
predictions.append(start_predicted_classes)
# Log probability tensor that mandates that the end token is selected.
# shape: (batch_size * beam_size, num_classes)
log_probs_after_end = start_class_log_probabilities.new_full(
(batch_size * self.beam_size, num_classes),
float("-inf")
)
log_probs_after_end[:, self._end_index] = 0.
# Set the same state for each element in the beam.
for key, state_tensor in state.items():
_, *last_dims = state_tensor.size()
# shape: (batch_size * beam_size, *)
state[key] = state_tensor.\
unsqueeze(1).\
expand(batch_size, self.beam_size, *last_dims).\
reshape(batch_size * self.beam_size, *last_dims)
for timestep in range(self.max_steps - 1):
# shape: (batch_size * beam_size,)
last_predictions = predictions[-1].reshape(batch_size * self.beam_size)
# If every predicted token from the last step is `self._end_index`,
# then we can stop early.
if (last_predictions == self._end_index).all():
break
# Take a step. This get the predicted log probs of the next classes
# and updates the state.
# shape: (batch_size * beam_size, num_classes)
class_log_probabilities, state = step(last_predictions, state)
# shape: (batch_size * beam_size, num_classes)
last_predictions_expanded = last_predictions.unsqueeze(-1).expand(
batch_size * self.beam_size,
num_classes
)
# Here we are finding any beams where we predicted the end token in
# the previous timestep and replacing the distribution with a
# one-hot distribution, forcing the beam to predict the end token
# this timestep as well.
# shape: (batch_size * beam_size, num_classes)
cleaned_log_probabilities = torch.where(
last_predictions_expanded == self._end_index,
log_probs_after_end,
class_log_probabilities
)
# shape (both): (batch_size * beam_size, per_node_beam_size)
top_log_probabilities, predicted_classes = \
cleaned_log_probabilities.topk(self.per_node_beam_size)
# Here we expand the last log probabilities to (batch_size * beam_size, per_node_beam_size)
# so that we can add them to the current log probs for this timestep.
# This lets us maintain the log probability of each element on the beam.
# shape: (batch_size * beam_size, per_node_beam_size)
expanded_last_log_probabilities = last_log_probabilities.\
unsqueeze(2).\
expand(batch_size, self.beam_size, self.per_node_beam_size).\
reshape(batch_size * self.beam_size, self.per_node_beam_size)
# shape: (batch_size * beam_size, per_node_beam_size)
summed_top_log_probabilities = top_log_probabilities + expanded_last_log_probabilities
# shape: (batch_size, beam_size * per_node_beam_size)
reshaped_summed = summed_top_log_probabilities.\
reshape(batch_size, self.beam_size * self.per_node_beam_size)
# shape: (batch_size, beam_size * per_node_beam_size)
reshaped_predicted_classes = predicted_classes.\
reshape(batch_size, self.beam_size * self.per_node_beam_size)
# Keep only the top `beam_size` beam indices.
# shape: (batch_size, beam_size), (batch_size, beam_size)
restricted_beam_log_probs, restricted_beam_indices = reshaped_summed.topk(self.beam_size)
# Use the beam indices to extract the corresponding classes.
# shape: (batch_size, beam_size)
restricted_predicted_classes = reshaped_predicted_classes.gather(1, restricted_beam_indices)
predictions.append(restricted_predicted_classes)
# shape: (batch_size, beam_size)
last_log_probabilities = restricted_beam_log_probs
# The beam indices come from a `beam_size * per_node_beam_size` dimension where the
# indices with a common ancestor are grouped together. Hence
# dividing by per_node_beam_size gives the ancestor. (Note that this is integer
# division as the tensor is a LongTensor.)
# shape: (batch_size, beam_size)
backpointer = restricted_beam_indices / self.per_node_beam_size
backpointers.append(backpointer)
# Keep only the pieces of the state tensors corresponding to the
# ancestors created this iteration.
for key, state_tensor in state.items():
_, *last_dims = state_tensor.size()
# shape: (batch_size, beam_size, *)
expanded_backpointer = backpointer.\
view(batch_size, self.beam_size, *([1] * len(last_dims))).\
expand(batch_size, self.beam_size, *last_dims)
# shape: (batch_size * beam_size, *)
state[key] = state_tensor.\
reshape(batch_size, self.beam_size, *last_dims).\
gather(1, expanded_backpointer).\
reshape(batch_size * self.beam_size, *last_dims)
if not torch.isfinite(last_log_probabilities).all():
warnings.warn("Infinite log probabilities encountered. Some final sequences may not make sense. "
"This can happen when the beam size is larger than the number of valid (non-zero "
"probability) transitions that the step function produces.",
RuntimeWarning)
# Reconstruct the sequences.
# shape: [(batch_size, beam_size, 1)]
reconstructed_predictions = [predictions[-1].unsqueeze(2)]
# shape: (batch_size, beam_size)
cur_backpointers = backpointers[-1]
for timestep in range(len(predictions) - 2, 0, -1):
# shape: (batch_size, beam_size, 1)
cur_preds = predictions[timestep].gather(1, cur_backpointers).unsqueeze(2)
reconstructed_predictions.append(cur_preds)
# shape: (batch_size, beam_size)
cur_backpointers = backpointers[timestep - 1].gather(1, cur_backpointers)
# shape: (batch_size, beam_size, 1)
final_preds = predictions[0].gather(1, cur_backpointers).unsqueeze(2)
reconstructed_predictions.append(final_preds)
# shape: (batch_size, beam_size, max_steps)
all_predictions = torch.cat(list(reversed(reconstructed_predictions)), 2)
return all_predictions, last_log_probabilities | [
"\n Given a starting state and a step function, apply beam search to find the\n most likely target sequences.\n\n Notes\n -----\n If your step function returns ``-inf`` for some log probabilities\n (like if you're using a masked log-softmax) then some of the \"best\"\n sequences returned may also have ``-inf`` log probability. Specifically\n this happens when the beam size is smaller than the number of actions\n with finite log probability (non-zero probability) returned by the step function.\n Therefore if you're using a mask you may want to check the results from ``search``\n and potentially discard sequences with non-finite log probability.\n\n Parameters\n ----------\n start_predictions : ``torch.Tensor``\n A tensor containing the initial predictions with shape ``(batch_size,)``.\n Usually the initial predictions are just the index of the \"start\" token\n in the target vocabulary.\n start_state : ``StateType``\n The initial state passed to the ``step`` function. Each value of the state dict\n should be a tensor of shape ``(batch_size, *)``, where ``*`` means any other\n number of dimensions.\n step : ``StepFunctionType``\n A function that is responsible for computing the next most likely tokens,\n given the current state and the predictions from the last time step.\n The function should accept two arguments. The first being a tensor\n of shape ``(group_size,)``, representing the index of the predicted\n tokens from the last time step, and the second being the current state.\n The ``group_size`` will be ``batch_size * beam_size``, except in the initial\n step, for which it will just be ``batch_size``.\n The function is expected to return a tuple, where the first element\n is a tensor of shape ``(group_size, target_vocab_size)`` containing\n the log probabilities of the tokens for the next step, and the second\n element is the updated state. The tensor in the state should have shape\n ``(group_size, *)``, where ``*`` means any other number of dimensions.\n\n Returns\n -------\n Tuple[torch.Tensor, torch.Tensor]\n Tuple of ``(predictions, log_probabilities)``, where ``predictions``\n has shape ``(batch_size, beam_size, max_steps)`` and ``log_probabilities``\n has shape ``(batch_size, beam_size)``.\n "
] |
Please provide a description of the function:def main(data_directory: int, dataset: str = None, filter_by: str = None, verbose: bool = False) -> None:
directory_dict = {path: files for path, names, files in os.walk(data_directory) if files}
for directory, data_files in directory_dict.items():
if "query_split" in directory or (dataset is not None and dataset not in directory):
continue
print(f"Parsing dataset at {directory}")
parsed = 0
total_non_aliases = 0
total_as_count = 0
total_queries_with_weird_as = 0
total = 0
for json_file in data_files:
print(f"\tParsing split at {json_file}")
file_path = os.path.join(directory, json_file)
num_parsed, num_queries, filtered_errors, non_basic_as_aliases, as_count, queries_with_weird_as = parse_dataset(file_path, filter_by, verbose)
parsed += num_parsed
total += num_queries
total_non_aliases += non_basic_as_aliases
total_as_count += as_count
total_queries_with_weird_as += queries_with_weird_as
print(f"\tParsed {parsed} out of {total} queries, coverage {parsed/total}")
print(f"\tFound {total_non_aliases} out of {total_as_count} non simple AS aliases. percentage: {total_non_aliases/total_as_count}")
print(f"\tFound {total_queries_with_weird_as} out of {total} queries with > 1 weird AS. percentage: {total_queries_with_weird_as/total}")
if filter_by is not None:
print(f"\tOf {total - parsed} errors, {filtered_errors/ (total - parsed + 1e-13)} contain {filter_by}") | [
"\n Parameters\n ----------\n data_directory : str, required.\n The path to the data directory of https://github.com/jkkummerfeld/text2sql-data\n which has been preprocessed using scripts/reformat_text2sql_data.py.\n dataset : str, optional.\n The dataset to parse. By default all are parsed.\n filter_by : str, optional\n Compute statistics about a particular error and only print errors which don't contain this string.\n verbose : bool, optional.\n Whether to print information about incorrectly parsed SQL.\n "
] |
Please provide a description of the function:def takes_arg(obj, arg: str) -> bool:
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 arg in signature.parameters | [
"\n Checks whether the provided obj takes a certain arg.\n If it's a class, we're really checking whether its constructor does.\n If it's a function or method, we're checking the object itself.\n Otherwise, we raise an error.\n "
] |
Please provide a description of the function:def takes_kwargs(obj) -> bool:
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 bool(any([p.kind == inspect.Parameter.VAR_KEYWORD # type: ignore
for p in signature.parameters.values()])) | [
"\n Checks whether a provided object takes in any positional arguments.\n Similar to takes_arg, we do this for both the __init__ function of\n the class or a function / method\n Otherwise, we raise an error\n "
] |
Please provide a description of the function:def remove_optional(annotation: type):
origin = getattr(annotation, '__origin__', None)
args = getattr(annotation, '__args__', ())
if origin == Union and len(args) == 2 and args[1] == type(None):
return args[0]
else:
return annotation | [
"\n Optional[X] annotations are actually represented as Union[X, NoneType].\n For our purposes, the \"Optional\" part is not interesting, so here we\n throw it away.\n "
] |
Please provide a description of the function:def create_kwargs(cls: Type[T], params: Params, **extras) -> Dict[str, Any]:
# Get the signature of the constructor.
signature = inspect.signature(cls.__init__)
kwargs: Dict[str, Any] = {}
# Iterate over all the constructor parameters and their annotations.
for name, param in signature.parameters.items():
# Skip "self". You're not *required* to call the first parameter "self",
# so in theory this logic is fragile, but if you don't call the self parameter
# "self" you kind of deserve what happens.
if name == "self":
continue
# If the annotation is a compound type like typing.Dict[str, int],
# it will have an __origin__ field indicating `typing.Dict`
# and an __args__ field indicating `(str, int)`. We capture both.
annotation = remove_optional(param.annotation)
kwargs[name] = construct_arg(cls, name, annotation, param.default, params, **extras)
params.assert_empty(cls.__name__)
return kwargs | [
"\n Given some class, a `Params` object, and potentially other keyword arguments,\n create a dict of keyword args suitable for passing to the class's constructor.\n\n The function does this by finding the class's constructor, matching the constructor\n arguments to entries in the `params` object, and instantiating values for the parameters\n using the type annotation and possibly a from_params method.\n\n Any values that are provided in the `extras` will just be used as is.\n For instance, you might provide an existing `Vocabulary` this way.\n "
] |
Please provide a description of the function:def create_extras(cls: Type[T],
extras: Dict[str, Any]) -> Dict[str, Any]:
subextras: Dict[str, Any] = {}
if hasattr(cls, "from_params"):
from_params_method = cls.from_params # type: ignore
else:
# In some rare cases, we get a registered subclass that does _not_ have a
# from_params method (this happens with Activations, for instance, where we
# register pytorch modules directly). This is a bit of a hack to make those work,
# instead of adding a `from_params` method for them somehow. Then the extras
# in the class constructor are what we are looking for, to pass on.
from_params_method = cls
if takes_kwargs(from_params_method):
# If annotation.params accepts **kwargs, we need to pass them all along.
# For example, `BasicTextFieldEmbedder.from_params` requires a Vocabulary
# object, but `TextFieldEmbedder.from_params` does not.
subextras = extras
else:
# Otherwise, only supply the ones that are actual args; any additional ones
# will cause a TypeError.
subextras = {k: v for k, v in extras.items()
if takes_arg(from_params_method, k)}
return subextras | [
"\n Given a dictionary of extra arguments, returns a dictionary of\n kwargs that actually are a part of the signature of the cls.from_params\n (or cls) method.\n "
] |
Please provide a description of the function:def construct_arg(cls: Type[T], # pylint: disable=inconsistent-return-statements,too-many-return-statements
param_name: str,
annotation: Type,
default: Any,
params: Params,
**extras) -> Any:
from allennlp.models.archival import load_archive # import here to avoid circular imports
# We used `param_name` as the method argument to avoid conflicts with 'name' being a key in
# `extras`, which isn't _that_ unlikely. Now that we are inside the method, we can switch back
# to using `name`.
name = param_name
origin = getattr(annotation, '__origin__', None)
args = getattr(annotation, '__args__', [])
# The parameter is optional if its default value is not the "no default" sentinel.
optional = default != _NO_DEFAULT
# Some constructors expect extra non-parameter items, e.g. vocab: Vocabulary.
# We check the provided `extras` for these and just use them if they exist.
if name in extras:
return extras[name]
# Next case is when argument should be loaded from pretrained archive.
elif name in params and isinstance(params.get(name), Params) and "_pretrained" in params.get(name):
load_module_params = params.pop(name).pop("_pretrained")
archive_file = load_module_params.pop("archive_file")
module_path = load_module_params.pop("module_path")
freeze = load_module_params.pop("freeze", True)
archive = load_archive(archive_file)
result = archive.extract_module(module_path, freeze) # pylint: disable=no-member
if not isinstance(result, annotation):
raise ConfigurationError(f"The module from model at {archive_file} at path {module_path} "
f"was expected of type {annotation} but is of type {type(result)}")
return result
# The next case is when the parameter type is itself constructible from_params.
elif hasattr(annotation, 'from_params'):
if name in params:
# Our params have an entry for this, so we use that.
subparams = params.pop(name)
subextras = create_extras(annotation, extras)
# In some cases we allow a string instead of a param dict, so
# we need to handle that case separately.
if isinstance(subparams, str):
return annotation.by_name(subparams)()
else:
return annotation.from_params(params=subparams, **subextras)
elif not optional:
# Not optional and not supplied, that's an error!
raise ConfigurationError(f"expected key {name} for {cls.__name__}")
else:
return default
# If the parameter type is a Python primitive, just pop it off
# using the correct casting pop_xyz operation.
elif annotation == str:
return params.pop(name, default) if optional else params.pop(name)
elif annotation == int:
return params.pop_int(name, default) if optional else params.pop_int(name)
elif annotation == bool:
return params.pop_bool(name, default) if optional else params.pop_bool(name)
elif annotation == float:
return params.pop_float(name, default) if optional else params.pop_float(name)
# This is special logic for handling types like Dict[str, TokenIndexer],
# List[TokenIndexer], Tuple[TokenIndexer, Tokenizer], and Set[TokenIndexer],
# which it creates by instantiating each value from_params and returning the resulting structure.
elif origin in (Dict, dict) and len(args) == 2 and hasattr(args[-1], 'from_params'):
value_cls = annotation.__args__[-1]
value_dict = {}
for key, value_params in params.pop(name, Params({})).items():
subextras = create_extras(value_cls, extras)
value_dict[key] = value_cls.from_params(params=value_params, **subextras)
return value_dict
elif origin in (List, list) and len(args) == 1 and hasattr(args[0], 'from_params'):
value_cls = annotation.__args__[0]
value_list = []
for value_params in params.pop(name, Params({})):
subextras = create_extras(value_cls, extras)
value_list.append(value_cls.from_params(params=value_params, **subextras))
return value_list
elif origin in (Tuple, tuple) and all(hasattr(arg, 'from_params') for arg in args):
value_list = []
for value_cls, value_params in zip(annotation.__args__, params.pop(name, Params({}))):
subextras = create_extras(value_cls, extras)
value_list.append(value_cls.from_params(params=value_params, **subextras))
return tuple(value_list)
elif origin in (Set, set) and len(args) == 1 and hasattr(args[0], 'from_params'):
value_cls = annotation.__args__[0]
value_set = set()
for value_params in params.pop(name, Params({})):
subextras = create_extras(value_cls, extras)
value_set.add(value_cls.from_params(params=value_params, **subextras))
return value_set
elif origin == Union:
# Storing this so we can recover it later if we need to.
param_value = params.get(name, Params({}))
if isinstance(param_value, Params):
param_value = param_value.duplicate()
# We'll try each of the given types in the union sequentially, returning the first one that
# succeeds.
for arg in args:
try:
return construct_arg(cls, name, arg, default, params, **extras)
except (ValueError, TypeError, ConfigurationError, AttributeError):
# Our attempt to construct the argument may have popped `params[name]`, so we
# restore it here.
params[name] = param_value
if isinstance(param_value, Params):
param_value = param_value.duplicate()
continue
# If none of them succeeded, we crash.
raise ConfigurationError(f"Failed to construct argument {name} with type {annotation}")
else:
# Pass it on as is and hope for the best. ¯\_(ツ)_/¯
if optional:
return params.pop(name, default)
else:
return params.pop(name) | [
"\n Does the work of actually constructing an individual argument for :func:`create_kwargs`.\n\n Here we're in the inner loop of iterating over the parameters to a particular constructor,\n trying to construct just one of them. The information we get for that parameter is its name,\n its type annotation, and its default value; we also get the full set of ``Params`` for\n constructing the object (which we may mutate), and any ``extras`` that the constructor might\n need.\n\n We take the type annotation and default value here separately, instead of using an\n ``inspect.Parameter`` object directly, so that we can handle ``Union`` types using recursion on\n this method, trying the different annotation types in the union in turn.\n "
] |
Please provide a description of the function:def from_params(cls: Type[T], params: Params, **extras) -> T:
# pylint: disable=protected-access
from allennlp.common.registrable import Registrable # import here to avoid circular imports
logger.info(f"instantiating class {cls} from params {getattr(params, 'params', params)} "
f"and extras {set(extras.keys())}")
if params is None:
return None
if isinstance(params, str):
params = Params({"type": params})
registered_subclasses = Registrable._registry.get(cls)
if registered_subclasses is not None:
# We know ``cls`` inherits from Registrable, so we'll use a cast to make mypy happy.
# We have to use a disable to make pylint happy.
# pylint: disable=no-member
as_registrable = cast(Type[Registrable], cls)
default_to_first_choice = as_registrable.default_implementation is not None
choice = params.pop_choice("type",
choices=as_registrable.list_available(),
default_to_first_choice=default_to_first_choice)
subclass = registered_subclasses[choice]
if hasattr(subclass, 'from_params'):
# We want to call subclass.from_params
extras = create_extras(subclass, extras)
return subclass.from_params(params=params, **extras)
else:
# In some rare cases, we get a registered subclass that does _not_ have a
# from_params method (this happens with Activations, for instance, where we
# register pytorch modules directly). This is a bit of a hack to make those work,
# instead of adding a `from_params` method for them somehow. We just trust that
# you've done the right thing in passing your parameters, and nothing else needs to
# be recursively constructed.
extras = create_extras(subclass, extras)
constructor_args = {**params, **extras}
return subclass(**constructor_args)
else:
# This is not a base class, so convert our params and extras into a dict of kwargs.
if cls.__init__ == object.__init__:
# This class does not have an explicit constructor, so don't give it any kwargs.
# Without this logic, create_kwargs will look at object.__init__ and see that
# it takes *args and **kwargs and look for those.
kwargs: Dict[str, Any] = {}
else:
# This class has a constructor, so create kwargs for it.
kwargs = create_kwargs(cls, params, **extras)
return cls(**kwargs) | [
"\n This is the automatic implementation of `from_params`. Any class that subclasses `FromParams`\n (or `Registrable`, which itself subclasses `FromParams`) gets this implementation for free.\n If you want your class to be instantiated from params in the \"obvious\" way -- pop off parameters\n and hand them to your constructor with the same names -- this provides that functionality.\n\n If you need more complex logic in your from `from_params` method, you'll have to implement\n your own method that overrides this one.\n "
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.