pytext.models.semantic_parsers.rnng package¶
Submodules¶
pytext.models.semantic_parsers.rnng.rnng_constant module¶
pytext.models.semantic_parsers.rnng.rnng_data_structures module¶
-
class
pytext.models.semantic_parsers.rnng.rnng_data_structures.
CompositionalNN
(lstm_dim: int)[source]¶ Bases:
torch.jit._script.ScriptModule
Combines a list / sequence of embeddings into one using a biLSTM
-
class
pytext.models.semantic_parsers.rnng.rnng_data_structures.
CompositionalSummationNN
(lstm_dim: int)[source]¶ Bases:
torch.jit._script.ScriptModule
Simpler version of CompositionalNN
-
class
pytext.models.semantic_parsers.rnng.rnng_data_structures.
Element
(node: Any)[source]¶ Bases:
object
Generic element representing a token / non-terminal / sub-tree on a stack. Used to compute valid actions in the RNNG parser.
-
class
pytext.models.semantic_parsers.rnng.rnng_data_structures.
ParserState
(parser=None)[source]¶ Bases:
object
Maintains state of the Parser. Useful for beam search
-
class
pytext.models.semantic_parsers.rnng.rnng_data_structures.
StackLSTM
(lstm: torch.nn.modules.rnn.LSTM)[source]¶ Bases:
collections.abc.Sized
,typing.Generic
The Stack LSTM from Dyer et al: https://arxiv.org/abs/1505.08075
-
element_from_top
(index: int) → pytext.models.semantic_parsers.rnng.rnng_data_structures.Element[source]¶
-
pytext.models.semantic_parsers.rnng.rnng_parser module¶
-
class
pytext.models.semantic_parsers.rnng.rnng_parser.
RNNGParser
(ablation: pytext.models.semantic_parsers.rnng.rnng_parser.RNNGParserBase.Config.AblationParams, constraints: pytext.models.semantic_parsers.rnng.rnng_parser.RNNGParserBase.Config.RNNGConstraints, lstm_num_layers: int, lstm_dim: int, max_open_NT: int, dropout: float, actions_vocab, shift_idx: int, reduce_idx: int, ignore_subNTs_roots: List[int], valid_NT_idxs: List[int], valid_IN_idxs: List[int], valid_SL_idxs: List[int], embedding: pytext.models.embeddings.embedding_list.EmbeddingList, p_compositional)[source]¶ Bases:
pytext.models.semantic_parsers.rnng.rnng_parser.RNNGParserBase
-
class
pytext.models.semantic_parsers.rnng.rnng_parser.
RNNGParserBase
(ablation: pytext.models.semantic_parsers.rnng.rnng_parser.RNNGParserBase.Config.AblationParams, constraints: pytext.models.semantic_parsers.rnng.rnng_parser.RNNGParserBase.Config.RNNGConstraints, lstm_num_layers: int, lstm_dim: int, max_open_NT: int, dropout: float, actions_vocab, shift_idx: int, reduce_idx: int, ignore_subNTs_roots: List[int], valid_NT_idxs: List[int], valid_IN_idxs: List[int], valid_SL_idxs: List[int], embedding: pytext.models.embeddings.embedding_list.EmbeddingList, p_compositional)[source]¶ Bases:
pytext.models.model.BaseModel
The Recurrent Neural Network Grammar (RNNG) parser from Dyer et al.: https://arxiv.org/abs/1602.07776 and Gupta et al.: https://arxiv.org/abs/1810.07942d. RNNG is a neural constituency parsing algorithm that explicitly models compositional structure of a sentence. It is able to learn about hierarchical relationship among the words and phrases in a given sentence thereby learning the underlying tree structure. The paper proposes generative as well as discriminative approaches. In PyText we have implemented the discriminative approach for modeling intent slot models. It is a top-down shift-reduce parser than can output trees with non-terminals (intent and slot labels) and terminals (tokens)
-
contextualize
(context)[source]¶ Add additional context into model. context can be anything that helps maintaining/updating state. For example, it is used by
DisjointMultitaskModel
for changing the task that should be trained with a given iterator.
-
forward
(tokens: torch.Tensor, seq_lens: torch.Tensor, dict_feat: Optional[Tuple[torch.Tensor, ...]] = None, actions: Optional[List[List[int]]] = None, contextual_token_embeddings: Optional[torch.Tensor] = None, beam_size=1, top_k=1) → List[Tuple[torch.Tensor, torch.Tensor]][source]¶ RNNG forward function.
Parameters: - tokens (torch.Tensor) – list of tokens
- seq_lens (torch.Tensor) – list of sequence lengths
- dict_feat (Optional[Tuple[torch.Tensor, ..]]) – dictionary or gazetteer features for each token
- actions (Optional[List[List[int]]]) – Used only during training. Oracle actions for the instances.
Returns: list of top k tuple of predicted actions tensor and corresponding scores tensor. Tensor shape: (batch_size, action_length) (batch_size, action_length, number_of_actions)
-
classmethod
from_config
(model_config, feature_config=None, metadata: pytext.data.data_handler.CommonMetadata = None, tensorizers: Dict[str, pytext.data.tensorizers.Tensorizer] = None)[source]¶
-
get_loss
(logits: List[Tuple[torch.Tensor, torch.Tensor]], target_actions: torch.Tensor, context: torch.Tensor)[source]¶ - Shapes:
- logits[1]: action scores: (1, action_length, number_of_actions) target_actions: (1, action_length)
-
get_pred
(logits: List[Tuple[torch.Tensor, torch.Tensor]], context=None, *args)[source]¶ - Return Shapes:
- preds: batch (1) * topk * action_len scores: batch (1) * topk * (action_len * number_of_actions)
-
push_action
(state: pytext.models.semantic_parsers.rnng.rnng_data_structures.ParserState, target_action_idx: int) → None[source]¶ Used for updating the state with a target next action
Parameters: - state (ParserState) – The state of the stack, buffer and action
- target_action_idx (int) – Index of the action to process
-
valid_actions
(state: pytext.models.semantic_parsers.rnng.rnng_data_structures.ParserState) → List[int][source]¶ Used for restricting the set of possible action predictions
Parameters: state (ParserState) – The state of the stack, buffer and action Returns: indices of the valid actions Return type: List[int]
-