Code
stringlengths 103
85.9k
| Summary
sequencelengths 0
94
|
---|---|
Please provide a description of the function:def peak_memory_mb() -> float:
if resource is None or sys.platform not in ('linux', 'darwin'):
return 0.0
# TODO(joelgrus): For whatever, our pinned version 0.521 of mypy does not like
# next line, but later versions (e.g. 0.530) are fine with it. Once we get that
# figured out, remove the type: ignore.
peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss # type: ignore
if sys.platform == 'darwin':
# On OSX the result is in bytes.
return peak / 1_000_000
else:
# On Linux the result is in kilobytes.
return peak / 1_000 | [
"\n Get peak memory usage for this process, as measured by\n max-resident-set size:\n\n https://unix.stackexchange.com/questions/30940/getrusage-system-call-what-is-maximum-resident-set-size\n\n Only works on OSX and Linux, returns 0.0 otherwise.\n "
] |
Please provide a description of the function:def gpu_memory_mb() -> Dict[int, int]:
# pylint: disable=bare-except
try:
result = subprocess.check_output(['nvidia-smi', '--query-gpu=memory.used',
'--format=csv,nounits,noheader'],
encoding='utf-8')
gpu_memory = [int(x) for x in result.strip().split('\n')]
return {gpu: memory for gpu, memory in enumerate(gpu_memory)}
except FileNotFoundError:
# `nvidia-smi` doesn't exist, assume that means no GPU.
return {}
except:
# Catch *all* exceptions, because this memory check is a nice-to-have
# and we'd never want a training run to fail because of it.
logger.exception("unable to check gpu_memory_mb(), continuing")
return {} | [
"\n Get the current GPU memory usage.\n Based on https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/4\n\n Returns\n -------\n ``Dict[int, int]``\n Keys are device ids as integers.\n Values are memory usage as integers in MB.\n Returns an empty ``dict`` if GPUs are not available.\n "
] |
Please provide a description of the function:def ensure_list(iterable: Iterable[A]) -> List[A]:
if isinstance(iterable, list):
return iterable
else:
return list(iterable) | [
"\n An Iterable may be a list or a generator.\n This ensures we get a list without making an unnecessary copy.\n "
] |
Please provide a description of the function:def update(self, action: torch.Tensor) -> 'ChecklistStatelet':
checklist_addition = (self.terminal_actions == action).float()
new_checklist = self.checklist + checklist_addition
new_checklist_state = ChecklistStatelet(terminal_actions=self.terminal_actions,
checklist_target=self.checklist_target,
checklist_mask=self.checklist_mask,
checklist=new_checklist,
terminal_indices_dict=self.terminal_indices_dict)
return new_checklist_state | [
"\n Takes an action index, updates checklist and returns an updated state.\n "
] |
Please provide a description of the function:def _remove_action_from_type(valid_actions: Dict[str, List[str]],
type_: str,
filter_function: Callable[[str], bool]) -> None:
action_list = valid_actions[type_]
matching_action_index = [i for i, action in enumerate(action_list) if filter_function(action)]
assert len(matching_action_index) == 1, "Filter function didn't find one action"
action_list.pop(matching_action_index[0]) | [
"\n Finds the production rule matching the filter function in the given type's valid action\n list, and removes it. If there is more than one matching function, we crash.\n "
] |
Please provide a description of the function:def forward(self, # pylint: disable=arguments-differ
inputs: torch.FloatTensor,
batch_lengths: List[int],
initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None):
batch_size = inputs.size()[0]
total_timesteps = inputs.size()[1]
output_accumulator = inputs.new_zeros(batch_size, total_timesteps, self.hidden_size)
if initial_state is None:
full_batch_previous_memory = inputs.new_zeros(batch_size, self.cell_size)
full_batch_previous_state = inputs.new_zeros(batch_size, self.hidden_size)
else:
full_batch_previous_state = initial_state[0].squeeze(0)
full_batch_previous_memory = initial_state[1].squeeze(0)
current_length_index = batch_size - 1 if self.go_forward else 0
if self.recurrent_dropout_probability > 0.0 and self.training:
dropout_mask = get_dropout_mask(self.recurrent_dropout_probability,
full_batch_previous_state)
else:
dropout_mask = None
for timestep in range(total_timesteps):
# The index depends on which end we start.
index = timestep if self.go_forward else total_timesteps - timestep - 1
# What we are doing here is finding the index into the batch dimension
# which we need to use for this timestep, because the sequences have
# variable length, so once the index is greater than the length of this
# particular batch sequence, we no longer need to do the computation for
# this sequence. The key thing to recognise here is that the batch inputs
# must be _ordered_ by length from longest (first in batch) to shortest
# (last) so initially, we are going forwards with every sequence and as we
# pass the index at which the shortest elements of the batch finish,
# we stop picking them up for the computation.
if self.go_forward:
while batch_lengths[current_length_index] <= index:
current_length_index -= 1
# If we're going backwards, we are _picking up_ more indices.
else:
# First conditional: Are we already at the maximum number of elements in the batch?
# Second conditional: Does the next shortest sequence beyond the current batch
# index require computation use this timestep?
while current_length_index < (len(batch_lengths) - 1) and \
batch_lengths[current_length_index + 1] > index:
current_length_index += 1
# Actually get the slices of the batch which we
# need for the computation at this timestep.
# shape (batch_size, cell_size)
previous_memory = full_batch_previous_memory[0: current_length_index + 1].clone()
# Shape (batch_size, hidden_size)
previous_state = full_batch_previous_state[0: current_length_index + 1].clone()
# Shape (batch_size, input_size)
timestep_input = inputs[0: current_length_index + 1, index]
# Do the projections for all the gates all at once.
# Both have shape (batch_size, 4 * cell_size)
projected_input = self.input_linearity(timestep_input)
projected_state = self.state_linearity(previous_state)
# Main LSTM equations using relevant chunks of the big linear
# projections of the hidden state and inputs.
input_gate = torch.sigmoid(projected_input[:, (0 * self.cell_size):(1 * self.cell_size)] +
projected_state[:, (0 * self.cell_size):(1 * self.cell_size)])
forget_gate = torch.sigmoid(projected_input[:, (1 * self.cell_size):(2 * self.cell_size)] +
projected_state[:, (1 * self.cell_size):(2 * self.cell_size)])
memory_init = torch.tanh(projected_input[:, (2 * self.cell_size):(3 * self.cell_size)] +
projected_state[:, (2 * self.cell_size):(3 * self.cell_size)])
output_gate = torch.sigmoid(projected_input[:, (3 * self.cell_size):(4 * self.cell_size)] +
projected_state[:, (3 * self.cell_size):(4 * self.cell_size)])
memory = input_gate * memory_init + forget_gate * previous_memory
# Here is the non-standard part of this LSTM cell; first, we clip the
# memory cell, then we project the output of the timestep to a smaller size
# and again clip it.
if self.memory_cell_clip_value:
# pylint: disable=invalid-unary-operand-type
memory = torch.clamp(memory, -self.memory_cell_clip_value, self.memory_cell_clip_value)
# shape (current_length_index, cell_size)
pre_projection_timestep_output = output_gate * torch.tanh(memory)
# shape (current_length_index, hidden_size)
timestep_output = self.state_projection(pre_projection_timestep_output)
if self.state_projection_clip_value:
# pylint: disable=invalid-unary-operand-type
timestep_output = torch.clamp(timestep_output,
-self.state_projection_clip_value,
self.state_projection_clip_value)
# Only do dropout if the dropout prob is > 0.0 and we are in training mode.
if dropout_mask is not None:
timestep_output = timestep_output * dropout_mask[0: current_length_index + 1]
# We've been doing computation with less than the full batch, so here we create a new
# variable for the the whole batch at this timestep and insert the result for the
# relevant elements of the batch into it.
full_batch_previous_memory = full_batch_previous_memory.clone()
full_batch_previous_state = full_batch_previous_state.clone()
full_batch_previous_memory[0:current_length_index + 1] = memory
full_batch_previous_state[0:current_length_index + 1] = timestep_output
output_accumulator[0:current_length_index + 1, index] = timestep_output
# Mimic the pytorch API by returning state in the following shape:
# (num_layers * num_directions, batch_size, ...). As this
# LSTM cell cannot be stacked, the first dimension here is just 1.
final_state = (full_batch_previous_state.unsqueeze(0),
full_batch_previous_memory.unsqueeze(0))
return output_accumulator, final_state | [
"\n Parameters\n ----------\n inputs : ``torch.FloatTensor``, required.\n A tensor of shape (batch_size, num_timesteps, input_size)\n to apply the LSTM over.\n batch_lengths : ``List[int]``, required.\n A list of length batch_size containing the lengths of the sequences in batch.\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. The ``state`` has shape (1, batch_size, hidden_size) and the\n ``memory`` has shape (1, batch_size, cell_size).\n\n Returns\n -------\n output_accumulator : ``torch.FloatTensor``\n The outputs of the LSTM for each timestep. A tensor of shape\n (batch_size, max_timesteps, hidden_size) where for a given batch\n element, all outputs past the sequence length for that batch are\n zero tensors.\n final_state : ``Tuple[``torch.FloatTensor, torch.FloatTensor]``\n A tuple (state, memory) representing the initial hidden state and memory\n of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the\n ``memory`` has shape (1, batch_size, cell_size).\n "
] |
Please provide a description of the function:def linkcode_resolve(domain, info):
if domain != 'py':
return None
modname = info['module']
fullname = info['fullname']
submod = sys.modules.get(modname)
if submod is None:
return None
obj = submod
for part in fullname.split('.'):
try:
obj = getattr(obj, part)
except:
return None
try:
fn = inspect.getsourcefile(obj)
except:
fn = None
if not fn:
return None
try:
source, lineno = inspect.getsourcelines(obj)
except:
lineno = None
if lineno:
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
else:
linespec = ""
filename = info['module'].replace('.', '/')
return "http://github.com/allenai/allennlp/blob/master/%s.py%s" % (filename, linespec) | [
"\n Determine the URL corresponding to Python object\n This code is from\n https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L290\n and https://github.com/Lasagne/Lasagne/pull/262\n "
] |
Please provide a description of the function:def _get_initial_rnn_and_grammar_state(self,
question: Dict[str, torch.LongTensor],
table: Dict[str, torch.LongTensor],
world: List[WikiTablesWorld],
actions: List[List[ProductionRule]],
outputs: Dict[str, Any]) -> Tuple[List[RnnStatelet],
List[LambdaGrammarStatelet]]:
table_text = table['text']
# (batch_size, question_length, embedding_dim)
embedded_question = self._question_embedder(question)
question_mask = util.get_text_field_mask(question).float()
# (batch_size, num_entities, num_entity_tokens, embedding_dim)
embedded_table = self._question_embedder(table_text, num_wrapping_dims=1)
table_mask = util.get_text_field_mask(table_text, num_wrapping_dims=1).float()
batch_size, num_entities, num_entity_tokens, _ = embedded_table.size()
num_question_tokens = embedded_question.size(1)
# (batch_size, num_entities, embedding_dim)
encoded_table = self._entity_encoder(embedded_table, table_mask)
# (batch_size, num_entities, num_neighbors)
neighbor_indices = self._get_neighbor_indices(world, num_entities, encoded_table)
# Neighbor_indices is padded with -1 since 0 is a potential neighbor index.
# Thus, the absolute value needs to be taken in the index_select, and 1 needs to
# be added for the mask since that method expects 0 for padding.
# (batch_size, num_entities, num_neighbors, embedding_dim)
embedded_neighbors = util.batched_index_select(encoded_table, torch.abs(neighbor_indices))
neighbor_mask = util.get_text_field_mask({'ignored': neighbor_indices + 1},
num_wrapping_dims=1).float()
# Encoder initialized to easily obtain a masked average.
neighbor_encoder = TimeDistributed(BagOfEmbeddingsEncoder(self._embedding_dim, averaged=True))
# (batch_size, num_entities, embedding_dim)
embedded_neighbors = neighbor_encoder(embedded_neighbors, neighbor_mask)
# entity_types: tensor with shape (batch_size, num_entities), where each entry is the
# entity's type id.
# entity_type_dict: Dict[int, int], mapping flattened_entity_index -> type_index
# These encode the same information, but for efficiency reasons later it's nice
# to have one version as a tensor and one that's accessible on the cpu.
entity_types, entity_type_dict = self._get_type_vector(world, num_entities, encoded_table)
entity_type_embeddings = self._entity_type_encoder_embedding(entity_types)
projected_neighbor_embeddings = self._neighbor_params(embedded_neighbors.float())
# (batch_size, num_entities, embedding_dim)
entity_embeddings = torch.tanh(entity_type_embeddings + projected_neighbor_embeddings)
# Compute entity and question word similarity. We tried using cosine distance here, but
# because this similarity is the main mechanism that the model can use to push apart logit
# scores for certain actions (like "n -> 1" and "n -> -1"), this needs to have a larger
# output range than [-1, 1].
question_entity_similarity = torch.bmm(embedded_table.view(batch_size,
num_entities * num_entity_tokens,
self._embedding_dim),
torch.transpose(embedded_question, 1, 2))
question_entity_similarity = question_entity_similarity.view(batch_size,
num_entities,
num_entity_tokens,
num_question_tokens)
# (batch_size, num_entities, num_question_tokens)
question_entity_similarity_max_score, _ = torch.max(question_entity_similarity, 2)
# (batch_size, num_entities, num_question_tokens, num_features)
linking_features = table['linking']
linking_scores = question_entity_similarity_max_score
if self._use_neighbor_similarity_for_linking:
# The linking score is computed as a linear projection of two terms. The first is the
# maximum similarity score over the entity's words and the question token. The second
# is the maximum similarity over the words in the entity's neighbors and the question
# token.
#
# The second term, projected_question_neighbor_similarity, is useful when a column
# needs to be selected. For example, the question token might have no similarity with
# the column name, but is similar with the cells in the column.
#
# Note that projected_question_neighbor_similarity is intended to capture the same
# information as the related_column feature.
#
# Also note that this block needs to be _before_ the `linking_params` block, because
# we're overwriting `linking_scores`, not adding to it.
# (batch_size, num_entities, num_neighbors, num_question_tokens)
question_neighbor_similarity = util.batched_index_select(question_entity_similarity_max_score,
torch.abs(neighbor_indices))
# (batch_size, num_entities, num_question_tokens)
question_neighbor_similarity_max_score, _ = torch.max(question_neighbor_similarity, 2)
projected_question_entity_similarity = self._question_entity_params(
question_entity_similarity_max_score.unsqueeze(-1)).squeeze(-1)
projected_question_neighbor_similarity = self._question_neighbor_params(
question_neighbor_similarity_max_score.unsqueeze(-1)).squeeze(-1)
linking_scores = projected_question_entity_similarity + projected_question_neighbor_similarity
feature_scores = None
if self._linking_params is not None:
feature_scores = self._linking_params(linking_features).squeeze(3)
linking_scores = linking_scores + feature_scores
# (batch_size, num_question_tokens, num_entities)
linking_probabilities = self._get_linking_probabilities(world, linking_scores.transpose(1, 2),
question_mask, entity_type_dict)
# (batch_size, num_question_tokens, embedding_dim)
link_embedding = util.weighted_sum(entity_embeddings, linking_probabilities)
encoder_input = torch.cat([link_embedding, embedded_question], 2)
# (batch_size, question_length, encoder_output_dim)
encoder_outputs = self._dropout(self._encoder(encoder_input, question_mask))
# This will be our initial hidden state and memory cell for the decoder LSTM.
final_encoder_output = util.get_final_encoder_states(encoder_outputs,
question_mask,
self._encoder.is_bidirectional())
memory_cell = encoder_outputs.new_zeros(batch_size, self._encoder.get_output_dim())
# To make grouping states together in the decoder easier, we convert the batch dimension in
# all of our tensors into an outer list. For instance, the encoder outputs have shape
# `(batch_size, question_length, encoder_output_dim)`. We need to convert this into a list
# of `batch_size` tensors, each of shape `(question_length, encoder_output_dim)`. Then we
# won't have to do any index selects, or anything, we'll just do some `torch.cat()`s.
encoder_output_list = [encoder_outputs[i] for i in range(batch_size)]
question_mask_list = [question_mask[i] for i in range(batch_size)]
initial_rnn_state = []
for i in range(batch_size):
initial_rnn_state.append(RnnStatelet(final_encoder_output[i],
memory_cell[i],
self._first_action_embedding,
self._first_attended_question,
encoder_output_list,
question_mask_list))
initial_grammar_state = [self._create_grammar_state(world[i],
actions[i],
linking_scores[i],
entity_types[i])
for i in range(batch_size)]
if not self.training:
# We add a few things to the outputs that will be returned from `forward` at evaluation
# time, for visualization in a demo.
outputs['linking_scores'] = linking_scores
if feature_scores is not None:
outputs['feature_scores'] = feature_scores
outputs['similarity_scores'] = question_entity_similarity_max_score
return initial_rnn_state, initial_grammar_state | [
"\n Encodes the question and table, computes a linking between the two, and constructs an\n initial RnnStatelet and LambdaGrammarStatelet for each batch instance to pass to the\n decoder.\n\n We take ``outputs`` as a parameter here and `modify` it, adding things that we want to\n visualize in a demo.\n "
] |
Please provide a description of the function:def _get_neighbor_indices(worlds: List[WikiTablesWorld],
num_entities: int,
tensor: torch.Tensor) -> torch.LongTensor:
num_neighbors = 0
for world in worlds:
for entity in world.table_graph.entities:
if len(world.table_graph.neighbors[entity]) > num_neighbors:
num_neighbors = len(world.table_graph.neighbors[entity])
batch_neighbors = []
for world in worlds:
# Each batch instance has its own world, which has a corresponding table.
entities = world.table_graph.entities
entity2index = {entity: i for i, entity in enumerate(entities)}
entity2neighbors = world.table_graph.neighbors
neighbor_indexes = []
for entity in entities:
entity_neighbors = [entity2index[n] for n in entity2neighbors[entity]]
# Pad with -1 instead of 0, since 0 represents a neighbor index.
padded = pad_sequence_to_length(entity_neighbors, num_neighbors, lambda: -1)
neighbor_indexes.append(padded)
neighbor_indexes = pad_sequence_to_length(neighbor_indexes,
num_entities,
lambda: [-1] * num_neighbors)
batch_neighbors.append(neighbor_indexes)
return tensor.new_tensor(batch_neighbors, dtype=torch.long) | [
"\n This method returns the indices of each entity's neighbors. A tensor\n is accepted as a parameter for copying purposes.\n\n Parameters\n ----------\n worlds : ``List[WikiTablesWorld]``\n num_entities : ``int``\n tensor : ``torch.Tensor``\n Used for copying the constructed list onto the right device.\n\n Returns\n -------\n A ``torch.LongTensor`` with shape ``(batch_size, num_entities, num_neighbors)``. It is padded\n with -1 instead of 0, since 0 is a valid neighbor index.\n "
] |
Please provide a description of the function:def _get_type_vector(worlds: List[WikiTablesWorld],
num_entities: int,
tensor: torch.Tensor) -> Tuple[torch.LongTensor, Dict[int, int]]:
entity_types = {}
batch_types = []
for batch_index, world in enumerate(worlds):
types = []
for entity_index, entity in enumerate(world.table_graph.entities):
# We need numbers to be first, then cells, then parts, then row, because our
# entities are going to be sorted. We do a split by type and then a merge later,
# and it relies on this sorting.
if entity.startswith('fb:cell'):
entity_type = 1
elif entity.startswith('fb:part'):
entity_type = 2
elif entity.startswith('fb:row'):
entity_type = 3
else:
entity_type = 0
types.append(entity_type)
# For easier lookups later, we're actually using a _flattened_ version
# of (batch_index, entity_index) for the key, because this is how the
# linking scores are stored.
flattened_entity_index = batch_index * num_entities + entity_index
entity_types[flattened_entity_index] = entity_type
padded = pad_sequence_to_length(types, num_entities, lambda: 0)
batch_types.append(padded)
return tensor.new_tensor(batch_types, dtype=torch.long), entity_types | [
"\n Produces a tensor with shape ``(batch_size, num_entities)`` that encodes each entity's\n type. In addition, a map from a flattened entity index to type is returned to combine\n entity type operations into one method.\n\n Parameters\n ----------\n worlds : ``List[WikiTablesWorld]``\n num_entities : ``int``\n tensor : ``torch.Tensor``\n Used for copying the constructed list onto the right device.\n\n Returns\n -------\n A ``torch.LongTensor`` with shape ``(batch_size, num_entities)``.\n entity_types : ``Dict[int, int]``\n This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id.\n "
] |
Please provide a description of the function:def _get_linking_probabilities(self,
worlds: List[WikiTablesWorld],
linking_scores: torch.FloatTensor,
question_mask: torch.LongTensor,
entity_type_dict: Dict[int, int]) -> torch.FloatTensor:
_, num_question_tokens, num_entities = linking_scores.size()
batch_probabilities = []
for batch_index, world in enumerate(worlds):
all_probabilities = []
num_entities_in_instance = 0
# NOTE: The way that we're doing this here relies on the fact that entities are
# implicitly sorted by their types when we sort them by name, and that numbers come
# before "fb:cell", and "fb:cell" comes before "fb:row". This is not a great
# assumption, and could easily break later, but it should work for now.
for type_index in range(self._num_entity_types):
# This index of 0 is for the null entity for each type, representing the case where a
# word doesn't link to any entity.
entity_indices = [0]
entities = world.table_graph.entities
for entity_index, _ in enumerate(entities):
if entity_type_dict[batch_index * num_entities + entity_index] == type_index:
entity_indices.append(entity_index)
if len(entity_indices) == 1:
# No entities of this type; move along...
continue
# We're subtracting one here because of the null entity we added above.
num_entities_in_instance += len(entity_indices) - 1
# We separate the scores by type, since normalization is done per type. There's an
# extra "null" entity per type, also, so we have `num_entities_per_type + 1`. We're
# selecting from a (num_question_tokens, num_entities) linking tensor on _dimension 1_,
# so we get back something of shape (num_question_tokens,) for each index we're
# selecting. All of the selected indices together then make a tensor of shape
# (num_question_tokens, num_entities_per_type + 1).
indices = linking_scores.new_tensor(entity_indices, dtype=torch.long)
entity_scores = linking_scores[batch_index].index_select(1, indices)
# We used index 0 for the null entity, so this will actually have some values in it.
# But we want the null entity's score to be 0, so we set that here.
entity_scores[:, 0] = 0
# No need for a mask here, as this is done per batch instance, with no padding.
type_probabilities = torch.nn.functional.softmax(entity_scores, dim=1)
all_probabilities.append(type_probabilities[:, 1:])
# We need to add padding here if we don't have the right number of entities.
if num_entities_in_instance != num_entities:
zeros = linking_scores.new_zeros(num_question_tokens,
num_entities - num_entities_in_instance)
all_probabilities.append(zeros)
# (num_question_tokens, num_entities)
probabilities = torch.cat(all_probabilities, dim=1)
batch_probabilities.append(probabilities)
batch_probabilities = torch.stack(batch_probabilities, dim=0)
return batch_probabilities * question_mask.unsqueeze(-1).float() | [
"\n Produces the probability of an entity given a question word and type. The logic below\n separates the entities by type since the softmax normalization term sums over entities\n of a single type.\n\n Parameters\n ----------\n worlds : ``List[WikiTablesWorld]``\n linking_scores : ``torch.FloatTensor``\n Has shape (batch_size, num_question_tokens, num_entities).\n question_mask: ``torch.LongTensor``\n Has shape (batch_size, num_question_tokens).\n entity_type_dict : ``Dict[int, int]``\n This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id.\n\n Returns\n -------\n batch_probabilities : ``torch.FloatTensor``\n Has shape ``(batch_size, num_question_tokens, num_entities)``.\n Contains all the probabilities for an entity given a question word.\n "
] |
Please provide a description of the function:def get_metrics(self, reset: bool = False) -> Dict[str, float]:
return {
'dpd_acc': self._action_sequence_accuracy.get_metric(reset),
'denotation_acc': self._denotation_accuracy.get_metric(reset),
'lf_percent': self._has_logical_form.get_metric(reset),
} | [
"\n We track three metrics here:\n\n 1. dpd_acc, which is the percentage of the time that our best output action sequence is\n in the set of action sequences provided by DPD. This is an easy-to-compute lower bound\n on denotation accuracy for the set of examples where we actually have DPD output. We\n only score dpd_acc on that subset.\n\n 2. denotation_acc, which is the percentage of examples where we get the correct\n denotation. This is the typical \"accuracy\" metric, and it is what you should usually\n report in an experimental result. You need to be careful, though, that you're\n computing this on the full data, and not just the subset that has DPD output (make sure\n you pass \"keep_if_no_dpd=True\" to the dataset reader, which we do for validation data,\n but not training data).\n\n 3. lf_percent, which is the percentage of time that decoding actually produces a\n finished logical form. We might not produce a valid logical form if the decoder gets\n into a repetitive loop, or we're trying to produce a super long logical form and run\n out of time steps, or something.\n "
] |
Please provide a description of the function:def _create_grammar_state(self,
world: WikiTablesWorld,
possible_actions: List[ProductionRule],
linking_scores: torch.Tensor,
entity_types: torch.Tensor) -> LambdaGrammarStatelet:
# TODO(mattg): Move the "valid_actions" construction to another method.
action_map = {}
for action_index, action in enumerate(possible_actions):
action_string = action[0]
action_map[action_string] = action_index
entity_map = {}
for entity_index, entity in enumerate(world.table_graph.entities):
entity_map[entity] = entity_index
valid_actions = world.get_valid_actions()
translated_valid_actions: Dict[str, Dict[str, Tuple[torch.Tensor, torch.Tensor, List[int]]]] = {}
for key, action_strings in valid_actions.items():
translated_valid_actions[key] = {}
# `key` here is a non-terminal from the grammar, and `action_strings` are all the valid
# productions of that non-terminal. We'll first split those productions by global vs.
# linked action.
action_indices = [action_map[action_string] for action_string in action_strings]
production_rule_arrays = [(possible_actions[index], index) for index in action_indices]
global_actions = []
linked_actions = []
for production_rule_array, action_index in production_rule_arrays:
if production_rule_array[1]:
global_actions.append((production_rule_array[2], action_index))
else:
linked_actions.append((production_rule_array[0], action_index))
# Then we get the embedded representations of the global actions.
global_action_tensors, global_action_ids = zip(*global_actions)
global_action_tensor = torch.cat(global_action_tensors, dim=0)
global_input_embeddings = self._action_embedder(global_action_tensor)
if self._add_action_bias:
global_action_biases = self._action_biases(global_action_tensor)
global_input_embeddings = torch.cat([global_input_embeddings, global_action_biases], dim=-1)
global_output_embeddings = self._output_action_embedder(global_action_tensor)
translated_valid_actions[key]['global'] = (global_input_embeddings,
global_output_embeddings,
list(global_action_ids))
# Then the representations of the linked actions.
if linked_actions:
linked_rules, linked_action_ids = zip(*linked_actions)
entities = [rule.split(' -> ')[1] for rule in linked_rules]
entity_ids = [entity_map[entity] for entity in entities]
# (num_linked_actions, num_question_tokens)
entity_linking_scores = linking_scores[entity_ids]
# (num_linked_actions,)
entity_type_tensor = entity_types[entity_ids]
# (num_linked_actions, entity_type_embedding_dim)
entity_type_embeddings = self._entity_type_decoder_embedding(entity_type_tensor)
translated_valid_actions[key]['linked'] = (entity_linking_scores,
entity_type_embeddings,
list(linked_action_ids))
# Lastly, we need to also create embedded representations of context-specific actions. In
# this case, those are only variable productions, like "r -> x". Note that our language
# only permits one lambda at a time, so we don't need to worry about how nested lambdas
# might impact this.
context_actions = {}
for action_id, action in enumerate(possible_actions):
if action[0].endswith(" -> x"):
input_embedding = self._action_embedder(action[2])
if self._add_action_bias:
input_bias = self._action_biases(action[2])
input_embedding = torch.cat([input_embedding, input_bias], dim=-1)
output_embedding = self._output_action_embedder(action[2])
context_actions[action[0]] = (input_embedding, output_embedding, action_id)
return LambdaGrammarStatelet([START_SYMBOL],
{},
translated_valid_actions,
context_actions,
type_declaration.is_nonterminal) | [
"\n This method creates the LambdaGrammarStatelet object that's used for decoding. Part of\n creating that is creating the `valid_actions` dictionary, which contains embedded\n representations of all of the valid actions. So, we create that here as well.\n\n The way we represent the valid expansions is a little complicated: we use a\n dictionary of `action types`, where the key is the action type (like \"global\", \"linked\", or\n whatever your model is expecting), and the value is a tuple representing all actions of\n that type. The tuple is (input tensor, output tensor, action id). The input tensor has\n the representation that is used when `selecting` actions, for all actions of this type.\n The output tensor has the representation that is used when feeding the action to the next\n step of the decoder (this could just be the same as the input tensor). The action ids are\n a list of indices into the main action list for each batch instance.\n\n The inputs to this method are for a `single instance in the batch`; none of the tensors we\n create here are batched. We grab the global action ids from the input\n ``ProductionRules``, and we use those to embed the valid actions for every\n non-terminal type. We use the input ``linking_scores`` for non-global actions.\n\n Parameters\n ----------\n world : ``WikiTablesWorld``\n From the input to ``forward`` for a single batch instance.\n possible_actions : ``List[ProductionRule]``\n From the input to ``forward`` for a single batch instance.\n linking_scores : ``torch.Tensor``\n Assumed to have shape ``(num_entities, num_question_tokens)`` (i.e., there is no batch\n dimension).\n entity_types : ``torch.Tensor``\n Assumed to have shape ``(num_entities,)`` (i.e., there is no batch dimension).\n "
] |
Please provide a description of the function:def _compute_validation_outputs(self,
actions: List[List[ProductionRule]],
best_final_states: Mapping[int, Sequence[GrammarBasedState]],
world: List[WikiTablesWorld],
example_lisp_string: List[str],
metadata: List[Dict[str, Any]],
outputs: Dict[str, Any]) -> None:
batch_size = len(actions)
action_mapping = {}
for batch_index, batch_actions in enumerate(actions):
for action_index, action in enumerate(batch_actions):
action_mapping[(batch_index, action_index)] = action[0]
outputs['action_mapping'] = action_mapping
outputs['best_action_sequence'] = []
outputs['debug_info'] = []
outputs['entities'] = []
outputs['logical_form'] = []
for i in range(batch_size):
# Decoding may not have terminated with any completed logical forms, if `num_steps`
# isn't long enough (or if the model is not trained enough and gets into an
# infinite action loop).
if i in best_final_states:
best_action_indices = best_final_states[i][0].action_history[0]
action_strings = [action_mapping[(i, action_index)] for action_index in best_action_indices]
try:
logical_form = world[i].get_logical_form(action_strings, add_var_function=False)
self._has_logical_form(1.0)
except ParsingError:
self._has_logical_form(0.0)
logical_form = 'Error producing logical form'
if example_lisp_string:
denotation_correct = self._executor.evaluate_logical_form(logical_form,
example_lisp_string[i])
self._denotation_accuracy(1.0 if denotation_correct else 0.0)
outputs['best_action_sequence'].append(action_strings)
outputs['logical_form'].append(logical_form)
outputs['debug_info'].append(best_final_states[i][0].debug_info[0]) # type: ignore
outputs['entities'].append(world[i].table_graph.entities)
else:
outputs['logical_form'].append('')
self._has_logical_form(0.0)
self._denotation_accuracy(0.0)
if metadata is not None:
outputs["question_tokens"] = [x["question_tokens"] for x in metadata]
outputs["original_table"] = [x["original_table"] for x in metadata] | [
"\n Does common things for validation time: computing logical form accuracy (which is expensive\n and unnecessary during training), adding visualization info to the output dictionary, etc.\n\n This doesn't return anything; instead it `modifies` the given ``outputs`` dictionary, and\n calls metrics on ``self``.\n "
] |
Please provide a description of the function:def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
action_mapping = output_dict['action_mapping']
best_actions = output_dict["best_action_sequence"]
debug_infos = output_dict['debug_info']
batch_action_info = []
for batch_index, (predicted_actions, debug_info) in enumerate(zip(best_actions, debug_infos)):
instance_action_info = []
for predicted_action, action_debug_info in zip(predicted_actions, debug_info):
action_info = {}
action_info['predicted_action'] = predicted_action
considered_actions = action_debug_info['considered_actions']
probabilities = action_debug_info['probabilities']
actions = []
for action, probability in zip(considered_actions, probabilities):
if action != -1:
actions.append((action_mapping[(batch_index, action)], probability))
actions.sort()
considered_actions, probabilities = zip(*actions)
action_info['considered_actions'] = considered_actions
action_info['action_probabilities'] = probabilities
action_info['question_attention'] = action_debug_info.get('question_attention', [])
instance_action_info.append(action_info)
batch_action_info.append(instance_action_info)
output_dict["predicted_actions"] = batch_action_info
return output_dict | [
"\n This method overrides ``Model.decode``, which gets called after ``Model.forward``, at test\n time, to finalize predictions. This is (confusingly) a separate notion from the \"decoder\"\n in \"encoder/decoder\", where that decoder logic lives in the ``TransitionFunction``.\n\n This method trims the output predictions to the first end symbol, replaces indices with\n corresponding tokens, and adds a field called ``predicted_tokens`` to the ``output_dict``.\n "
] |
Please provide a description of the function:def _get_linked_logits_addition(checklist_state: ChecklistStatelet,
action_ids: List[int],
action_logits: 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: (num_current_actions,). This is the sum of the action embeddings that we want
# the model to prefer.
logit_addition = action_logits * actions_to_encourage
return logit_addition | [
"\n Gets the logits of desired terminal actions yet to be produced by the decoder, and\n returns them for the decoder to add to the prior action logits, biasing the model towards\n predicting missing linked actions.\n "
] |
Please provide a description of the function:def attend_on_question(self,
query: torch.Tensor,
encoder_outputs: torch.Tensor,
encoder_output_mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
# (group_size, question_length)
question_attention_weights = self._input_attention(query,
encoder_outputs,
encoder_output_mask)
# (group_size, encoder_output_dim)
attended_question = util.weighted_sum(encoder_outputs, question_attention_weights)
return attended_question, question_attention_weights | [
"\n Given a query (which is typically the decoder hidden state), compute an attention over the\n output of the question encoder, and return a weighted sum of the question representations\n given this attention. We also return the attention weights themselves.\n\n This is a simple computation, but we have it as a separate method so that the ``forward``\n method on the main parser module can call it on the initial hidden state, to simplify the\n logic in ``take_step``.\n "
] |
Please provide a description of the function:def _walk(self) -> None:
# Buffer of NTs to expand, previous actions
incomplete_paths = [([str(type_)], [f"{START_SYMBOL} -> {type_}"]) for type_ in
self._world.get_valid_starting_types()]
self._completed_paths = []
actions = self._world.get_valid_actions()
# Keeps track of `MultiMatchNamedBasicTypes` to substitute them with appropriate types.
multi_match_substitutions = self._world.get_multi_match_mapping()
# Overview: We keep track of the buffer of non-terminals to expand, and the action history
# for each incomplete path. At every iteration in the while loop below, we iterate over all
# incomplete paths, expand one non-terminal from the buffer in a depth-first fashion, get
# all possible next actions triggered by that non-terminal and add to the paths. Then, we
# check the expanded paths, to see if they are 1) complete, in which case they are
# added to completed_paths, 2) longer than max_path_length, in which case they are
# discarded, or 3) neither, in which case they are used to form the incomplete_paths for the
# next iteration of this while loop.
# While the non-terminal expansion is done in a depth-first fashion, note that the search over
# the action space itself is breadth-first.
while incomplete_paths:
next_paths = []
for nonterminal_buffer, history in incomplete_paths:
# Taking the last non-terminal added to the buffer. We're going depth-first.
nonterminal = nonterminal_buffer.pop()
next_actions = []
if nonterminal in multi_match_substitutions:
for current_nonterminal in [nonterminal] + multi_match_substitutions[nonterminal]:
if current_nonterminal in actions:
next_actions.extend(actions[current_nonterminal])
elif nonterminal not in actions:
# This happens when the nonterminal corresponds to a type that does not exist in
# the context. For example, in the variable free variant of the WikiTables
# world, there are nonterminals for specific column types (like date). Say we
# produced a path containing "filter_date_greater" already, and we do not have
# an columns of type "date", then this condition would be triggered. We should
# just discard those paths.
continue
else:
next_actions.extend(actions[nonterminal])
# Iterating over all possible next actions.
for action in next_actions:
new_history = history + [action]
new_nonterminal_buffer = nonterminal_buffer[:]
# Since we expand the last action added to the buffer, the left child should be
# added after the right child.
for right_side_part in reversed(self._get_right_side_parts(action)):
if types.is_nonterminal(right_side_part):
new_nonterminal_buffer.append(right_side_part)
next_paths.append((new_nonterminal_buffer, new_history))
incomplete_paths = []
for nonterminal_buffer, path in next_paths:
# An empty buffer means that we've completed this path.
if not nonterminal_buffer:
# Indexing completed paths by the nonterminals they contain.
next_path_index = len(self._completed_paths)
for action in path:
for value in self._get_right_side_parts(action):
if not types.is_nonterminal(value):
self._terminal_path_index[action].add(next_path_index)
self._completed_paths.append(path)
# We're adding to incomplete_paths for the next iteration, only those paths that are
# shorter than the max_path_length. The remaining paths will be discarded.
elif len(path) <= self._max_path_length:
incomplete_paths.append((nonterminal_buffer, path)) | [
"\n Walk over action space to collect completed paths of at most ``self._max_path_length`` steps.\n "
] |
Please provide a description of the function:def _check_types(self) -> None:
all_instance_fields_and_types: List[Dict[str, str]] = [{k: v.__class__.__name__
for k, v in x.fields.items()}
for x in self.instances]
# Check all the field names and Field types are the same for every instance.
if not all([all_instance_fields_and_types[0] == x for x in all_instance_fields_and_types]):
raise ConfigurationError("You cannot construct a Batch with non-homogeneous Instances.") | [
"\n Check that all the instances have the same types.\n "
] |
Please provide a description of the function:def get_padding_lengths(self) -> Dict[str, Dict[str, int]]:
padding_lengths: Dict[str, Dict[str, int]] = defaultdict(dict)
all_instance_lengths: List[Dict[str, Dict[str, int]]] = [instance.get_padding_lengths()
for instance in self.instances]
if not all_instance_lengths:
return {**padding_lengths}
all_field_lengths: Dict[str, List[Dict[str, int]]] = defaultdict(list)
for instance_lengths in all_instance_lengths:
for field_name, instance_field_lengths in instance_lengths.items():
all_field_lengths[field_name].append(instance_field_lengths)
for field_name, field_lengths in all_field_lengths.items():
for padding_key in field_lengths[0].keys():
max_value = max(x[padding_key] if padding_key in x else 0 for x in field_lengths)
padding_lengths[field_name][padding_key] = max_value
return {**padding_lengths} | [
"\n Gets the maximum padding lengths from all ``Instances`` in this batch. Each ``Instance``\n has multiple ``Fields``, and each ``Field`` could have multiple things that need padding.\n We look at all fields in all instances, and find the max values for each (field_name,\n padding_key) pair, returning them in a dictionary.\n\n This can then be used to convert this batch into arrays of consistent length, or to set\n model parameters, etc.\n "
] |
Please provide a description of the function:def as_tensor_dict(self,
padding_lengths: Dict[str, Dict[str, int]] = None,
verbose: bool = False) -> Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]:
# This complex return type is actually predefined elsewhere as a DataArray,
# but we can't use it because mypy doesn't like it.
if padding_lengths is None:
padding_lengths = defaultdict(dict)
# First we need to decide _how much_ to pad. To do that, we find the max length for all
# relevant padding decisions from the instances themselves. Then we check whether we were
# given a max length for a particular field and padding key. If we were, we use that
# instead of the instance-based one.
if verbose:
logger.info("Padding batch of size %d to lengths %s", len(self.instances), str(padding_lengths))
logger.info("Getting max lengths from instances")
instance_padding_lengths = self.get_padding_lengths()
if verbose:
logger.info("Instance max lengths: %s", str(instance_padding_lengths))
lengths_to_use: Dict[str, Dict[str, int]] = defaultdict(dict)
for field_name, instance_field_lengths in instance_padding_lengths.items():
for padding_key in instance_field_lengths.keys():
if padding_lengths[field_name].get(padding_key) is not None:
lengths_to_use[field_name][padding_key] = padding_lengths[field_name][padding_key]
else:
lengths_to_use[field_name][padding_key] = instance_field_lengths[padding_key]
# Now we actually pad the instances to tensors.
field_tensors: Dict[str, list] = defaultdict(list)
if verbose:
logger.info("Now actually padding instances to length: %s", str(lengths_to_use))
for instance in self.instances:
for field, tensors in instance.as_tensor_dict(lengths_to_use).items():
field_tensors[field].append(tensors)
# Finally, we combine the tensors that we got for each instance into one big tensor (or set
# of tensors) per field. The `Field` classes themselves have the logic for batching the
# tensors together, so we grab a dictionary of field_name -> field class from the first
# instance in the batch.
field_classes = self.instances[0].fields
final_fields = {}
for field_name, field_tensor_list in field_tensors.items():
final_fields[field_name] = field_classes[field_name].batch_tensors(field_tensor_list)
return final_fields | [
"\n This method converts this ``Batch`` into a set of pytorch Tensors that can be passed\n through a model. In order for the tensors to be valid tensors, all ``Instances`` in this\n batch need to be padded to the same lengths wherever padding is necessary, so we do that\n first, then we combine all of the tensors for each field in each instance into a set of\n batched tensors for each field.\n\n Parameters\n ----------\n padding_lengths : ``Dict[str, Dict[str, int]]``\n If a key is present in this dictionary with a non-``None`` value, we will pad to that\n length instead of the length calculated from the data. This lets you, e.g., set a\n maximum value for sentence length if you want to throw out long sequences.\n\n Entries in this dictionary are keyed first by field name (e.g., \"question\"), then by\n padding key (e.g., \"num_tokens\").\n verbose : ``bool``, optional (default=``False``)\n Should we output logging information when we're doing this padding? If the batch is\n large, this is nice to have, because padding a large batch could take a long time.\n But if you're doing this inside of a data generator, having all of this output per\n batch is a bit obnoxious (and really slow).\n\n Returns\n -------\n tensors : ``Dict[str, DataArray]``\n A dictionary of tensors, keyed by field name, suitable for passing as input to a model.\n This is a `batch` of instances, so, e.g., if the instances have a \"question\" field and\n an \"answer\" field, the \"question\" fields for all of the instances will be grouped\n together into a single tensor, and the \"answer\" fields for all instances will be\n similarly grouped in a parallel set of tensors, for batched computation. Additionally,\n for complex ``Fields``, the value of the dictionary key is not necessarily a single\n tensor. For example, with the ``TextField``, the output is a dictionary mapping\n ``TokenIndexer`` keys to tensors. The number of elements in this sub-dictionary\n therefore corresponds to the number of ``TokenIndexers`` used to index the\n ``TextField``. Each ``Field`` class is responsible for batching its own output.\n "
] |
Please provide a description of the function:def get_strings_from_utterance(tokenized_utterance: List[Token]) -> Dict[str, List[int]]:
string_linking_scores: Dict[str, List[int]] = defaultdict(list)
for index, token in enumerate(tokenized_utterance):
for string in ATIS_TRIGGER_DICT.get(token.text.lower(), []):
string_linking_scores[string].append(index)
token_bigrams = bigrams([token.text for token in tokenized_utterance])
for index, token_bigram in enumerate(token_bigrams):
for string in ATIS_TRIGGER_DICT.get(' '.join(token_bigram).lower(), []):
string_linking_scores[string].extend([index,
index + 1])
trigrams = ngrams([token.text for token in tokenized_utterance], 3)
for index, trigram in enumerate(trigrams):
if trigram[0] == 'st':
natural_language_key = f'st. {trigram[2]}'.lower()
else:
natural_language_key = ' '.join(trigram).lower()
for string in ATIS_TRIGGER_DICT.get(natural_language_key, []):
string_linking_scores[string].extend([index,
index + 1,
index + 2])
return string_linking_scores | [
"\n Based on the current utterance, return a dictionary where the keys are the strings in\n the database that map to lists of the token indices that they are linked to.\n "
] |
Please provide a description of the function:def _update_grammar(self):
# This will give us a shallow copy. We have to be careful here because the ``Grammar`` object
# contains ``Expression`` objects that have tuples containing the members of that expression.
# We have to create new sub-expression objects so that original grammar is not mutated.
new_grammar = copy(AtisWorld.sql_table_context.grammar)
for numeric_nonterminal in NUMERIC_NONTERMINALS:
self._add_numeric_nonterminal_to_grammar(numeric_nonterminal, new_grammar)
self._update_expression_reference(new_grammar, 'pos_value', 'number')
ternary_expressions = [self._get_sequence_with_spacing(new_grammar,
[new_grammar['col_ref'],
Literal('BETWEEN'),
new_grammar['time_range_start'],
Literal(f'AND'),
new_grammar['time_range_end']]),
self._get_sequence_with_spacing(new_grammar,
[new_grammar['col_ref'],
Literal('NOT'),
Literal('BETWEEN'),
new_grammar['time_range_start'],
Literal(f'AND'),
new_grammar['time_range_end']]),
self._get_sequence_with_spacing(new_grammar,
[new_grammar['col_ref'],
Literal('not'),
Literal('BETWEEN'),
new_grammar['time_range_start'],
Literal(f'AND'),
new_grammar['time_range_end']])]
new_grammar['ternaryexpr'] = OneOf(*ternary_expressions, name='ternaryexpr')
self._update_expression_reference(new_grammar, 'condition', 'ternaryexpr')
new_binary_expressions = []
fare_round_trip_cost_expression = \
self._get_sequence_with_spacing(new_grammar,
[Literal('fare'),
Literal('.'),
Literal('round_trip_cost'),
new_grammar['binaryop'],
new_grammar['fare_round_trip_cost']])
new_binary_expressions.append(fare_round_trip_cost_expression)
fare_one_direction_cost_expression = \
self._get_sequence_with_spacing(new_grammar,
[Literal('fare'),
Literal('.'),
Literal('one_direction_cost'),
new_grammar['binaryop'],
new_grammar['fare_one_direction_cost']])
new_binary_expressions.append(fare_one_direction_cost_expression)
flight_number_expression = \
self._get_sequence_with_spacing(new_grammar,
[Literal('flight'),
Literal('.'),
Literal('flight_number'),
new_grammar['binaryop'],
new_grammar['flight_number']])
new_binary_expressions.append(flight_number_expression)
if self.dates:
year_binary_expression = self._get_sequence_with_spacing(new_grammar,
[Literal('date_day'),
Literal('.'),
Literal('year'),
new_grammar['binaryop'],
new_grammar['year_number']])
month_binary_expression = self._get_sequence_with_spacing(new_grammar,
[Literal('date_day'),
Literal('.'),
Literal('month_number'),
new_grammar['binaryop'],
new_grammar['month_number']])
day_binary_expression = self._get_sequence_with_spacing(new_grammar,
[Literal('date_day'),
Literal('.'),
Literal('day_number'),
new_grammar['binaryop'],
new_grammar['day_number']])
new_binary_expressions.extend([year_binary_expression,
month_binary_expression,
day_binary_expression])
new_binary_expressions = new_binary_expressions + list(new_grammar['biexpr'].members)
new_grammar['biexpr'] = OneOf(*new_binary_expressions, name='biexpr')
self._update_expression_reference(new_grammar, 'condition', 'biexpr')
return new_grammar | [
"\n We create a new ``Grammar`` object from the one in ``AtisSqlTableContext``, that also\n has the new entities that are extracted from the utterance. Stitching together the expressions\n to form the grammar is a little tedious here, but it is worth it because we don't have to create\n a new grammar from scratch. Creating a new grammar is expensive because we have many production\n rules that have all database values in the column on the right hand side. We update the expressions\n bottom up, since the higher level expressions may refer to the lower level ones. For example, the\n ternary expression will refer to the start and end times.\n "
] |
Please provide a description of the function:def _update_expression_reference(self, # pylint: disable=no-self-use
grammar: Grammar,
parent_expression_nonterminal: str,
child_expression_nonterminal: str) -> None:
grammar[parent_expression_nonterminal].members = \
[member if member.name != child_expression_nonterminal
else grammar[child_expression_nonterminal]
for member in grammar[parent_expression_nonterminal].members] | [
"\n When we add a new expression, there may be other expressions that refer to\n it, and we need to update those to point to the new expression.\n "
] |
Please provide a description of the function:def _get_sequence_with_spacing(self, # pylint: disable=no-self-use
new_grammar,
expressions: List[Expression],
name: str = '') -> Sequence:
expressions = [subexpression
for expression in expressions
for subexpression in (expression, new_grammar['ws'])]
return Sequence(*expressions, name=name) | [
"\n This is a helper method for generating sequences, since we often want a list of expressions\n with whitespaces between them.\n "
] |
Please provide a description of the function:def add_to_number_linking_scores(self,
all_numbers: Set[str],
number_linking_scores: Dict[str, Tuple[str, str, List[int]]],
get_number_linking_dict: Callable[[str, List[Token]],
Dict[str, List[int]]],
current_tokenized_utterance: List[Token],
nonterminal: str) -> None:
number_linking_dict: Dict[str, List[int]] = {}
for utterance, tokenized_utterance in zip(self.utterances, self.tokenized_utterances):
number_linking_dict = get_number_linking_dict(utterance, tokenized_utterance)
all_numbers.update(number_linking_dict.keys())
all_numbers_list: List[str] = sorted(all_numbers, reverse=True)
for number in all_numbers_list:
entity_linking = [0 for token in current_tokenized_utterance]
# ``number_linking_dict`` is for the last utterance here. If the number was triggered
# before the last utterance, then it will have linking scores of 0's.
for token_index in number_linking_dict.get(number, []):
if token_index < len(entity_linking):
entity_linking[token_index] = 1
action = format_action(nonterminal, number, is_number=True, keywords_to_uppercase=KEYWORDS)
number_linking_scores[action] = (nonterminal, number, entity_linking) | [
"\n This is a helper method for adding different types of numbers (eg. starting time ranges) as entities.\n We first go through all utterances in the interaction and find the numbers of a certain type and add\n them to the set ``all_numbers``, which is initialized with default values. We want to add all numbers\n that occur in the interaction, and not just the current turn because the query could contain numbers\n that were triggered before the current turn. For each entity, we then check if it is triggered by tokens\n in the current utterance and construct the linking score.\n "
] |
Please provide a description of the function:def _get_linked_entities(self) -> Dict[str, Dict[str, Tuple[str, str, List[int]]]]:
current_tokenized_utterance = [] if not self.tokenized_utterances \
else self.tokenized_utterances[-1]
# We generate a dictionary where the key is the type eg. ``number`` or ``string``.
# The value is another dictionary where the key is the action and the value is a tuple
# of the nonterminal, the string value and the linking score.
entity_linking_scores: Dict[str, Dict[str, Tuple[str, str, List[int]]]] = {}
number_linking_scores: Dict[str, Tuple[str, str, List[int]]] = {}
string_linking_scores: Dict[str, Tuple[str, str, List[int]]] = {}
# Get time range start
self.add_to_number_linking_scores({'0'},
number_linking_scores,
get_time_range_start_from_utterance,
current_tokenized_utterance,
'time_range_start')
self.add_to_number_linking_scores({'1200'},
number_linking_scores,
get_time_range_end_from_utterance,
current_tokenized_utterance,
'time_range_end')
self.add_to_number_linking_scores({'0', '1', '60', '41'},
number_linking_scores,
get_numbers_from_utterance,
current_tokenized_utterance,
'number')
self.add_to_number_linking_scores({'0'},
number_linking_scores,
get_costs_from_utterance,
current_tokenized_utterance,
'fare_round_trip_cost')
self.add_to_number_linking_scores({'0'},
number_linking_scores,
get_costs_from_utterance,
current_tokenized_utterance,
'fare_one_direction_cost')
self.add_to_number_linking_scores({'0'},
number_linking_scores,
get_flight_numbers_from_utterance,
current_tokenized_utterance,
'flight_number')
self.add_dates_to_number_linking_scores(number_linking_scores,
current_tokenized_utterance)
# Add string linking dict.
string_linking_dict: Dict[str, List[int]] = {}
for tokenized_utterance in self.tokenized_utterances:
string_linking_dict = get_strings_from_utterance(tokenized_utterance)
strings_list = AtisWorld.sql_table_context.strings_list
strings_list.append(('flight_airline_code_string -> ["\'EA\'"]', 'EA'))
strings_list.append(('airline_airline_name_string-> ["\'EA\'"]', 'EA'))
# We construct the linking scores for strings from the ``string_linking_dict`` here.
for string in strings_list:
entity_linking = [0 for token in current_tokenized_utterance]
# string_linking_dict has the strings and linking scores from the last utterance.
# If the string is not in the last utterance, then the linking scores will be all 0.
for token_index in string_linking_dict.get(string[1], []):
entity_linking[token_index] = 1
action = string[0]
string_linking_scores[action] = (action.split(' -> ')[0], string[1], entity_linking)
entity_linking_scores['number'] = number_linking_scores
entity_linking_scores['string'] = string_linking_scores
return entity_linking_scores | [
"\n This method gets entities from the current utterance finds which tokens they are linked to.\n The entities are divided into two main groups, ``numbers`` and ``strings``. We rely on these\n entities later for updating the valid actions and the grammar.\n "
] |
Please provide a description of the function:def all_possible_actions(self) -> List[str]:
all_actions = set()
for _, action_list in self.valid_actions.items():
for action in action_list:
all_actions.add(action)
return sorted(all_actions) | [
"\n Return a sorted list of strings representing all possible actions\n of the form: nonterminal -> [right_hand_side]\n "
] |
Please provide a description of the function:def _flatten_entities(self) -> Tuple[List[str], numpy.ndarray]:
entities = []
linking_scores = []
for entity in sorted(self.linked_entities['number']):
entities.append(entity)
linking_scores.append(self.linked_entities['number'][entity][2])
for entity in sorted(self.linked_entities['string']):
entities.append(entity)
linking_scores.append(self.linked_entities['string'][entity][2])
return entities, numpy.array(linking_scores) | [
"\n When we first get the entities and the linking scores in ``_get_linked_entities``\n we represent as dictionaries for easier updates to the grammar and valid actions.\n In this method, we flatten them for the model so that the entities are represented as\n a list, and the linking scores are a 2D numpy array of shape (num_entities, num_utterance_tokens).\n "
] |
Please provide a description of the function:def make_app(include_packages: Sequence[str] = ()) -> Flask:
# Load modules
for package_name in include_packages:
import_submodules(package_name)
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
return send_file('config_explorer.html')
@app.route('/api/config/')
def api_config() -> Response: # pylint: disable=unused-variable
class_name = request.args.get('class', '')
get_choices = request.args.get('get_choices', None)
# Get the configuration for this class name
config = configure(class_name)
try:
# May not have choices
choice5 = choices(class_name)
except ValueError:
choice5 = []
if get_choices and choice5:
return jsonify({
"className": class_name,
"choices": choice5
})
else:
return jsonify({
"className": class_name,
"config": config.to_json()
})
return app | [
"\n Creates a Flask app that serves up a simple configuration wizard.\n ",
"\n There are basically two things that can happen here.\n If this method is called with a ``Registrable`` class (e.g. ``Model``),\n it should return the list of possible ``Model`` subclasses.\n If it is called with an instantiable subclass (e.g. ``CrfTagger``),\n is should return the config for that subclass.\n\n This is complicated by the fact that some Registrable base classes\n (e.g. Vocabulary, Trainer) are _themselves_ instantiable.\n\n We handle this in two ways: first, we insist that the first case\n include an extra ``get_choices`` parameter. That is, if you call\n this method for ``Trainer`` with get_choices=true, you get the list\n of Trainer subclasses. If you call it without that extra flag, you\n get the config for the class itself.\n\n There are basically two UX situations in which this API is called.\n The first is when you have a dropdown list of choices (e.g. Model types)\n and you select one. Such an API request is made *without* the get_choices flag,\n which means that the config is returned *even if the class in question\n is a Registrable class that has subclass choices*.\n\n The second is when you click a \"Configure\" button, which configures\n a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``)\n have registrable subclasses. In this case the API request is made\n with the \"get_choices\" flag, but will return the corresponding config\n object if no choices are available (e.g. in the ``FeedForward``) case.\n\n This is not elegant, but it works.\n "
] |
Please provide a description of the function:def train_model_from_args(args: argparse.Namespace):
train_model_from_file(args.param_path,
args.serialization_dir,
args.overrides,
args.file_friendly_logging,
args.recover,
args.force,
args.cache_directory,
args.cache_prefix) | [
"\n Just converts from an ``argparse.Namespace`` object to string paths.\n "
] |
Please provide a description of the function:def train_model_from_file(parameter_filename: str,
serialization_dir: str,
overrides: str = "",
file_friendly_logging: bool = False,
recover: bool = False,
force: bool = False,
cache_directory: str = None,
cache_prefix: str = None) -> Model:
# Load the experiment config from a file and pass it to ``train_model``.
params = Params.from_file(parameter_filename, overrides)
return train_model(params,
serialization_dir,
file_friendly_logging,
recover,
force,
cache_directory, cache_prefix) | [
"\n A wrapper around :func:`train_model` which loads the params from a file.\n\n Parameters\n ----------\n parameter_filename : ``str``\n A json parameter file specifying an AllenNLP experiment.\n serialization_dir : ``str``\n The directory in which to save results and logs. We just pass this along to\n :func:`train_model`.\n overrides : ``str``\n A JSON string that we will use to override values in the input parameter file.\n file_friendly_logging : ``bool``, optional (default=False)\n If ``True``, we make our output more friendly to saved model files. We just pass this\n along to :func:`train_model`.\n recover : ``bool`, optional (default=False)\n If ``True``, we will try to recover a training run from an existing serialization\n directory. This is only intended for use when something actually crashed during the middle\n of a run. For continuing training a model on new data, see the ``fine-tune`` command.\n force : ``bool``, optional (default=False)\n If ``True``, we will overwrite the serialization directory if it already exists.\n cache_directory : ``str``, optional\n For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`.\n cache_prefix : ``str``, optional\n For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`.\n "
] |
Please provide a description of the function:def train_model(params: Params,
serialization_dir: str,
file_friendly_logging: bool = False,
recover: bool = False,
force: bool = False,
cache_directory: str = None,
cache_prefix: str = None) -> Model:
prepare_environment(params)
create_serialization_dir(params, serialization_dir, recover, force)
stdout_handler = prepare_global_logging(serialization_dir, file_friendly_logging)
cuda_device = params.params.get('trainer').get('cuda_device', -1)
check_for_gpu(cuda_device)
params.to_file(os.path.join(serialization_dir, CONFIG_NAME))
evaluate_on_test = params.pop_bool("evaluate_on_test", False)
trainer_type = params.get("trainer", {}).get("type", "default")
if trainer_type == "default":
# Special logic to instantiate backward-compatible trainer.
pieces = TrainerPieces.from_params(params, # pylint: disable=no-member
serialization_dir,
recover,
cache_directory,
cache_prefix)
trainer = Trainer.from_params(
model=pieces.model,
serialization_dir=serialization_dir,
iterator=pieces.iterator,
train_data=pieces.train_dataset,
validation_data=pieces.validation_dataset,
params=pieces.params,
validation_iterator=pieces.validation_iterator)
evaluation_iterator = pieces.validation_iterator or pieces.iterator
evaluation_dataset = pieces.test_dataset
else:
trainer = TrainerBase.from_params(params, serialization_dir, recover)
# TODO(joelgrus): handle evaluation in the general case
evaluation_iterator = evaluation_dataset = None
params.assert_empty('base train command')
try:
metrics = trainer.train()
except KeyboardInterrupt:
# if we have completed an epoch, try to create a model archive.
if os.path.exists(os.path.join(serialization_dir, _DEFAULT_WEIGHTS)):
logging.info("Training interrupted by the user. Attempting to create "
"a model archive using the current best epoch weights.")
archive_model(serialization_dir, files_to_archive=params.files_to_archive)
raise
# Evaluate
if evaluation_dataset and evaluate_on_test:
logger.info("The model will be evaluated using the best epoch weights.")
test_metrics = evaluate(trainer.model, evaluation_dataset, evaluation_iterator,
cuda_device=trainer._cuda_devices[0], # pylint: disable=protected-access,
# TODO(brendanr): Pass in an arg following Joel's trainer refactor.
batch_weight_key="")
for key, value in test_metrics.items():
metrics["test_" + key] = value
elif evaluation_dataset:
logger.info("To evaluate on the test set after training, pass the "
"'evaluate_on_test' flag, or use the 'allennlp evaluate' command.")
cleanup_global_logging(stdout_handler)
# Now tar up results
archive_model(serialization_dir, files_to_archive=params.files_to_archive)
dump_metrics(os.path.join(serialization_dir, "metrics.json"), metrics, log=True)
# We count on the trainer to have the model with best weights
return trainer.model | [
"\n Trains the model specified in the given :class:`Params` object, using the data and training\n parameters also specified in that object, and saves the results in ``serialization_dir``.\n\n Parameters\n ----------\n params : ``Params``\n A parameter object specifying an AllenNLP Experiment.\n serialization_dir : ``str``\n The directory in which to save results and logs.\n file_friendly_logging : ``bool``, optional (default=False)\n If ``True``, we add newlines to tqdm output, even on an interactive terminal, and we slow\n down tqdm's output to only once every 10 seconds.\n recover : ``bool``, optional (default=False)\n If ``True``, we will try to recover a training run from an existing serialization\n directory. This is only intended for use when something actually crashed during the middle\n of a run. For continuing training a model on new data, see the ``fine-tune`` command.\n force : ``bool``, optional (default=False)\n If ``True``, we will overwrite the serialization directory if it already exists.\n cache_directory : ``str``, optional\n For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`.\n cache_prefix : ``str``, optional\n For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`.\n\n Returns\n -------\n best_model: ``Model``\n The model with the best epoch weights.\n "
] |
Please provide a description of the function:def _prf_divide(numerator, denominator):
result = numerator / denominator
mask = denominator == 0.0
if not mask.any():
return result
# remove nan
result[mask] = 0.0
return result | [
"Performs division and handles divide-by-zero.\n\n On zero-division, sets the corresponding result elements to zero.\n "
] |
Please provide a description of the function:def load_data(file_path: str) -> Tuple[List[str], List[str]]:
data = []
with open(file_path) as f:
for line in f:
pairs = line.strip().split()
sentence, tags = zip(*(pair.split("###") for pair in pairs))
data.append((sentence, tags))
return data | [
"\n One sentence per line, formatted like\n\n The###DET dog###NN ate###V the###DET apple###NN\n\n Returns a list of pairs (tokenized_sentence, tags)\n "
] |
Please provide a description of the function:def pop_max_vocab_size(params: Params) -> Union[int, Dict[str, int]]:
size = params.pop("max_vocab_size", None)
if isinstance(size, Params):
# This is the Dict[str, int] case.
return size.as_dict()
elif size is not None:
# This is the int / str case.
return int(size)
else:
return None | [
"\n max_vocab_size limits the size of the vocabulary, not including the @@UNKNOWN@@ token.\n\n max_vocab_size is allowed to be either an int or a Dict[str, int] (or nothing).\n But it could also be a string representing an int (in the case of environment variable\n substitution). So we need some complex logic to handle it.\n "
] |
Please provide a description of the function:def save_to_files(self, directory: str) -> None:
os.makedirs(directory, exist_ok=True)
if os.listdir(directory):
logging.warning("vocabulary serialization directory %s is not empty", directory)
with codecs.open(os.path.join(directory, NAMESPACE_PADDING_FILE), 'w', 'utf-8') as namespace_file:
for namespace_str in self._non_padded_namespaces:
print(namespace_str, file=namespace_file)
for namespace, mapping in self._index_to_token.items():
# Each namespace gets written to its own file, in index order.
with codecs.open(os.path.join(directory, namespace + '.txt'), 'w', 'utf-8') as token_file:
num_tokens = len(mapping)
start_index = 1 if mapping[0] == self._padding_token else 0
for i in range(start_index, num_tokens):
print(mapping[i].replace('\n', '@@NEWLINE@@'), file=token_file) | [
"\n Persist this Vocabulary to files so it can be reloaded later.\n Each namespace corresponds to one file.\n\n Parameters\n ----------\n directory : ``str``\n The directory where we save the serialized vocabulary.\n "
] |
Please provide a description of the function:def from_files(cls, directory: str) -> 'Vocabulary':
logger.info("Loading token dictionary from %s.", directory)
with codecs.open(os.path.join(directory, NAMESPACE_PADDING_FILE), 'r', 'utf-8') as namespace_file:
non_padded_namespaces = [namespace_str.strip() for namespace_str in namespace_file]
vocab = cls(non_padded_namespaces=non_padded_namespaces)
# Check every file in the directory.
for namespace_filename in os.listdir(directory):
if namespace_filename == NAMESPACE_PADDING_FILE:
continue
if namespace_filename.startswith("."):
continue
namespace = namespace_filename.replace('.txt', '')
if any(namespace_match(pattern, namespace) for pattern in non_padded_namespaces):
is_padded = False
else:
is_padded = True
filename = os.path.join(directory, namespace_filename)
vocab.set_from_file(filename, is_padded, namespace=namespace)
return vocab | [
"\n Loads a ``Vocabulary`` that was serialized using ``save_to_files``.\n\n Parameters\n ----------\n directory : ``str``\n The directory containing the serialized vocabulary.\n "
] |
Please provide a description of the function:def set_from_file(self,
filename: str,
is_padded: bool = True,
oov_token: str = DEFAULT_OOV_TOKEN,
namespace: str = "tokens"):
if is_padded:
self._token_to_index[namespace] = {self._padding_token: 0}
self._index_to_token[namespace] = {0: self._padding_token}
else:
self._token_to_index[namespace] = {}
self._index_to_token[namespace] = {}
with codecs.open(filename, 'r', 'utf-8') as input_file:
lines = input_file.read().split('\n')
# Be flexible about having final newline or not
if lines and lines[-1] == '':
lines = lines[:-1]
for i, line in enumerate(lines):
index = i + 1 if is_padded else i
token = line.replace('@@NEWLINE@@', '\n')
if token == oov_token:
token = self._oov_token
self._token_to_index[namespace][token] = index
self._index_to_token[namespace][index] = token
if is_padded:
assert self._oov_token in self._token_to_index[namespace], "OOV token not found!" | [
"\n If you already have a vocabulary file for a trained model somewhere, and you really want to\n use that vocabulary file instead of just setting the vocabulary from a dataset, for\n whatever reason, you can do that with this method. You must specify the namespace to use,\n and we assume that you want to use padding and OOV tokens for this.\n\n Parameters\n ----------\n filename : ``str``\n The file containing the vocabulary to load. It should be formatted as one token per\n line, with nothing else in the line. The index we assign to the token is the line\n number in the file (1-indexed if ``is_padded``, 0-indexed otherwise). Note that this\n file should contain the OOV token string!\n is_padded : ``bool``, optional (default=True)\n Is this vocabulary padded? For token / word / character vocabularies, this should be\n ``True``; while for tag or label vocabularies, this should typically be ``False``. If\n ``True``, we add a padding token with index 0, and we enforce that the ``oov_token`` is\n present in the file.\n oov_token : ``str``, optional (default=DEFAULT_OOV_TOKEN)\n What token does this vocabulary use to represent out-of-vocabulary characters? This\n must show up as a line in the vocabulary file. When we find it, we replace\n ``oov_token`` with ``self._oov_token``, because we only use one OOV token across\n namespaces.\n namespace : ``str``, optional (default=\"tokens\")\n What namespace should we overwrite with this vocab file?\n "
] |
Please provide a description of the function:def from_instances(cls,
instances: Iterable['adi.Instance'],
min_count: Dict[str, int] = None,
max_vocab_size: Union[int, Dict[str, int]] = None,
non_padded_namespaces: Iterable[str] = DEFAULT_NON_PADDED_NAMESPACES,
pretrained_files: Optional[Dict[str, str]] = None,
only_include_pretrained_words: bool = False,
tokens_to_add: Dict[str, List[str]] = None,
min_pretrained_embeddings: Dict[str, int] = None) -> 'Vocabulary':
logger.info("Fitting token dictionary from dataset.")
namespace_token_counts: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int))
for instance in Tqdm.tqdm(instances):
instance.count_vocab_items(namespace_token_counts)
return cls(counter=namespace_token_counts,
min_count=min_count,
max_vocab_size=max_vocab_size,
non_padded_namespaces=non_padded_namespaces,
pretrained_files=pretrained_files,
only_include_pretrained_words=only_include_pretrained_words,
tokens_to_add=tokens_to_add,
min_pretrained_embeddings=min_pretrained_embeddings) | [
"\n Constructs a vocabulary given a collection of `Instances` and some parameters.\n We count all of the vocabulary items in the instances, then pass those counts\n and the other parameters, to :func:`__init__`. See that method for a description\n of what the other parameters do.\n "
] |
Please provide a description of the function:def from_params(cls, params: Params, instances: Iterable['adi.Instance'] = None): # type: ignore
# pylint: disable=arguments-differ
# Vocabulary is ``Registrable`` so that you can configure a custom subclass,
# but (unlike most of our registrables) almost everyone will want to use the
# base implementation. So instead of having an abstract ``VocabularyBase`` or
# such, we just add the logic for instantiating a registered subclass here,
# so that most users can continue doing what they were doing.
vocab_type = params.pop("type", None)
if vocab_type is not None:
return cls.by_name(vocab_type).from_params(params=params, instances=instances)
extend = params.pop("extend", False)
vocabulary_directory = params.pop("directory_path", None)
if not vocabulary_directory and not instances:
raise ConfigurationError("You must provide either a Params object containing a "
"vocab_directory key or a Dataset to build a vocabulary from.")
if extend and not instances:
raise ConfigurationError("'extend' is true but there are not instances passed to extend.")
if extend and not vocabulary_directory:
raise ConfigurationError("'extend' is true but there is not 'directory_path' to extend from.")
if vocabulary_directory and instances:
if extend:
logger.info("Loading Vocab from files and extending it with dataset.")
else:
logger.info("Loading Vocab from files instead of dataset.")
if vocabulary_directory:
vocab = cls.from_files(vocabulary_directory)
if not extend:
params.assert_empty("Vocabulary - from files")
return vocab
if extend:
vocab.extend_from_instances(params, instances=instances)
return vocab
min_count = params.pop("min_count", None)
max_vocab_size = pop_max_vocab_size(params)
non_padded_namespaces = params.pop("non_padded_namespaces", DEFAULT_NON_PADDED_NAMESPACES)
pretrained_files = params.pop("pretrained_files", {})
min_pretrained_embeddings = params.pop("min_pretrained_embeddings", None)
only_include_pretrained_words = params.pop_bool("only_include_pretrained_words", False)
tokens_to_add = params.pop("tokens_to_add", None)
params.assert_empty("Vocabulary - from dataset")
return cls.from_instances(instances=instances,
min_count=min_count,
max_vocab_size=max_vocab_size,
non_padded_namespaces=non_padded_namespaces,
pretrained_files=pretrained_files,
only_include_pretrained_words=only_include_pretrained_words,
tokens_to_add=tokens_to_add,
min_pretrained_embeddings=min_pretrained_embeddings) | [
"\n There are two possible ways to build a vocabulary; from a\n collection of instances, using :func:`Vocabulary.from_instances`, or\n from a pre-saved vocabulary, using :func:`Vocabulary.from_files`.\n You can also extend pre-saved vocabulary with collection of instances\n using this method. This method wraps these options, allowing their\n specification from a ``Params`` object, generated from a JSON\n configuration file.\n\n Parameters\n ----------\n params: Params, required.\n instances: Iterable['adi.Instance'], optional\n If ``params`` doesn't contain a ``directory_path`` key,\n the ``Vocabulary`` can be built directly from a collection of\n instances (i.e. a dataset). If ``extend`` key is set False,\n dataset instances will be ignored and final vocabulary will be\n one loaded from ``directory_path``. If ``extend`` key is set True,\n dataset instances will be used to extend the vocabulary loaded\n from ``directory_path`` and that will be final vocabulary used.\n\n Returns\n -------\n A ``Vocabulary``.\n "
] |
Please provide a description of the function:def _extend(self,
counter: Dict[str, Dict[str, int]] = None,
min_count: Dict[str, int] = None,
max_vocab_size: Union[int, Dict[str, int]] = None,
non_padded_namespaces: Iterable[str] = DEFAULT_NON_PADDED_NAMESPACES,
pretrained_files: Optional[Dict[str, str]] = None,
only_include_pretrained_words: bool = False,
tokens_to_add: Dict[str, List[str]] = None,
min_pretrained_embeddings: Dict[str, int] = None) -> None:
if not isinstance(max_vocab_size, dict):
int_max_vocab_size = max_vocab_size
max_vocab_size = defaultdict(lambda: int_max_vocab_size) # type: ignore
min_count = min_count or {}
pretrained_files = pretrained_files or {}
min_pretrained_embeddings = min_pretrained_embeddings or {}
non_padded_namespaces = set(non_padded_namespaces)
counter = counter or {}
tokens_to_add = tokens_to_add or {}
self._retained_counter = counter
# Make sure vocabulary extension is safe.
current_namespaces = {*self._token_to_index}
extension_namespaces = {*counter, *tokens_to_add}
for namespace in current_namespaces & extension_namespaces:
# if new namespace was already present
# Either both should be padded or none should be.
original_padded = not any(namespace_match(pattern, namespace)
for pattern in self._non_padded_namespaces)
extension_padded = not any(namespace_match(pattern, namespace)
for pattern in non_padded_namespaces)
if original_padded != extension_padded:
raise ConfigurationError("Common namespace {} has conflicting ".format(namespace)+
"setting of padded = True/False. "+
"Hence extension cannot be done.")
# Add new non-padded namespaces for extension
self._token_to_index.add_non_padded_namespaces(non_padded_namespaces)
self._index_to_token.add_non_padded_namespaces(non_padded_namespaces)
self._non_padded_namespaces.update(non_padded_namespaces)
for namespace in counter:
if namespace in pretrained_files:
pretrained_list = _read_pretrained_tokens(pretrained_files[namespace])
min_embeddings = min_pretrained_embeddings.get(namespace, 0)
if min_embeddings > 0:
tokens_old = tokens_to_add.get(namespace, [])
tokens_new = pretrained_list[:min_embeddings]
tokens_to_add[namespace] = tokens_old + tokens_new
pretrained_set = set(pretrained_list)
else:
pretrained_set = None
token_counts = list(counter[namespace].items())
token_counts.sort(key=lambda x: x[1], reverse=True)
try:
max_vocab = max_vocab_size[namespace]
except KeyError:
max_vocab = None
if max_vocab:
token_counts = token_counts[:max_vocab]
for token, count in token_counts:
if pretrained_set is not None:
if only_include_pretrained_words:
if token in pretrained_set and count >= min_count.get(namespace, 1):
self.add_token_to_namespace(token, namespace)
elif token in pretrained_set or count >= min_count.get(namespace, 1):
self.add_token_to_namespace(token, namespace)
elif count >= min_count.get(namespace, 1):
self.add_token_to_namespace(token, namespace)
for namespace, tokens in tokens_to_add.items():
for token in tokens:
self.add_token_to_namespace(token, namespace) | [
"\n This method can be used for extending already generated vocabulary.\n It takes same parameters as Vocabulary initializer. The token2index\n and indextotoken mappings of calling vocabulary will be retained.\n It is an inplace operation so None will be returned.\n "
] |
Please provide a description of the function:def extend_from_instances(self,
params: Params,
instances: Iterable['adi.Instance'] = ()) -> None:
min_count = params.pop("min_count", None)
max_vocab_size = pop_max_vocab_size(params)
non_padded_namespaces = params.pop("non_padded_namespaces", DEFAULT_NON_PADDED_NAMESPACES)
pretrained_files = params.pop("pretrained_files", {})
min_pretrained_embeddings = params.pop("min_pretrained_embeddings", None)
only_include_pretrained_words = params.pop_bool("only_include_pretrained_words", False)
tokens_to_add = params.pop("tokens_to_add", None)
params.assert_empty("Vocabulary - from dataset")
logger.info("Fitting token dictionary from dataset.")
namespace_token_counts: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int))
for instance in Tqdm.tqdm(instances):
instance.count_vocab_items(namespace_token_counts)
self._extend(counter=namespace_token_counts,
min_count=min_count,
max_vocab_size=max_vocab_size,
non_padded_namespaces=non_padded_namespaces,
pretrained_files=pretrained_files,
only_include_pretrained_words=only_include_pretrained_words,
tokens_to_add=tokens_to_add,
min_pretrained_embeddings=min_pretrained_embeddings) | [
"\n Extends an already generated vocabulary using a collection of instances.\n "
] |
Please provide a description of the function:def is_padded(self, namespace: str) -> bool:
return self._index_to_token[namespace][0] == self._padding_token | [
"\n Returns whether or not there are padding and OOV tokens added to the given namespace.\n "
] |
Please provide a description of the function:def add_token_to_namespace(self, token: str, namespace: str = 'tokens') -> int:
if not isinstance(token, str):
raise ValueError("Vocabulary tokens must be strings, or saving and loading will break."
" Got %s (with type %s)" % (repr(token), type(token)))
if token not in self._token_to_index[namespace]:
index = len(self._token_to_index[namespace])
self._token_to_index[namespace][token] = index
self._index_to_token[namespace][index] = token
return index
else:
return self._token_to_index[namespace][token] | [
"\n Adds ``token`` to the index, if it is not already present. Either way, we return the index of\n the token.\n "
] |
Please provide a description of the function:def get_regularization_penalty(self) -> Union[float, torch.Tensor]:
if self._regularizer is None:
return 0.0
else:
return self._regularizer(self) | [
"\n Computes the regularization penalty for the model.\n Returns 0 if the model was not configured to use regularization.\n "
] |
Please provide a description of the function:def forward_on_instance(self, instance: Instance) -> Dict[str, numpy.ndarray]:
return self.forward_on_instances([instance])[0] | [
"\n Takes an :class:`~allennlp.data.instance.Instance`, which typically has raw text in it,\n converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays\n through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing)\n and returns the result. Before returning the result, we convert any\n ``torch.Tensors`` into numpy arrays and remove the batch dimension.\n "
] |
Please provide a description of the function:def forward_on_instances(self,
instances: List[Instance]) -> List[Dict[str, numpy.ndarray]]:
batch_size = len(instances)
with torch.no_grad():
cuda_device = self._get_prediction_device()
dataset = Batch(instances)
dataset.index_instances(self.vocab)
model_input = util.move_to_device(dataset.as_tensor_dict(), cuda_device)
outputs = self.decode(self(**model_input))
instance_separated_output: List[Dict[str, numpy.ndarray]] = [{} for _ in dataset.instances]
for name, output in list(outputs.items()):
if isinstance(output, torch.Tensor):
# NOTE(markn): This is a hack because 0-dim pytorch tensors are not iterable.
# This occurs with batch size 1, because we still want to include the loss in that case.
if output.dim() == 0:
output = output.unsqueeze(0)
if output.size(0) != batch_size:
self._maybe_warn_for_unseparable_batches(name)
continue
output = output.detach().cpu().numpy()
elif len(output) != batch_size:
self._maybe_warn_for_unseparable_batches(name)
continue
for instance_output, batch_element in zip(instance_separated_output, output):
instance_output[name] = batch_element
return instance_separated_output | [
"\n Takes a list of :class:`~allennlp.data.instance.Instance`s, converts that text into\n arrays using this model's :class:`Vocabulary`, passes those arrays through\n :func:`self.forward()` and :func:`self.decode()` (which by default does nothing)\n and returns the result. Before returning the result, we convert any\n ``torch.Tensors`` into numpy arrays and separate the\n batched output into a list of individual dicts per instance. Note that typically\n this will be faster on a GPU (and conditionally, on a CPU) than repeated calls to\n :func:`forward_on_instance`.\n\n Parameters\n ----------\n instances : List[Instance], required\n The instances to run the model on.\n\n Returns\n -------\n A list of the models output for each instance.\n "
] |
Please provide a description of the function:def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
# pylint: disable=no-self-use
return output_dict | [
"\n Takes the result of :func:`forward` and runs inference / decoding / whatever\n post-processing you need to do your model. The intent is that ``model.forward()`` should\n produce potentials or probabilities, and then ``model.decode()`` can take those results and\n run some kind of beam search or constrained inference or whatever is necessary. This does\n not handle all possible decoding use cases, but it at least handles simple kinds of\n decoding.\n\n This method `modifies` the input dictionary, and also `returns` the same dictionary.\n\n By default in the base class we do nothing. If your model has some special decoding step,\n override this method.\n "
] |
Please provide a description of the function:def _get_prediction_device(self) -> int:
devices = {util.get_device_of(param) for param in self.parameters()}
if len(devices) > 1:
devices_string = ", ".join(str(x) for x in devices)
raise ConfigurationError(f"Parameters have mismatching cuda_devices: {devices_string}")
elif len(devices) == 1:
return devices.pop()
else:
return -1 | [
"\n This method checks the device of the model parameters to determine the cuda_device\n this model should be run on for predictions. If there are no parameters, it returns -1.\n\n Returns\n -------\n The cuda device this model should run on for predictions.\n "
] |
Please provide a description of the function:def _maybe_warn_for_unseparable_batches(self, output_key: str):
if output_key not in self._warn_for_unseparable_batches:
logger.warning(f"Encountered the {output_key} key in the model's return dictionary which "
"couldn't be split by the batch size. Key will be ignored.")
# We only want to warn once for this key,
# so we set this to false so we don't warn again.
self._warn_for_unseparable_batches.add(output_key) | [
"\n This method warns once if a user implements a model which returns a dictionary with\n values which we are unable to split back up into elements of the batch. This is controlled\n by a class attribute ``_warn_for_unseperable_batches`` because it would be extremely verbose\n otherwise.\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':
weights_file = weights_file or os.path.join(serialization_dir, _DEFAULT_WEIGHTS)
# Load vocabulary from file
vocab_dir = os.path.join(serialization_dir, 'vocabulary')
# If the config specifies a vocabulary subclass, we need to use it.
vocab_params = config.get("vocabulary", Params({}))
vocab_choice = vocab_params.pop_choice("type", Vocabulary.list_available(), True)
vocab = Vocabulary.by_name(vocab_choice).from_files(vocab_dir)
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=vocab, params=model_params)
# If vocab+embedding extension was done, the model initialized from from_params
# and one defined by state dict in weights_file might not have same embedding shapes.
# Eg. when model embedder module was transferred along with vocab extension, the
# initialized embedding weight shape would be smaller than one in the state_dict.
# So calling model embedding extension is required before load_state_dict.
# If vocab and model embeddings are in sync, following would be just a no-op.
model.extend_embedder_vocab()
model_state = torch.load(weights_file, map_location=util.device_mapping(cuda_device))
model.load_state_dict(model_state)
# 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 Instantiates an already-trained model, based on the experiment\n configuration and some optional overrides.\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':
# Peak at the class of the model.
model_type = config["model"]["type"]
# Load using an overridable _load method.
# This allows subclasses of Model to override _load.
# pylint: disable=protected-access
return cls.by_name(model_type)._load(config, serialization_dir, weights_file, cuda_device) | [
"\n Instantiates an already-trained model, based on the experiment\n configuration and some optional overrides.\n\n Parameters\n ----------\n config: Params\n The configuration that was used to train the model. It should definitely\n have a `model` section, and should probably have a `trainer` section\n as well.\n serialization_dir: str = None\n The directory containing the serialized weights, parameters, and vocabulary\n of the model.\n weights_file: str = None\n By default we load the weights from `best.th` in the serialization\n directory, but you can override that value here.\n cuda_device: int = -1\n By default we load the model on the CPU, but if you want to load it\n for GPU usage you can specify the id of your GPU here\n\n\n Returns\n -------\n model: Model\n The model specified in the configuration, loaded with the serialized\n vocabulary and the trained weights.\n "
] |
Please provide a description of the function:def extend_embedder_vocab(self, embedding_sources_mapping: Dict[str, str] = None) -> None:
# self.named_modules() gives all sub-modules (including nested children)
# The path nesting is already separated by ".": eg. parent_module_name.child_module_name
embedding_sources_mapping = embedding_sources_mapping or {}
for model_path, module in self.named_modules():
if hasattr(module, 'extend_vocab'):
pretrained_file = embedding_sources_mapping.get(model_path, None)
module.extend_vocab(self.vocab,
extension_pretrained_file=pretrained_file,
model_path=model_path) | [
"\n Iterates through all embedding modules in the model and assures it can embed\n with the extended vocab. This is required in fine-tuning or transfer learning\n scenarios where model was trained with original vocabulary but during\n fine-tuning/tranfer-learning, it will have it work with extended vocabulary\n (original + new-data vocabulary).\n\n Parameters\n ----------\n embedding_sources_mapping : Dict[str, str], (optional, default=None)\n Mapping from model_path to pretrained-file path of the embedding\n modules. If pretrained-file used at time of embedding initialization\n isn't available now, user should pass this mapping. Model path is\n path traversing the model attributes upto this embedding module.\n Eg. \"_text_field_embedder.token_embedder_tokens\".\n "
] |
Please provide a description of the function:def get_agenda(self,
conservative: bool = False):
agenda_items = []
question_tokens = [token.text for token in self.table_context.question_tokens]
question = " ".join(question_tokens)
added_number_filters = False
if self._table_has_number_columns:
if "at least" in question:
agenda_items.append("filter_number_greater_equals")
if "at most" in question:
agenda_items.append("filter_number_lesser_equals")
comparison_triggers = ["greater", "larger", "more"]
if any(f"no {word} than" in question for word in comparison_triggers):
agenda_items.append("filter_number_lesser_equals")
elif any(f"{word} than" in question for word in comparison_triggers):
agenda_items.append("filter_number_greater")
# We want to keep track of this because we do not want to add both number and date
# filters to the agenda if we want to be conservative.
if agenda_items:
added_number_filters = True
for token in question_tokens:
if token in ["next", "below"] or (token == "after" and not conservative):
agenda_items.append("next")
if token in ["previous", "above"] or (token == "before" and not conservative):
agenda_items.append("previous")
if token in ["first", "top"]:
agenda_items.append("first")
if token in ["last", "bottom"]:
agenda_items.append("last")
if token == "same":
agenda_items.append("same_as")
if self._table_has_number_columns:
# "total" does not always map to an actual summing operation.
if token == "total" and not conservative:
agenda_items.append("sum")
if token == "difference" or "how many more" in question or "how much more" in question:
agenda_items.append("diff")
if token == "average":
agenda_items.append("average")
if token in ["least", "smallest", "shortest", "lowest"] and "at least" not in question:
# This condition is too brittle. But for most logical forms with "min", there are
# semantically equivalent ones with "argmin". The exceptions are rare.
if "what is the least" not in question:
agenda_items.append("argmin")
if token in ["most", "largest", "highest", "longest", "greatest"] and "at most" not in question:
# This condition is too brittle. But for most logical forms with "max", there are
# semantically equivalent ones with "argmax". The exceptions are rare.
if "what is the most" not in question:
agenda_items.append("argmax")
if self._table_has_date_columns:
if token in MONTH_NUMBERS or (token.isdigit() and len(token) == 4 and
int(token) < 2100 and int(token) > 1100):
# Token is either a month or an year. We'll add date functions.
if not added_number_filters or not conservative:
if "after" in question_tokens:
agenda_items.append("filter_date_greater")
elif "before" in question_tokens:
agenda_items.append("filter_date_lesser")
elif "not" in question_tokens:
agenda_items.append("filter_date_not_equals")
else:
agenda_items.append("filter_date_equals")
if "what is the least" in question and self._table_has_number_columns:
agenda_items.append("min_number")
if "what is the most" in question and self._table_has_number_columns:
agenda_items.append("max_number")
if "when" in question_tokens and self._table_has_date_columns:
if "last" in question_tokens:
agenda_items.append("max_date")
elif "first" in question_tokens:
agenda_items.append("min_date")
else:
agenda_items.append("select_date")
if "how many" in question:
if "sum" not in agenda_items and "average" not in agenda_items:
# The question probably just requires counting the rows. But this is not very
# accurate. The question could also be asking for a value that is in the table.
agenda_items.append("count")
agenda = []
# Adding productions from the global set.
for agenda_item in set(agenda_items):
# Some agenda items may not be present in the terminal productions because some of these
# terminals are table-content specific. For example, if the question triggered "sum",
# and the table does not have number columns, we should not add "<r,<f,n>> -> sum" to
# the agenda.
if agenda_item in self.terminal_productions:
agenda.append(self.terminal_productions[agenda_item])
if conservative:
# Some of the columns in the table have multiple types, and thus occur in the KG as
# different columns. We do not want to add them all to the agenda if their names,
# because it is unlikely that logical forms use them all. In fact, to be conservative,
# we won't add any of them. So we'll first identify such column names.
refined_column_productions: Dict[str, str] = {}
for column_name, signature in self._column_productions_for_agenda.items():
column_type, name = column_name.split(":")
if column_type == "string_column":
if f"number_column:{name}" not in self._column_productions_for_agenda and \
f"date_column:{name}" not in self._column_productions_for_agenda:
refined_column_productions[column_name] = signature
elif column_type == "number_column":
if f"string_column:{name}" not in self._column_productions_for_agenda and \
f"date_column:{name}" not in self._column_productions_for_agenda:
refined_column_productions[column_name] = signature
else:
if f"string_column:{name}" not in self._column_productions_for_agenda and \
f"number_column:{name}" not in self._column_productions_for_agenda:
refined_column_productions[column_name] = signature
# Similarly, we do not want the same spans in the question to be added to the agenda as
# both string and number productions.
refined_entities: List[str] = []
refined_numbers: List[str] = []
for entity in self._question_entities:
if entity.replace("string:", "") not in self._question_numbers:
refined_entities.append(entity)
for number in self._question_numbers:
if f"string:{number}" not in self._question_entities:
refined_numbers.append(number)
else:
refined_column_productions = dict(self._column_productions_for_agenda)
refined_entities = list(self._question_entities)
refined_numbers = list(self._question_numbers)
# Adding column names that occur in question.
question_with_underscores = "_".join(question_tokens)
normalized_question = re.sub("[^a-z0-9_]", "", question_with_underscores)
# We keep track of tokens that are in column names being added to the agenda. We will not
# add string productions to the agenda if those tokens were already captured as column
# names.
# Note: If the same string occurs multiple times, this may cause string productions being
# omitted from the agenda unnecessarily. That is fine, as we want to err on the side of
# adding fewer rules to the agenda.
tokens_in_column_names: Set[str] = set()
for column_name_with_type, signature in refined_column_productions.items():
column_name = column_name_with_type.split(":")[1]
# Underscores ensure that the match is of whole words.
if f"_{column_name}_" in normalized_question:
agenda.append(signature)
for token in column_name.split("_"):
tokens_in_column_names.add(token)
# Adding all productions that lead to entities and numbers extracted from the question.
for entity in refined_entities:
if entity.replace("string:", "") not in tokens_in_column_names:
agenda.append(f"str -> {entity}")
for number in refined_numbers:
# The reason we check for the presence of the number in the question again is because
# some of these numbers are extracted from number words like month names and ordinals
# like "first". On looking at some agenda outputs, I found that they hurt more than help
# in the agenda.
if f"_{number}_" in normalized_question:
agenda.append(f"Number -> {number}")
return agenda | [
"\n Returns an agenda that can be used guide search.\n\n Parameters\n ----------\n conservative : ``bool``\n Setting this flag will return a subset of the agenda items that correspond to high\n confidence lexical matches. You'll need this if you are going to use this agenda to\n penalize a model for producing logical forms that do not contain some items in it. In\n that case, you'll want this agenda to have close to perfect precision, at the cost of a\n lower recall. You may not want to set this flag if you are sorting the output from a\n search procedure based on how much of this agenda is satisfied.\n "
] |
Please provide a description of the function:def evaluate_logical_form(self, logical_form: str, target_list: List[str]) -> bool:
normalized_target_list = [TableQuestionContext.normalize_string(value) for value in
target_list]
target_value_list = evaluator.to_value_list(normalized_target_list)
try:
denotation = self.execute(logical_form)
except ExecutionError:
logger.warning(f'Failed to execute: {logical_form}')
return False
if isinstance(denotation, list):
denotation_list = [str(denotation_item) for denotation_item in denotation]
else:
denotation_list = [str(denotation)]
denotation_value_list = evaluator.to_value_list(denotation_list)
return evaluator.check_denotation(target_value_list, denotation_value_list) | [
"\n Takes a logical form, and the list of target values as strings from the original lisp\n string, and returns True iff the logical form executes to the target list, using the\n official WikiTableQuestions evaluation script.\n "
] |
Please provide a description of the function:def select_string(self, rows: List[Row], column: StringColumn) -> List[str]:
return [str(row.values[column.name]) for row in rows if row.values[column.name] is not None] | [
"\n Select function takes a list of rows and a column name and returns a list of strings as\n in cells.\n "
] |
Please provide a description of the function:def select_number(self, rows: List[Row], column: NumberColumn) -> Number:
numbers: List[float] = []
for row in rows:
cell_value = row.values[column.name]
if isinstance(cell_value, float):
numbers.append(cell_value)
return numbers[0] if numbers else -1 | [
"\n Select function takes a row (as a list) and a column name and returns the number in that\n column. If multiple rows are given, will return the first number that is not None.\n "
] |
Please provide a description of the function:def select_date(self, rows: List[Row], column: DateColumn) -> Date:
dates: List[Date] = []
for row in rows:
cell_value = row.values[column.name]
if isinstance(cell_value, Date):
dates.append(cell_value)
return dates[0] if dates else Date(-1, -1, -1) | [
"\n Select function takes a row as a list and a column name and returns the date in that column.\n "
] |
Please provide a description of the function:def same_as(self, rows: List[Row], column: Column) -> List[Row]:
cell_value = rows[0].values[column.name]
return_list = []
for table_row in self.table_data:
if table_row.values[column.name] == cell_value:
return_list.append(table_row)
return return_list | [
"\n Takes a row and a column and returns a list of rows from the full set of rows that contain\n the same value under the given column as the given row.\n "
] |
Please provide a description of the function:def date(self, year: Number, month: Number, day: Number) -> Date:
return Date(year, month, day) | [
"\n Takes three numbers and returns a ``Date`` object whose year, month, and day are the three\n numbers in that order.\n "
] |
Please provide a description of the function:def first(self, rows: List[Row]) -> List[Row]:
if not rows:
logger.warning("Trying to get first row from an empty list")
return []
return [rows[0]] | [
"\n Takes an expression that evaluates to a list of rows, and returns the first one in that\n list.\n "
] |
Please provide a description of the function:def last(self, rows: List[Row]) -> List[Row]:
if not rows:
logger.warning("Trying to get last row from an empty list")
return []
return [rows[-1]] | [
"\n Takes an expression that evaluates to a list of rows, and returns the last one in that\n list.\n "
] |
Please provide a description of the function:def previous(self, rows: List[Row]) -> List[Row]:
if not rows:
return []
input_row_index = self._get_row_index(rows[0])
if input_row_index > 0:
return [self.table_data[input_row_index - 1]]
return [] | [
"\n Takes an expression that evaluates to a single row, and returns the row that occurs before\n the input row in the original set of rows. If the input row happens to be the top row, we\n will return an empty list.\n "
] |
Please provide a description of the function:def next(self, rows: List[Row]) -> List[Row]:
if not rows:
return []
input_row_index = self._get_row_index(rows[0])
if input_row_index < len(self.table_data) - 1 and input_row_index != -1:
return [self.table_data[input_row_index + 1]]
return [] | [
"\n Takes an expression that evaluates to a single row, and returns the row that occurs after\n the input row in the original set of rows. If the input row happens to be the last row, we\n will return an empty list.\n "
] |
Please provide a description of the function:def mode_string(self, rows: List[Row], column: StringColumn) -> List[str]:
most_frequent_list = self._get_most_frequent_values(rows, column)
if not most_frequent_list:
return []
if not all([isinstance(value, str) for value in most_frequent_list]):
raise ExecutionError(f"Invalid values for mode_string: {most_frequent_list}")
return most_frequent_list | [
"\n Takes a list of rows and a column and returns the most frequent values (one or more) under\n that column in those rows.\n "
] |
Please provide a description of the function:def mode_number(self, rows: List[Row], column: NumberColumn) -> Number:
most_frequent_list = self._get_most_frequent_values(rows, column)
if not most_frequent_list:
return 0.0 # type: ignore
most_frequent_value = most_frequent_list[0]
if not isinstance(most_frequent_value, Number):
raise ExecutionError(f"Invalid valus for mode_number: {most_frequent_value}")
return most_frequent_value | [
"\n Takes a list of rows and a column and returns the most frequent value under\n that column in those rows.\n "
] |
Please provide a description of the function:def mode_date(self, rows: List[Row], column: DateColumn) -> Date:
most_frequent_list = self._get_most_frequent_values(rows, column)
if not most_frequent_list:
return Date(-1, -1, -1)
most_frequent_value = most_frequent_list[0]
if not isinstance(most_frequent_value, Date):
raise ExecutionError(f"Invalid valus for mode_date: {most_frequent_value}")
return most_frequent_value | [
"\n Takes a list of rows and a column and returns the most frequent value under\n that column in those rows.\n "
] |
Please provide a description of the function:def argmax(self, rows: List[Row], column: ComparableColumn) -> List[Row]:
if not rows:
return []
value_row_pairs = [(row.values[column.name], row) for row in rows]
if not value_row_pairs:
return []
# Returns a list containing the row with the max cell value.
return [sorted(value_row_pairs, key=lambda x: x[0], reverse=True)[0][1]] | [
"\n Takes a list of rows and a column name and returns a list containing a single row (dict from\n columns to cells) that has the maximum numerical value in the given column. We return a list\n instead of a single dict to be consistent with the return type of ``select`` and\n ``all_rows``.\n "
] |
Please provide a description of the function:def argmin(self, rows: List[Row], column: ComparableColumn) -> List[Row]:
if not rows:
return []
value_row_pairs = [(row.values[column.name], row) for row in rows]
if not value_row_pairs:
return []
# Returns a list containing the row with the max cell value.
return [sorted(value_row_pairs, key=lambda x: x[0])[0][1]] | [
"\n Takes a list of rows and a column and returns a list containing a single row (dict from\n columns to cells) that has the minimum numerical value in the given column. We return a list\n instead of a single dict to be consistent with the return type of ``select`` and\n ``all_rows``.\n "
] |
Please provide a description of the function:def max_date(self, rows: List[Row], column: DateColumn) -> Date:
cell_values = [row.values[column.name] for row in rows]
if not cell_values:
return Date(-1, -1, -1)
if not all([isinstance(value, Date) for value in cell_values]):
raise ExecutionError(f"Invalid values for date selection function: {cell_values}")
return max(cell_values) | [
"\n Takes a list of rows and a column and returns the max of the values under that column in\n those rows.\n "
] |
Please provide a description of the function:def max_number(self, rows: List[Row], column: NumberColumn) -> Number:
cell_values = [row.values[column.name] for row in rows]
if not cell_values:
return 0.0 # type: ignore
if not all([isinstance(value, Number) for value in cell_values]):
raise ExecutionError(f"Invalid values for number selection function: {cell_values}")
return max(cell_values) | [
"\n Takes a list of rows and a column and returns the max of the values under that column in\n those rows.\n "
] |
Please provide a description of the function:def average(self, rows: List[Row], column: NumberColumn) -> Number:
cell_values = [row.values[column.name] for row in rows]
if not cell_values:
return 0.0 # type: ignore
return sum(cell_values) / len(cell_values) | [
"\n Takes a list of rows and a column and returns the mean of the values under that column in\n those rows.\n "
] |
Please provide a description of the function:def diff(self, first_row: List[Row], second_row: List[Row], column: NumberColumn) -> Number:
if not first_row or not second_row:
return 0.0 # type: ignore
first_value = first_row[0].values[column.name]
second_value = second_row[0].values[column.name]
if isinstance(first_value, float) and isinstance(second_value, float):
return first_value - second_value # type: ignore
else:
raise ExecutionError(f"Invalid column for diff: {column.name}") | [
"\n Takes a two rows and a number column and returns the difference between the values under\n that column in those two rows.\n "
] |
Please provide a description of the function:def _get_row_index(self, row: Row) -> int:
row_index = -1
for index, table_row in enumerate(self.table_data):
if table_row.values == row.values:
row_index = index
break
return row_index | [
"\n Takes a row and returns its index in the full list of rows. If the row does not occur in the\n table (which should never happen because this function will only be called with a row that\n is the result of applying one or more functions on the table rows), the method returns -1.\n "
] |
Please provide a description of the function:def is_terminal(self, symbol: str) -> bool:
# We special-case 'lambda' here because it behaves weirdly in action sequences.
return (symbol in self.global_name_mapping or
symbol in self.local_name_mapping or
'lambda' in symbol) | [
"\n This function will be called on nodes of a logical form tree, which are either non-terminal\n symbols that can be expanded or terminal symbols that must be leaf nodes. Returns ``True``\n if the given symbol is a terminal symbol.\n "
] |
Please provide a description of the function:def get_paths_to_root(self,
action: str,
max_path_length: int = 20,
beam_size: int = 30,
max_num_paths: int = 10) -> List[List[str]]:
action_left_side, _ = action.split(' -> ')
right_side_indexed_actions = self._get_right_side_indexed_actions()
lists_to_expand: List[Tuple[str, List[str]]] = [(action_left_side, [action])]
completed_paths = []
while lists_to_expand:
need_to_expand = False
for left_side, path in lists_to_expand:
if left_side == types.START_SYMBOL:
completed_paths.append(path)
else:
need_to_expand = True
if not need_to_expand or len(completed_paths) >= max_num_paths:
break
# We keep track of finished and unfinished lists separately because we truncate the beam
# later, and we want the finished lists to be at the top of the beam.
finished_new_lists = []
unfinished_new_lists = []
for left_side, actions in lists_to_expand:
for next_left_side, next_action in right_side_indexed_actions[left_side]:
if next_action in actions:
# Ignoring paths with loops (of size 1)
continue
new_actions = list(actions)
new_actions.append(next_action)
# Ignoring lists that are too long, and have too many repetitions.
path_length = len(new_actions)
if path_length <= max_path_length or next_left_side == types.START_SYMBOL:
if next_left_side == types.START_SYMBOL:
finished_new_lists.append((next_left_side, new_actions))
else:
unfinished_new_lists.append((next_left_side, new_actions))
new_lists = finished_new_lists + unfinished_new_lists
lists_to_expand = new_lists[:beam_size]
return completed_paths[:max_num_paths] | [
"\n For a given action, returns at most ``max_num_paths`` paths to the root (production with\n ``START_SYMBOL``) that are not longer than ``max_path_length``.\n "
] |
Please provide a description of the function:def get_multi_match_mapping(self) -> Dict[Type, List[Type]]:
if self._multi_match_mapping is None:
self._multi_match_mapping = {}
basic_types = self.get_basic_types()
for basic_type in basic_types:
if isinstance(basic_type, types.MultiMatchNamedBasicType):
matched_types: List[str] = []
# We need to check if each type in the `types_to_match` field for the given
# MultiMatchNamedBasic type is itself in the set of basic types allowed in this
# world, and add it to the mapping only if it is. Some basic types that the
# multi match type can match with may be diallowed in the world due to the
# instance-specific context.
for type_ in basic_type.types_to_match:
if type_ in basic_types:
matched_types.append(type_)
self._multi_match_mapping[basic_type] = matched_types
return self._multi_match_mapping | [
"\n Returns a mapping from each `MultiMatchNamedBasicType` to all the `NamedBasicTypes` that it\n matches.\n "
] |
Please provide a description of the function:def parse_logical_form(self,
logical_form: str,
remove_var_function: bool = True) -> Expression:
if not logical_form.startswith("("):
logical_form = f"({logical_form})"
if remove_var_function:
# Replace "(x)" with "x"
logical_form = re.sub(r'\(([x-z])\)', r'\1', logical_form)
# Replace "(var x)" with "(x)"
logical_form = re.sub(r'\(var ([x-z])\)', r'(\1)', logical_form)
parsed_lisp = semparse_util.lisp_to_nested_expression(logical_form)
translated_string = self._process_nested_expression(parsed_lisp)
type_signature = self.local_type_signatures.copy()
type_signature.update(self.global_type_signatures)
return self._logic_parser.parse(translated_string, signature=type_signature) | [
"\n Takes a logical form as a string, maps its tokens using the mapping and returns a parsed expression.\n\n Parameters\n ----------\n logical_form : ``str``\n Logical form to parse\n remove_var_function : ``bool`` (optional)\n ``var`` is a special function that some languages use within lambda functions to\n indicate the usage of a variable. If your language uses it, and you do not want to\n include it in the parsed expression, set this flag. You may want to do this if you are\n generating an action sequence from this parsed expression, because it is easier to let\n the decoder not produce this function due to the way constrained decoding is currently\n implemented.\n "
] |
Please provide a description of the function:def get_action_sequence(self, expression: Expression) -> List[str]:
# Starting with the type of the whole expression
return self._get_transitions(expression,
[f"{types.START_TYPE} -> {expression.type}"]) | [
"\n Returns the sequence of actions (as strings) that resulted in the given expression.\n "
] |
Please provide a description of the function:def get_logical_form(self,
action_sequence: List[str],
add_var_function: bool = True) -> str:
# Basic outline: we assume that the bracketing that we get in the RHS of each action is the
# correct bracketing for reconstructing the logical form. This is true when there is no
# currying in the action sequence. Given this assumption, we just need to construct a tree
# from the action sequence, then output all of the leaves in the tree, with brackets around
# the children of all non-terminal nodes.
remaining_actions = [action.split(" -> ") for action in action_sequence]
tree = Tree(remaining_actions[0][1], [])
try:
remaining_actions = self._construct_node_from_actions(tree,
remaining_actions[1:],
add_var_function)
except ParsingError:
logger.error("Error parsing action sequence: %s", action_sequence)
raise
if remaining_actions:
logger.error("Error parsing action sequence: %s", action_sequence)
logger.error("Remaining actions were: %s", remaining_actions)
raise ParsingError("Extra actions in action sequence")
return nltk_tree_to_logical_form(tree) | [
"\n Takes an action sequence and constructs a logical form from it. This is useful if you want\n to get a logical form from a decoded sequence of actions generated by a transition based\n semantic parser.\n\n Parameters\n ----------\n action_sequence : ``List[str]``\n The sequence of actions as strings (eg.: ``['{START_SYMBOL} -> t', 't -> <e,t>', ...]``).\n add_var_function : ``bool`` (optional)\n ``var`` is a special function that some languages use within lambda functions to\n indicate the use of a variable (eg.: ``(lambda x (fb:row.row.year (var x)))``). Due to\n the way constrained decoding is currently implemented, it is easier for the decoder to\n not produce these functions. In that case, setting this flag adds the function in the\n logical form even though it is not present in the action sequence.\n "
] |
Please provide a description of the function:def _construct_node_from_actions(self,
current_node: Tree,
remaining_actions: List[List[str]],
add_var_function: bool) -> List[List[str]]:
if not remaining_actions:
logger.error("No actions left to construct current node: %s", current_node)
raise ParsingError("Incomplete action sequence")
left_side, right_side = remaining_actions.pop(0)
if left_side != current_node.label():
mismatch = True
multi_match_mapping = {str(key): [str(value) for value in values] for key,
values in self.get_multi_match_mapping().items()}
current_label = current_node.label()
if current_label in multi_match_mapping and left_side in multi_match_mapping[current_label]:
mismatch = False
if mismatch:
logger.error("Current node: %s", current_node)
logger.error("Next action: %s -> %s", left_side, right_side)
logger.error("Remaining actions were: %s", remaining_actions)
raise ParsingError("Current node does not match next action")
if right_side[0] == '[':
# This is a non-terminal expansion, with more than one child node.
for child_type in right_side[1:-1].split(', '):
if child_type.startswith("'lambda"):
# We need to special-case the handling of lambda here, because it's handled a
# bit weirdly in the action sequence. This is stripping off the single quotes
# around something like `'lambda x'`.
child_type = child_type[1:-1]
child_node = Tree(child_type, [])
current_node.append(child_node) # you add a child to an nltk.Tree with `append`
if not self.is_terminal(child_type):
remaining_actions = self._construct_node_from_actions(child_node,
remaining_actions,
add_var_function)
elif self.is_terminal(right_side):
# The current node is a pre-terminal; we'll add a single terminal child. We need to
# check first for whether we need to add a (var _) around the terminal node, though.
if add_var_function and right_side in self._lambda_variables:
right_side = f"(var {right_side})"
if add_var_function and right_side == 'var':
raise ParsingError('add_var_function was true, but action sequence already had var')
current_node.append(Tree(right_side, [])) # you add a child to an nltk.Tree with `append`
else:
# The only way this can happen is if you have a unary non-terminal production rule.
# That is almost certainly not what you want with this kind of grammar, so we'll crash.
# If you really do want this, open a PR with a valid use case.
raise ParsingError(f"Found a unary production rule: {left_side} -> {right_side}. "
"Are you sure you want a unary production rule in your grammar?")
return remaining_actions | [
"\n Given a current node in the logical form tree, and a list of actions in an action sequence,\n this method fills in the children of the current node from the action sequence, then\n returns whatever actions are left.\n\n For example, we could get a node with type ``c``, and an action sequence that begins with\n ``c -> [<r,c>, r]``. This method will add two children to the input node, consuming\n actions from the action sequence for nodes of type ``<r,c>`` (and all of its children,\n recursively) and ``r`` (and all of its children, recursively). This method assumes that\n action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>``\n appear before actions for the subtree under ``r``. If there are any actions in the action\n sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be\n returned.\n "
] |
Please provide a description of the function:def _infer_num_arguments(cls, type_signature: str) -> int:
if not "<" in type_signature:
return 0
# We need to find the return type from the signature. We do that by removing the outer most
# angular brackets and traversing the remaining substring till the angular brackets (if any)
# balance. Once we hit a comma after the angular brackets are balanced, whatever is left
# after it is the return type.
type_signature = type_signature[1:-1]
num_brackets = 0
char_index = 0
for char in type_signature:
if char == '<':
num_brackets += 1
elif char == '>':
num_brackets -= 1
elif char == ',':
if num_brackets == 0:
break
char_index += 1
return_type = type_signature[char_index+1:]
return 1 + cls._infer_num_arguments(return_type) | [
"\n Takes a type signature and infers the number of arguments the corresponding function takes.\n Examples:\n e -> 0\n <r,e> -> 1\n <e,<e,t>> -> 2\n <b,<<b,#1>,<#1,b>>> -> 3\n "
] |
Please provide a description of the function:def _process_nested_expression(self, nested_expression) -> str:
expression_is_list = isinstance(nested_expression, list)
expression_size = len(nested_expression)
if expression_is_list and expression_size == 1 and isinstance(nested_expression[0], list):
return self._process_nested_expression(nested_expression[0])
elements_are_leaves = [isinstance(element, str) for element in nested_expression]
if all(elements_are_leaves):
mapped_names = [self._map_name(name) for name in nested_expression]
else:
mapped_names = []
for element, is_leaf in zip(nested_expression, elements_are_leaves):
if is_leaf:
mapped_names.append(self._map_name(element))
else:
mapped_names.append(self._process_nested_expression(element))
if mapped_names[0] == "\\":
# This means the predicate is lambda. NLTK wants the variable name to not be within parantheses.
# Adding parentheses after the variable.
arguments = [mapped_names[1]] + [f"({name})" for name in mapped_names[2:]]
else:
arguments = [f"({name})" for name in mapped_names[1:]]
return f'({mapped_names[0]} {" ".join(arguments)})' | [
"\n ``nested_expression`` is the result of parsing a logical form in Lisp format.\n We process it recursively and return a string in the format that NLTK's ``LogicParser``\n would understand.\n "
] |
Please provide a description of the function:def _add_name_mapping(self, name: str, translated_name: str, name_type: Type = None):
self.local_name_mapping[name] = translated_name
self.reverse_name_mapping[translated_name] = name
if name_type:
self.local_type_signatures[translated_name] = name_type | [
"\n Utility method to add a name and its translation to the local name mapping, and the corresponding\n signature, if available to the local type signatures. This method also updates the reverse name\n mapping.\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):
if not isinstance(inputs, PackedSequence):
raise ConfigurationError('inputs must be PackedSequence but got %s' % (type(inputs)))
sequence_tensor, batch_lengths = pad_packed_sequence(inputs, batch_first=True)
batch_size = sequence_tensor.size()[0]
total_timesteps = sequence_tensor.size()[1]
output_accumulator = sequence_tensor.new_zeros(batch_size, total_timesteps, self.hidden_size)
if initial_state is None:
full_batch_previous_memory = sequence_tensor.new_zeros(batch_size, self.hidden_size)
full_batch_previous_state = sequence_tensor.new_zeros(batch_size, self.hidden_size)
else:
full_batch_previous_state = initial_state[0].squeeze(0)
full_batch_previous_memory = initial_state[1].squeeze(0)
current_length_index = batch_size - 1 if self.go_forward else 0
if self.recurrent_dropout_probability > 0.0:
dropout_mask = get_dropout_mask(self.recurrent_dropout_probability, full_batch_previous_memory)
else:
dropout_mask = None
for timestep in range(total_timesteps):
# The index depends on which end we start.
index = timestep if self.go_forward else total_timesteps - timestep - 1
# What we are doing here is finding the index into the batch dimension
# which we need to use for this timestep, because the sequences have
# variable length, so once the index is greater than the length of this
# particular batch sequence, we no longer need to do the computation for
# this sequence. The key thing to recognise here is that the batch inputs
# must be _ordered_ by length from longest (first in batch) to shortest
# (last) so initially, we are going forwards with every sequence and as we
# pass the index at which the shortest elements of the batch finish,
# we stop picking them up for the computation.
if self.go_forward:
while batch_lengths[current_length_index] <= index:
current_length_index -= 1
# If we're going backwards, we are _picking up_ more indices.
else:
# First conditional: Are we already at the maximum number of elements in the batch?
# Second conditional: Does the next shortest sequence beyond the current batch
# index require computation use this timestep?
while current_length_index < (len(batch_lengths) - 1) and \
batch_lengths[current_length_index + 1] > index:
current_length_index += 1
# Actually get the slices of the batch which we need for the computation at this timestep.
previous_memory = full_batch_previous_memory[0: current_length_index + 1].clone()
previous_state = full_batch_previous_state[0: current_length_index + 1].clone()
# Only do recurrent dropout if the dropout prob is > 0.0 and we are in training mode.
if dropout_mask is not None and self.training:
previous_state = previous_state * dropout_mask[0: current_length_index + 1]
timestep_input = sequence_tensor[0: current_length_index + 1, index]
# Do the projections for all the gates all at once.
projected_input = self.input_linearity(timestep_input)
projected_state = self.state_linearity(previous_state)
# Main LSTM equations using relevant chunks of the big linear
# projections of the hidden state and inputs.
input_gate = torch.sigmoid(projected_input[:, 0 * self.hidden_size:1 * self.hidden_size] +
projected_state[:, 0 * self.hidden_size:1 * self.hidden_size])
forget_gate = torch.sigmoid(projected_input[:, 1 * self.hidden_size:2 * self.hidden_size] +
projected_state[:, 1 * self.hidden_size:2 * self.hidden_size])
memory_init = torch.tanh(projected_input[:, 2 * self.hidden_size:3 * self.hidden_size] +
projected_state[:, 2 * self.hidden_size:3 * self.hidden_size])
output_gate = torch.sigmoid(projected_input[:, 3 * self.hidden_size:4 * self.hidden_size] +
projected_state[:, 3 * self.hidden_size:4 * self.hidden_size])
memory = input_gate * memory_init + forget_gate * previous_memory
timestep_output = output_gate * torch.tanh(memory)
if self.use_highway:
highway_gate = torch.sigmoid(projected_input[:, 4 * self.hidden_size:5 * self.hidden_size] +
projected_state[:, 4 * self.hidden_size:5 * self.hidden_size])
highway_input_projection = projected_input[:, 5 * self.hidden_size:6 * self.hidden_size]
timestep_output = highway_gate * timestep_output + (1 - highway_gate) * highway_input_projection
# We've been doing computation with less than the full batch, so here we create a new
# variable for the the whole batch at this timestep and insert the result for the
# relevant elements of the batch into it.
full_batch_previous_memory = full_batch_previous_memory.clone()
full_batch_previous_state = full_batch_previous_state.clone()
full_batch_previous_memory[0:current_length_index + 1] = memory
full_batch_previous_state[0:current_length_index + 1] = timestep_output
output_accumulator[0:current_length_index + 1, index] = timestep_output
output_accumulator = pack_padded_sequence(output_accumulator, batch_lengths, batch_first=True)
# Mimic the pytorch API by returning state in the following shape:
# (num_layers * num_directions, batch_size, hidden_size). As this
# LSTM cannot be stacked, the first dimension here is just 1.
final_state = (full_batch_previous_state.unsqueeze(0),
full_batch_previous_memory.unsqueeze(0))
return output_accumulator, final_state | [
"\n Parameters\n ----------\n inputs : PackedSequence, required.\n A tensor of shape (batch_size, num_timesteps, input_size)\n to apply the LSTM over.\n\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 A PackedSequence containing a torch.FloatTensor of shape\n (batch_size, num_timesteps, output_dimension) representing\n the outputs of the LSTM per timestep and a tuple containing\n the LSTM state, with shape (1, batch_size, hidden_size) to\n match the Pytorch API.\n "
] |
Please provide a description of the function:def _create_sempre_executor(self) -> None:
if self._executor_process:
return
# It'd be much nicer to just use `cached_path` for these files. However, the SEMPRE jar
# that we're using expects to find these files in a particular location, so we need to make
# sure we put the files in that location.
os.makedirs(SEMPRE_DIR, exist_ok=True)
abbreviations_path = os.path.join(SEMPRE_DIR, 'abbreviations.tsv')
if not os.path.exists(abbreviations_path):
result = requests.get(ABBREVIATIONS_FILE)
with open(abbreviations_path, 'wb') as downloaded_file:
downloaded_file.write(result.content)
grammar_path = os.path.join(SEMPRE_DIR, 'grow.grammar')
if not os.path.exists(grammar_path):
result = requests.get(GROW_FILE)
with open(grammar_path, 'wb') as downloaded_file:
downloaded_file.write(result.content)
if not check_for_java():
raise RuntimeError('Java is not installed properly.')
args = ['java', '-jar', cached_path(SEMPRE_EXECUTOR_JAR), 'serve', self._table_directory]
self._executor_process = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
bufsize=1)
lines = []
for _ in range(6):
# SEMPRE outputs six lines of stuff when it loads that I can't disable. So, we clear
# that here.
lines.append(str(self._executor_process.stdout.readline()))
assert 'Parser' in lines[-1], "SEMPRE server output unexpected; the server may have changed"
logger.info("Started SEMPRE server for evaluating logical forms")
# This is supposed to ensure that the subprocess gets killed when python exits.
atexit.register(self._stop_sempre_executor) | [
"\n Creates a server running SEMPRE that we can send logical forms to for evaluation. This\n uses inter-process communication, because SEMPRE is java code. We also need to be careful\n to clean up the process when our program exits.\n "
] |
Please provide a description of the function:def b_cubed(clusters, mention_to_gold):
numerator, denominator = 0, 0
for cluster in clusters:
if len(cluster) == 1:
continue
gold_counts = Counter()
correct = 0
for mention in cluster:
if mention in mention_to_gold:
gold_counts[tuple(mention_to_gold[mention])] += 1
for cluster2, count in gold_counts.items():
if len(cluster2) != 1:
correct += count * count
numerator += correct / float(len(cluster))
denominator += len(cluster)
return numerator, denominator | [
"\n Averaged per-mention precision and recall.\n <https://pdfs.semanticscholar.org/cfe3/c24695f1c14b78a5b8e95bcbd1c666140fd1.pdf>\n "
] |
Please provide a description of the function:def muc(clusters, mention_to_gold):
true_p, all_p = 0, 0
for cluster in clusters:
all_p += len(cluster) - 1
true_p += len(cluster)
linked = set()
for mention in cluster:
if mention in mention_to_gold:
linked.add(mention_to_gold[mention])
else:
true_p -= 1
true_p -= len(linked)
return true_p, all_p | [
"\n Counts the mentions in each predicted cluster which need to be re-allocated in\n order for each predicted cluster to be contained by the respective gold cluster.\n <http://aclweb.org/anthology/M/M95/M95-1005.pdf>\n "
] |
Please provide a description of the function:def phi4(gold_clustering, predicted_clustering):
return 2 * len([mention for mention in gold_clustering if mention in predicted_clustering]) \
/ float(len(gold_clustering) + len(predicted_clustering)) | [
"\n Subroutine for ceafe. Computes the mention F measure between gold and\n predicted mentions in a cluster.\n "
] |
Please provide a description of the function:def ceafe(clusters, gold_clusters):
clusters = [cluster for cluster in clusters if len(cluster) != 1]
scores = np.zeros((len(gold_clusters), len(clusters)))
for i, gold_cluster in enumerate(gold_clusters):
for j, cluster in enumerate(clusters):
scores[i, j] = Scorer.phi4(gold_cluster, cluster)
matching = linear_assignment(-scores)
similarity = sum(scores[matching[:, 0], matching[:, 1]])
return similarity, len(clusters), similarity, len(gold_clusters) | [
"\n Computes the Constrained EntityAlignment F-Measure (CEAF) for evaluating coreference.\n Gold and predicted mentions are aligned into clusterings which maximise a metric - in\n this case, the F measure between gold and predicted clusters.\n\n <https://www.semanticscholar.org/paper/On-Coreference-Resolution-Performance-Metrics-Luo/de133c1f22d0dfe12539e25dda70f28672459b99>\n "
] |
Please provide a description of the function:def take_action(self, production_rule: str) -> 'GrammarStatelet':
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}")
new_stack = self._nonterminal_stack[:-1]
productions = self._get_productions_from_string(right_side)
if self._reverse_productions:
productions = list(reversed(productions))
for production in productions:
if self._is_nonterminal(production):
new_stack.append(production)
return GrammarStatelet(nonterminal_stack=new_stack,
valid_actions=self._valid_actions,
is_nonterminal=self._is_nonterminal,
reverse_productions=self._reverse_productions) | [
"\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. Updating the non-terminal stack involves popping\n the non-terminal that was expanded off of the stack, then pushing on any non-terminals in\n the production rule back on the stack.\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\n If ``self._reverse_productions`` is set to ``False`` then we push the non-terminals on in\n in their given order, which means that the first non-terminal in the production rule gets\n popped off the stack `last`.\n "
] |
Please provide a description of the function:def sparse_clip_norm(parameters, max_norm, norm_type=2) -> float:
# pylint: disable=invalid-name,protected-access
parameters = list(filter(lambda p: p.grad is not None, parameters))
max_norm = float(max_norm)
norm_type = float(norm_type)
if norm_type == float('inf'):
total_norm = max(p.grad.data.abs().max() for p in parameters)
else:
total_norm = 0
for p in parameters:
if p.grad.is_sparse:
# need to coalesce the repeated indices before finding norm
grad = p.grad.data.coalesce()
param_norm = grad._values().norm(norm_type)
else:
param_norm = p.grad.data.norm(norm_type)
total_norm += param_norm ** norm_type
total_norm = total_norm ** (1. / norm_type)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for p in parameters:
if p.grad.is_sparse:
p.grad.data._values().mul_(clip_coef)
else:
p.grad.data.mul_(clip_coef)
return total_norm | [
"Clips gradient norm of an iterable of parameters.\n\n The norm is computed over all gradients together, as if they were\n concatenated into a single vector. Gradients are modified in-place.\n Supports sparse gradients.\n\n Parameters\n ----------\n parameters : ``(Iterable[torch.Tensor])``\n An iterable of Tensors that will have gradients normalized.\n max_norm : ``float``\n The max norm of the gradients.\n norm_type : ``float``\n The type of the used p-norm. Can be ``'inf'`` for infinity norm.\n\n Returns\n -------\n Total norm of the parameters (viewed as a single vector).\n "
] |
Please provide a description of the function:def move_optimizer_to_cuda(optimizer):
for param_group in optimizer.param_groups:
for param in param_group['params']:
if param.is_cuda:
param_state = optimizer.state[param]
for k in param_state.keys():
if isinstance(param_state[k], torch.Tensor):
param_state[k] = param_state[k].cuda(device=param.get_device()) | [
"\n Move the optimizer state to GPU, if necessary.\n After calling, any parameter specific state in the optimizer\n will be located on the same device as the parameter.\n "
] |
Please provide a description of the function:def get_batch_size(batch: Union[Dict, torch.Tensor]) -> int:
if isinstance(batch, torch.Tensor):
return batch.size(0) # type: ignore
elif isinstance(batch, Dict):
return get_batch_size(next(iter(batch.values())))
else:
return 0 | [
"\n Returns the size of the batch dimension. Assumes a well-formed batch,\n returns 0 otherwise.\n "
] |
Please provide a description of the function:def time_to_str(timestamp: int) -> str:
datetimestamp = datetime.datetime.fromtimestamp(timestamp)
return '{:04d}-{:02d}-{:02d}-{:02d}-{:02d}-{:02d}'.format(
datetimestamp.year, datetimestamp.month, datetimestamp.day,
datetimestamp.hour, datetimestamp.minute, datetimestamp.second
) | [
"\n Convert seconds past Epoch to human readable string.\n "
] |
Please provide a description of the function:def str_to_time(time_str: str) -> datetime.datetime:
pieces: Any = [int(piece) for piece in time_str.split('-')]
return datetime.datetime(*pieces) | [
"\n Convert human readable string to datetime.datetime.\n "
] |
Please provide a description of the function:def datasets_from_params(params: Params,
cache_directory: str = None,
cache_prefix: str = None) -> Dict[str, Iterable[Instance]]:
dataset_reader_params = params.pop('dataset_reader')
validation_dataset_reader_params = params.pop('validation_dataset_reader', None)
train_cache_dir, validation_cache_dir = _set_up_cache_files(dataset_reader_params,
validation_dataset_reader_params,
cache_directory,
cache_prefix)
dataset_reader = DatasetReader.from_params(dataset_reader_params)
validation_and_test_dataset_reader: DatasetReader = dataset_reader
if validation_dataset_reader_params is not None:
logger.info("Using a separate dataset reader to load validation and test data.")
validation_and_test_dataset_reader = DatasetReader.from_params(validation_dataset_reader_params)
if train_cache_dir:
dataset_reader.cache_data(train_cache_dir)
validation_and_test_dataset_reader.cache_data(validation_cache_dir)
train_data_path = params.pop('train_data_path')
logger.info("Reading training data from %s", train_data_path)
train_data = dataset_reader.read(train_data_path)
datasets: Dict[str, Iterable[Instance]] = {"train": train_data}
validation_data_path = params.pop('validation_data_path', None)
if validation_data_path is not None:
logger.info("Reading validation data from %s", validation_data_path)
validation_data = validation_and_test_dataset_reader.read(validation_data_path)
datasets["validation"] = validation_data
test_data_path = params.pop("test_data_path", None)
if test_data_path is not None:
logger.info("Reading test data from %s", test_data_path)
test_data = validation_and_test_dataset_reader.read(test_data_path)
datasets["test"] = test_data
return datasets | [
"\n Load all the datasets specified by the config.\n\n Parameters\n ----------\n params : ``Params``\n cache_directory : ``str``, optional\n If given, we will instruct the ``DatasetReaders`` that we construct to cache their\n instances in this location (or read their instances from caches in this location, if a\n suitable cache already exists). This is essentially a `base` directory for the cache, as\n we will additionally add the ``cache_prefix`` to this directory, giving an actual cache\n location of ``cache_directory + cache_prefix``.\n cache_prefix : ``str``, optional\n This works in conjunction with the ``cache_directory``. The idea is that the\n ``cache_directory`` contains caches for all different parameter settings, while the\n ``cache_prefix`` captures a specific set of parameters that led to a particular cache file.\n That is, if you change the tokenization settings inside your ``DatasetReader``, you don't\n want to read cached data that used the old settings. In order to avoid this, we compute a\n hash of the parameters used to construct each ``DatasetReader`` and use that as a \"prefix\"\n to the cache files inside the base ``cache_directory``. So, a given ``input_file`` would\n be cached essentially as ``cache_directory + cache_prefix + input_file``, where you specify\n a ``cache_directory``, the ``cache_prefix`` is based on the dataset reader parameters, and\n the ``input_file`` is whatever path you provided to ``DatasetReader.read()``. In order to\n allow you to give recognizable names to these prefixes if you want them, you can manually\n specify the ``cache_prefix``. Note that in some rare cases this can be dangerous, as we'll\n use the `same` prefix for both train and validation dataset readers.\n "
] |
Please provide a description of the function:def create_serialization_dir(
params: Params,
serialization_dir: str,
recover: bool,
force: bool) -> None:
if recover and force:
raise ConfigurationError("Illegal arguments: both force and recover are true.")
if os.path.exists(serialization_dir) and force:
shutil.rmtree(serialization_dir)
if os.path.exists(serialization_dir) and os.listdir(serialization_dir):
if not recover:
raise ConfigurationError(f"Serialization directory ({serialization_dir}) already exists and is "
f"not empty. Specify --recover to recover training from existing output.")
logger.info(f"Recovering from prior training at {serialization_dir}.")
recovered_config_file = os.path.join(serialization_dir, CONFIG_NAME)
if not os.path.exists(recovered_config_file):
raise ConfigurationError("The serialization directory already exists but doesn't "
"contain a config.json. You probably gave the wrong directory.")
else:
loaded_params = Params.from_file(recovered_config_file)
# Check whether any of the training configuration differs from the configuration we are
# resuming. If so, warn the user that training may fail.
fail = False
flat_params = params.as_flat_dict()
flat_loaded = loaded_params.as_flat_dict()
for key in flat_params.keys() - flat_loaded.keys():
logger.error(f"Key '{key}' found in training configuration but not in the serialization "
f"directory we're recovering from.")
fail = True
for key in flat_loaded.keys() - flat_params.keys():
logger.error(f"Key '{key}' found in the serialization directory we're recovering from "
f"but not in the training config.")
fail = True
for key in flat_params.keys():
if flat_params.get(key, None) != flat_loaded.get(key, None):
logger.error(f"Value for '{key}' in training configuration does not match that the value in "
f"the serialization directory we're recovering from: "
f"{flat_params[key]} != {flat_loaded[key]}")
fail = True
if fail:
raise ConfigurationError("Training configuration does not match the configuration we're "
"recovering from.")
else:
if recover:
raise ConfigurationError(f"--recover specified but serialization_dir ({serialization_dir}) "
"does not exist. There is nothing to recover from.")
os.makedirs(serialization_dir, exist_ok=True) | [
"\n This function creates the serialization directory if it doesn't exist. If it already exists\n and is non-empty, then it verifies that we're recovering from a training with an identical configuration.\n\n Parameters\n ----------\n params: ``Params``\n A parameter object specifying an AllenNLP Experiment.\n serialization_dir: ``str``\n The directory in which to save results and logs.\n recover: ``bool``\n If ``True``, we will try to recover from an existing serialization directory, and crash if\n the directory doesn't exist, or doesn't match the configuration we're given.\n force: ``bool``\n If ``True``, we will overwrite the serialization directory if it already exists.\n "
] |
Please provide a description of the function:def data_parallel(batch_group: List[TensorDict],
model: Model,
cuda_devices: List) -> Dict[str, torch.Tensor]:
assert len(batch_group) <= len(cuda_devices)
moved = [nn_util.move_to_device(batch, device)
for batch, device in zip(batch_group, cuda_devices)]
used_device_ids = cuda_devices[:len(moved)]
# Counterintuitively, it appears replicate expects the source device id to be the first element
# in the device id list. See torch.cuda.comm.broadcast_coalesced, which is called indirectly.
replicas = replicate(model, used_device_ids)
# We pass all our arguments as kwargs. Create a list of empty tuples of the
# correct shape to serve as (non-existent) positional arguments.
inputs = [()] * len(batch_group)
outputs = parallel_apply(replicas, inputs, moved, used_device_ids)
# Only the 'loss' is needed.
# a (num_gpu, ) tensor with loss on each GPU
losses = gather([output['loss'].unsqueeze(0) for output in outputs], used_device_ids[0], 0)
return {'loss': losses.mean()} | [
"\n Performs a forward pass using multiple GPUs. This is a simplification\n of torch.nn.parallel.data_parallel to support the allennlp model\n interface.\n "
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.