repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
facebookresearch/fastText
python/fastText/FastText.py
_FastText.get_line
def get_line(self, text, on_unicode_error='strict'): """ Split a line of text into words and labels. Labels must start with the prefix used to create the model (__label__ by default). """ def check(entry): if entry.find('\n') != -1: raise ValueError( "get_line processes one line at a time (remove \'\\n\')" ) entry += "\n" return entry if type(text) == list: text = [check(entry) for entry in text] return self.f.multilineGetLine(text, on_unicode_error) else: text = check(text) return self.f.getLine(text, on_unicode_error)
python
def get_line(self, text, on_unicode_error='strict'): """ Split a line of text into words and labels. Labels must start with the prefix used to create the model (__label__ by default). """ def check(entry): if entry.find('\n') != -1: raise ValueError( "get_line processes one line at a time (remove \'\\n\')" ) entry += "\n" return entry if type(text) == list: text = [check(entry) for entry in text] return self.f.multilineGetLine(text, on_unicode_error) else: text = check(text) return self.f.getLine(text, on_unicode_error)
[ "def", "get_line", "(", "self", ",", "text", ",", "on_unicode_error", "=", "'strict'", ")", ":", "def", "check", "(", "entry", ")", ":", "if", "entry", ".", "find", "(", "'\\n'", ")", "!=", "-", "1", ":", "raise", "ValueError", "(", "\"get_line processes one line at a time (remove \\'\\\\n\\')\"", ")", "entry", "+=", "\"\\n\"", "return", "entry", "if", "type", "(", "text", ")", "==", "list", ":", "text", "=", "[", "check", "(", "entry", ")", "for", "entry", "in", "text", "]", "return", "self", ".", "f", ".", "multilineGetLine", "(", "text", ",", "on_unicode_error", ")", "else", ":", "text", "=", "check", "(", "text", ")", "return", "self", ".", "f", ".", "getLine", "(", "text", ",", "on_unicode_error", ")" ]
Split a line of text into words and labels. Labels must start with the prefix used to create the model (__label__ by default).
[ "Split", "a", "line", "of", "text", "into", "words", "and", "labels", ".", "Labels", "must", "start", "with", "the", "prefix", "used", "to", "create", "the", "model", "(", "__label__", "by", "default", ")", "." ]
6dd2e11b5fe82854c4529d2a58d699b2cb182b1b
https://github.com/facebookresearch/fastText/blob/6dd2e11b5fe82854c4529d2a58d699b2cb182b1b/python/fastText/FastText.py#L194-L213
train
facebookresearch/fastText
python/fastText/FastText.py
_FastText.quantize
def quantize( self, input=None, qout=False, cutoff=0, retrain=False, epoch=None, lr=None, thread=None, verbose=None, dsub=2, qnorm=False ): """ Quantize the model reducing the size of the model and it's memory footprint. """ a = self.f.getArgs() if not epoch: epoch = a.epoch if not lr: lr = a.lr if not thread: thread = a.thread if not verbose: verbose = a.verbose if retrain and not input: raise ValueError("Need input file path if retraining") if input is None: input = "" self.f.quantize( input, qout, cutoff, retrain, epoch, lr, thread, verbose, dsub, qnorm )
python
def quantize( self, input=None, qout=False, cutoff=0, retrain=False, epoch=None, lr=None, thread=None, verbose=None, dsub=2, qnorm=False ): """ Quantize the model reducing the size of the model and it's memory footprint. """ a = self.f.getArgs() if not epoch: epoch = a.epoch if not lr: lr = a.lr if not thread: thread = a.thread if not verbose: verbose = a.verbose if retrain and not input: raise ValueError("Need input file path if retraining") if input is None: input = "" self.f.quantize( input, qout, cutoff, retrain, epoch, lr, thread, verbose, dsub, qnorm )
[ "def", "quantize", "(", "self", ",", "input", "=", "None", ",", "qout", "=", "False", ",", "cutoff", "=", "0", ",", "retrain", "=", "False", ",", "epoch", "=", "None", ",", "lr", "=", "None", ",", "thread", "=", "None", ",", "verbose", "=", "None", ",", "dsub", "=", "2", ",", "qnorm", "=", "False", ")", ":", "a", "=", "self", ".", "f", ".", "getArgs", "(", ")", "if", "not", "epoch", ":", "epoch", "=", "a", ".", "epoch", "if", "not", "lr", ":", "lr", "=", "a", ".", "lr", "if", "not", "thread", ":", "thread", "=", "a", ".", "thread", "if", "not", "verbose", ":", "verbose", "=", "a", ".", "verbose", "if", "retrain", "and", "not", "input", ":", "raise", "ValueError", "(", "\"Need input file path if retraining\"", ")", "if", "input", "is", "None", ":", "input", "=", "\"\"", "self", ".", "f", ".", "quantize", "(", "input", ",", "qout", ",", "cutoff", ",", "retrain", ",", "epoch", ",", "lr", ",", "thread", ",", "verbose", ",", "dsub", ",", "qnorm", ")" ]
Quantize the model reducing the size of the model and it's memory footprint.
[ "Quantize", "the", "model", "reducing", "the", "size", "of", "the", "model", "and", "it", "s", "memory", "footprint", "." ]
6dd2e11b5fe82854c4529d2a58d699b2cb182b1b
https://github.com/facebookresearch/fastText/blob/6dd2e11b5fe82854c4529d2a58d699b2cb182b1b/python/fastText/FastText.py#L234-L267
train
allenai/allennlp
allennlp/modules/stacked_bidirectional_lstm.py
StackedBidirectionalLstm.forward
def forward(self, # pylint: disable=arguments-differ inputs: PackedSequence, initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None ) -> Tuple[PackedSequence, Tuple[torch.Tensor, torch.Tensor]]: """ Parameters ---------- inputs : ``PackedSequence``, required. A batch first ``PackedSequence`` to run the stacked LSTM over. initial_state : Tuple[torch.Tensor, torch.Tensor], optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. Each tensor has shape (num_layers, batch_size, output_dimension * 2). Returns ------- output_sequence : PackedSequence The encoded sequence of shape (batch_size, sequence_length, hidden_size * 2) final_states: torch.Tensor The per-layer final (state, memory) states of the LSTM, each with shape (num_layers * 2, batch_size, hidden_size * 2). """ if not initial_state: hidden_states = [None] * len(self.lstm_layers) elif initial_state[0].size()[0] != len(self.lstm_layers): raise ConfigurationError("Initial states were passed to forward() but the number of " "initial states does not match the number of layers.") else: hidden_states = list(zip(initial_state[0].split(1, 0), initial_state[1].split(1, 0))) output_sequence = inputs final_h = [] final_c = [] for i, state in enumerate(hidden_states): forward_layer = getattr(self, 'forward_layer_{}'.format(i)) backward_layer = getattr(self, 'backward_layer_{}'.format(i)) # The state is duplicated to mirror the Pytorch API for LSTMs. forward_output, final_forward_state = forward_layer(output_sequence, state) backward_output, final_backward_state = backward_layer(output_sequence, state) forward_output, lengths = pad_packed_sequence(forward_output, batch_first=True) backward_output, _ = pad_packed_sequence(backward_output, batch_first=True) output_sequence = torch.cat([forward_output, backward_output], -1) # Apply layer wise dropout on each output sequence apart from the # first (input) and last if i < (self.num_layers - 1): output_sequence = self.layer_dropout(output_sequence) output_sequence = pack_padded_sequence(output_sequence, lengths, batch_first=True) final_h.extend([final_forward_state[0], final_backward_state[0]]) final_c.extend([final_forward_state[1], final_backward_state[1]]) final_h = torch.cat(final_h, dim=0) final_c = torch.cat(final_c, dim=0) final_state_tuple = (final_h, final_c) return output_sequence, final_state_tuple
python
def forward(self, # pylint: disable=arguments-differ inputs: PackedSequence, initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None ) -> Tuple[PackedSequence, Tuple[torch.Tensor, torch.Tensor]]: """ Parameters ---------- inputs : ``PackedSequence``, required. A batch first ``PackedSequence`` to run the stacked LSTM over. initial_state : Tuple[torch.Tensor, torch.Tensor], optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. Each tensor has shape (num_layers, batch_size, output_dimension * 2). Returns ------- output_sequence : PackedSequence The encoded sequence of shape (batch_size, sequence_length, hidden_size * 2) final_states: torch.Tensor The per-layer final (state, memory) states of the LSTM, each with shape (num_layers * 2, batch_size, hidden_size * 2). """ if not initial_state: hidden_states = [None] * len(self.lstm_layers) elif initial_state[0].size()[0] != len(self.lstm_layers): raise ConfigurationError("Initial states were passed to forward() but the number of " "initial states does not match the number of layers.") else: hidden_states = list(zip(initial_state[0].split(1, 0), initial_state[1].split(1, 0))) output_sequence = inputs final_h = [] final_c = [] for i, state in enumerate(hidden_states): forward_layer = getattr(self, 'forward_layer_{}'.format(i)) backward_layer = getattr(self, 'backward_layer_{}'.format(i)) # The state is duplicated to mirror the Pytorch API for LSTMs. forward_output, final_forward_state = forward_layer(output_sequence, state) backward_output, final_backward_state = backward_layer(output_sequence, state) forward_output, lengths = pad_packed_sequence(forward_output, batch_first=True) backward_output, _ = pad_packed_sequence(backward_output, batch_first=True) output_sequence = torch.cat([forward_output, backward_output], -1) # Apply layer wise dropout on each output sequence apart from the # first (input) and last if i < (self.num_layers - 1): output_sequence = self.layer_dropout(output_sequence) output_sequence = pack_padded_sequence(output_sequence, lengths, batch_first=True) final_h.extend([final_forward_state[0], final_backward_state[0]]) final_c.extend([final_forward_state[1], final_backward_state[1]]) final_h = torch.cat(final_h, dim=0) final_c = torch.cat(final_c, dim=0) final_state_tuple = (final_h, final_c) return output_sequence, final_state_tuple
[ "def", "forward", "(", "self", ",", "# pylint: disable=arguments-differ", "inputs", ":", "PackedSequence", ",", "initial_state", ":", "Optional", "[", "Tuple", "[", "torch", ".", "Tensor", ",", "torch", ".", "Tensor", "]", "]", "=", "None", ")", "->", "Tuple", "[", "PackedSequence", ",", "Tuple", "[", "torch", ".", "Tensor", ",", "torch", ".", "Tensor", "]", "]", ":", "if", "not", "initial_state", ":", "hidden_states", "=", "[", "None", "]", "*", "len", "(", "self", ".", "lstm_layers", ")", "elif", "initial_state", "[", "0", "]", ".", "size", "(", ")", "[", "0", "]", "!=", "len", "(", "self", ".", "lstm_layers", ")", ":", "raise", "ConfigurationError", "(", "\"Initial states were passed to forward() but the number of \"", "\"initial states does not match the number of layers.\"", ")", "else", ":", "hidden_states", "=", "list", "(", "zip", "(", "initial_state", "[", "0", "]", ".", "split", "(", "1", ",", "0", ")", ",", "initial_state", "[", "1", "]", ".", "split", "(", "1", ",", "0", ")", ")", ")", "output_sequence", "=", "inputs", "final_h", "=", "[", "]", "final_c", "=", "[", "]", "for", "i", ",", "state", "in", "enumerate", "(", "hidden_states", ")", ":", "forward_layer", "=", "getattr", "(", "self", ",", "'forward_layer_{}'", ".", "format", "(", "i", ")", ")", "backward_layer", "=", "getattr", "(", "self", ",", "'backward_layer_{}'", ".", "format", "(", "i", ")", ")", "# The state is duplicated to mirror the Pytorch API for LSTMs.", "forward_output", ",", "final_forward_state", "=", "forward_layer", "(", "output_sequence", ",", "state", ")", "backward_output", ",", "final_backward_state", "=", "backward_layer", "(", "output_sequence", ",", "state", ")", "forward_output", ",", "lengths", "=", "pad_packed_sequence", "(", "forward_output", ",", "batch_first", "=", "True", ")", "backward_output", ",", "_", "=", "pad_packed_sequence", "(", "backward_output", ",", "batch_first", "=", "True", ")", "output_sequence", "=", "torch", ".", "cat", "(", "[", "forward_output", ",", "backward_output", "]", ",", "-", "1", ")", "# Apply layer wise dropout on each output sequence apart from the", "# first (input) and last", "if", "i", "<", "(", "self", ".", "num_layers", "-", "1", ")", ":", "output_sequence", "=", "self", ".", "layer_dropout", "(", "output_sequence", ")", "output_sequence", "=", "pack_padded_sequence", "(", "output_sequence", ",", "lengths", ",", "batch_first", "=", "True", ")", "final_h", ".", "extend", "(", "[", "final_forward_state", "[", "0", "]", ",", "final_backward_state", "[", "0", "]", "]", ")", "final_c", ".", "extend", "(", "[", "final_forward_state", "[", "1", "]", ",", "final_backward_state", "[", "1", "]", "]", ")", "final_h", "=", "torch", ".", "cat", "(", "final_h", ",", "dim", "=", "0", ")", "final_c", "=", "torch", ".", "cat", "(", "final_c", ",", "dim", "=", "0", ")", "final_state_tuple", "=", "(", "final_h", ",", "final_c", ")", "return", "output_sequence", ",", "final_state_tuple" ]
Parameters ---------- inputs : ``PackedSequence``, required. A batch first ``PackedSequence`` to run the stacked LSTM over. initial_state : Tuple[torch.Tensor, torch.Tensor], optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. Each tensor has shape (num_layers, batch_size, output_dimension * 2). Returns ------- output_sequence : PackedSequence The encoded sequence of shape (batch_size, sequence_length, hidden_size * 2) final_states: torch.Tensor The per-layer final (state, memory) states of the LSTM, each with shape (num_layers * 2, batch_size, hidden_size * 2).
[ "Parameters", "----------", "inputs", ":", "PackedSequence", "required", ".", "A", "batch", "first", "PackedSequence", "to", "run", "the", "stacked", "LSTM", "over", ".", "initial_state", ":", "Tuple", "[", "torch", ".", "Tensor", "torch", ".", "Tensor", "]", "optional", "(", "default", "=", "None", ")", "A", "tuple", "(", "state", "memory", ")", "representing", "the", "initial", "hidden", "state", "and", "memory", "of", "the", "LSTM", ".", "Each", "tensor", "has", "shape", "(", "num_layers", "batch_size", "output_dimension", "*", "2", ")", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/modules/stacked_bidirectional_lstm.py#L78-L134
train
allenai/allennlp
allennlp/nn/regularizers/regularizer_applicator.py
RegularizerApplicator.from_params
def from_params(cls, params: Iterable[Tuple[str, Params]] = ()) -> Optional['RegularizerApplicator']: """ Converts a List of pairs (regex, params) into an RegularizerApplicator. This list should look like [["regex1", {"type": "l2", "alpha": 0.01}], ["regex2", "l1"]] where each parameter receives the penalty corresponding to the first regex that matches its name (which may be no regex and hence no penalty). The values can either be strings, in which case they correspond to the names of regularizers, or dictionaries, in which case they must contain the "type" key, corresponding to the name of a regularizer. In addition, they may contain auxiliary named parameters which will be fed to the regularizer itself. To determine valid auxiliary parameters, please refer to the torch.nn.init documentation. Parameters ---------- params : ``Params``, required. A Params object containing a "regularizers" key. Returns ------- A RegularizerApplicator containing the specified Regularizers, or ``None`` if no Regularizers are specified. """ if not params: return None instantiated_regularizers = [] for parameter_regex, regularizer_params in params: if isinstance(regularizer_params, str): regularizer = Regularizer.by_name(regularizer_params)() else: regularizer_type = Regularizer.by_name(regularizer_params.pop("type")) regularizer = regularizer_type(**regularizer_params) # type: ignore instantiated_regularizers.append((parameter_regex, regularizer)) return RegularizerApplicator(instantiated_regularizers)
python
def from_params(cls, params: Iterable[Tuple[str, Params]] = ()) -> Optional['RegularizerApplicator']: """ Converts a List of pairs (regex, params) into an RegularizerApplicator. This list should look like [["regex1", {"type": "l2", "alpha": 0.01}], ["regex2", "l1"]] where each parameter receives the penalty corresponding to the first regex that matches its name (which may be no regex and hence no penalty). The values can either be strings, in which case they correspond to the names of regularizers, or dictionaries, in which case they must contain the "type" key, corresponding to the name of a regularizer. In addition, they may contain auxiliary named parameters which will be fed to the regularizer itself. To determine valid auxiliary parameters, please refer to the torch.nn.init documentation. Parameters ---------- params : ``Params``, required. A Params object containing a "regularizers" key. Returns ------- A RegularizerApplicator containing the specified Regularizers, or ``None`` if no Regularizers are specified. """ if not params: return None instantiated_regularizers = [] for parameter_regex, regularizer_params in params: if isinstance(regularizer_params, str): regularizer = Regularizer.by_name(regularizer_params)() else: regularizer_type = Regularizer.by_name(regularizer_params.pop("type")) regularizer = regularizer_type(**regularizer_params) # type: ignore instantiated_regularizers.append((parameter_regex, regularizer)) return RegularizerApplicator(instantiated_regularizers)
[ "def", "from_params", "(", "cls", ",", "params", ":", "Iterable", "[", "Tuple", "[", "str", ",", "Params", "]", "]", "=", "(", ")", ")", "->", "Optional", "[", "'RegularizerApplicator'", "]", ":", "if", "not", "params", ":", "return", "None", "instantiated_regularizers", "=", "[", "]", "for", "parameter_regex", ",", "regularizer_params", "in", "params", ":", "if", "isinstance", "(", "regularizer_params", ",", "str", ")", ":", "regularizer", "=", "Regularizer", ".", "by_name", "(", "regularizer_params", ")", "(", ")", "else", ":", "regularizer_type", "=", "Regularizer", ".", "by_name", "(", "regularizer_params", ".", "pop", "(", "\"type\"", ")", ")", "regularizer", "=", "regularizer_type", "(", "*", "*", "regularizer_params", ")", "# type: ignore", "instantiated_regularizers", ".", "append", "(", "(", "parameter_regex", ",", "regularizer", ")", ")", "return", "RegularizerApplicator", "(", "instantiated_regularizers", ")" ]
Converts a List of pairs (regex, params) into an RegularizerApplicator. This list should look like [["regex1", {"type": "l2", "alpha": 0.01}], ["regex2", "l1"]] where each parameter receives the penalty corresponding to the first regex that matches its name (which may be no regex and hence no penalty). The values can either be strings, in which case they correspond to the names of regularizers, or dictionaries, in which case they must contain the "type" key, corresponding to the name of a regularizer. In addition, they may contain auxiliary named parameters which will be fed to the regularizer itself. To determine valid auxiliary parameters, please refer to the torch.nn.init documentation. Parameters ---------- params : ``Params``, required. A Params object containing a "regularizers" key. Returns ------- A RegularizerApplicator containing the specified Regularizers, or ``None`` if no Regularizers are specified.
[ "Converts", "a", "List", "of", "pairs", "(", "regex", "params", ")", "into", "an", "RegularizerApplicator", ".", "This", "list", "should", "look", "like" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/nn/regularizers/regularizer_applicator.py#L45-L81
train
allenai/allennlp
allennlp/common/registrable.py
Registrable.list_available
def list_available(cls) -> List[str]: """List default first if it exists""" keys = list(Registrable._registry[cls].keys()) default = cls.default_implementation if default is None: return keys elif default not in keys: message = "Default implementation %s is not registered" % default raise ConfigurationError(message) else: return [default] + [k for k in keys if k != default]
python
def list_available(cls) -> List[str]: """List default first if it exists""" keys = list(Registrable._registry[cls].keys()) default = cls.default_implementation if default is None: return keys elif default not in keys: message = "Default implementation %s is not registered" % default raise ConfigurationError(message) else: return [default] + [k for k in keys if k != default]
[ "def", "list_available", "(", "cls", ")", "->", "List", "[", "str", "]", ":", "keys", "=", "list", "(", "Registrable", ".", "_registry", "[", "cls", "]", ".", "keys", "(", ")", ")", "default", "=", "cls", ".", "default_implementation", "if", "default", "is", "None", ":", "return", "keys", "elif", "default", "not", "in", "keys", ":", "message", "=", "\"Default implementation %s is not registered\"", "%", "default", "raise", "ConfigurationError", "(", "message", ")", "else", ":", "return", "[", "default", "]", "+", "[", "k", "for", "k", "in", "keys", "if", "k", "!=", "default", "]" ]
List default first if it exists
[ "List", "default", "first", "if", "it", "exists" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/registrable.py#L62-L73
train
allenai/allennlp
allennlp/common/util.py
sanitize
def sanitize(x: Any) -> Any: # pylint: disable=invalid-name,too-many-return-statements """ Sanitize turns PyTorch and Numpy types into basic Python types so they can be serialized into JSON. """ if isinstance(x, (str, float, int, bool)): # x is already serializable return x elif isinstance(x, torch.Tensor): # tensor needs to be converted to a list (and moved to cpu if necessary) return x.cpu().tolist() elif isinstance(x, numpy.ndarray): # array needs to be converted to a list return x.tolist() elif isinstance(x, numpy.number): # pylint: disable=no-member # NumPy numbers need to be converted to Python numbers return x.item() elif isinstance(x, dict): # Dicts need their values sanitized return {key: sanitize(value) for key, value in x.items()} elif isinstance(x, (spacy.tokens.Token, allennlp.data.Token)): # Tokens get sanitized to just their text. return x.text elif isinstance(x, (list, tuple)): # Lists and Tuples need their values sanitized return [sanitize(x_i) for x_i in x] elif x is None: return "None" elif hasattr(x, 'to_json'): return x.to_json() else: raise ValueError(f"Cannot sanitize {x} of type {type(x)}. " "If this is your own custom class, add a `to_json(self)` method " "that returns a JSON-like object.")
python
def sanitize(x: Any) -> Any: # pylint: disable=invalid-name,too-many-return-statements """ Sanitize turns PyTorch and Numpy types into basic Python types so they can be serialized into JSON. """ if isinstance(x, (str, float, int, bool)): # x is already serializable return x elif isinstance(x, torch.Tensor): # tensor needs to be converted to a list (and moved to cpu if necessary) return x.cpu().tolist() elif isinstance(x, numpy.ndarray): # array needs to be converted to a list return x.tolist() elif isinstance(x, numpy.number): # pylint: disable=no-member # NumPy numbers need to be converted to Python numbers return x.item() elif isinstance(x, dict): # Dicts need their values sanitized return {key: sanitize(value) for key, value in x.items()} elif isinstance(x, (spacy.tokens.Token, allennlp.data.Token)): # Tokens get sanitized to just their text. return x.text elif isinstance(x, (list, tuple)): # Lists and Tuples need their values sanitized return [sanitize(x_i) for x_i in x] elif x is None: return "None" elif hasattr(x, 'to_json'): return x.to_json() else: raise ValueError(f"Cannot sanitize {x} of type {type(x)}. " "If this is your own custom class, add a `to_json(self)` method " "that returns a JSON-like object.")
[ "def", "sanitize", "(", "x", ":", "Any", ")", "->", "Any", ":", "# pylint: disable=invalid-name,too-many-return-statements", "if", "isinstance", "(", "x", ",", "(", "str", ",", "float", ",", "int", ",", "bool", ")", ")", ":", "# x is already serializable", "return", "x", "elif", "isinstance", "(", "x", ",", "torch", ".", "Tensor", ")", ":", "# tensor needs to be converted to a list (and moved to cpu if necessary)", "return", "x", ".", "cpu", "(", ")", ".", "tolist", "(", ")", "elif", "isinstance", "(", "x", ",", "numpy", ".", "ndarray", ")", ":", "# array needs to be converted to a list", "return", "x", ".", "tolist", "(", ")", "elif", "isinstance", "(", "x", ",", "numpy", ".", "number", ")", ":", "# pylint: disable=no-member", "# NumPy numbers need to be converted to Python numbers", "return", "x", ".", "item", "(", ")", "elif", "isinstance", "(", "x", ",", "dict", ")", ":", "# Dicts need their values sanitized", "return", "{", "key", ":", "sanitize", "(", "value", ")", "for", "key", ",", "value", "in", "x", ".", "items", "(", ")", "}", "elif", "isinstance", "(", "x", ",", "(", "spacy", ".", "tokens", ".", "Token", ",", "allennlp", ".", "data", ".", "Token", ")", ")", ":", "# Tokens get sanitized to just their text.", "return", "x", ".", "text", "elif", "isinstance", "(", "x", ",", "(", "list", ",", "tuple", ")", ")", ":", "# Lists and Tuples need their values sanitized", "return", "[", "sanitize", "(", "x_i", ")", "for", "x_i", "in", "x", "]", "elif", "x", "is", "None", ":", "return", "\"None\"", "elif", "hasattr", "(", "x", ",", "'to_json'", ")", ":", "return", "x", ".", "to_json", "(", ")", "else", ":", "raise", "ValueError", "(", "f\"Cannot sanitize {x} of type {type(x)}. \"", "\"If this is your own custom class, add a `to_json(self)` method \"", "\"that returns a JSON-like object.\"", ")" ]
Sanitize turns PyTorch and Numpy types into basic Python types so they can be serialized into JSON.
[ "Sanitize", "turns", "PyTorch", "and", "Numpy", "types", "into", "basic", "Python", "types", "so", "they", "can", "be", "serialized", "into", "JSON", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L48-L81
train
allenai/allennlp
allennlp/common/util.py
group_by_count
def group_by_count(iterable: List[Any], count: int, default_value: Any) -> List[List[Any]]: """ Takes a list and groups it into sublists of size ``count``, using ``default_value`` to pad the list at the end if the list is not divisable by ``count``. For example: >>> group_by_count([1, 2, 3, 4, 5, 6, 7], 3, 0) [[1, 2, 3], [4, 5, 6], [7, 0, 0]] This is a short method, but it's complicated and hard to remember as a one-liner, so we just make a function out of it. """ return [list(l) for l in zip_longest(*[iter(iterable)] * count, fillvalue=default_value)]
python
def group_by_count(iterable: List[Any], count: int, default_value: Any) -> List[List[Any]]: """ Takes a list and groups it into sublists of size ``count``, using ``default_value`` to pad the list at the end if the list is not divisable by ``count``. For example: >>> group_by_count([1, 2, 3, 4, 5, 6, 7], 3, 0) [[1, 2, 3], [4, 5, 6], [7, 0, 0]] This is a short method, but it's complicated and hard to remember as a one-liner, so we just make a function out of it. """ return [list(l) for l in zip_longest(*[iter(iterable)] * count, fillvalue=default_value)]
[ "def", "group_by_count", "(", "iterable", ":", "List", "[", "Any", "]", ",", "count", ":", "int", ",", "default_value", ":", "Any", ")", "->", "List", "[", "List", "[", "Any", "]", "]", ":", "return", "[", "list", "(", "l", ")", "for", "l", "in", "zip_longest", "(", "*", "[", "iter", "(", "iterable", ")", "]", "*", "count", ",", "fillvalue", "=", "default_value", ")", "]" ]
Takes a list and groups it into sublists of size ``count``, using ``default_value`` to pad the list at the end if the list is not divisable by ``count``. For example: >>> group_by_count([1, 2, 3, 4, 5, 6, 7], 3, 0) [[1, 2, 3], [4, 5, 6], [7, 0, 0]] This is a short method, but it's complicated and hard to remember as a one-liner, so we just make a function out of it.
[ "Takes", "a", "list", "and", "groups", "it", "into", "sublists", "of", "size", "count", "using", "default_value", "to", "pad", "the", "list", "at", "the", "end", "if", "the", "list", "is", "not", "divisable", "by", "count", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L83-L95
train
allenai/allennlp
allennlp/common/util.py
lazy_groups_of
def lazy_groups_of(iterator: Iterator[A], group_size: int) -> Iterator[List[A]]: """ Takes an iterator and batches the individual instances into lists of the specified size. The last list may be smaller if there are instances left over. """ return iter(lambda: list(islice(iterator, 0, group_size)), [])
python
def lazy_groups_of(iterator: Iterator[A], group_size: int) -> Iterator[List[A]]: """ Takes an iterator and batches the individual instances into lists of the specified size. The last list may be smaller if there are instances left over. """ return iter(lambda: list(islice(iterator, 0, group_size)), [])
[ "def", "lazy_groups_of", "(", "iterator", ":", "Iterator", "[", "A", "]", ",", "group_size", ":", "int", ")", "->", "Iterator", "[", "List", "[", "A", "]", "]", ":", "return", "iter", "(", "lambda", ":", "list", "(", "islice", "(", "iterator", ",", "0", ",", "group_size", ")", ")", ",", "[", "]", ")" ]
Takes an iterator and batches the individual instances into lists of the specified size. The last list may be smaller if there are instances left over.
[ "Takes", "an", "iterator", "and", "batches", "the", "individual", "instances", "into", "lists", "of", "the", "specified", "size", ".", "The", "last", "list", "may", "be", "smaller", "if", "there", "are", "instances", "left", "over", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L99-L104
train
allenai/allennlp
allennlp/common/util.py
pad_sequence_to_length
def pad_sequence_to_length(sequence: List, desired_length: int, default_value: Callable[[], Any] = lambda: 0, padding_on_right: bool = True) -> List: """ Take a list of objects and pads it to the desired length, returning the padded list. The original list is not modified. Parameters ---------- sequence : List A list of objects to be padded. desired_length : int Maximum length of each sequence. Longer sequences are truncated to this length, and shorter ones are padded to it. default_value: Callable, default=lambda: 0 Callable that outputs a default value (of any type) to use as padding values. This is a lambda to avoid using the same object when the default value is more complex, like a list. padding_on_right : bool, default=True When we add padding tokens (or truncate the sequence), should we do it on the right or the left? Returns ------- padded_sequence : List """ # Truncates the sequence to the desired length. if padding_on_right: padded_sequence = sequence[:desired_length] else: padded_sequence = sequence[-desired_length:] # Continues to pad with default_value() until we reach the desired length. for _ in range(desired_length - len(padded_sequence)): if padding_on_right: padded_sequence.append(default_value()) else: padded_sequence.insert(0, default_value()) return padded_sequence
python
def pad_sequence_to_length(sequence: List, desired_length: int, default_value: Callable[[], Any] = lambda: 0, padding_on_right: bool = True) -> List: """ Take a list of objects and pads it to the desired length, returning the padded list. The original list is not modified. Parameters ---------- sequence : List A list of objects to be padded. desired_length : int Maximum length of each sequence. Longer sequences are truncated to this length, and shorter ones are padded to it. default_value: Callable, default=lambda: 0 Callable that outputs a default value (of any type) to use as padding values. This is a lambda to avoid using the same object when the default value is more complex, like a list. padding_on_right : bool, default=True When we add padding tokens (or truncate the sequence), should we do it on the right or the left? Returns ------- padded_sequence : List """ # Truncates the sequence to the desired length. if padding_on_right: padded_sequence = sequence[:desired_length] else: padded_sequence = sequence[-desired_length:] # Continues to pad with default_value() until we reach the desired length. for _ in range(desired_length - len(padded_sequence)): if padding_on_right: padded_sequence.append(default_value()) else: padded_sequence.insert(0, default_value()) return padded_sequence
[ "def", "pad_sequence_to_length", "(", "sequence", ":", "List", ",", "desired_length", ":", "int", ",", "default_value", ":", "Callable", "[", "[", "]", ",", "Any", "]", "=", "lambda", ":", "0", ",", "padding_on_right", ":", "bool", "=", "True", ")", "->", "List", ":", "# Truncates the sequence to the desired length.", "if", "padding_on_right", ":", "padded_sequence", "=", "sequence", "[", ":", "desired_length", "]", "else", ":", "padded_sequence", "=", "sequence", "[", "-", "desired_length", ":", "]", "# Continues to pad with default_value() until we reach the desired length.", "for", "_", "in", "range", "(", "desired_length", "-", "len", "(", "padded_sequence", ")", ")", ":", "if", "padding_on_right", ":", "padded_sequence", ".", "append", "(", "default_value", "(", ")", ")", "else", ":", "padded_sequence", ".", "insert", "(", "0", ",", "default_value", "(", ")", ")", "return", "padded_sequence" ]
Take a list of objects and pads it to the desired length, returning the padded list. The original list is not modified. Parameters ---------- sequence : List A list of objects to be padded. desired_length : int Maximum length of each sequence. Longer sequences are truncated to this length, and shorter ones are padded to it. default_value: Callable, default=lambda: 0 Callable that outputs a default value (of any type) to use as padding values. This is a lambda to avoid using the same object when the default value is more complex, like a list. padding_on_right : bool, default=True When we add padding tokens (or truncate the sequence), should we do it on the right or the left? Returns ------- padded_sequence : List
[ "Take", "a", "list", "of", "objects", "and", "pads", "it", "to", "the", "desired", "length", "returning", "the", "padded", "list", ".", "The", "original", "list", "is", "not", "modified", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L106-L147
train
allenai/allennlp
allennlp/common/util.py
add_noise_to_dict_values
def add_noise_to_dict_values(dictionary: Dict[A, float], noise_param: float) -> Dict[A, float]: """ Returns a new dictionary with noise added to every key in ``dictionary``. The noise is uniformly distributed within ``noise_param`` percent of the value for every value in the dictionary. """ new_dict = {} for key, value in dictionary.items(): noise_value = value * noise_param noise = random.uniform(-noise_value, noise_value) new_dict[key] = value + noise return new_dict
python
def add_noise_to_dict_values(dictionary: Dict[A, float], noise_param: float) -> Dict[A, float]: """ Returns a new dictionary with noise added to every key in ``dictionary``. The noise is uniformly distributed within ``noise_param`` percent of the value for every value in the dictionary. """ new_dict = {} for key, value in dictionary.items(): noise_value = value * noise_param noise = random.uniform(-noise_value, noise_value) new_dict[key] = value + noise return new_dict
[ "def", "add_noise_to_dict_values", "(", "dictionary", ":", "Dict", "[", "A", ",", "float", "]", ",", "noise_param", ":", "float", ")", "->", "Dict", "[", "A", ",", "float", "]", ":", "new_dict", "=", "{", "}", "for", "key", ",", "value", "in", "dictionary", ".", "items", "(", ")", ":", "noise_value", "=", "value", "*", "noise_param", "noise", "=", "random", ".", "uniform", "(", "-", "noise_value", ",", "noise_value", ")", "new_dict", "[", "key", "]", "=", "value", "+", "noise", "return", "new_dict" ]
Returns a new dictionary with noise added to every key in ``dictionary``. The noise is uniformly distributed within ``noise_param`` percent of the value for every value in the dictionary.
[ "Returns", "a", "new", "dictionary", "with", "noise", "added", "to", "every", "key", "in", "dictionary", ".", "The", "noise", "is", "uniformly", "distributed", "within", "noise_param", "percent", "of", "the", "value", "for", "every", "value", "in", "the", "dictionary", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L150-L161
train
allenai/allennlp
allennlp/common/util.py
namespace_match
def namespace_match(pattern: str, namespace: str): """ Matches a namespace pattern against a namespace string. For example, ``*tags`` matches ``passage_tags`` and ``question_tags`` and ``tokens`` matches ``tokens`` but not ``stemmed_tokens``. """ if pattern[0] == '*' and namespace.endswith(pattern[1:]): return True elif pattern == namespace: return True return False
python
def namespace_match(pattern: str, namespace: str): """ Matches a namespace pattern against a namespace string. For example, ``*tags`` matches ``passage_tags`` and ``question_tags`` and ``tokens`` matches ``tokens`` but not ``stemmed_tokens``. """ if pattern[0] == '*' and namespace.endswith(pattern[1:]): return True elif pattern == namespace: return True return False
[ "def", "namespace_match", "(", "pattern", ":", "str", ",", "namespace", ":", "str", ")", ":", "if", "pattern", "[", "0", "]", "==", "'*'", "and", "namespace", ".", "endswith", "(", "pattern", "[", "1", ":", "]", ")", ":", "return", "True", "elif", "pattern", "==", "namespace", ":", "return", "True", "return", "False" ]
Matches a namespace pattern against a namespace string. For example, ``*tags`` matches ``passage_tags`` and ``question_tags`` and ``tokens`` matches ``tokens`` but not ``stemmed_tokens``.
[ "Matches", "a", "namespace", "pattern", "against", "a", "namespace", "string", ".", "For", "example", "*", "tags", "matches", "passage_tags", "and", "question_tags", "and", "tokens", "matches", "tokens", "but", "not", "stemmed_tokens", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L164-L174
train
allenai/allennlp
allennlp/common/util.py
prepare_environment
def prepare_environment(params: Params): """ Sets random seeds for reproducible experiments. This may not work as expected if you use this from within a python project in which you have already imported Pytorch. If you use the scripts/run_model.py entry point to training models with this library, your experiments should be reasonably reproducible. If you are using this from your own project, you will want to call this function before importing Pytorch. Complete determinism is very difficult to achieve with libraries doing optimized linear algebra due to massively parallel execution, which is exacerbated by using GPUs. Parameters ---------- params: Params object or dict, required. A ``Params`` object or dict holding the json parameters. """ seed = params.pop_int("random_seed", 13370) numpy_seed = params.pop_int("numpy_seed", 1337) torch_seed = params.pop_int("pytorch_seed", 133) if seed is not None: random.seed(seed) if numpy_seed is not None: numpy.random.seed(numpy_seed) if torch_seed is not None: torch.manual_seed(torch_seed) # Seed all GPUs with the same seed if available. if torch.cuda.is_available(): torch.cuda.manual_seed_all(torch_seed) log_pytorch_version_info()
python
def prepare_environment(params: Params): """ Sets random seeds for reproducible experiments. This may not work as expected if you use this from within a python project in which you have already imported Pytorch. If you use the scripts/run_model.py entry point to training models with this library, your experiments should be reasonably reproducible. If you are using this from your own project, you will want to call this function before importing Pytorch. Complete determinism is very difficult to achieve with libraries doing optimized linear algebra due to massively parallel execution, which is exacerbated by using GPUs. Parameters ---------- params: Params object or dict, required. A ``Params`` object or dict holding the json parameters. """ seed = params.pop_int("random_seed", 13370) numpy_seed = params.pop_int("numpy_seed", 1337) torch_seed = params.pop_int("pytorch_seed", 133) if seed is not None: random.seed(seed) if numpy_seed is not None: numpy.random.seed(numpy_seed) if torch_seed is not None: torch.manual_seed(torch_seed) # Seed all GPUs with the same seed if available. if torch.cuda.is_available(): torch.cuda.manual_seed_all(torch_seed) log_pytorch_version_info()
[ "def", "prepare_environment", "(", "params", ":", "Params", ")", ":", "seed", "=", "params", ".", "pop_int", "(", "\"random_seed\"", ",", "13370", ")", "numpy_seed", "=", "params", ".", "pop_int", "(", "\"numpy_seed\"", ",", "1337", ")", "torch_seed", "=", "params", ".", "pop_int", "(", "\"pytorch_seed\"", ",", "133", ")", "if", "seed", "is", "not", "None", ":", "random", ".", "seed", "(", "seed", ")", "if", "numpy_seed", "is", "not", "None", ":", "numpy", ".", "random", ".", "seed", "(", "numpy_seed", ")", "if", "torch_seed", "is", "not", "None", ":", "torch", ".", "manual_seed", "(", "torch_seed", ")", "# Seed all GPUs with the same seed if available.", "if", "torch", ".", "cuda", ".", "is_available", "(", ")", ":", "torch", ".", "cuda", ".", "manual_seed_all", "(", "torch_seed", ")", "log_pytorch_version_info", "(", ")" ]
Sets random seeds for reproducible experiments. This may not work as expected if you use this from within a python project in which you have already imported Pytorch. If you use the scripts/run_model.py entry point to training models with this library, your experiments should be reasonably reproducible. If you are using this from your own project, you will want to call this function before importing Pytorch. Complete determinism is very difficult to achieve with libraries doing optimized linear algebra due to massively parallel execution, which is exacerbated by using GPUs. Parameters ---------- params: Params object or dict, required. A ``Params`` object or dict holding the json parameters.
[ "Sets", "random", "seeds", "for", "reproducible", "experiments", ".", "This", "may", "not", "work", "as", "expected", "if", "you", "use", "this", "from", "within", "a", "python", "project", "in", "which", "you", "have", "already", "imported", "Pytorch", ".", "If", "you", "use", "the", "scripts", "/", "run_model", ".", "py", "entry", "point", "to", "training", "models", "with", "this", "library", "your", "experiments", "should", "be", "reasonably", "reproducible", ".", "If", "you", "are", "using", "this", "from", "your", "own", "project", "you", "will", "want", "to", "call", "this", "function", "before", "importing", "Pytorch", ".", "Complete", "determinism", "is", "very", "difficult", "to", "achieve", "with", "libraries", "doing", "optimized", "linear", "algebra", "due", "to", "massively", "parallel", "execution", "which", "is", "exacerbated", "by", "using", "GPUs", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L177-L206
train
allenai/allennlp
allennlp/common/util.py
prepare_global_logging
def prepare_global_logging(serialization_dir: str, file_friendly_logging: bool) -> logging.FileHandler: """ This function configures 3 global logging attributes - streaming stdout and stderr to a file as well as the terminal, setting the formatting for the python logging library and setting the interval frequency for the Tqdm progress bar. Note that this function does not set the logging level, which is set in ``allennlp/run.py``. Parameters ---------- serialization_dir : ``str``, required. The directory to stream logs to. file_friendly_logging : ``bool``, required. Whether logs should clean the output to prevent carriage returns (used to update progress bars on a single terminal line). This option is typically only used if you are running in an environment without a terminal. Returns ------- ``logging.FileHandler`` A logging file handler that can later be closed and removed from the global logger. """ # If we don't have a terminal as stdout, # force tqdm to be nicer. if not sys.stdout.isatty(): file_friendly_logging = True Tqdm.set_slower_interval(file_friendly_logging) std_out_file = os.path.join(serialization_dir, "stdout.log") sys.stdout = TeeLogger(std_out_file, # type: ignore sys.stdout, file_friendly_logging) sys.stderr = TeeLogger(os.path.join(serialization_dir, "stderr.log"), # type: ignore sys.stderr, file_friendly_logging) stdout_handler = logging.FileHandler(std_out_file) stdout_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')) logging.getLogger().addHandler(stdout_handler) return stdout_handler
python
def prepare_global_logging(serialization_dir: str, file_friendly_logging: bool) -> logging.FileHandler: """ This function configures 3 global logging attributes - streaming stdout and stderr to a file as well as the terminal, setting the formatting for the python logging library and setting the interval frequency for the Tqdm progress bar. Note that this function does not set the logging level, which is set in ``allennlp/run.py``. Parameters ---------- serialization_dir : ``str``, required. The directory to stream logs to. file_friendly_logging : ``bool``, required. Whether logs should clean the output to prevent carriage returns (used to update progress bars on a single terminal line). This option is typically only used if you are running in an environment without a terminal. Returns ------- ``logging.FileHandler`` A logging file handler that can later be closed and removed from the global logger. """ # If we don't have a terminal as stdout, # force tqdm to be nicer. if not sys.stdout.isatty(): file_friendly_logging = True Tqdm.set_slower_interval(file_friendly_logging) std_out_file = os.path.join(serialization_dir, "stdout.log") sys.stdout = TeeLogger(std_out_file, # type: ignore sys.stdout, file_friendly_logging) sys.stderr = TeeLogger(os.path.join(serialization_dir, "stderr.log"), # type: ignore sys.stderr, file_friendly_logging) stdout_handler = logging.FileHandler(std_out_file) stdout_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')) logging.getLogger().addHandler(stdout_handler) return stdout_handler
[ "def", "prepare_global_logging", "(", "serialization_dir", ":", "str", ",", "file_friendly_logging", ":", "bool", ")", "->", "logging", ".", "FileHandler", ":", "# If we don't have a terminal as stdout,", "# force tqdm to be nicer.", "if", "not", "sys", ".", "stdout", ".", "isatty", "(", ")", ":", "file_friendly_logging", "=", "True", "Tqdm", ".", "set_slower_interval", "(", "file_friendly_logging", ")", "std_out_file", "=", "os", ".", "path", ".", "join", "(", "serialization_dir", ",", "\"stdout.log\"", ")", "sys", ".", "stdout", "=", "TeeLogger", "(", "std_out_file", ",", "# type: ignore", "sys", ".", "stdout", ",", "file_friendly_logging", ")", "sys", ".", "stderr", "=", "TeeLogger", "(", "os", ".", "path", ".", "join", "(", "serialization_dir", ",", "\"stderr.log\"", ")", ",", "# type: ignore", "sys", ".", "stderr", ",", "file_friendly_logging", ")", "stdout_handler", "=", "logging", ".", "FileHandler", "(", "std_out_file", ")", "stdout_handler", ".", "setFormatter", "(", "logging", ".", "Formatter", "(", "'%(asctime)s - %(levelname)s - %(name)s - %(message)s'", ")", ")", "logging", ".", "getLogger", "(", ")", ".", "addHandler", "(", "stdout_handler", ")", "return", "stdout_handler" ]
This function configures 3 global logging attributes - streaming stdout and stderr to a file as well as the terminal, setting the formatting for the python logging library and setting the interval frequency for the Tqdm progress bar. Note that this function does not set the logging level, which is set in ``allennlp/run.py``. Parameters ---------- serialization_dir : ``str``, required. The directory to stream logs to. file_friendly_logging : ``bool``, required. Whether logs should clean the output to prevent carriage returns (used to update progress bars on a single terminal line). This option is typically only used if you are running in an environment without a terminal. Returns ------- ``logging.FileHandler`` A logging file handler that can later be closed and removed from the global logger.
[ "This", "function", "configures", "3", "global", "logging", "attributes", "-", "streaming", "stdout", "and", "stderr", "to", "a", "file", "as", "well", "as", "the", "terminal", "setting", "the", "formatting", "for", "the", "python", "logging", "library", "and", "setting", "the", "interval", "frequency", "for", "the", "Tqdm", "progress", "bar", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L208-L250
train
allenai/allennlp
allennlp/common/util.py
cleanup_global_logging
def cleanup_global_logging(stdout_handler: logging.FileHandler) -> None: """ This function closes any open file handles and logs set up by `prepare_global_logging`. Parameters ---------- stdout_handler : ``logging.FileHandler``, required. The file handler returned from `prepare_global_logging`, attached to the global logger. """ stdout_handler.close() logging.getLogger().removeHandler(stdout_handler) if isinstance(sys.stdout, TeeLogger): sys.stdout = sys.stdout.cleanup() if isinstance(sys.stderr, TeeLogger): sys.stderr = sys.stderr.cleanup()
python
def cleanup_global_logging(stdout_handler: logging.FileHandler) -> None: """ This function closes any open file handles and logs set up by `prepare_global_logging`. Parameters ---------- stdout_handler : ``logging.FileHandler``, required. The file handler returned from `prepare_global_logging`, attached to the global logger. """ stdout_handler.close() logging.getLogger().removeHandler(stdout_handler) if isinstance(sys.stdout, TeeLogger): sys.stdout = sys.stdout.cleanup() if isinstance(sys.stderr, TeeLogger): sys.stderr = sys.stderr.cleanup()
[ "def", "cleanup_global_logging", "(", "stdout_handler", ":", "logging", ".", "FileHandler", ")", "->", "None", ":", "stdout_handler", ".", "close", "(", ")", "logging", ".", "getLogger", "(", ")", ".", "removeHandler", "(", "stdout_handler", ")", "if", "isinstance", "(", "sys", ".", "stdout", ",", "TeeLogger", ")", ":", "sys", ".", "stdout", "=", "sys", ".", "stdout", ".", "cleanup", "(", ")", "if", "isinstance", "(", "sys", ".", "stderr", ",", "TeeLogger", ")", ":", "sys", ".", "stderr", "=", "sys", ".", "stderr", ".", "cleanup", "(", ")" ]
This function closes any open file handles and logs set up by `prepare_global_logging`. Parameters ---------- stdout_handler : ``logging.FileHandler``, required. The file handler returned from `prepare_global_logging`, attached to the global logger.
[ "This", "function", "closes", "any", "open", "file", "handles", "and", "logs", "set", "up", "by", "prepare_global_logging", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L252-L267
train
allenai/allennlp
allennlp/common/util.py
get_spacy_model
def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: bool) -> SpacyModelType: """ In order to avoid loading spacy models a whole bunch of times, we'll save references to them, keyed by the options we used to create the spacy model, so any particular configuration only gets loaded once. """ options = (spacy_model_name, pos_tags, parse, ner) if options not in LOADED_SPACY_MODELS: disable = ['vectors', 'textcat'] if not pos_tags: disable.append('tagger') if not parse: disable.append('parser') if not ner: disable.append('ner') try: spacy_model = spacy.load(spacy_model_name, disable=disable) except OSError: logger.warning(f"Spacy models '{spacy_model_name}' not found. Downloading and installing.") spacy_download(spacy_model_name) # NOTE(mattg): The following four lines are a workaround suggested by Ines for spacy # 2.1.0, which removed the linking that was done in spacy 2.0. importlib doesn't find # packages that were installed in the same python session, so the way `spacy_download` # works in 2.1.0 is broken for this use case. These four lines can probably be removed # at some point in the future, once spacy has figured out a better way to handle this. # See https://github.com/explosion/spaCy/issues/3435. from spacy.cli import link from spacy.util import get_package_path package_path = get_package_path(spacy_model_name) link(spacy_model_name, spacy_model_name, model_path=package_path) spacy_model = spacy.load(spacy_model_name, disable=disable) LOADED_SPACY_MODELS[options] = spacy_model return LOADED_SPACY_MODELS[options]
python
def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: bool) -> SpacyModelType: """ In order to avoid loading spacy models a whole bunch of times, we'll save references to them, keyed by the options we used to create the spacy model, so any particular configuration only gets loaded once. """ options = (spacy_model_name, pos_tags, parse, ner) if options not in LOADED_SPACY_MODELS: disable = ['vectors', 'textcat'] if not pos_tags: disable.append('tagger') if not parse: disable.append('parser') if not ner: disable.append('ner') try: spacy_model = spacy.load(spacy_model_name, disable=disable) except OSError: logger.warning(f"Spacy models '{spacy_model_name}' not found. Downloading and installing.") spacy_download(spacy_model_name) # NOTE(mattg): The following four lines are a workaround suggested by Ines for spacy # 2.1.0, which removed the linking that was done in spacy 2.0. importlib doesn't find # packages that were installed in the same python session, so the way `spacy_download` # works in 2.1.0 is broken for this use case. These four lines can probably be removed # at some point in the future, once spacy has figured out a better way to handle this. # See https://github.com/explosion/spaCy/issues/3435. from spacy.cli import link from spacy.util import get_package_path package_path = get_package_path(spacy_model_name) link(spacy_model_name, spacy_model_name, model_path=package_path) spacy_model = spacy.load(spacy_model_name, disable=disable) LOADED_SPACY_MODELS[options] = spacy_model return LOADED_SPACY_MODELS[options]
[ "def", "get_spacy_model", "(", "spacy_model_name", ":", "str", ",", "pos_tags", ":", "bool", ",", "parse", ":", "bool", ",", "ner", ":", "bool", ")", "->", "SpacyModelType", ":", "options", "=", "(", "spacy_model_name", ",", "pos_tags", ",", "parse", ",", "ner", ")", "if", "options", "not", "in", "LOADED_SPACY_MODELS", ":", "disable", "=", "[", "'vectors'", ",", "'textcat'", "]", "if", "not", "pos_tags", ":", "disable", ".", "append", "(", "'tagger'", ")", "if", "not", "parse", ":", "disable", ".", "append", "(", "'parser'", ")", "if", "not", "ner", ":", "disable", ".", "append", "(", "'ner'", ")", "try", ":", "spacy_model", "=", "spacy", ".", "load", "(", "spacy_model_name", ",", "disable", "=", "disable", ")", "except", "OSError", ":", "logger", ".", "warning", "(", "f\"Spacy models '{spacy_model_name}' not found. Downloading and installing.\"", ")", "spacy_download", "(", "spacy_model_name", ")", "# NOTE(mattg): The following four lines are a workaround suggested by Ines for spacy", "# 2.1.0, which removed the linking that was done in spacy 2.0. importlib doesn't find", "# packages that were installed in the same python session, so the way `spacy_download`", "# works in 2.1.0 is broken for this use case. These four lines can probably be removed", "# at some point in the future, once spacy has figured out a better way to handle this.", "# See https://github.com/explosion/spaCy/issues/3435.", "from", "spacy", ".", "cli", "import", "link", "from", "spacy", ".", "util", "import", "get_package_path", "package_path", "=", "get_package_path", "(", "spacy_model_name", ")", "link", "(", "spacy_model_name", ",", "spacy_model_name", ",", "model_path", "=", "package_path", ")", "spacy_model", "=", "spacy", ".", "load", "(", "spacy_model_name", ",", "disable", "=", "disable", ")", "LOADED_SPACY_MODELS", "[", "options", "]", "=", "spacy_model", "return", "LOADED_SPACY_MODELS", "[", "options", "]" ]
In order to avoid loading spacy models a whole bunch of times, we'll save references to them, keyed by the options we used to create the spacy model, so any particular configuration only gets loaded once.
[ "In", "order", "to", "avoid", "loading", "spacy", "models", "a", "whole", "bunch", "of", "times", "we", "ll", "save", "references", "to", "them", "keyed", "by", "the", "options", "we", "used", "to", "create", "the", "spacy", "model", "so", "any", "particular", "configuration", "only", "gets", "loaded", "once", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L272-L306
train
allenai/allennlp
allennlp/common/util.py
import_submodules
def import_submodules(package_name: str) -> None: """ Import all submodules under the given package. Primarily useful so that people using AllenNLP as a library can specify their own custom packages and have their custom classes get loaded and registered. """ importlib.invalidate_caches() # For some reason, python doesn't always add this by default to your path, but you pretty much # always want it when using `--include-package`. And if it's already there, adding it again at # the end won't hurt anything. sys.path.append('.') # Import at top level module = importlib.import_module(package_name) path = getattr(module, '__path__', []) path_string = '' if not path else path[0] # walk_packages only finds immediate children, so need to recurse. for module_finder, name, _ in pkgutil.walk_packages(path): # Sometimes when you import third-party libraries that are on your path, # `pkgutil.walk_packages` returns those too, so we need to skip them. if path_string and module_finder.path != path_string: continue subpackage = f"{package_name}.{name}" import_submodules(subpackage)
python
def import_submodules(package_name: str) -> None: """ Import all submodules under the given package. Primarily useful so that people using AllenNLP as a library can specify their own custom packages and have their custom classes get loaded and registered. """ importlib.invalidate_caches() # For some reason, python doesn't always add this by default to your path, but you pretty much # always want it when using `--include-package`. And if it's already there, adding it again at # the end won't hurt anything. sys.path.append('.') # Import at top level module = importlib.import_module(package_name) path = getattr(module, '__path__', []) path_string = '' if not path else path[0] # walk_packages only finds immediate children, so need to recurse. for module_finder, name, _ in pkgutil.walk_packages(path): # Sometimes when you import third-party libraries that are on your path, # `pkgutil.walk_packages` returns those too, so we need to skip them. if path_string and module_finder.path != path_string: continue subpackage = f"{package_name}.{name}" import_submodules(subpackage)
[ "def", "import_submodules", "(", "package_name", ":", "str", ")", "->", "None", ":", "importlib", ".", "invalidate_caches", "(", ")", "# For some reason, python doesn't always add this by default to your path, but you pretty much", "# always want it when using `--include-package`. And if it's already there, adding it again at", "# the end won't hurt anything.", "sys", ".", "path", ".", "append", "(", "'.'", ")", "# Import at top level", "module", "=", "importlib", ".", "import_module", "(", "package_name", ")", "path", "=", "getattr", "(", "module", ",", "'__path__'", ",", "[", "]", ")", "path_string", "=", "''", "if", "not", "path", "else", "path", "[", "0", "]", "# walk_packages only finds immediate children, so need to recurse.", "for", "module_finder", ",", "name", ",", "_", "in", "pkgutil", ".", "walk_packages", "(", "path", ")", ":", "# Sometimes when you import third-party libraries that are on your path,", "# `pkgutil.walk_packages` returns those too, so we need to skip them.", "if", "path_string", "and", "module_finder", ".", "path", "!=", "path_string", ":", "continue", "subpackage", "=", "f\"{package_name}.{name}\"", "import_submodules", "(", "subpackage", ")" ]
Import all submodules under the given package. Primarily useful so that people using AllenNLP as a library can specify their own custom packages and have their custom classes get loaded and registered.
[ "Import", "all", "submodules", "under", "the", "given", "package", ".", "Primarily", "useful", "so", "that", "people", "using", "AllenNLP", "as", "a", "library", "can", "specify", "their", "own", "custom", "packages", "and", "have", "their", "custom", "classes", "get", "loaded", "and", "registered", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L308-L334
train
allenai/allennlp
allennlp/common/util.py
peak_memory_mb
def peak_memory_mb() -> float: """ Get peak memory usage for this process, as measured by max-resident-set size: https://unix.stackexchange.com/questions/30940/getrusage-system-call-what-is-maximum-resident-set-size Only works on OSX and Linux, returns 0.0 otherwise. """ 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
python
def peak_memory_mb() -> float: """ Get peak memory usage for this process, as measured by max-resident-set size: https://unix.stackexchange.com/questions/30940/getrusage-system-call-what-is-maximum-resident-set-size Only works on OSX and Linux, returns 0.0 otherwise. """ 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
[ "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" ]
Get peak memory usage for this process, as measured by max-resident-set size: https://unix.stackexchange.com/questions/30940/getrusage-system-call-what-is-maximum-resident-set-size Only works on OSX and Linux, returns 0.0 otherwise.
[ "Get", "peak", "memory", "usage", "for", "this", "process", "as", "measured", "by", "max", "-", "resident", "-", "set", "size", ":" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L337-L360
train
allenai/allennlp
allennlp/common/util.py
gpu_memory_mb
def gpu_memory_mb() -> Dict[int, int]: """ Get the current GPU memory usage. Based on https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/4 Returns ------- ``Dict[int, int]`` Keys are device ids as integers. Values are memory usage as integers in MB. Returns an empty ``dict`` if GPUs are not available. """ # 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 {}
python
def gpu_memory_mb() -> Dict[int, int]: """ Get the current GPU memory usage. Based on https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/4 Returns ------- ``Dict[int, int]`` Keys are device ids as integers. Values are memory usage as integers in MB. Returns an empty ``dict`` if GPUs are not available. """ # 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 {}
[ "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", "{", "}" ]
Get the current GPU memory usage. Based on https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/4 Returns ------- ``Dict[int, int]`` Keys are device ids as integers. Values are memory usage as integers in MB. Returns an empty ``dict`` if GPUs are not available.
[ "Get", "the", "current", "GPU", "memory", "usage", ".", "Based", "on", "https", ":", "//", "discuss", ".", "pytorch", ".", "org", "/", "t", "/", "access", "-", "gpu", "-", "memory", "-", "usage", "-", "in", "-", "pytorch", "/", "3192", "/", "4" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L362-L388
train
allenai/allennlp
allennlp/common/util.py
ensure_list
def ensure_list(iterable: Iterable[A]) -> List[A]: """ An Iterable may be a list or a generator. This ensures we get a list without making an unnecessary copy. """ if isinstance(iterable, list): return iterable else: return list(iterable)
python
def ensure_list(iterable: Iterable[A]) -> List[A]: """ An Iterable may be a list or a generator. This ensures we get a list without making an unnecessary copy. """ if isinstance(iterable, list): return iterable else: return list(iterable)
[ "def", "ensure_list", "(", "iterable", ":", "Iterable", "[", "A", "]", ")", "->", "List", "[", "A", "]", ":", "if", "isinstance", "(", "iterable", ",", "list", ")", ":", "return", "iterable", "else", ":", "return", "list", "(", "iterable", ")" ]
An Iterable may be a list or a generator. This ensures we get a list without making an unnecessary copy.
[ "An", "Iterable", "may", "be", "a", "list", "or", "a", "generator", ".", "This", "ensures", "we", "get", "a", "list", "without", "making", "an", "unnecessary", "copy", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/common/util.py#L391-L399
train
allenai/allennlp
allennlp/state_machines/states/checklist_statelet.py
ChecklistStatelet.update
def update(self, action: torch.Tensor) -> 'ChecklistStatelet': """ Takes an action index, updates checklist and returns an updated state. """ 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
python
def update(self, action: torch.Tensor) -> 'ChecklistStatelet': """ Takes an action index, updates checklist and returns an updated state. """ 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
[ "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" ]
Takes an action index, updates checklist and returns an updated state.
[ "Takes", "an", "action", "index", "updates", "checklist", "and", "returns", "an", "updated", "state", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/state_machines/states/checklist_statelet.py#L57-L68
train
allenai/allennlp
allennlp/semparse/worlds/wikitables_world.py
WikiTablesWorld._remove_action_from_type
def _remove_action_from_type(valid_actions: Dict[str, List[str]], type_: str, filter_function: Callable[[str], bool]) -> None: """ Finds the production rule matching the filter function in the given type's valid action list, and removes it. If there is more than one matching function, we crash. """ 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])
python
def _remove_action_from_type(valid_actions: Dict[str, List[str]], type_: str, filter_function: Callable[[str], bool]) -> None: """ Finds the production rule matching the filter function in the given type's valid action list, and removes it. If there is more than one matching function, we crash. """ 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])
[ "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", "]", ")" ]
Finds the production rule matching the filter function in the given type's valid action list, and removes it. If there is more than one matching function, we crash.
[ "Finds", "the", "production", "rule", "matching", "the", "filter", "function", "in", "the", "given", "type", "s", "valid", "action", "list", "and", "removes", "it", ".", "If", "there", "is", "more", "than", "one", "matching", "function", "we", "crash", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/wikitables_world.py#L124-L134
train
allenai/allennlp
allennlp/modules/lstm_cell_with_projection.py
LstmCellWithProjection.forward
def forward(self, # pylint: disable=arguments-differ inputs: torch.FloatTensor, batch_lengths: List[int], initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None): """ Parameters ---------- inputs : ``torch.FloatTensor``, required. A tensor of shape (batch_size, num_timesteps, input_size) to apply the LSTM over. batch_lengths : ``List[int]``, required. A list of length batch_size containing the lengths of the sequences in batch. initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size). Returns ------- output_accumulator : ``torch.FloatTensor`` The outputs of the LSTM for each timestep. A tensor of shape (batch_size, max_timesteps, hidden_size) where for a given batch element, all outputs past the sequence length for that batch are zero tensors. final_state : ``Tuple[``torch.FloatTensor, torch.FloatTensor]`` A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size). """ 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
python
def forward(self, # pylint: disable=arguments-differ inputs: torch.FloatTensor, batch_lengths: List[int], initial_state: Optional[Tuple[torch.Tensor, torch.Tensor]] = None): """ Parameters ---------- inputs : ``torch.FloatTensor``, required. A tensor of shape (batch_size, num_timesteps, input_size) to apply the LSTM over. batch_lengths : ``List[int]``, required. A list of length batch_size containing the lengths of the sequences in batch. initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size). Returns ------- output_accumulator : ``torch.FloatTensor`` The outputs of the LSTM for each timestep. A tensor of shape (batch_size, max_timesteps, hidden_size) where for a given batch element, all outputs past the sequence length for that batch are zero tensors. final_state : ``Tuple[``torch.FloatTensor, torch.FloatTensor]`` A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size). """ 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
[ "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" ]
Parameters ---------- inputs : ``torch.FloatTensor``, required. A tensor of shape (batch_size, num_timesteps, input_size) to apply the LSTM over. batch_lengths : ``List[int]``, required. A list of length batch_size containing the lengths of the sequences in batch. initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None) A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size). Returns ------- output_accumulator : ``torch.FloatTensor`` The outputs of the LSTM for each timestep. A tensor of shape (batch_size, max_timesteps, hidden_size) where for a given batch element, all outputs past the sequence length for that batch are zero tensors. final_state : ``Tuple[``torch.FloatTensor, torch.FloatTensor]`` A tuple (state, memory) representing the initial hidden state and memory of the LSTM. The ``state`` has shape (1, batch_size, hidden_size) and the ``memory`` has shape (1, batch_size, cell_size).
[ "Parameters", "----------", "inputs", ":", "torch", ".", "FloatTensor", "required", ".", "A", "tensor", "of", "shape", "(", "batch_size", "num_timesteps", "input_size", ")", "to", "apply", "the", "LSTM", "over", ".", "batch_lengths", ":", "List", "[", "int", "]", "required", ".", "A", "list", "of", "length", "batch_size", "containing", "the", "lengths", "of", "the", "sequences", "in", "batch", ".", "initial_state", ":", "Tuple", "[", "torch", ".", "Tensor", "torch", ".", "Tensor", "]", "optional", "(", "default", "=", "None", ")", "A", "tuple", "(", "state", "memory", ")", "representing", "the", "initial", "hidden", "state", "and", "memory", "of", "the", "LSTM", ".", "The", "state", "has", "shape", "(", "1", "batch_size", "hidden_size", ")", "and", "the", "memory", "has", "shape", "(", "1", "batch_size", "cell_size", ")", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/modules/lstm_cell_with_projection.py#L93-L230
train
allenai/allennlp
doc/conf.py
linkcode_resolve
def linkcode_resolve(domain, info): """ Determine the URL corresponding to Python object This code is from https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L290 and https://github.com/Lasagne/Lasagne/pull/262 """ 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)
python
def linkcode_resolve(domain, info): """ Determine the URL corresponding to Python object This code is from https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L290 and https://github.com/Lasagne/Lasagne/pull/262 """ 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)
[ "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", ")" ]
Determine the URL corresponding to Python object This code is from https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L290 and https://github.com/Lasagne/Lasagne/pull/262
[ "Determine", "the", "URL", "corresponding", "to", "Python", "object", "This", "code", "is", "from", "https", ":", "//", "github", ".", "com", "/", "numpy", "/", "numpy", "/", "blob", "/", "master", "/", "doc", "/", "source", "/", "conf", ".", "py#L290", "and", "https", ":", "//", "github", ".", "com", "/", "Lasagne", "/", "Lasagne", "/", "pull", "/", "262" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/doc/conf.py#L199-L241
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._get_initial_rnn_and_grammar_state
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]]: """ Encodes the question and table, computes a linking between the two, and constructs an initial RnnStatelet and LambdaGrammarStatelet for each batch instance to pass to the decoder. We take ``outputs`` as a parameter here and `modify` it, adding things that we want to visualize in a demo. """ 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
python
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]]: """ Encodes the question and table, computes a linking between the two, and constructs an initial RnnStatelet and LambdaGrammarStatelet for each batch instance to pass to the decoder. We take ``outputs`` as a parameter here and `modify` it, adding things that we want to visualize in a demo. """ 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
[ "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" ]
Encodes the question and table, computes a linking between the two, and constructs an initial RnnStatelet and LambdaGrammarStatelet for each batch instance to pass to the decoder. We take ``outputs`` as a parameter here and `modify` it, adding things that we want to visualize in a demo.
[ "Encodes", "the", "question", "and", "table", "computes", "a", "linking", "between", "the", "two", "and", "constructs", "an", "initial", "RnnStatelet", "and", "LambdaGrammarStatelet", "for", "each", "batch", "instance", "to", "pass", "to", "the", "decoder", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L140-L296
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._get_neighbor_indices
def _get_neighbor_indices(worlds: List[WikiTablesWorld], num_entities: int, tensor: torch.Tensor) -> torch.LongTensor: """ This method returns the indices of each entity's neighbors. A tensor is accepted as a parameter for copying purposes. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities, num_neighbors)``. It is padded with -1 instead of 0, since 0 is a valid neighbor index. """ 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)
python
def _get_neighbor_indices(worlds: List[WikiTablesWorld], num_entities: int, tensor: torch.Tensor) -> torch.LongTensor: """ This method returns the indices of each entity's neighbors. A tensor is accepted as a parameter for copying purposes. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities, num_neighbors)``. It is padded with -1 instead of 0, since 0 is a valid neighbor index. """ 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)
[ "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", ")" ]
This method returns the indices of each entity's neighbors. A tensor is accepted as a parameter for copying purposes. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities, num_neighbors)``. It is padded with -1 instead of 0, since 0 is a valid neighbor index.
[ "This", "method", "returns", "the", "indices", "of", "each", "entity", "s", "neighbors", ".", "A", "tensor", "is", "accepted", "as", "a", "parameter", "for", "copying", "purposes", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L299-L341
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._get_type_vector
def _get_type_vector(worlds: List[WikiTablesWorld], num_entities: int, tensor: torch.Tensor) -> Tuple[torch.LongTensor, Dict[int, int]]: """ Produces a tensor with shape ``(batch_size, num_entities)`` that encodes each entity's type. In addition, a map from a flattened entity index to type is returned to combine entity type operations into one method. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities)``. entity_types : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id. """ 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
python
def _get_type_vector(worlds: List[WikiTablesWorld], num_entities: int, tensor: torch.Tensor) -> Tuple[torch.LongTensor, Dict[int, int]]: """ Produces a tensor with shape ``(batch_size, num_entities)`` that encodes each entity's type. In addition, a map from a flattened entity index to type is returned to combine entity type operations into one method. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities)``. entity_types : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id. """ 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
[ "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" ]
Produces a tensor with shape ``(batch_size, num_entities)`` that encodes each entity's type. In addition, a map from a flattened entity index to type is returned to combine entity type operations into one method. Parameters ---------- worlds : ``List[WikiTablesWorld]`` num_entities : ``int`` tensor : ``torch.Tensor`` Used for copying the constructed list onto the right device. Returns ------- A ``torch.LongTensor`` with shape ``(batch_size, num_entities)``. entity_types : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id.
[ "Produces", "a", "tensor", "with", "shape", "(", "batch_size", "num_entities", ")", "that", "encodes", "each", "entity", "s", "type", ".", "In", "addition", "a", "map", "from", "a", "flattened", "entity", "index", "to", "type", "is", "returned", "to", "combine", "entity", "type", "operations", "into", "one", "method", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L344-L390
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._get_linking_probabilities
def _get_linking_probabilities(self, worlds: List[WikiTablesWorld], linking_scores: torch.FloatTensor, question_mask: torch.LongTensor, entity_type_dict: Dict[int, int]) -> torch.FloatTensor: """ Produces the probability of an entity given a question word and type. The logic below separates the entities by type since the softmax normalization term sums over entities of a single type. Parameters ---------- worlds : ``List[WikiTablesWorld]`` linking_scores : ``torch.FloatTensor`` Has shape (batch_size, num_question_tokens, num_entities). question_mask: ``torch.LongTensor`` Has shape (batch_size, num_question_tokens). entity_type_dict : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id. Returns ------- batch_probabilities : ``torch.FloatTensor`` Has shape ``(batch_size, num_question_tokens, num_entities)``. Contains all the probabilities for an entity given a question word. """ _, 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()
python
def _get_linking_probabilities(self, worlds: List[WikiTablesWorld], linking_scores: torch.FloatTensor, question_mask: torch.LongTensor, entity_type_dict: Dict[int, int]) -> torch.FloatTensor: """ Produces the probability of an entity given a question word and type. The logic below separates the entities by type since the softmax normalization term sums over entities of a single type. Parameters ---------- worlds : ``List[WikiTablesWorld]`` linking_scores : ``torch.FloatTensor`` Has shape (batch_size, num_question_tokens, num_entities). question_mask: ``torch.LongTensor`` Has shape (batch_size, num_question_tokens). entity_type_dict : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id. Returns ------- batch_probabilities : ``torch.FloatTensor`` Has shape ``(batch_size, num_question_tokens, num_entities)``. Contains all the probabilities for an entity given a question word. """ _, 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()
[ "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", "(", ")" ]
Produces the probability of an entity given a question word and type. The logic below separates the entities by type since the softmax normalization term sums over entities of a single type. Parameters ---------- worlds : ``List[WikiTablesWorld]`` linking_scores : ``torch.FloatTensor`` Has shape (batch_size, num_question_tokens, num_entities). question_mask: ``torch.LongTensor`` Has shape (batch_size, num_question_tokens). entity_type_dict : ``Dict[int, int]`` This is a mapping from ((batch_index * num_entities) + entity_index) to entity type id. Returns ------- batch_probabilities : ``torch.FloatTensor`` Has shape ``(batch_size, num_question_tokens, num_entities)``. Contains all the probabilities for an entity given a question word.
[ "Produces", "the", "probability", "of", "an", "entity", "given", "a", "question", "word", "and", "type", ".", "The", "logic", "below", "separates", "the", "entities", "by", "type", "since", "the", "softmax", "normalization", "term", "sums", "over", "entities", "of", "a", "single", "type", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L392-L472
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser.get_metrics
def get_metrics(self, reset: bool = False) -> Dict[str, float]: """ We track three metrics here: 1. dpd_acc, which is the percentage of the time that our best output action sequence is in the set of action sequences provided by DPD. This is an easy-to-compute lower bound on denotation accuracy for the set of examples where we actually have DPD output. We only score dpd_acc on that subset. 2. denotation_acc, which is the percentage of examples where we get the correct denotation. This is the typical "accuracy" metric, and it is what you should usually report in an experimental result. You need to be careful, though, that you're computing this on the full data, and not just the subset that has DPD output (make sure you pass "keep_if_no_dpd=True" to the dataset reader, which we do for validation data, but not training data). 3. lf_percent, which is the percentage of time that decoding actually produces a finished logical form. We might not produce a valid logical form if the decoder gets into a repetitive loop, or we're trying to produce a super long logical form and run out of time steps, or something. """ 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), }
python
def get_metrics(self, reset: bool = False) -> Dict[str, float]: """ We track three metrics here: 1. dpd_acc, which is the percentage of the time that our best output action sequence is in the set of action sequences provided by DPD. This is an easy-to-compute lower bound on denotation accuracy for the set of examples where we actually have DPD output. We only score dpd_acc on that subset. 2. denotation_acc, which is the percentage of examples where we get the correct denotation. This is the typical "accuracy" metric, and it is what you should usually report in an experimental result. You need to be careful, though, that you're computing this on the full data, and not just the subset that has DPD output (make sure you pass "keep_if_no_dpd=True" to the dataset reader, which we do for validation data, but not training data). 3. lf_percent, which is the percentage of time that decoding actually produces a finished logical form. We might not produce a valid logical form if the decoder gets into a repetitive loop, or we're trying to produce a super long logical form and run out of time steps, or something. """ 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), }
[ "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", ")", ",", "}" ]
We track three metrics here: 1. dpd_acc, which is the percentage of the time that our best output action sequence is in the set of action sequences provided by DPD. This is an easy-to-compute lower bound on denotation accuracy for the set of examples where we actually have DPD output. We only score dpd_acc on that subset. 2. denotation_acc, which is the percentage of examples where we get the correct denotation. This is the typical "accuracy" metric, and it is what you should usually report in an experimental result. You need to be careful, though, that you're computing this on the full data, and not just the subset that has DPD output (make sure you pass "keep_if_no_dpd=True" to the dataset reader, which we do for validation data, but not training data). 3. lf_percent, which is the percentage of time that decoding actually produces a finished logical form. We might not produce a valid logical form if the decoder gets into a repetitive loop, or we're trying to produce a super long logical form and run out of time steps, or something.
[ "We", "track", "three", "metrics", "here", ":" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L486-L511
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._create_grammar_state
def _create_grammar_state(self, world: WikiTablesWorld, possible_actions: List[ProductionRule], linking_scores: torch.Tensor, entity_types: torch.Tensor) -> LambdaGrammarStatelet: """ This method creates the LambdaGrammarStatelet object that's used for decoding. Part of creating that is creating the `valid_actions` dictionary, which contains embedded representations of all of the valid actions. So, we create that here as well. The way we represent the valid expansions is a little complicated: we use a dictionary of `action types`, where the key is the action type (like "global", "linked", or whatever your model is expecting), and the value is a tuple representing all actions of that type. The tuple is (input tensor, output tensor, action id). The input tensor has the representation that is used when `selecting` actions, for all actions of this type. The output tensor has the representation that is used when feeding the action to the next step of the decoder (this could just be the same as the input tensor). The action ids are a list of indices into the main action list for each batch instance. The inputs to this method are for a `single instance in the batch`; none of the tensors we create here are batched. We grab the global action ids from the input ``ProductionRules``, and we use those to embed the valid actions for every non-terminal type. We use the input ``linking_scores`` for non-global actions. Parameters ---------- world : ``WikiTablesWorld`` From the input to ``forward`` for a single batch instance. possible_actions : ``List[ProductionRule]`` From the input to ``forward`` for a single batch instance. linking_scores : ``torch.Tensor`` Assumed to have shape ``(num_entities, num_question_tokens)`` (i.e., there is no batch dimension). entity_types : ``torch.Tensor`` Assumed to have shape ``(num_entities,)`` (i.e., there is no batch dimension). """ # 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)
python
def _create_grammar_state(self, world: WikiTablesWorld, possible_actions: List[ProductionRule], linking_scores: torch.Tensor, entity_types: torch.Tensor) -> LambdaGrammarStatelet: """ This method creates the LambdaGrammarStatelet object that's used for decoding. Part of creating that is creating the `valid_actions` dictionary, which contains embedded representations of all of the valid actions. So, we create that here as well. The way we represent the valid expansions is a little complicated: we use a dictionary of `action types`, where the key is the action type (like "global", "linked", or whatever your model is expecting), and the value is a tuple representing all actions of that type. The tuple is (input tensor, output tensor, action id). The input tensor has the representation that is used when `selecting` actions, for all actions of this type. The output tensor has the representation that is used when feeding the action to the next step of the decoder (this could just be the same as the input tensor). The action ids are a list of indices into the main action list for each batch instance. The inputs to this method are for a `single instance in the batch`; none of the tensors we create here are batched. We grab the global action ids from the input ``ProductionRules``, and we use those to embed the valid actions for every non-terminal type. We use the input ``linking_scores`` for non-global actions. Parameters ---------- world : ``WikiTablesWorld`` From the input to ``forward`` for a single batch instance. possible_actions : ``List[ProductionRule]`` From the input to ``forward`` for a single batch instance. linking_scores : ``torch.Tensor`` Assumed to have shape ``(num_entities, num_question_tokens)`` (i.e., there is no batch dimension). entity_types : ``torch.Tensor`` Assumed to have shape ``(num_entities,)`` (i.e., there is no batch dimension). """ # 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)
[ "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", ")" ]
This method creates the LambdaGrammarStatelet object that's used for decoding. Part of creating that is creating the `valid_actions` dictionary, which contains embedded representations of all of the valid actions. So, we create that here as well. The way we represent the valid expansions is a little complicated: we use a dictionary of `action types`, where the key is the action type (like "global", "linked", or whatever your model is expecting), and the value is a tuple representing all actions of that type. The tuple is (input tensor, output tensor, action id). The input tensor has the representation that is used when `selecting` actions, for all actions of this type. The output tensor has the representation that is used when feeding the action to the next step of the decoder (this could just be the same as the input tensor). The action ids are a list of indices into the main action list for each batch instance. The inputs to this method are for a `single instance in the batch`; none of the tensors we create here are batched. We grab the global action ids from the input ``ProductionRules``, and we use those to embed the valid actions for every non-terminal type. We use the input ``linking_scores`` for non-global actions. Parameters ---------- world : ``WikiTablesWorld`` From the input to ``forward`` for a single batch instance. possible_actions : ``List[ProductionRule]`` From the input to ``forward`` for a single batch instance. linking_scores : ``torch.Tensor`` Assumed to have shape ``(num_entities, num_question_tokens)`` (i.e., there is no batch dimension). entity_types : ``torch.Tensor`` Assumed to have shape ``(num_entities,)`` (i.e., there is no batch dimension).
[ "This", "method", "creates", "the", "LambdaGrammarStatelet", "object", "that", "s", "used", "for", "decoding", ".", "Part", "of", "creating", "that", "is", "creating", "the", "valid_actions", "dictionary", "which", "contains", "embedded", "representations", "of", "all", "of", "the", "valid", "actions", ".", "So", "we", "create", "that", "here", "as", "well", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L513-L620
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser._compute_validation_outputs
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: """ Does common things for validation time: computing logical form accuracy (which is expensive and unnecessary during training), adding visualization info to the output dictionary, etc. This doesn't return anything; instead it `modifies` the given ``outputs`` dictionary, and calls metrics on ``self``. """ 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]
python
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: """ Does common things for validation time: computing logical form accuracy (which is expensive and unnecessary during training), adding visualization info to the output dictionary, etc. This doesn't return anything; instead it `modifies` the given ``outputs`` dictionary, and calls metrics on ``self``. """ 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]
[ "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", "]" ]
Does common things for validation time: computing logical form accuracy (which is expensive and unnecessary during training), adding visualization info to the output dictionary, etc. This doesn't return anything; instead it `modifies` the given ``outputs`` dictionary, and calls metrics on ``self``.
[ "Does", "common", "things", "for", "validation", "time", ":", "computing", "logical", "form", "accuracy", "(", "which", "is", "expensive", "and", "unnecessary", "during", "training", ")", "adding", "visualization", "info", "to", "the", "output", "dictionary", "etc", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L622-L673
train
allenai/allennlp
allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py
WikiTablesSemanticParser.decode
def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: """ This method overrides ``Model.decode``, which gets called after ``Model.forward``, at test time, to finalize predictions. This is (confusingly) a separate notion from the "decoder" in "encoder/decoder", where that decoder logic lives in the ``TransitionFunction``. This method trims the output predictions to the first end symbol, replaces indices with corresponding tokens, and adds a field called ``predicted_tokens`` to the ``output_dict``. """ 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
python
def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: """ This method overrides ``Model.decode``, which gets called after ``Model.forward``, at test time, to finalize predictions. This is (confusingly) a separate notion from the "decoder" in "encoder/decoder", where that decoder logic lives in the ``TransitionFunction``. This method trims the output predictions to the first end symbol, replaces indices with corresponding tokens, and adds a field called ``predicted_tokens`` to the ``output_dict``. """ 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
[ "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" ]
This method overrides ``Model.decode``, which gets called after ``Model.forward``, at test time, to finalize predictions. This is (confusingly) a separate notion from the "decoder" in "encoder/decoder", where that decoder logic lives in the ``TransitionFunction``. This method trims the output predictions to the first end symbol, replaces indices with corresponding tokens, and adds a field called ``predicted_tokens`` to the ``output_dict``.
[ "This", "method", "overrides", "Model", ".", "decode", "which", "gets", "called", "after", "Model", ".", "forward", "at", "test", "time", "to", "finalize", "predictions", ".", "This", "is", "(", "confusingly", ")", "a", "separate", "notion", "from", "the", "decoder", "in", "encoder", "/", "decoder", "where", "that", "decoder", "logic", "lives", "in", "the", "TransitionFunction", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/semantic_parsing/wikitables/wikitables_semantic_parser.py#L676-L708
train
allenai/allennlp
allennlp/state_machines/transition_functions/linking_coverage_transition_function.py
LinkingCoverageTransitionFunction._get_linked_logits_addition
def _get_linked_logits_addition(checklist_state: ChecklistStatelet, action_ids: List[int], action_logits: torch.Tensor) -> torch.Tensor: """ Gets the logits of desired terminal actions yet to be produced by the decoder, and returns them for the decoder to add to the prior action logits, biasing the model towards predicting missing linked actions. """ # 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
python
def _get_linked_logits_addition(checklist_state: ChecklistStatelet, action_ids: List[int], action_logits: torch.Tensor) -> torch.Tensor: """ Gets the logits of desired terminal actions yet to be produced by the decoder, and returns them for the decoder to add to the prior action logits, biasing the model towards predicting missing linked actions. """ # 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
[ "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" ]
Gets the logits of desired terminal actions yet to be produced by the decoder, and returns them for the decoder to add to the prior action logits, biasing the model towards predicting missing linked actions.
[ "Gets", "the", "logits", "of", "desired", "terminal", "actions", "yet", "to", "be", "produced", "by", "the", "decoder", "and", "returns", "them", "for", "the", "decoder", "to", "add", "to", "the", "prior", "action", "logits", "biasing", "the", "model", "towards", "predicting", "missing", "linked", "actions", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/state_machines/transition_functions/linking_coverage_transition_function.py#L161-L196
train
allenai/allennlp
allennlp/state_machines/transition_functions/basic_transition_function.py
BasicTransitionFunction.attend_on_question
def attend_on_question(self, query: torch.Tensor, encoder_outputs: torch.Tensor, encoder_output_mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Given a query (which is typically the decoder hidden state), compute an attention over the output of the question encoder, and return a weighted sum of the question representations given this attention. We also return the attention weights themselves. This is a simple computation, but we have it as a separate method so that the ``forward`` method on the main parser module can call it on the initial hidden state, to simplify the logic in ``take_step``. """ # (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
python
def attend_on_question(self, query: torch.Tensor, encoder_outputs: torch.Tensor, encoder_output_mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Given a query (which is typically the decoder hidden state), compute an attention over the output of the question encoder, and return a weighted sum of the question representations given this attention. We also return the attention weights themselves. This is a simple computation, but we have it as a separate method so that the ``forward`` method on the main parser module can call it on the initial hidden state, to simplify the logic in ``take_step``. """ # (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
[ "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" ]
Given a query (which is typically the decoder hidden state), compute an attention over the output of the question encoder, and return a weighted sum of the question representations given this attention. We also return the attention weights themselves. This is a simple computation, but we have it as a separate method so that the ``forward`` method on the main parser module can call it on the initial hidden state, to simplify the logic in ``take_step``.
[ "Given", "a", "query", "(", "which", "is", "typically", "the", "decoder", "hidden", "state", ")", "compute", "an", "attention", "over", "the", "output", "of", "the", "question", "encoder", "and", "return", "a", "weighted", "sum", "of", "the", "question", "representations", "given", "this", "attention", ".", "We", "also", "return", "the", "attention", "weights", "themselves", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/state_machines/transition_functions/basic_transition_function.py#L393-L412
train
allenai/allennlp
allennlp/semparse/action_space_walker.py
ActionSpaceWalker._walk
def _walk(self) -> None: """ Walk over action space to collect completed paths of at most ``self._max_path_length`` steps. """ # 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))
python
def _walk(self) -> None: """ Walk over action space to collect completed paths of at most ``self._max_path_length`` steps. """ # 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))
[ "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", ")", ")" ]
Walk over action space to collect completed paths of at most ``self._max_path_length`` steps.
[ "Walk", "over", "action", "space", "to", "collect", "completed", "paths", "of", "at", "most", "self", ".", "_max_path_length", "steps", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/action_space_walker.py#L35-L101
train
allenai/allennlp
allennlp/data/dataset.py
Batch._check_types
def _check_types(self) -> None: """ Check that all the instances have the same types. """ 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.")
python
def _check_types(self) -> None: """ Check that all the instances have the same types. """ 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.")
[ "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.\"", ")" ]
Check that all the instances have the same types.
[ "Check", "that", "all", "the", "instances", "have", "the", "same", "types", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/dataset.py#L35-L44
train
allenai/allennlp
allennlp/data/dataset.py
Batch.get_padding_lengths
def get_padding_lengths(self) -> Dict[str, Dict[str, int]]: """ Gets the maximum padding lengths from all ``Instances`` in this batch. Each ``Instance`` has multiple ``Fields``, and each ``Field`` could have multiple things that need padding. We look at all fields in all instances, and find the max values for each (field_name, padding_key) pair, returning them in a dictionary. This can then be used to convert this batch into arrays of consistent length, or to set model parameters, etc. """ 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}
python
def get_padding_lengths(self) -> Dict[str, Dict[str, int]]: """ Gets the maximum padding lengths from all ``Instances`` in this batch. Each ``Instance`` has multiple ``Fields``, and each ``Field`` could have multiple things that need padding. We look at all fields in all instances, and find the max values for each (field_name, padding_key) pair, returning them in a dictionary. This can then be used to convert this batch into arrays of consistent length, or to set model parameters, etc. """ 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}
[ "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", "}" ]
Gets the maximum padding lengths from all ``Instances`` in this batch. Each ``Instance`` has multiple ``Fields``, and each ``Field`` could have multiple things that need padding. We look at all fields in all instances, and find the max values for each (field_name, padding_key) pair, returning them in a dictionary. This can then be used to convert this batch into arrays of consistent length, or to set model parameters, etc.
[ "Gets", "the", "maximum", "padding", "lengths", "from", "all", "Instances", "in", "this", "batch", ".", "Each", "Instance", "has", "multiple", "Fields", "and", "each", "Field", "could", "have", "multiple", "things", "that", "need", "padding", ".", "We", "look", "at", "all", "fields", "in", "all", "instances", "and", "find", "the", "max", "values", "for", "each", "(", "field_name", "padding_key", ")", "pair", "returning", "them", "in", "a", "dictionary", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/dataset.py#L46-L69
train
allenai/allennlp
allennlp/data/dataset.py
Batch.as_tensor_dict
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. """ This method converts this ``Batch`` into a set of pytorch Tensors that can be passed through a model. In order for the tensors to be valid tensors, all ``Instances`` in this batch need to be padded to the same lengths wherever padding is necessary, so we do that first, then we combine all of the tensors for each field in each instance into a set of batched tensors for each field. Parameters ---------- padding_lengths : ``Dict[str, Dict[str, int]]`` If a key is present in this dictionary with a non-``None`` value, we will pad to that length instead of the length calculated from the data. This lets you, e.g., set a maximum value for sentence length if you want to throw out long sequences. Entries in this dictionary are keyed first by field name (e.g., "question"), then by padding key (e.g., "num_tokens"). verbose : ``bool``, optional (default=``False``) Should we output logging information when we're doing this padding? If the batch is large, this is nice to have, because padding a large batch could take a long time. But if you're doing this inside of a data generator, having all of this output per batch is a bit obnoxious (and really slow). Returns ------- tensors : ``Dict[str, DataArray]`` A dictionary of tensors, keyed by field name, suitable for passing as input to a model. This is a `batch` of instances, so, e.g., if the instances have a "question" field and an "answer" field, the "question" fields for all of the instances will be grouped together into a single tensor, and the "answer" fields for all instances will be similarly grouped in a parallel set of tensors, for batched computation. Additionally, for complex ``Fields``, the value of the dictionary key is not necessarily a single tensor. For example, with the ``TextField``, the output is a dictionary mapping ``TokenIndexer`` keys to tensors. The number of elements in this sub-dictionary therefore corresponds to the number of ``TokenIndexers`` used to index the ``TextField``. Each ``Field`` class is responsible for batching its own output. """ 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
python
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. """ This method converts this ``Batch`` into a set of pytorch Tensors that can be passed through a model. In order for the tensors to be valid tensors, all ``Instances`` in this batch need to be padded to the same lengths wherever padding is necessary, so we do that first, then we combine all of the tensors for each field in each instance into a set of batched tensors for each field. Parameters ---------- padding_lengths : ``Dict[str, Dict[str, int]]`` If a key is present in this dictionary with a non-``None`` value, we will pad to that length instead of the length calculated from the data. This lets you, e.g., set a maximum value for sentence length if you want to throw out long sequences. Entries in this dictionary are keyed first by field name (e.g., "question"), then by padding key (e.g., "num_tokens"). verbose : ``bool``, optional (default=``False``) Should we output logging information when we're doing this padding? If the batch is large, this is nice to have, because padding a large batch could take a long time. But if you're doing this inside of a data generator, having all of this output per batch is a bit obnoxious (and really slow). Returns ------- tensors : ``Dict[str, DataArray]`` A dictionary of tensors, keyed by field name, suitable for passing as input to a model. This is a `batch` of instances, so, e.g., if the instances have a "question" field and an "answer" field, the "question" fields for all of the instances will be grouped together into a single tensor, and the "answer" fields for all instances will be similarly grouped in a parallel set of tensors, for batched computation. Additionally, for complex ``Fields``, the value of the dictionary key is not necessarily a single tensor. For example, with the ``TextField``, the output is a dictionary mapping ``TokenIndexer`` keys to tensors. The number of elements in this sub-dictionary therefore corresponds to the number of ``TokenIndexers`` used to index the ``TextField``. Each ``Field`` class is responsible for batching its own output. """ 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
[ "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" ]
This method converts this ``Batch`` into a set of pytorch Tensors that can be passed through a model. In order for the tensors to be valid tensors, all ``Instances`` in this batch need to be padded to the same lengths wherever padding is necessary, so we do that first, then we combine all of the tensors for each field in each instance into a set of batched tensors for each field. Parameters ---------- padding_lengths : ``Dict[str, Dict[str, int]]`` If a key is present in this dictionary with a non-``None`` value, we will pad to that length instead of the length calculated from the data. This lets you, e.g., set a maximum value for sentence length if you want to throw out long sequences. Entries in this dictionary are keyed first by field name (e.g., "question"), then by padding key (e.g., "num_tokens"). verbose : ``bool``, optional (default=``False``) Should we output logging information when we're doing this padding? If the batch is large, this is nice to have, because padding a large batch could take a long time. But if you're doing this inside of a data generator, having all of this output per batch is a bit obnoxious (and really slow). Returns ------- tensors : ``Dict[str, DataArray]`` A dictionary of tensors, keyed by field name, suitable for passing as input to a model. This is a `batch` of instances, so, e.g., if the instances have a "question" field and an "answer" field, the "question" fields for all of the instances will be grouped together into a single tensor, and the "answer" fields for all instances will be similarly grouped in a parallel set of tensors, for batched computation. Additionally, for complex ``Fields``, the value of the dictionary key is not necessarily a single tensor. For example, with the ``TextField``, the output is a dictionary mapping ``TokenIndexer`` keys to tensors. The number of elements in this sub-dictionary therefore corresponds to the number of ``TokenIndexers`` used to index the ``TextField``. Each ``Field`` class is responsible for batching its own output.
[ "This", "method", "converts", "this", "Batch", "into", "a", "set", "of", "pytorch", "Tensors", "that", "can", "be", "passed", "through", "a", "model", ".", "In", "order", "for", "the", "tensors", "to", "be", "valid", "tensors", "all", "Instances", "in", "this", "batch", "need", "to", "be", "padded", "to", "the", "same", "lengths", "wherever", "padding", "is", "necessary", "so", "we", "do", "that", "first", "then", "we", "combine", "all", "of", "the", "tensors", "for", "each", "field", "in", "each", "instance", "into", "a", "set", "of", "batched", "tensors", "for", "each", "field", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/dataset.py#L71-L148
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
get_strings_from_utterance
def get_strings_from_utterance(tokenized_utterance: List[Token]) -> Dict[str, List[int]]: """ Based on the current utterance, return a dictionary where the keys are the strings in the database that map to lists of the token indices that they are linked to. """ 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
python
def get_strings_from_utterance(tokenized_utterance: List[Token]) -> Dict[str, List[int]]: """ Based on the current utterance, return a dictionary where the keys are the strings in the database that map to lists of the token indices that they are linked to. """ 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
[ "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" ]
Based on the current utterance, return a dictionary where the keys are the strings in the database that map to lists of the token indices that they are linked to.
[ "Based", "on", "the", "current", "utterance", "return", "a", "dictionary", "where", "the", "keys", "are", "the", "strings", "in", "the", "database", "that", "map", "to", "lists", "of", "the", "token", "indices", "that", "they", "are", "linked", "to", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L15-L42
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld._update_grammar
def _update_grammar(self): """ We create a new ``Grammar`` object from the one in ``AtisSqlTableContext``, that also has the new entities that are extracted from the utterance. Stitching together the expressions to form the grammar is a little tedious here, but it is worth it because we don't have to create a new grammar from scratch. Creating a new grammar is expensive because we have many production rules that have all database values in the column on the right hand side. We update the expressions bottom up, since the higher level expressions may refer to the lower level ones. For example, the ternary expression will refer to the start and end times. """ # 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
python
def _update_grammar(self): """ We create a new ``Grammar`` object from the one in ``AtisSqlTableContext``, that also has the new entities that are extracted from the utterance. Stitching together the expressions to form the grammar is a little tedious here, but it is worth it because we don't have to create a new grammar from scratch. Creating a new grammar is expensive because we have many production rules that have all database values in the column on the right hand side. We update the expressions bottom up, since the higher level expressions may refer to the lower level ones. For example, the ternary expression will refer to the start and end times. """ # 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
[ "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" ]
We create a new ``Grammar`` object from the one in ``AtisSqlTableContext``, that also has the new entities that are extracted from the utterance. Stitching together the expressions to form the grammar is a little tedious here, but it is worth it because we don't have to create a new grammar from scratch. Creating a new grammar is expensive because we have many production rules that have all database values in the column on the right hand side. We update the expressions bottom up, since the higher level expressions may refer to the lower level ones. For example, the ternary expression will refer to the start and end times.
[ "We", "create", "a", "new", "Grammar", "object", "from", "the", "one", "in", "AtisSqlTableContext", "that", "also", "has", "the", "new", "entities", "that", "are", "extracted", "from", "the", "utterance", ".", "Stitching", "together", "the", "expressions", "to", "form", "the", "grammar", "is", "a", "little", "tedious", "here", "but", "it", "is", "worth", "it", "because", "we", "don", "t", "have", "to", "create", "a", "new", "grammar", "from", "scratch", ".", "Creating", "a", "new", "grammar", "is", "expensive", "because", "we", "have", "many", "production", "rules", "that", "have", "all", "database", "values", "in", "the", "column", "on", "the", "right", "hand", "side", ".", "We", "update", "the", "expressions", "bottom", "up", "since", "the", "higher", "level", "expressions", "may", "refer", "to", "the", "lower", "level", "ones", ".", "For", "example", "the", "ternary", "expression", "will", "refer", "to", "the", "start", "and", "end", "times", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L83-L183
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld._update_expression_reference
def _update_expression_reference(self, # pylint: disable=no-self-use grammar: Grammar, parent_expression_nonterminal: str, child_expression_nonterminal: str) -> None: """ When we add a new expression, there may be other expressions that refer to it, and we need to update those to point to the new expression. """ 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]
python
def _update_expression_reference(self, # pylint: disable=no-self-use grammar: Grammar, parent_expression_nonterminal: str, child_expression_nonterminal: str) -> None: """ When we add a new expression, there may be other expressions that refer to it, and we need to update those to point to the new expression. """ 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]
[ "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", "]" ]
When we add a new expression, there may be other expressions that refer to it, and we need to update those to point to the new expression.
[ "When", "we", "add", "a", "new", "expression", "there", "may", "be", "other", "expressions", "that", "refer", "to", "it", "and", "we", "need", "to", "update", "those", "to", "point", "to", "the", "new", "expression", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L198-L209
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld._get_sequence_with_spacing
def _get_sequence_with_spacing(self, # pylint: disable=no-self-use new_grammar, expressions: List[Expression], name: str = '') -> Sequence: """ This is a helper method for generating sequences, since we often want a list of expressions with whitespaces between them. """ expressions = [subexpression for expression in expressions for subexpression in (expression, new_grammar['ws'])] return Sequence(*expressions, name=name)
python
def _get_sequence_with_spacing(self, # pylint: disable=no-self-use new_grammar, expressions: List[Expression], name: str = '') -> Sequence: """ This is a helper method for generating sequences, since we often want a list of expressions with whitespaces between them. """ expressions = [subexpression for expression in expressions for subexpression in (expression, new_grammar['ws'])] return Sequence(*expressions, name=name)
[ "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", ")" ]
This is a helper method for generating sequences, since we often want a list of expressions with whitespaces between them.
[ "This", "is", "a", "helper", "method", "for", "generating", "sequences", "since", "we", "often", "want", "a", "list", "of", "expressions", "with", "whitespaces", "between", "them", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L211-L222
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld.add_to_number_linking_scores
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: """ This is a helper method for adding different types of numbers (eg. starting time ranges) as entities. We first go through all utterances in the interaction and find the numbers of a certain type and add them to the set ``all_numbers``, which is initialized with default values. We want to add all numbers that occur in the interaction, and not just the current turn because the query could contain numbers that were triggered before the current turn. For each entity, we then check if it is triggered by tokens in the current utterance and construct the linking score. """ 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)
python
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: """ This is a helper method for adding different types of numbers (eg. starting time ranges) as entities. We first go through all utterances in the interaction and find the numbers of a certain type and add them to the set ``all_numbers``, which is initialized with default values. We want to add all numbers that occur in the interaction, and not just the current turn because the query could contain numbers that were triggered before the current turn. For each entity, we then check if it is triggered by tokens in the current utterance and construct the linking score. """ 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)
[ "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", ")" ]
This is a helper method for adding different types of numbers (eg. starting time ranges) as entities. We first go through all utterances in the interaction and find the numbers of a certain type and add them to the set ``all_numbers``, which is initialized with default values. We want to add all numbers that occur in the interaction, and not just the current turn because the query could contain numbers that were triggered before the current turn. For each entity, we then check if it is triggered by tokens in the current utterance and construct the linking score.
[ "This", "is", "a", "helper", "method", "for", "adding", "different", "types", "of", "numbers", "(", "eg", ".", "starting", "time", "ranges", ")", "as", "entities", ".", "We", "first", "go", "through", "all", "utterances", "in", "the", "interaction", "and", "find", "the", "numbers", "of", "a", "certain", "type", "and", "add", "them", "to", "the", "set", "all_numbers", "which", "is", "initialized", "with", "default", "values", ".", "We", "want", "to", "add", "all", "numbers", "that", "occur", "in", "the", "interaction", "and", "not", "just", "the", "current", "turn", "because", "the", "query", "could", "contain", "numbers", "that", "were", "triggered", "before", "the", "current", "turn", ".", "For", "each", "entity", "we", "then", "check", "if", "it", "is", "triggered", "by", "tokens", "in", "the", "current", "utterance", "and", "construct", "the", "linking", "score", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L274-L302
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld._get_linked_entities
def _get_linked_entities(self) -> Dict[str, Dict[str, Tuple[str, str, List[int]]]]: """ This method gets entities from the current utterance finds which tokens they are linked to. The entities are divided into two main groups, ``numbers`` and ``strings``. We rely on these entities later for updating the valid actions and the grammar. """ 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
python
def _get_linked_entities(self) -> Dict[str, Dict[str, Tuple[str, str, List[int]]]]: """ This method gets entities from the current utterance finds which tokens they are linked to. The entities are divided into two main groups, ``numbers`` and ``strings``. We rely on these entities later for updating the valid actions and the grammar. """ 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
[ "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" ]
This method gets entities from the current utterance finds which tokens they are linked to. The entities are divided into two main groups, ``numbers`` and ``strings``. We rely on these entities later for updating the valid actions and the grammar.
[ "This", "method", "gets", "entities", "from", "the", "current", "utterance", "finds", "which", "tokens", "they", "are", "linked", "to", ".", "The", "entities", "are", "divided", "into", "two", "main", "groups", "numbers", "and", "strings", ".", "We", "rely", "on", "these", "entities", "later", "for", "updating", "the", "valid", "actions", "and", "the", "grammar", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L305-L381
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld.all_possible_actions
def all_possible_actions(self) -> List[str]: """ Return a sorted list of strings representing all possible actions of the form: nonterminal -> [right_hand_side] """ all_actions = set() for _, action_list in self.valid_actions.items(): for action in action_list: all_actions.add(action) return sorted(all_actions)
python
def all_possible_actions(self) -> List[str]: """ Return a sorted list of strings representing all possible actions of the form: nonterminal -> [right_hand_side] """ all_actions = set() for _, action_list in self.valid_actions.items(): for action in action_list: all_actions.add(action) return sorted(all_actions)
[ "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", ")" ]
Return a sorted list of strings representing all possible actions of the form: nonterminal -> [right_hand_side]
[ "Return", "a", "sorted", "list", "of", "strings", "representing", "all", "possible", "actions", "of", "the", "form", ":", "nonterminal", "-", ">", "[", "right_hand_side", "]" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L413-L422
train
allenai/allennlp
allennlp/semparse/worlds/atis_world.py
AtisWorld._flatten_entities
def _flatten_entities(self) -> Tuple[List[str], numpy.ndarray]: """ When we first get the entities and the linking scores in ``_get_linked_entities`` we represent as dictionaries for easier updates to the grammar and valid actions. In this method, we flatten them for the model so that the entities are represented as a list, and the linking scores are a 2D numpy array of shape (num_entities, num_utterance_tokens). """ 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)
python
def _flatten_entities(self) -> Tuple[List[str], numpy.ndarray]: """ When we first get the entities and the linking scores in ``_get_linked_entities`` we represent as dictionaries for easier updates to the grammar and valid actions. In this method, we flatten them for the model so that the entities are represented as a list, and the linking scores are a 2D numpy array of shape (num_entities, num_utterance_tokens). """ 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)
[ "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", ")" ]
When we first get the entities and the linking scores in ``_get_linked_entities`` we represent as dictionaries for easier updates to the grammar and valid actions. In this method, we flatten them for the model so that the entities are represented as a list, and the linking scores are a 2D numpy array of shape (num_entities, num_utterance_tokens).
[ "When", "we", "first", "get", "the", "entities", "and", "the", "linking", "scores", "in", "_get_linked_entities", "we", "represent", "as", "dictionaries", "for", "easier", "updates", "to", "the", "grammar", "and", "valid", "actions", ".", "In", "this", "method", "we", "flatten", "them", "for", "the", "model", "so", "that", "the", "entities", "are", "represented", "as", "a", "list", "and", "the", "linking", "scores", "are", "a", "2D", "numpy", "array", "of", "shape", "(", "num_entities", "num_utterance_tokens", ")", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/atis_world.py#L424-L441
train
allenai/allennlp
allennlp/service/config_explorer.py
make_app
def make_app(include_packages: Sequence[str] = ()) -> Flask: """ Creates a Flask app that serves up a simple configuration wizard. """ # 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 """ There are basically two things that can happen here. If this method is called with a ``Registrable`` class (e.g. ``Model``), it should return the list of possible ``Model`` subclasses. If it is called with an instantiable subclass (e.g. ``CrfTagger``), is should return the config for that subclass. This is complicated by the fact that some Registrable base classes (e.g. Vocabulary, Trainer) are _themselves_ instantiable. We handle this in two ways: first, we insist that the first case include an extra ``get_choices`` parameter. That is, if you call this method for ``Trainer`` with get_choices=true, you get the list of Trainer subclasses. If you call it without that extra flag, you get the config for the class itself. There are basically two UX situations in which this API is called. The first is when you have a dropdown list of choices (e.g. Model types) and you select one. Such an API request is made *without* the get_choices flag, which means that the config is returned *even if the class in question is a Registrable class that has subclass choices*. The second is when you click a "Configure" button, which configures a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``) have registrable subclasses. In this case the API request is made with the "get_choices" flag, but will return the corresponding config object if no choices are available (e.g. in the ``FeedForward``) case. This is not elegant, but it works. """ 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
python
def make_app(include_packages: Sequence[str] = ()) -> Flask: """ Creates a Flask app that serves up a simple configuration wizard. """ # 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 """ There are basically two things that can happen here. If this method is called with a ``Registrable`` class (e.g. ``Model``), it should return the list of possible ``Model`` subclasses. If it is called with an instantiable subclass (e.g. ``CrfTagger``), is should return the config for that subclass. This is complicated by the fact that some Registrable base classes (e.g. Vocabulary, Trainer) are _themselves_ instantiable. We handle this in two ways: first, we insist that the first case include an extra ``get_choices`` parameter. That is, if you call this method for ``Trainer`` with get_choices=true, you get the list of Trainer subclasses. If you call it without that extra flag, you get the config for the class itself. There are basically two UX situations in which this API is called. The first is when you have a dropdown list of choices (e.g. Model types) and you select one. Such an API request is made *without* the get_choices flag, which means that the config is returned *even if the class in question is a Registrable class that has subclass choices*. The second is when you click a "Configure" button, which configures a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``) have registrable subclasses. In this case the API request is made with the "get_choices" flag, but will return the corresponding config object if no choices are available (e.g. in the ``FeedForward``) case. This is not elegant, but it works. """ 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
[ "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", "\"\"\"\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 \"\"\"", "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" ]
Creates a Flask app that serves up a simple configuration wizard.
[ "Creates", "a", "Flask", "app", "that", "serves", "up", "a", "simple", "configuration", "wizard", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/service/config_explorer.py#L31-L105
train
allenai/allennlp
allennlp/commands/train.py
train_model_from_args
def train_model_from_args(args: argparse.Namespace): """ Just converts from an ``argparse.Namespace`` object to string paths. """ 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)
python
def train_model_from_args(args: argparse.Namespace): """ Just converts from an ``argparse.Namespace`` object to string paths. """ 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)
[ "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", ")" ]
Just converts from an ``argparse.Namespace`` object to string paths.
[ "Just", "converts", "from", "an", "argparse", ".", "Namespace", "object", "to", "string", "paths", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/commands/train.py#L105-L116
train
allenai/allennlp
allennlp/commands/train.py
train_model_from_file
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: """ A wrapper around :func:`train_model` which loads the params from a file. Parameters ---------- parameter_filename : ``str`` A json parameter file specifying an AllenNLP experiment. serialization_dir : ``str`` The directory in which to save results and logs. We just pass this along to :func:`train_model`. overrides : ``str`` A JSON string that we will use to override values in the input parameter file. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we make our output more friendly to saved model files. We just pass this along to :func:`train_model`. recover : ``bool`, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. """ # 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)
python
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: """ A wrapper around :func:`train_model` which loads the params from a file. Parameters ---------- parameter_filename : ``str`` A json parameter file specifying an AllenNLP experiment. serialization_dir : ``str`` The directory in which to save results and logs. We just pass this along to :func:`train_model`. overrides : ``str`` A JSON string that we will use to override values in the input parameter file. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we make our output more friendly to saved model files. We just pass this along to :func:`train_model`. recover : ``bool`, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. """ # 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)
[ "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", ")" ]
A wrapper around :func:`train_model` which loads the params from a file. Parameters ---------- parameter_filename : ``str`` A json parameter file specifying an AllenNLP experiment. serialization_dir : ``str`` The directory in which to save results and logs. We just pass this along to :func:`train_model`. overrides : ``str`` A JSON string that we will use to override values in the input parameter file. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we make our output more friendly to saved model files. We just pass this along to :func:`train_model`. recover : ``bool`, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`.
[ "A", "wrapper", "around", ":", "func", ":", "train_model", "which", "loads", "the", "params", "from", "a", "file", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/commands/train.py#L119-L160
train
allenai/allennlp
allennlp/commands/train.py
train_model
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: """ Trains the model specified in the given :class:`Params` object, using the data and training parameters also specified in that object, and saves the results in ``serialization_dir``. Parameters ---------- params : ``Params`` A parameter object specifying an AllenNLP Experiment. serialization_dir : ``str`` The directory in which to save results and logs. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we add newlines to tqdm output, even on an interactive terminal, and we slow down tqdm's output to only once every 10 seconds. recover : ``bool``, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. Returns ------- best_model: ``Model`` The model with the best epoch weights. """ 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
python
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: """ Trains the model specified in the given :class:`Params` object, using the data and training parameters also specified in that object, and saves the results in ``serialization_dir``. Parameters ---------- params : ``Params`` A parameter object specifying an AllenNLP Experiment. serialization_dir : ``str`` The directory in which to save results and logs. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we add newlines to tqdm output, even on an interactive terminal, and we slow down tqdm's output to only once every 10 seconds. recover : ``bool``, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. Returns ------- best_model: ``Model`` The model with the best epoch weights. """ 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
[ "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" ]
Trains the model specified in the given :class:`Params` object, using the data and training parameters also specified in that object, and saves the results in ``serialization_dir``. Parameters ---------- params : ``Params`` A parameter object specifying an AllenNLP Experiment. serialization_dir : ``str`` The directory in which to save results and logs. file_friendly_logging : ``bool``, optional (default=False) If ``True``, we add newlines to tqdm output, even on an interactive terminal, and we slow down tqdm's output to only once every 10 seconds. recover : ``bool``, optional (default=False) If ``True``, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see the ``fine-tune`` command. force : ``bool``, optional (default=False) If ``True``, we will overwrite the serialization directory if it already exists. cache_directory : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. cache_prefix : ``str``, optional For caching data pre-processing. See :func:`allennlp.training.util.datasets_from_params`. Returns ------- best_model: ``Model`` The model with the best epoch weights.
[ "Trains", "the", "model", "specified", "in", "the", "given", ":", "class", ":", "Params", "object", "using", "the", "data", "and", "training", "parameters", "also", "specified", "in", "that", "object", "and", "saves", "the", "results", "in", "serialization_dir", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/commands/train.py#L163-L269
train
allenai/allennlp
allennlp/training/metrics/fbeta_measure.py
_prf_divide
def _prf_divide(numerator, denominator): """Performs division and handles divide-by-zero. On zero-division, sets the corresponding result elements to zero. """ result = numerator / denominator mask = denominator == 0.0 if not mask.any(): return result # remove nan result[mask] = 0.0 return result
python
def _prf_divide(numerator, denominator): """Performs division and handles divide-by-zero. On zero-division, sets the corresponding result elements to zero. """ result = numerator / denominator mask = denominator == 0.0 if not mask.any(): return result # remove nan result[mask] = 0.0 return result
[ "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. On zero-division, sets the corresponding result elements to zero.
[ "Performs", "division", "and", "handles", "divide", "-", "by", "-", "zero", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/training/metrics/fbeta_measure.py#L231-L243
train
allenai/allennlp
tutorials/tagger/basic_pytorch.py
load_data
def load_data(file_path: str) -> Tuple[List[str], List[str]]: """ One sentence per line, formatted like The###DET dog###NN ate###V the###DET apple###NN Returns a list of pairs (tokenized_sentence, tags) """ 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
python
def load_data(file_path: str) -> Tuple[List[str], List[str]]: """ One sentence per line, formatted like The###DET dog###NN ate###V the###DET apple###NN Returns a list of pairs (tokenized_sentence, tags) """ 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
[ "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" ]
One sentence per line, formatted like The###DET dog###NN ate###V the###DET apple###NN Returns a list of pairs (tokenized_sentence, tags)
[ "One", "sentence", "per", "line", "formatted", "like" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/tutorials/tagger/basic_pytorch.py#L23-L39
train
allenai/allennlp
allennlp/data/vocabulary.py
pop_max_vocab_size
def pop_max_vocab_size(params: Params) -> Union[int, Dict[str, int]]: """ max_vocab_size limits the size of the vocabulary, not including the @@UNKNOWN@@ token. max_vocab_size is allowed to be either an int or a Dict[str, int] (or nothing). But it could also be a string representing an int (in the case of environment variable substitution). So we need some complex logic to handle it. """ 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
python
def pop_max_vocab_size(params: Params) -> Union[int, Dict[str, int]]: """ max_vocab_size limits the size of the vocabulary, not including the @@UNKNOWN@@ token. max_vocab_size is allowed to be either an int or a Dict[str, int] (or nothing). But it could also be a string representing an int (in the case of environment variable substitution). So we need some complex logic to handle it. """ 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
[ "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" ]
max_vocab_size limits the size of the vocabulary, not including the @@UNKNOWN@@ token. max_vocab_size is allowed to be either an int or a Dict[str, int] (or nothing). But it could also be a string representing an int (in the case of environment variable substitution). So we need some complex logic to handle it.
[ "max_vocab_size", "limits", "the", "size", "of", "the", "vocabulary", "not", "including", "the", "@@UNKNOWN@@", "token", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L117-L134
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.save_to_files
def save_to_files(self, directory: str) -> None: """ Persist this Vocabulary to files so it can be reloaded later. Each namespace corresponds to one file. Parameters ---------- directory : ``str`` The directory where we save the serialized vocabulary. """ 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)
python
def save_to_files(self, directory: str) -> None: """ Persist this Vocabulary to files so it can be reloaded later. Each namespace corresponds to one file. Parameters ---------- directory : ``str`` The directory where we save the serialized vocabulary. """ 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)
[ "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", ")" ]
Persist this Vocabulary to files so it can be reloaded later. Each namespace corresponds to one file. Parameters ---------- directory : ``str`` The directory where we save the serialized vocabulary.
[ "Persist", "this", "Vocabulary", "to", "files", "so", "it", "can", "be", "reloaded", "later", ".", "Each", "namespace", "corresponds", "to", "one", "file", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L270-L294
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.from_files
def from_files(cls, directory: str) -> 'Vocabulary': """ Loads a ``Vocabulary`` that was serialized using ``save_to_files``. Parameters ---------- directory : ``str`` The directory containing the serialized 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
python
def from_files(cls, directory: str) -> 'Vocabulary': """ Loads a ``Vocabulary`` that was serialized using ``save_to_files``. Parameters ---------- directory : ``str`` The directory containing the serialized 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
[ "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" ]
Loads a ``Vocabulary`` that was serialized using ``save_to_files``. Parameters ---------- directory : ``str`` The directory containing the serialized vocabulary.
[ "Loads", "a", "Vocabulary", "that", "was", "serialized", "using", "save_to_files", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L297-L326
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.set_from_file
def set_from_file(self, filename: str, is_padded: bool = True, oov_token: str = DEFAULT_OOV_TOKEN, namespace: str = "tokens"): """ If you already have a vocabulary file for a trained model somewhere, and you really want to use that vocabulary file instead of just setting the vocabulary from a dataset, for whatever reason, you can do that with this method. You must specify the namespace to use, and we assume that you want to use padding and OOV tokens for this. Parameters ---------- filename : ``str`` The file containing the vocabulary to load. It should be formatted as one token per line, with nothing else in the line. The index we assign to the token is the line number in the file (1-indexed if ``is_padded``, 0-indexed otherwise). Note that this file should contain the OOV token string! is_padded : ``bool``, optional (default=True) Is this vocabulary padded? For token / word / character vocabularies, this should be ``True``; while for tag or label vocabularies, this should typically be ``False``. If ``True``, we add a padding token with index 0, and we enforce that the ``oov_token`` is present in the file. oov_token : ``str``, optional (default=DEFAULT_OOV_TOKEN) What token does this vocabulary use to represent out-of-vocabulary characters? This must show up as a line in the vocabulary file. When we find it, we replace ``oov_token`` with ``self._oov_token``, because we only use one OOV token across namespaces. namespace : ``str``, optional (default="tokens") What namespace should we overwrite with this vocab file? """ 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!"
python
def set_from_file(self, filename: str, is_padded: bool = True, oov_token: str = DEFAULT_OOV_TOKEN, namespace: str = "tokens"): """ If you already have a vocabulary file for a trained model somewhere, and you really want to use that vocabulary file instead of just setting the vocabulary from a dataset, for whatever reason, you can do that with this method. You must specify the namespace to use, and we assume that you want to use padding and OOV tokens for this. Parameters ---------- filename : ``str`` The file containing the vocabulary to load. It should be formatted as one token per line, with nothing else in the line. The index we assign to the token is the line number in the file (1-indexed if ``is_padded``, 0-indexed otherwise). Note that this file should contain the OOV token string! is_padded : ``bool``, optional (default=True) Is this vocabulary padded? For token / word / character vocabularies, this should be ``True``; while for tag or label vocabularies, this should typically be ``False``. If ``True``, we add a padding token with index 0, and we enforce that the ``oov_token`` is present in the file. oov_token : ``str``, optional (default=DEFAULT_OOV_TOKEN) What token does this vocabulary use to represent out-of-vocabulary characters? This must show up as a line in the vocabulary file. When we find it, we replace ``oov_token`` with ``self._oov_token``, because we only use one OOV token across namespaces. namespace : ``str``, optional (default="tokens") What namespace should we overwrite with this vocab file? """ 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!"
[ "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!\"" ]
If you already have a vocabulary file for a trained model somewhere, and you really want to use that vocabulary file instead of just setting the vocabulary from a dataset, for whatever reason, you can do that with this method. You must specify the namespace to use, and we assume that you want to use padding and OOV tokens for this. Parameters ---------- filename : ``str`` The file containing the vocabulary to load. It should be formatted as one token per line, with nothing else in the line. The index we assign to the token is the line number in the file (1-indexed if ``is_padded``, 0-indexed otherwise). Note that this file should contain the OOV token string! is_padded : ``bool``, optional (default=True) Is this vocabulary padded? For token / word / character vocabularies, this should be ``True``; while for tag or label vocabularies, this should typically be ``False``. If ``True``, we add a padding token with index 0, and we enforce that the ``oov_token`` is present in the file. oov_token : ``str``, optional (default=DEFAULT_OOV_TOKEN) What token does this vocabulary use to represent out-of-vocabulary characters? This must show up as a line in the vocabulary file. When we find it, we replace ``oov_token`` with ``self._oov_token``, because we only use one OOV token across namespaces. namespace : ``str``, optional (default="tokens") What namespace should we overwrite with this vocab file?
[ "If", "you", "already", "have", "a", "vocabulary", "file", "for", "a", "trained", "model", "somewhere", "and", "you", "really", "want", "to", "use", "that", "vocabulary", "file", "instead", "of", "just", "setting", "the", "vocabulary", "from", "a", "dataset", "for", "whatever", "reason", "you", "can", "do", "that", "with", "this", "method", ".", "You", "must", "specify", "the", "namespace", "to", "use", "and", "we", "assume", "that", "you", "want", "to", "use", "padding", "and", "OOV", "tokens", "for", "this", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L328-L378
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.from_instances
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': """ Constructs a vocabulary given a collection of `Instances` and some parameters. We count all of the vocabulary items in the instances, then pass those counts and the other parameters, to :func:`__init__`. See that method for a description of what the other parameters do. """ 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)
python
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': """ Constructs a vocabulary given a collection of `Instances` and some parameters. We count all of the vocabulary items in the instances, then pass those counts and the other parameters, to :func:`__init__`. See that method for a description of what the other parameters do. """ 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)
[ "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", ")" ]
Constructs a vocabulary given a collection of `Instances` and some parameters. We count all of the vocabulary items in the instances, then pass those counts and the other parameters, to :func:`__init__`. See that method for a description of what the other parameters do.
[ "Constructs", "a", "vocabulary", "given", "a", "collection", "of", "Instances", "and", "some", "parameters", ".", "We", "count", "all", "of", "the", "vocabulary", "items", "in", "the", "instances", "then", "pass", "those", "counts", "and", "the", "other", "parameters", "to", ":", "func", ":", "__init__", ".", "See", "that", "method", "for", "a", "description", "of", "what", "the", "other", "parameters", "do", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L381-L408
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.from_params
def from_params(cls, params: Params, instances: Iterable['adi.Instance'] = None): # type: ignore """ There are two possible ways to build a vocabulary; from a collection of instances, using :func:`Vocabulary.from_instances`, or from a pre-saved vocabulary, using :func:`Vocabulary.from_files`. You can also extend pre-saved vocabulary with collection of instances using this method. This method wraps these options, allowing their specification from a ``Params`` object, generated from a JSON configuration file. Parameters ---------- params: Params, required. instances: Iterable['adi.Instance'], optional If ``params`` doesn't contain a ``directory_path`` key, the ``Vocabulary`` can be built directly from a collection of instances (i.e. a dataset). If ``extend`` key is set False, dataset instances will be ignored and final vocabulary will be one loaded from ``directory_path``. If ``extend`` key is set True, dataset instances will be used to extend the vocabulary loaded from ``directory_path`` and that will be final vocabulary used. Returns ------- A ``Vocabulary``. """ # 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)
python
def from_params(cls, params: Params, instances: Iterable['adi.Instance'] = None): # type: ignore """ There are two possible ways to build a vocabulary; from a collection of instances, using :func:`Vocabulary.from_instances`, or from a pre-saved vocabulary, using :func:`Vocabulary.from_files`. You can also extend pre-saved vocabulary with collection of instances using this method. This method wraps these options, allowing their specification from a ``Params`` object, generated from a JSON configuration file. Parameters ---------- params: Params, required. instances: Iterable['adi.Instance'], optional If ``params`` doesn't contain a ``directory_path`` key, the ``Vocabulary`` can be built directly from a collection of instances (i.e. a dataset). If ``extend`` key is set False, dataset instances will be ignored and final vocabulary will be one loaded from ``directory_path``. If ``extend`` key is set True, dataset instances will be used to extend the vocabulary loaded from ``directory_path`` and that will be final vocabulary used. Returns ------- A ``Vocabulary``. """ # 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)
[ "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", ")" ]
There are two possible ways to build a vocabulary; from a collection of instances, using :func:`Vocabulary.from_instances`, or from a pre-saved vocabulary, using :func:`Vocabulary.from_files`. You can also extend pre-saved vocabulary with collection of instances using this method. This method wraps these options, allowing their specification from a ``Params`` object, generated from a JSON configuration file. Parameters ---------- params: Params, required. instances: Iterable['adi.Instance'], optional If ``params`` doesn't contain a ``directory_path`` key, the ``Vocabulary`` can be built directly from a collection of instances (i.e. a dataset). If ``extend`` key is set False, dataset instances will be ignored and final vocabulary will be one loaded from ``directory_path``. If ``extend`` key is set True, dataset instances will be used to extend the vocabulary loaded from ``directory_path`` and that will be final vocabulary used. Returns ------- A ``Vocabulary``.
[ "There", "are", "two", "possible", "ways", "to", "build", "a", "vocabulary", ";", "from", "a", "collection", "of", "instances", "using", ":", "func", ":", "Vocabulary", ".", "from_instances", "or", "from", "a", "pre", "-", "saved", "vocabulary", "using", ":", "func", ":", "Vocabulary", ".", "from_files", ".", "You", "can", "also", "extend", "pre", "-", "saved", "vocabulary", "with", "collection", "of", "instances", "using", "this", "method", ".", "This", "method", "wraps", "these", "options", "allowing", "their", "specification", "from", "a", "Params", "object", "generated", "from", "a", "JSON", "configuration", "file", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L412-L487
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary._extend
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: """ This method can be used for extending already generated vocabulary. It takes same parameters as Vocabulary initializer. The token2index and indextotoken mappings of calling vocabulary will be retained. It is an inplace operation so None will be returned. """ 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)
python
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: """ This method can be used for extending already generated vocabulary. It takes same parameters as Vocabulary initializer. The token2index and indextotoken mappings of calling vocabulary will be retained. It is an inplace operation so None will be returned. """ 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)
[ "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", ")" ]
This method can be used for extending already generated vocabulary. It takes same parameters as Vocabulary initializer. The token2index and indextotoken mappings of calling vocabulary will be retained. It is an inplace operation so None will be returned.
[ "This", "method", "can", "be", "used", "for", "extending", "already", "generated", "vocabulary", ".", "It", "takes", "same", "parameters", "as", "Vocabulary", "initializer", ".", "The", "token2index", "and", "indextotoken", "mappings", "of", "calling", "vocabulary", "will", "be", "retained", ".", "It", "is", "an", "inplace", "operation", "so", "None", "will", "be", "returned", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L489-L567
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.extend_from_instances
def extend_from_instances(self, params: Params, instances: Iterable['adi.Instance'] = ()) -> None: """ Extends an already generated vocabulary using a collection of instances. """ 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)
python
def extend_from_instances(self, params: Params, instances: Iterable['adi.Instance'] = ()) -> None: """ Extends an already generated vocabulary using a collection of instances. """ 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)
[ "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", ")" ]
Extends an already generated vocabulary using a collection of instances.
[ "Extends", "an", "already", "generated", "vocabulary", "using", "a", "collection", "of", "instances", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L569-L595
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.is_padded
def is_padded(self, namespace: str) -> bool: """ Returns whether or not there are padding and OOV tokens added to the given namespace. """ return self._index_to_token[namespace][0] == self._padding_token
python
def is_padded(self, namespace: str) -> bool: """ Returns whether or not there are padding and OOV tokens added to the given namespace. """ return self._index_to_token[namespace][0] == self._padding_token
[ "def", "is_padded", "(", "self", ",", "namespace", ":", "str", ")", "->", "bool", ":", "return", "self", ".", "_index_to_token", "[", "namespace", "]", "[", "0", "]", "==", "self", ".", "_padding_token" ]
Returns whether or not there are padding and OOV tokens added to the given namespace.
[ "Returns", "whether", "or", "not", "there", "are", "padding", "and", "OOV", "tokens", "added", "to", "the", "given", "namespace", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L597-L601
train
allenai/allennlp
allennlp/data/vocabulary.py
Vocabulary.add_token_to_namespace
def add_token_to_namespace(self, token: str, namespace: str = 'tokens') -> int: """ Adds ``token`` to the index, if it is not already present. Either way, we return the index of the token. """ 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]
python
def add_token_to_namespace(self, token: str, namespace: str = 'tokens') -> int: """ Adds ``token`` to the index, if it is not already present. Either way, we return the index of the token. """ 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]
[ "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", "]" ]
Adds ``token`` to the index, if it is not already present. Either way, we return the index of the token.
[ "Adds", "token", "to", "the", "index", "if", "it", "is", "not", "already", "present", ".", "Either", "way", "we", "return", "the", "index", "of", "the", "token", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/data/vocabulary.py#L603-L617
train
allenai/allennlp
allennlp/models/model.py
Model.get_regularization_penalty
def get_regularization_penalty(self) -> Union[float, torch.Tensor]: """ Computes the regularization penalty for the model. Returns 0 if the model was not configured to use regularization. """ if self._regularizer is None: return 0.0 else: return self._regularizer(self)
python
def get_regularization_penalty(self) -> Union[float, torch.Tensor]: """ Computes the regularization penalty for the model. Returns 0 if the model was not configured to use regularization. """ if self._regularizer is None: return 0.0 else: return self._regularizer(self)
[ "def", "get_regularization_penalty", "(", "self", ")", "->", "Union", "[", "float", ",", "torch", ".", "Tensor", "]", ":", "if", "self", ".", "_regularizer", "is", "None", ":", "return", "0.0", "else", ":", "return", "self", ".", "_regularizer", "(", "self", ")" ]
Computes the regularization penalty for the model. Returns 0 if the model was not configured to use regularization.
[ "Computes", "the", "regularization", "penalty", "for", "the", "model", ".", "Returns", "0", "if", "the", "model", "was", "not", "configured", "to", "use", "regularization", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L58-L66
train
allenai/allennlp
allennlp/models/model.py
Model.forward_on_instance
def forward_on_instance(self, instance: Instance) -> Dict[str, numpy.ndarray]: """ Takes an :class:`~allennlp.data.instance.Instance`, which typically has raw text in it, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and remove the batch dimension. """ return self.forward_on_instances([instance])[0]
python
def forward_on_instance(self, instance: Instance) -> Dict[str, numpy.ndarray]: """ Takes an :class:`~allennlp.data.instance.Instance`, which typically has raw text in it, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and remove the batch dimension. """ return self.forward_on_instances([instance])[0]
[ "def", "forward_on_instance", "(", "self", ",", "instance", ":", "Instance", ")", "->", "Dict", "[", "str", ",", "numpy", ".", "ndarray", "]", ":", "return", "self", ".", "forward_on_instances", "(", "[", "instance", "]", ")", "[", "0", "]" ]
Takes an :class:`~allennlp.data.instance.Instance`, which typically has raw text in it, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and remove the batch dimension.
[ "Takes", "an", ":", "class", ":", "~allennlp", ".", "data", ".", "instance", ".", "Instance", "which", "typically", "has", "raw", "text", "in", "it", "converts", "that", "text", "into", "arrays", "using", "this", "model", "s", ":", "class", ":", "Vocabulary", "passes", "those", "arrays", "through", ":", "func", ":", "self", ".", "forward", "()", "and", ":", "func", ":", "self", ".", "decode", "()", "(", "which", "by", "default", "does", "nothing", ")", "and", "returns", "the", "result", ".", "Before", "returning", "the", "result", "we", "convert", "any", "torch", ".", "Tensors", "into", "numpy", "arrays", "and", "remove", "the", "batch", "dimension", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L116-L124
train
allenai/allennlp
allennlp/models/model.py
Model.forward_on_instances
def forward_on_instances(self, instances: List[Instance]) -> List[Dict[str, numpy.ndarray]]: """ Takes a list of :class:`~allennlp.data.instance.Instance`s, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and separate the batched output into a list of individual dicts per instance. Note that typically this will be faster on a GPU (and conditionally, on a CPU) than repeated calls to :func:`forward_on_instance`. Parameters ---------- instances : List[Instance], required The instances to run the model on. Returns ------- A list of the models output for each instance. """ 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
python
def forward_on_instances(self, instances: List[Instance]) -> List[Dict[str, numpy.ndarray]]: """ Takes a list of :class:`~allennlp.data.instance.Instance`s, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and separate the batched output into a list of individual dicts per instance. Note that typically this will be faster on a GPU (and conditionally, on a CPU) than repeated calls to :func:`forward_on_instance`. Parameters ---------- instances : List[Instance], required The instances to run the model on. Returns ------- A list of the models output for each instance. """ 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
[ "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" ]
Takes a list of :class:`~allennlp.data.instance.Instance`s, converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing) and returns the result. Before returning the result, we convert any ``torch.Tensors`` into numpy arrays and separate the batched output into a list of individual dicts per instance. Note that typically this will be faster on a GPU (and conditionally, on a CPU) than repeated calls to :func:`forward_on_instance`. Parameters ---------- instances : List[Instance], required The instances to run the model on. Returns ------- A list of the models output for each instance.
[ "Takes", "a", "list", "of", ":", "class", ":", "~allennlp", ".", "data", ".", "instance", ".", "Instance", "s", "converts", "that", "text", "into", "arrays", "using", "this", "model", "s", ":", "class", ":", "Vocabulary", "passes", "those", "arrays", "through", ":", "func", ":", "self", ".", "forward", "()", "and", ":", "func", ":", "self", ".", "decode", "()", "(", "which", "by", "default", "does", "nothing", ")", "and", "returns", "the", "result", ".", "Before", "returning", "the", "result", "we", "convert", "any", "torch", ".", "Tensors", "into", "numpy", "arrays", "and", "separate", "the", "batched", "output", "into", "a", "list", "of", "individual", "dicts", "per", "instance", ".", "Note", "that", "typically", "this", "will", "be", "faster", "on", "a", "GPU", "(", "and", "conditionally", "on", "a", "CPU", ")", "than", "repeated", "calls", "to", ":", "func", ":", "forward_on_instance", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L126-L172
train
allenai/allennlp
allennlp/models/model.py
Model.decode
def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: """ Takes the result of :func:`forward` and runs inference / decoding / whatever post-processing you need to do your model. The intent is that ``model.forward()`` should produce potentials or probabilities, and then ``model.decode()`` can take those results and run some kind of beam search or constrained inference or whatever is necessary. This does not handle all possible decoding use cases, but it at least handles simple kinds of decoding. This method `modifies` the input dictionary, and also `returns` the same dictionary. By default in the base class we do nothing. If your model has some special decoding step, override this method. """ # pylint: disable=no-self-use return output_dict
python
def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: """ Takes the result of :func:`forward` and runs inference / decoding / whatever post-processing you need to do your model. The intent is that ``model.forward()`` should produce potentials or probabilities, and then ``model.decode()`` can take those results and run some kind of beam search or constrained inference or whatever is necessary. This does not handle all possible decoding use cases, but it at least handles simple kinds of decoding. This method `modifies` the input dictionary, and also `returns` the same dictionary. By default in the base class we do nothing. If your model has some special decoding step, override this method. """ # pylint: disable=no-self-use return output_dict
[ "def", "decode", "(", "self", ",", "output_dict", ":", "Dict", "[", "str", ",", "torch", ".", "Tensor", "]", ")", "->", "Dict", "[", "str", ",", "torch", ".", "Tensor", "]", ":", "# pylint: disable=no-self-use", "return", "output_dict" ]
Takes the result of :func:`forward` and runs inference / decoding / whatever post-processing you need to do your model. The intent is that ``model.forward()`` should produce potentials or probabilities, and then ``model.decode()`` can take those results and run some kind of beam search or constrained inference or whatever is necessary. This does not handle all possible decoding use cases, but it at least handles simple kinds of decoding. This method `modifies` the input dictionary, and also `returns` the same dictionary. By default in the base class we do nothing. If your model has some special decoding step, override this method.
[ "Takes", "the", "result", "of", ":", "func", ":", "forward", "and", "runs", "inference", "/", "decoding", "/", "whatever", "post", "-", "processing", "you", "need", "to", "do", "your", "model", ".", "The", "intent", "is", "that", "model", ".", "forward", "()", "should", "produce", "potentials", "or", "probabilities", "and", "then", "model", ".", "decode", "()", "can", "take", "those", "results", "and", "run", "some", "kind", "of", "beam", "search", "or", "constrained", "inference", "or", "whatever", "is", "necessary", ".", "This", "does", "not", "handle", "all", "possible", "decoding", "use", "cases", "but", "it", "at", "least", "handles", "simple", "kinds", "of", "decoding", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L174-L189
train
allenai/allennlp
allennlp/models/model.py
Model._get_prediction_device
def _get_prediction_device(self) -> int: """ This method checks the device of the model parameters to determine the cuda_device this model should be run on for predictions. If there are no parameters, it returns -1. Returns ------- The cuda device this model should run on for predictions. """ devices = {util.get_device_of(param) for param in self.parameters()} if len(devices) > 1: devices_string = ", ".join(str(x) for x in devices) raise ConfigurationError(f"Parameters have mismatching cuda_devices: {devices_string}") elif len(devices) == 1: return devices.pop() else: return -1
python
def _get_prediction_device(self) -> int: """ This method checks the device of the model parameters to determine the cuda_device this model should be run on for predictions. If there are no parameters, it returns -1. Returns ------- The cuda device this model should run on for predictions. """ devices = {util.get_device_of(param) for param in self.parameters()} if len(devices) > 1: devices_string = ", ".join(str(x) for x in devices) raise ConfigurationError(f"Parameters have mismatching cuda_devices: {devices_string}") elif len(devices) == 1: return devices.pop() else: return -1
[ "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" ]
This method checks the device of the model parameters to determine the cuda_device this model should be run on for predictions. If there are no parameters, it returns -1. Returns ------- The cuda device this model should run on for predictions.
[ "This", "method", "checks", "the", "device", "of", "the", "model", "parameters", "to", "determine", "the", "cuda_device", "this", "model", "should", "be", "run", "on", "for", "predictions", ".", "If", "there", "are", "no", "parameters", "it", "returns", "-", "1", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L206-L223
train
allenai/allennlp
allennlp/models/model.py
Model._maybe_warn_for_unseparable_batches
def _maybe_warn_for_unseparable_batches(self, output_key: str): """ This method warns once if a user implements a model which returns a dictionary with values which we are unable to split back up into elements of the batch. This is controlled by a class attribute ``_warn_for_unseperable_batches`` because it would be extremely verbose otherwise. """ 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)
python
def _maybe_warn_for_unseparable_batches(self, output_key: str): """ This method warns once if a user implements a model which returns a dictionary with values which we are unable to split back up into elements of the batch. This is controlled by a class attribute ``_warn_for_unseperable_batches`` because it would be extremely verbose otherwise. """ 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)
[ "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", ")" ]
This method warns once if a user implements a model which returns a dictionary with values which we are unable to split back up into elements of the batch. This is controlled by a class attribute ``_warn_for_unseperable_batches`` because it would be extremely verbose otherwise.
[ "This", "method", "warns", "once", "if", "a", "user", "implements", "a", "model", "which", "returns", "a", "dictionary", "with", "values", "which", "we", "are", "unable", "to", "split", "back", "up", "into", "elements", "of", "the", "batch", ".", "This", "is", "controlled", "by", "a", "class", "attribute", "_warn_for_unseperable_batches", "because", "it", "would", "be", "extremely", "verbose", "otherwise", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L225-L237
train
allenai/allennlp
allennlp/models/model.py
Model._load
def _load(cls, config: Params, serialization_dir: str, weights_file: str = None, cuda_device: int = -1) -> 'Model': """ Instantiates an already-trained model, based on the experiment configuration and some optional overrides. """ 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
python
def _load(cls, config: Params, serialization_dir: str, weights_file: str = None, cuda_device: int = -1) -> 'Model': """ Instantiates an already-trained model, based on the experiment configuration and some optional overrides. """ 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
[ "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" ]
Instantiates an already-trained model, based on the experiment configuration and some optional overrides.
[ "Instantiates", "an", "already", "-", "trained", "model", "based", "on", "the", "experiment", "configuration", "and", "some", "optional", "overrides", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L240-L285
train
allenai/allennlp
allennlp/models/model.py
Model.load
def load(cls, config: Params, serialization_dir: str, weights_file: str = None, cuda_device: int = -1) -> 'Model': """ Instantiates an already-trained model, based on the experiment configuration and some optional overrides. Parameters ---------- config: Params The configuration that was used to train the model. It should definitely have a `model` section, and should probably have a `trainer` section as well. serialization_dir: str = None The directory containing the serialized weights, parameters, and vocabulary of the model. weights_file: str = None By default we load the weights from `best.th` in the serialization directory, but you can override that value here. cuda_device: int = -1 By default we load the model on the CPU, but if you want to load it for GPU usage you can specify the id of your GPU here Returns ------- model: Model The model specified in the configuration, loaded with the serialized vocabulary and the trained weights. """ # 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)
python
def load(cls, config: Params, serialization_dir: str, weights_file: str = None, cuda_device: int = -1) -> 'Model': """ Instantiates an already-trained model, based on the experiment configuration and some optional overrides. Parameters ---------- config: Params The configuration that was used to train the model. It should definitely have a `model` section, and should probably have a `trainer` section as well. serialization_dir: str = None The directory containing the serialized weights, parameters, and vocabulary of the model. weights_file: str = None By default we load the weights from `best.th` in the serialization directory, but you can override that value here. cuda_device: int = -1 By default we load the model on the CPU, but if you want to load it for GPU usage you can specify the id of your GPU here Returns ------- model: Model The model specified in the configuration, loaded with the serialized vocabulary and the trained weights. """ # 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)
[ "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", ")" ]
Instantiates an already-trained model, based on the experiment configuration and some optional overrides. Parameters ---------- config: Params The configuration that was used to train the model. It should definitely have a `model` section, and should probably have a `trainer` section as well. serialization_dir: str = None The directory containing the serialized weights, parameters, and vocabulary of the model. weights_file: str = None By default we load the weights from `best.th` in the serialization directory, but you can override that value here. cuda_device: int = -1 By default we load the model on the CPU, but if you want to load it for GPU usage you can specify the id of your GPU here Returns ------- model: Model The model specified in the configuration, loaded with the serialized vocabulary and the trained weights.
[ "Instantiates", "an", "already", "-", "trained", "model", "based", "on", "the", "experiment", "configuration", "and", "some", "optional", "overrides", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L288-L327
train
allenai/allennlp
allennlp/models/model.py
Model.extend_embedder_vocab
def extend_embedder_vocab(self, embedding_sources_mapping: Dict[str, str] = None) -> None: """ Iterates through all embedding modules in the model and assures it can embed with the extended vocab. This is required in fine-tuning or transfer learning scenarios where model was trained with original vocabulary but during fine-tuning/tranfer-learning, it will have it work with extended vocabulary (original + new-data vocabulary). Parameters ---------- embedding_sources_mapping : Dict[str, str], (optional, default=None) Mapping from model_path to pretrained-file path of the embedding modules. If pretrained-file used at time of embedding initialization isn't available now, user should pass this mapping. Model path is path traversing the model attributes upto this embedding module. Eg. "_text_field_embedder.token_embedder_tokens". """ # 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)
python
def extend_embedder_vocab(self, embedding_sources_mapping: Dict[str, str] = None) -> None: """ Iterates through all embedding modules in the model and assures it can embed with the extended vocab. This is required in fine-tuning or transfer learning scenarios where model was trained with original vocabulary but during fine-tuning/tranfer-learning, it will have it work with extended vocabulary (original + new-data vocabulary). Parameters ---------- embedding_sources_mapping : Dict[str, str], (optional, default=None) Mapping from model_path to pretrained-file path of the embedding modules. If pretrained-file used at time of embedding initialization isn't available now, user should pass this mapping. Model path is path traversing the model attributes upto this embedding module. Eg. "_text_field_embedder.token_embedder_tokens". """ # 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)
[ "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", ")" ]
Iterates through all embedding modules in the model and assures it can embed with the extended vocab. This is required in fine-tuning or transfer learning scenarios where model was trained with original vocabulary but during fine-tuning/tranfer-learning, it will have it work with extended vocabulary (original + new-data vocabulary). Parameters ---------- embedding_sources_mapping : Dict[str, str], (optional, default=None) Mapping from model_path to pretrained-file path of the embedding modules. If pretrained-file used at time of embedding initialization isn't available now, user should pass this mapping. Model path is path traversing the model attributes upto this embedding module. Eg. "_text_field_embedder.token_embedder_tokens".
[ "Iterates", "through", "all", "embedding", "modules", "in", "the", "model", "and", "assures", "it", "can", "embed", "with", "the", "extended", "vocab", ".", "This", "is", "required", "in", "fine", "-", "tuning", "or", "transfer", "learning", "scenarios", "where", "model", "was", "trained", "with", "original", "vocabulary", "but", "during", "fine", "-", "tuning", "/", "tranfer", "-", "learning", "it", "will", "have", "it", "work", "with", "extended", "vocabulary", "(", "original", "+", "new", "-", "data", "vocabulary", ")", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/models/model.py#L329-L354
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.get_agenda
def get_agenda(self, conservative: bool = False): """ Returns an agenda that can be used guide search. Parameters ---------- conservative : ``bool`` Setting this flag will return a subset of the agenda items that correspond to high confidence lexical matches. You'll need this if you are going to use this agenda to penalize a model for producing logical forms that do not contain some items in it. In that case, you'll want this agenda to have close to perfect precision, at the cost of a lower recall. You may not want to set this flag if you are sorting the output from a search procedure based on how much of this agenda is satisfied. """ 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
python
def get_agenda(self, conservative: bool = False): """ Returns an agenda that can be used guide search. Parameters ---------- conservative : ``bool`` Setting this flag will return a subset of the agenda items that correspond to high confidence lexical matches. You'll need this if you are going to use this agenda to penalize a model for producing logical forms that do not contain some items in it. In that case, you'll want this agenda to have close to perfect precision, at the cost of a lower recall. You may not want to set this flag if you are sorting the output from a search procedure based on how much of this agenda is satisfied. """ 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
[ "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" ]
Returns an agenda that can be used guide search. Parameters ---------- conservative : ``bool`` Setting this flag will return a subset of the agenda items that correspond to high confidence lexical matches. You'll need this if you are going to use this agenda to penalize a model for producing logical forms that do not contain some items in it. In that case, you'll want this agenda to have close to perfect precision, at the cost of a lower recall. You may not want to set this flag if you are sorting the output from a search procedure based on how much of this agenda is satisfied.
[ "Returns", "an", "agenda", "that", "can", "be", "used", "guide", "search", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L145-L321
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.evaluate_logical_form
def evaluate_logical_form(self, logical_form: str, target_list: List[str]) -> bool: """ Takes a logical form, and the list of target values as strings from the original lisp string, and returns True iff the logical form executes to the target list, using the official WikiTableQuestions evaluation script. """ 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)
python
def evaluate_logical_form(self, logical_form: str, target_list: List[str]) -> bool: """ Takes a logical form, and the list of target values as strings from the original lisp string, and returns True iff the logical form executes to the target list, using the official WikiTableQuestions evaluation script. """ 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)
[ "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", ")" ]
Takes a logical form, and the list of target values as strings from the original lisp string, and returns True iff the logical form executes to the target list, using the official WikiTableQuestions evaluation script.
[ "Takes", "a", "logical", "form", "and", "the", "list", "of", "target", "values", "as", "strings", "from", "the", "original", "lisp", "string", "and", "returns", "True", "iff", "the", "logical", "form", "executes", "to", "the", "target", "list", "using", "the", "official", "WikiTableQuestions", "evaluation", "script", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L323-L342
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.select_string
def select_string(self, rows: List[Row], column: StringColumn) -> List[str]: """ Select function takes a list of rows and a column name and returns a list of strings as in cells. """ return [str(row.values[column.name]) for row in rows if row.values[column.name] is not None]
python
def select_string(self, rows: List[Row], column: StringColumn) -> List[str]: """ Select function takes a list of rows and a column name and returns a list of strings as in cells. """ return [str(row.values[column.name]) for row in rows if row.values[column.name] is not None]
[ "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", "]" ]
Select function takes a list of rows and a column name and returns a list of strings as in cells.
[ "Select", "function", "takes", "a", "list", "of", "rows", "and", "a", "column", "name", "and", "returns", "a", "list", "of", "strings", "as", "in", "cells", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L354-L359
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.select_number
def select_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Select function takes a row (as a list) and a column name and returns the number in that column. If multiple rows are given, will return the first number that is not None. """ 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
python
def select_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Select function takes a row (as a list) and a column name and returns the number in that column. If multiple rows are given, will return the first number that is not None. """ 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
[ "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" ]
Select function takes a row (as a list) and a column name and returns the number in that column. If multiple rows are given, will return the first number that is not None.
[ "Select", "function", "takes", "a", "row", "(", "as", "a", "list", ")", "and", "a", "column", "name", "and", "returns", "the", "number", "in", "that", "column", ".", "If", "multiple", "rows", "are", "given", "will", "return", "the", "first", "number", "that", "is", "not", "None", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L362-L373
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.select_date
def select_date(self, rows: List[Row], column: DateColumn) -> Date: """ Select function takes a row as a list and a column name and returns the date in that column. """ 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)
python
def select_date(self, rows: List[Row], column: DateColumn) -> Date: """ Select function takes a row as a list and a column name and returns the date in that column. """ 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)
[ "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", ")" ]
Select function takes a row as a list and a column name and returns the date in that column.
[ "Select", "function", "takes", "a", "row", "as", "a", "list", "and", "a", "column", "name", "and", "returns", "the", "date", "in", "that", "column", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L376-L386
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.same_as
def same_as(self, rows: List[Row], column: Column) -> List[Row]: """ Takes a row and a column and returns a list of rows from the full set of rows that contain the same value under the given column as the given 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
python
def same_as(self, rows: List[Row], column: Column) -> List[Row]: """ Takes a row and a column and returns a list of rows from the full set of rows that contain the same value under the given column as the given 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
[ "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" ]
Takes a row and a column and returns a list of rows from the full set of rows that contain the same value under the given column as the given row.
[ "Takes", "a", "row", "and", "a", "column", "and", "returns", "a", "list", "of", "rows", "from", "the", "full", "set", "of", "rows", "that", "contain", "the", "same", "value", "under", "the", "given", "column", "as", "the", "given", "row", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L389-L399
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.date
def date(self, year: Number, month: Number, day: Number) -> Date: """ Takes three numbers and returns a ``Date`` object whose year, month, and day are the three numbers in that order. """ return Date(year, month, day)
python
def date(self, year: Number, month: Number, day: Number) -> Date: """ Takes three numbers and returns a ``Date`` object whose year, month, and day are the three numbers in that order. """ return Date(year, month, day)
[ "def", "date", "(", "self", ",", "year", ":", "Number", ",", "month", ":", "Number", ",", "day", ":", "Number", ")", "->", "Date", ":", "return", "Date", "(", "year", ",", "month", ",", "day", ")" ]
Takes three numbers and returns a ``Date`` object whose year, month, and day are the three numbers in that order.
[ "Takes", "three", "numbers", "and", "returns", "a", "Date", "object", "whose", "year", "month", "and", "day", "are", "the", "three", "numbers", "in", "that", "order", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L402-L407
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.first
def first(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a list of rows, and returns the first one in that list. """ if not rows: logger.warning("Trying to get first row from an empty list") return [] return [rows[0]]
python
def first(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a list of rows, and returns the first one in that list. """ if not rows: logger.warning("Trying to get first row from an empty list") return [] return [rows[0]]
[ "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", "]", "]" ]
Takes an expression that evaluates to a list of rows, and returns the first one in that list.
[ "Takes", "an", "expression", "that", "evaluates", "to", "a", "list", "of", "rows", "and", "returns", "the", "first", "one", "in", "that", "list", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L410-L418
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.last
def last(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a list of rows, and returns the last one in that list. """ if not rows: logger.warning("Trying to get last row from an empty list") return [] return [rows[-1]]
python
def last(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a list of rows, and returns the last one in that list. """ if not rows: logger.warning("Trying to get last row from an empty list") return [] return [rows[-1]]
[ "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", "]", "]" ]
Takes an expression that evaluates to a list of rows, and returns the last one in that list.
[ "Takes", "an", "expression", "that", "evaluates", "to", "a", "list", "of", "rows", "and", "returns", "the", "last", "one", "in", "that", "list", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L421-L429
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.previous
def previous(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a single row, and returns the row that occurs before the input row in the original set of rows. If the input row happens to be the top row, we will return an empty list. """ 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 []
python
def previous(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a single row, and returns the row that occurs before the input row in the original set of rows. If the input row happens to be the top row, we will return an empty list. """ 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 []
[ "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", "[", "]" ]
Takes an expression that evaluates to a single row, and returns the row that occurs before the input row in the original set of rows. If the input row happens to be the top row, we will return an empty list.
[ "Takes", "an", "expression", "that", "evaluates", "to", "a", "single", "row", "and", "returns", "the", "row", "that", "occurs", "before", "the", "input", "row", "in", "the", "original", "set", "of", "rows", ".", "If", "the", "input", "row", "happens", "to", "be", "the", "top", "row", "we", "will", "return", "an", "empty", "list", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L432-L443
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.next
def next(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a single row, and returns the row that occurs after the input row in the original set of rows. If the input row happens to be the last row, we will return an empty list. """ 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 []
python
def next(self, rows: List[Row]) -> List[Row]: """ Takes an expression that evaluates to a single row, and returns the row that occurs after the input row in the original set of rows. If the input row happens to be the last row, we will return an empty list. """ 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 []
[ "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", "[", "]" ]
Takes an expression that evaluates to a single row, and returns the row that occurs after the input row in the original set of rows. If the input row happens to be the last row, we will return an empty list.
[ "Takes", "an", "expression", "that", "evaluates", "to", "a", "single", "row", "and", "returns", "the", "row", "that", "occurs", "after", "the", "input", "row", "in", "the", "original", "set", "of", "rows", ".", "If", "the", "input", "row", "happens", "to", "be", "the", "last", "row", "we", "will", "return", "an", "empty", "list", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L446-L457
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.mode_string
def mode_string(self, rows: List[Row], column: StringColumn) -> List[str]: """ Takes a list of rows and a column and returns the most frequent values (one or more) under that column in those rows. """ 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
python
def mode_string(self, rows: List[Row], column: StringColumn) -> List[str]: """ Takes a list of rows and a column and returns the most frequent values (one or more) under that column in those rows. """ 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
[ "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" ]
Takes a list of rows and a column and returns the most frequent values (one or more) under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "most", "frequent", "values", "(", "one", "or", "more", ")", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L464-L474
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.mode_number
def mode_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the most frequent value under that column in those rows. """ 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
python
def mode_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the most frequent value under that column in those rows. """ 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
[ "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" ]
Takes a list of rows and a column and returns the most frequent value under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "most", "frequent", "value", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L477-L488
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.mode_date
def mode_date(self, rows: List[Row], column: DateColumn) -> Date: """ Takes a list of rows and a column and returns the most frequent value under that column in those rows. """ 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
python
def mode_date(self, rows: List[Row], column: DateColumn) -> Date: """ Takes a list of rows and a column and returns the most frequent value under that column in those rows. """ 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
[ "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" ]
Takes a list of rows and a column and returns the most frequent value under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "most", "frequent", "value", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L491-L502
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.argmax
def argmax(self, rows: List[Row], column: ComparableColumn) -> List[Row]: """ Takes a list of rows and a column name and returns a list containing a single row (dict from columns to cells) that has the maximum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``. """ 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]]
python
def argmax(self, rows: List[Row], column: ComparableColumn) -> List[Row]: """ Takes a list of rows and a column name and returns a list containing a single row (dict from columns to cells) that has the maximum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``. """ 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]]
[ "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", "]", "]" ]
Takes a list of rows and a column name and returns a list containing a single row (dict from columns to cells) that has the maximum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "name", "and", "returns", "a", "list", "containing", "a", "single", "row", "(", "dict", "from", "columns", "to", "cells", ")", "that", "has", "the", "maximum", "numerical", "value", "in", "the", "given", "column", ".", "We", "return", "a", "list", "instead", "of", "a", "single", "dict", "to", "be", "consistent", "with", "the", "return", "type", "of", "select", "and", "all_rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L507-L520
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.argmin
def argmin(self, rows: List[Row], column: ComparableColumn) -> List[Row]: """ Takes a list of rows and a column and returns a list containing a single row (dict from columns to cells) that has the minimum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``. """ 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]]
python
def argmin(self, rows: List[Row], column: ComparableColumn) -> List[Row]: """ Takes a list of rows and a column and returns a list containing a single row (dict from columns to cells) that has the minimum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``. """ 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]]
[ "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", "]", "]" ]
Takes a list of rows and a column and returns a list containing a single row (dict from columns to cells) that has the minimum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of ``select`` and ``all_rows``.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "a", "list", "containing", "a", "single", "row", "(", "dict", "from", "columns", "to", "cells", ")", "that", "has", "the", "minimum", "numerical", "value", "in", "the", "given", "column", ".", "We", "return", "a", "list", "instead", "of", "a", "single", "dict", "to", "be", "consistent", "with", "the", "return", "type", "of", "select", "and", "all_rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L522-L535
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.max_date
def max_date(self, rows: List[Row], column: DateColumn) -> Date: """ Takes a list of rows and a column and returns the max of the values under that column in those rows. """ 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)
python
def max_date(self, rows: List[Row], column: DateColumn) -> Date: """ Takes a list of rows and a column and returns the max of the values under that column in those rows. """ 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)
[ "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", ")" ]
Takes a list of rows and a column and returns the max of the values under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "max", "of", "the", "values", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L676-L686
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.max_number
def max_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the max of the values under that column in those rows. """ 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)
python
def max_number(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the max of the values under that column in those rows. """ 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)
[ "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", ")" ]
Takes a list of rows and a column and returns the max of the values under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "max", "of", "the", "values", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L703-L713
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.average
def average(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the mean of the values under that column in those rows. """ 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)
python
def average(self, rows: List[Row], column: NumberColumn) -> Number: """ Takes a list of rows and a column and returns the mean of the values under that column in those rows. """ 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)
[ "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", ")" ]
Takes a list of rows and a column and returns the mean of the values under that column in those rows.
[ "Takes", "a", "list", "of", "rows", "and", "a", "column", "and", "returns", "the", "mean", "of", "the", "values", "under", "that", "column", "in", "those", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L737-L745
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage.diff
def diff(self, first_row: List[Row], second_row: List[Row], column: NumberColumn) -> Number: """ Takes a two rows and a number column and returns the difference between the values under that column in those two rows. """ 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}")
python
def diff(self, first_row: List[Row], second_row: List[Row], column: NumberColumn) -> Number: """ Takes a two rows and a number column and returns the difference between the values under that column in those two rows. """ 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}")
[ "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}\"", ")" ]
Takes a two rows and a number column and returns the difference between the values under that column in those two rows.
[ "Takes", "a", "two", "rows", "and", "a", "number", "column", "and", "returns", "the", "difference", "between", "the", "values", "under", "that", "column", "in", "those", "two", "rows", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L747-L759
train
allenai/allennlp
allennlp/semparse/domain_languages/wikitables_language.py
WikiTablesLanguage._get_row_index
def _get_row_index(self, row: Row) -> int: """ Takes a row and returns its index in the full list of rows. If the row does not occur in the table (which should never happen because this function will only be called with a row that is the result of applying one or more functions on the table rows), the method returns -1. """ 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
python
def _get_row_index(self, row: Row) -> int: """ Takes a row and returns its index in the full list of rows. If the row does not occur in the table (which should never happen because this function will only be called with a row that is the result of applying one or more functions on the table rows), the method returns -1. """ 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
[ "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" ]
Takes a row and returns its index in the full list of rows. If the row does not occur in the table (which should never happen because this function will only be called with a row that is the result of applying one or more functions on the table rows), the method returns -1.
[ "Takes", "a", "row", "and", "returns", "its", "index", "in", "the", "full", "list", "of", "rows", ".", "If", "the", "row", "does", "not", "occur", "in", "the", "table", "(", "which", "should", "never", "happen", "because", "this", "function", "will", "only", "be", "called", "with", "a", "row", "that", "is", "the", "result", "of", "applying", "one", "or", "more", "functions", "on", "the", "table", "rows", ")", "the", "method", "returns", "-", "1", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/domain_languages/wikitables_language.py#L785-L796
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.is_terminal
def is_terminal(self, symbol: str) -> bool: """ This function will be called on nodes of a logical form tree, which are either non-terminal symbols that can be expanded or terminal symbols that must be leaf nodes. Returns ``True`` if the given symbol is a terminal symbol. """ # 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)
python
def is_terminal(self, symbol: str) -> bool: """ This function will be called on nodes of a logical form tree, which are either non-terminal symbols that can be expanded or terminal symbols that must be leaf nodes. Returns ``True`` if the given symbol is a terminal symbol. """ # 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)
[ "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", ")" ]
This function will be called on nodes of a logical form tree, which are either non-terminal symbols that can be expanded or terminal symbols that must be leaf nodes. Returns ``True`` if the given symbol is a terminal symbol.
[ "This", "function", "will", "be", "called", "on", "nodes", "of", "a", "logical", "form", "tree", "which", "are", "either", "non", "-", "terminal", "symbols", "that", "can", "be", "expanded", "or", "terminal", "symbols", "that", "must", "be", "leaf", "nodes", ".", "Returns", "True", "if", "the", "given", "symbol", "is", "a", "terminal", "symbol", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L76-L85
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.get_paths_to_root
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]]: """ For a given action, returns at most ``max_num_paths`` paths to the root (production with ``START_SYMBOL``) that are not longer than ``max_path_length``. """ 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]
python
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]]: """ For a given action, returns at most ``max_num_paths`` paths to the root (production with ``START_SYMBOL``) that are not longer than ``max_path_length``. """ 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]
[ "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", "]" ]
For a given action, returns at most ``max_num_paths`` paths to the root (production with ``START_SYMBOL``) that are not longer than ``max_path_length``.
[ "For", "a", "given", "action", "returns", "at", "most", "max_num_paths", "paths", "to", "the", "root", "(", "production", "with", "START_SYMBOL", ")", "that", "are", "not", "longer", "than", "max_path_length", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L98-L140
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.get_multi_match_mapping
def get_multi_match_mapping(self) -> Dict[Type, List[Type]]: """ Returns a mapping from each `MultiMatchNamedBasicType` to all the `NamedBasicTypes` that it matches. """ 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
python
def get_multi_match_mapping(self) -> Dict[Type, List[Type]]: """ Returns a mapping from each `MultiMatchNamedBasicType` to all the `NamedBasicTypes` that it matches. """ 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
[ "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" ]
Returns a mapping from each `MultiMatchNamedBasicType` to all the `NamedBasicTypes` that it matches.
[ "Returns", "a", "mapping", "from", "each", "MultiMatchNamedBasicType", "to", "all", "the", "NamedBasicTypes", "that", "it", "matches", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L184-L204
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.parse_logical_form
def parse_logical_form(self, logical_form: str, remove_var_function: bool = True) -> Expression: """ Takes a logical form as a string, maps its tokens using the mapping and returns a parsed expression. Parameters ---------- logical_form : ``str`` Logical form to parse remove_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the usage of a variable. If your language uses it, and you do not want to include it in the parsed expression, set this flag. You may want to do this if you are generating an action sequence from this parsed expression, because it is easier to let the decoder not produce this function due to the way constrained decoding is currently implemented. """ 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)
python
def parse_logical_form(self, logical_form: str, remove_var_function: bool = True) -> Expression: """ Takes a logical form as a string, maps its tokens using the mapping and returns a parsed expression. Parameters ---------- logical_form : ``str`` Logical form to parse remove_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the usage of a variable. If your language uses it, and you do not want to include it in the parsed expression, set this flag. You may want to do this if you are generating an action sequence from this parsed expression, because it is easier to let the decoder not produce this function due to the way constrained decoding is currently implemented. """ 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)
[ "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", ")" ]
Takes a logical form as a string, maps its tokens using the mapping and returns a parsed expression. Parameters ---------- logical_form : ``str`` Logical form to parse remove_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the usage of a variable. If your language uses it, and you do not want to include it in the parsed expression, set this flag. You may want to do this if you are generating an action sequence from this parsed expression, because it is easier to let the decoder not produce this function due to the way constrained decoding is currently implemented.
[ "Takes", "a", "logical", "form", "as", "a", "string", "maps", "its", "tokens", "using", "the", "mapping", "and", "returns", "a", "parsed", "expression", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L206-L235
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.get_action_sequence
def get_action_sequence(self, expression: Expression) -> List[str]: """ Returns the sequence of actions (as strings) that resulted in the given expression. """ # Starting with the type of the whole expression return self._get_transitions(expression, [f"{types.START_TYPE} -> {expression.type}"])
python
def get_action_sequence(self, expression: Expression) -> List[str]: """ Returns the sequence of actions (as strings) that resulted in the given expression. """ # Starting with the type of the whole expression return self._get_transitions(expression, [f"{types.START_TYPE} -> {expression.type}"])
[ "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}\"", "]", ")" ]
Returns the sequence of actions (as strings) that resulted in the given expression.
[ "Returns", "the", "sequence", "of", "actions", "(", "as", "strings", ")", "that", "resulted", "in", "the", "given", "expression", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L237-L243
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World.get_logical_form
def get_logical_form(self, action_sequence: List[str], add_var_function: bool = True) -> str: """ Takes an action sequence and constructs a logical form from it. This is useful if you want to get a logical form from a decoded sequence of actions generated by a transition based semantic parser. Parameters ---------- action_sequence : ``List[str]`` The sequence of actions as strings (eg.: ``['{START_SYMBOL} -> t', 't -> <e,t>', ...]``). add_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the use of a variable (eg.: ``(lambda x (fb:row.row.year (var x)))``). Due to the way constrained decoding is currently implemented, it is easier for the decoder to not produce these functions. In that case, setting this flag adds the function in the logical form even though it is not present in the action sequence. """ # 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)
python
def get_logical_form(self, action_sequence: List[str], add_var_function: bool = True) -> str: """ Takes an action sequence and constructs a logical form from it. This is useful if you want to get a logical form from a decoded sequence of actions generated by a transition based semantic parser. Parameters ---------- action_sequence : ``List[str]`` The sequence of actions as strings (eg.: ``['{START_SYMBOL} -> t', 't -> <e,t>', ...]``). add_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the use of a variable (eg.: ``(lambda x (fb:row.row.year (var x)))``). Due to the way constrained decoding is currently implemented, it is easier for the decoder to not produce these functions. In that case, setting this flag adds the function in the logical form even though it is not present in the action sequence. """ # 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)
[ "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", ")" ]
Takes an action sequence and constructs a logical form from it. This is useful if you want to get a logical form from a decoded sequence of actions generated by a transition based semantic parser. Parameters ---------- action_sequence : ``List[str]`` The sequence of actions as strings (eg.: ``['{START_SYMBOL} -> t', 't -> <e,t>', ...]``). add_var_function : ``bool`` (optional) ``var`` is a special function that some languages use within lambda functions to indicate the use of a variable (eg.: ``(lambda x (fb:row.row.year (var x)))``). Due to the way constrained decoding is currently implemented, it is easier for the decoder to not produce these functions. In that case, setting this flag adds the function in the logical form even though it is not present in the action sequence.
[ "Takes", "an", "action", "sequence", "and", "constructs", "a", "logical", "form", "from", "it", ".", "This", "is", "useful", "if", "you", "want", "to", "get", "a", "logical", "form", "from", "a", "decoded", "sequence", "of", "actions", "generated", "by", "a", "transition", "based", "semantic", "parser", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L245-L285
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World._construct_node_from_actions
def _construct_node_from_actions(self, current_node: Tree, remaining_actions: List[List[str]], add_var_function: bool) -> List[List[str]]: """ Given a current node in the logical form tree, and a list of actions in an action sequence, this method fills in the children of the current node from the action sequence, then returns whatever actions are left. For example, we could get a node with type ``c``, and an action sequence that begins with ``c -> [<r,c>, r]``. This method will add two children to the input node, consuming actions from the action sequence for nodes of type ``<r,c>`` (and all of its children, recursively) and ``r`` (and all of its children, recursively). This method assumes that action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>`` appear before actions for the subtree under ``r``. If there are any actions in the action sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be returned. """ 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
python
def _construct_node_from_actions(self, current_node: Tree, remaining_actions: List[List[str]], add_var_function: bool) -> List[List[str]]: """ Given a current node in the logical form tree, and a list of actions in an action sequence, this method fills in the children of the current node from the action sequence, then returns whatever actions are left. For example, we could get a node with type ``c``, and an action sequence that begins with ``c -> [<r,c>, r]``. This method will add two children to the input node, consuming actions from the action sequence for nodes of type ``<r,c>`` (and all of its children, recursively) and ``r`` (and all of its children, recursively). This method assumes that action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>`` appear before actions for the subtree under ``r``. If there are any actions in the action sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be returned. """ 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
[ "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" ]
Given a current node in the logical form tree, and a list of actions in an action sequence, this method fills in the children of the current node from the action sequence, then returns whatever actions are left. For example, we could get a node with type ``c``, and an action sequence that begins with ``c -> [<r,c>, r]``. This method will add two children to the input node, consuming actions from the action sequence for nodes of type ``<r,c>`` (and all of its children, recursively) and ``r`` (and all of its children, recursively). This method assumes that action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>`` appear before actions for the subtree under ``r``. If there are any actions in the action sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be returned.
[ "Given", "a", "current", "node", "in", "the", "logical", "form", "tree", "and", "a", "list", "of", "actions", "in", "an", "action", "sequence", "this", "method", "fills", "in", "the", "children", "of", "the", "current", "node", "from", "the", "action", "sequence", "then", "returns", "whatever", "actions", "are", "left", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L287-L349
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World._infer_num_arguments
def _infer_num_arguments(cls, type_signature: str) -> int: """ Takes a type signature and infers the number of arguments the corresponding function takes. Examples: e -> 0 <r,e> -> 1 <e,<e,t>> -> 2 <b,<<b,#1>,<#1,b>>> -> 3 """ 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)
python
def _infer_num_arguments(cls, type_signature: str) -> int: """ Takes a type signature and infers the number of arguments the corresponding function takes. Examples: e -> 0 <r,e> -> 1 <e,<e,t>> -> 2 <b,<<b,#1>,<#1,b>>> -> 3 """ 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)
[ "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", ")" ]
Takes a type signature and infers the number of arguments the corresponding function takes. Examples: e -> 0 <r,e> -> 1 <e,<e,t>> -> 2 <b,<<b,#1>,<#1,b>>> -> 3
[ "Takes", "a", "type", "signature", "and", "infers", "the", "number", "of", "arguments", "the", "corresponding", "function", "takes", ".", "Examples", ":", "e", "-", ">", "0", "<r", "e", ">", "-", ">", "1", "<e", "<e", "t", ">>", "-", ">", "2", "<b", "<<b", "#1", ">", "<#1", "b", ">>>", "-", ">", "3" ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L352-L380
train
allenai/allennlp
allennlp/semparse/worlds/world.py
World._process_nested_expression
def _process_nested_expression(self, nested_expression) -> str: """ ``nested_expression`` is the result of parsing a logical form in Lisp format. We process it recursively and return a string in the format that NLTK's ``LogicParser`` would understand. """ 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)})'
python
def _process_nested_expression(self, nested_expression) -> str: """ ``nested_expression`` is the result of parsing a logical form in Lisp format. We process it recursively and return a string in the format that NLTK's ``LogicParser`` would understand. """ 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)})'
[ "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)})'" ]
``nested_expression`` is the result of parsing a logical form in Lisp format. We process it recursively and return a string in the format that NLTK's ``LogicParser`` would understand.
[ "nested_expression", "is", "the", "result", "of", "parsing", "a", "logical", "form", "in", "Lisp", "format", ".", "We", "process", "it", "recursively", "and", "return", "a", "string", "in", "the", "format", "that", "NLTK", "s", "LogicParser", "would", "understand", "." ]
648a36f77db7e45784c047176074f98534c76636
https://github.com/allenai/allennlp/blob/648a36f77db7e45784c047176074f98534c76636/allennlp/semparse/worlds/world.py#L382-L408
train