Python allennlp.modules.Seq2SeqEncoder() Examples
The following are 30
code examples of allennlp.modules.Seq2SeqEncoder().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
allennlp.modules
, or try the search function
.
Example #1
Source File: esim_comparator.py From multee with Apache License 2.0 | 6 votes |
def __init__(self, projection_feedforward: FeedForward, inference_encoder: Seq2SeqEncoder, output_feedforward: FeedForward, dropout: float = 0.5) -> None: super().__init__() if dropout: self.dropout = torch.nn.Dropout(dropout) self.rnn_input_dropout = InputVariationalDropout(dropout) else: self.dropout = None self.rnn_input_dropout = None self._projection_feedforward = projection_feedforward self._inference_encoder = inference_encoder self._output_feedforward = output_feedforward # self._weight_premise_token = weight_premise_token
Example #2
Source File: esim_comparator.py From multee with Apache License 2.0 | 6 votes |
def __init__(self, encoder: Seq2SeqEncoder, projection_feedforward: FeedForward, inference_encoder: Seq2SeqEncoder, output_feedforward: FeedForward, similarity_function: SimilarityFunction = None, dropout: float = 0.5) -> None: super().__init__() self._encoder = encoder self._matrix_attention = LegacyMatrixAttention(similarity_function) self._projection_feedforward = projection_feedforward self._inference_encoder = inference_encoder if dropout: self.dropout = torch.nn.Dropout(dropout) self.rnn_input_dropout = InputVariationalDropout(dropout) else: self.dropout = None self.rnn_input_dropout = None self._output_feedforward = output_feedforward
Example #3
Source File: text_classifier.py From scibert with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, text_encoder: Seq2SeqEncoder, classifier_feedforward: FeedForward, verbose_metrics: False, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None, ) -> None: super(TextClassifier, self).__init__(vocab, regularizer) self.text_field_embedder = text_field_embedder self.num_classes = self.vocab.get_vocab_size("labels") self.text_encoder = text_encoder self.classifier_feedforward = classifier_feedforward self.prediction_layer = torch.nn.Linear(self.classifier_feedforward.get_output_dim() , self.num_classes) self.label_accuracy = CategoricalAccuracy() self.label_f1_metrics = {} self.verbose_metrics = verbose_metrics for i in range(self.num_classes): self.label_f1_metrics[vocab.get_token_from_index(index=i, namespace="labels")] = F1Measure(positive_label=i) self.loss = torch.nn.CrossEntropyLoss() self.pool = lambda text, mask: util.get_final_encoder_states(text, mask, bidirectional=True) initializer(self)
Example #4
Source File: simple_tagger.py From HIT-SCIR-CoNLL2019 with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, calculate_span_f1: bool = None, label_encoding: Optional[str] = None, label_namespace: str = "labels", verbose_metrics: bool = False, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super(SimpleTagger, self).__init__(vocab, regularizer) self.label_namespace = label_namespace self.text_field_embedder = text_field_embedder self.num_classes = self.vocab.get_vocab_size(label_namespace) self.encoder = encoder self._verbose_metrics = verbose_metrics self.tag_projection_layer = TimeDistributed(Linear(self.encoder.get_output_dim(), self.num_classes)) check_dimensions_match(text_field_embedder.get_output_dim(), encoder.get_input_dim(), "text field embedding dim", "encoder input dim") # We keep calculate_span_f1 as a constructor argument for API consistency with # the CrfTagger, even it is redundant in this class # (label_encoding serves the same purpose). if calculate_span_f1 and not label_encoding: raise ConfigurationError("calculate_span_f1 is True, but " "no label_encoding was specified.") self.metrics = { "accuracy": CategoricalAccuracy(), "accuracy3": CategoricalAccuracy(top_k=3) } if calculate_span_f1 or label_encoding: self._f1_metric = SpanBasedF1Measure(vocab, tag_namespace=label_namespace, label_encoding=label_encoding) else: self._f1_metric = None initializer(self)
Example #5
Source File: encoder.py From vampire with Apache License 2.0 | 5 votes |
def __init__(self, architecture: Seq2SeqEncoder, aggregations: str) -> None: super(Seq2Seq, self).__init__(architecture) self._architecture = architecture self._aggregations = aggregations if "attention" in self._aggregations: self._attention_layer = torch.nn.Linear(self._architecture.get_output_dim(), 1)
Example #6
Source File: rnet.py From R-net with MIT License | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, question_encoder: Seq2SeqEncoder, passage_encoder: Seq2SeqEncoder, pair_encoder: AttentionEncoder, self_encoder: AttentionEncoder, output_layer: QAOutputLayer, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None, share_encoder: bool = False): super().__init__(vocab, regularizer) self.text_field_embedder = text_field_embedder self.question_encoder = question_encoder self.passage_encoder = passage_encoder self.pair_encoder = pair_encoder self.self_encoder = self_encoder self.output_layer = output_layer self._span_start_accuracy = CategoricalAccuracy() self._span_end_accuracy = CategoricalAccuracy() self._span_accuracy = BooleanAccuracy() self._squad_metrics = SquadEmAndF1() self.share_encoder = share_encoder self.loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #7
Source File: custom_composed_seq2seq.py From summarus with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, source_text_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, decoder: SeqDecoder, tied_source_embedder_key: Optional[str] = None, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super(CustomComposedSeq2Seq, self).__init__(vocab, regularizer) self._source_text_embedder = source_text_embedder self._encoder = encoder self._decoder = decoder if self._encoder.get_output_dim() != self._decoder.get_output_dim(): raise ConfigurationError(f"Encoder output dimension {self._encoder.get_output_dim()} should be" f" equal to decoder dimension {self._decoder.get_output_dim()}.") if tied_source_embedder_key: if not isinstance(self._source_text_embedder, BasicTextFieldEmbedder): raise ConfigurationError("Unable to tie embeddings," "Source text embedder is not an instance of `BasicTextFieldEmbedder`.") source_embedder = self._source_text_embedder._token_embedders[tied_source_embedder_key] if not isinstance(source_embedder, Embedding): raise ConfigurationError("Unable to tie embeddings," "Selected source embedder is not an instance of `Embedding`.") if source_embedder.get_output_dim() != self._decoder.target_embedder.get_output_dim(): raise ConfigurationError(f"Output Dimensions mismatch between" f"source embedder and target embedder.") self._source_text_embedder._token_embedders[tied_source_embedder_key] = self._decoder.target_embedder initializer(self)
Example #8
Source File: copynet.py From summarus with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, source_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, attention: Attention, beam_size: int, max_decoding_steps: int, target_embedding_dim: int = None, copy_token: str = "@COPY@", source_namespace: str = "tokens", target_namespace: str = "target_tokens", tensor_based_metric: Metric = None, token_based_metric: Metric = None, tie_embeddings: bool = False) -> None: target_embedding_dim = target_embedding_dim or source_embedder.get_output_dim() CopyNetSeq2Seq.__init__( self, vocab, source_embedder, encoder, attention, beam_size, max_decoding_steps, target_embedding_dim, copy_token, source_namespace, target_namespace, tensor_based_metric, token_based_metric ) self._tie_embeddings = tie_embeddings if self._tie_embeddings: assert source_namespace == target_namespace assert "token_embedder_tokens" in dict(self._source_embedder.named_children()) source_token_embedder = dict(self._source_embedder.named_children())["token_embedder_tokens"] self._target_embedder.weight = source_token_embedder.weight if tensor_based_metric is None: self._tensor_based_metric = None
Example #9
Source File: slqa_h.py From SLQA with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, phrase_layer: Seq2SeqEncoder, projected_layer: Seq2SeqEncoder, flow_layer: Seq2SeqEncoder, contextual_passage: Seq2SeqEncoder, contextual_question: Seq2SeqEncoder, dropout: float = 0.2, regularizer: Optional[RegularizerApplicator] = None, initializer: InitializerApplicator = InitializerApplicator(), ): super(MultiGranularityHierarchicalAttentionFusionNetworks, self).__init__(vocab, regularizer) self._text_field_embedder = text_field_embedder self._phrase_layer = phrase_layer self._encoding_dim = self._phrase_layer.get_output_dim() self.projected_layer = torch.nn.Linear(self._encoding_dim + 1024, self._encoding_dim) self.fuse = FusionLayer(self._encoding_dim) self.projected_lstm = projected_layer self.flow = flow_layer self.contextual_layer_p = contextual_passage self.contextual_layer_q = contextual_question self.linear_self_align = torch.nn.Linear(self._encoding_dim, 1) self.bilinear_layer_s = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.bilinear_layer_e = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.yesno_predictor = torch.nn.Linear(self._encoding_dim, 3) self.relu = torch.nn.ReLU() self._max_span_length = 30 self._span_start_accuracy = CategoricalAccuracy() self._span_end_accuracy = CategoricalAccuracy() self._span_accuracy = BooleanAccuracy() self._squad_metrics = SquadEmAndF1() self._span_yesno_accuracy = CategoricalAccuracy() self._official_f1 = Average() self._variational_dropout = InputVariationalDropout(dropout) self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #10
Source File: multiple_correct_mcq_multee_esim.py From multee with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, projection_feedforward: FeedForward, inference_encoder: Seq2SeqEncoder, output_feedforward: FeedForward, output_logit: FeedForward, final_feedforward: FeedForward, coverage_loss: CoverageLoss, similarity_function: SimilarityFunction = DotProductSimilarity(), dropout: float = 0.5, contextualize_pair_comparators: bool = False, pair_context_encoder: Seq2SeqEncoder = None, pair_feedforward: FeedForward = None, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super().__init__(vocab=vocab, text_field_embedder=text_field_embedder, encoder=encoder, similarity_function=similarity_function, projection_feedforward=projection_feedforward, inference_encoder=inference_encoder, output_feedforward=output_feedforward, output_logit=output_logit, final_feedforward=final_feedforward, coverage_loss=coverage_loss, contextualize_pair_comparators=contextualize_pair_comparators, pair_context_encoder=pair_context_encoder, pair_feedforward=pair_feedforward, dropout=dropout, initializer=initializer, regularizer=regularizer) self._ignore_index = -1 self._answer_loss = torch.nn.CrossEntropyLoss(ignore_index=self._ignore_index) self._coverage_loss = coverage_loss self._accuracy = CategoricalAccuracy() self._entailment_f1 = F1Measure(self._label2idx["entailment"])
Example #11
Source File: single_correct_mcq_multee_esim.py From multee with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, projection_feedforward: FeedForward, inference_encoder: Seq2SeqEncoder, output_feedforward: FeedForward, output_logit: FeedForward, final_feedforward: FeedForward, coverage_loss: CoverageLoss, similarity_function: SimilarityFunction = DotProductSimilarity(), dropout: float = 0.5, contextualize_pair_comparators: bool = False, pair_context_encoder: Seq2SeqEncoder = None, pair_feedforward: FeedForward = None, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: # Need to send it verbatim because otherwise FromParams doesn't work appropriately. super().__init__(vocab=vocab, text_field_embedder=text_field_embedder, encoder=encoder, similarity_function=similarity_function, projection_feedforward=projection_feedforward, inference_encoder=inference_encoder, output_feedforward=output_feedforward, output_logit=output_logit, final_feedforward=final_feedforward, contextualize_pair_comparators=contextualize_pair_comparators, coverage_loss=coverage_loss, pair_context_encoder=pair_context_encoder, pair_feedforward=pair_feedforward, dropout=dropout, initializer=initializer, regularizer=regularizer) self._answer_loss = torch.nn.CrossEntropyLoss() self._accuracy = CategoricalAccuracy()
Example #12
Source File: esim_comparator.py From multee with Apache License 2.0 | 5 votes |
def __init__(self, encoder: Seq2SeqEncoder, dropout: float = 0.5) -> None: super().__init__() self._encoder = encoder if dropout: self.dropout = torch.nn.Dropout(dropout) self.rnn_input_dropout = InputVariationalDropout(dropout) else: self.dropout = None self.rnn_input_dropout = None
Example #13
Source File: pico_crf_tagger.py From scibert with Apache License 2.0 | 5 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, include_start_end_transitions: bool = True, dropout: Optional[float] = None, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super().__init__(vocab, regularizer) self.label_namespace = 'labels' self.num_tags = self.vocab.get_vocab_size(self.label_namespace) # encode text self.text_field_embedder = text_field_embedder self.encoder = encoder self.dropout = torch.nn.Dropout(dropout) if dropout else None # crf output_dim = self.encoder.get_output_dim() self.tag_projection_layer = TimeDistributed(Linear(output_dim, self.num_tags)) self.crf = ConditionalRandomField(self.num_tags, constraints=None, include_start_end_transitions=include_start_end_transitions) self.metrics = { "accuracy": CategoricalAccuracy(), "accuracy3": CategoricalAccuracy(top_k=3) } for index, label in self.vocab.get_index_to_token_vocabulary(self.label_namespace).items(): self.metrics['F1_' + label] = F1Measure(positive_label=index) initializer(self)
Example #14
Source File: nlvr_direct_semantic_parser.py From allennlp-semparse with Apache License 2.0 | 5 votes |
def __init__( self, vocab: Vocabulary, sentence_embedder: TextFieldEmbedder, action_embedding_dim: int, encoder: Seq2SeqEncoder, attention: Attention, decoder_beam_search: BeamSearch, max_decoding_steps: int, dropout: float = 0.0, ) -> None: super(NlvrDirectSemanticParser, self).__init__( vocab=vocab, sentence_embedder=sentence_embedder, action_embedding_dim=action_embedding_dim, encoder=encoder, dropout=dropout, ) self._decoder_trainer = MaximumMarginalLikelihood() self._decoder_step = BasicTransitionFunction( encoder_output_dim=self._encoder.get_output_dim(), action_embedding_dim=action_embedding_dim, input_attention=attention, activation=Activation.by_name("tanh")(), add_action_bias=False, dropout=dropout, ) self._decoder_beam_search = decoder_beam_search self._max_decoding_steps = max_decoding_steps self._action_padding_index = -1
Example #15
Source File: nlvr_semantic_parser.py From allennlp-semparse with Apache License 2.0 | 5 votes |
def __init__( self, vocab: Vocabulary, sentence_embedder: TextFieldEmbedder, action_embedding_dim: int, encoder: Seq2SeqEncoder, dropout: float = 0.0, rule_namespace: str = "rule_labels", ) -> None: super(NlvrSemanticParser, self).__init__(vocab=vocab) self._sentence_embedder = sentence_embedder self._denotation_accuracy = Average() self._consistency = Average() self._encoder = encoder if dropout > 0: self._dropout = torch.nn.Dropout(p=dropout) else: self._dropout = lambda x: x self._rule_namespace = rule_namespace self._action_embedder = Embedding( num_embeddings=vocab.get_vocab_size(self._rule_namespace), embedding_dim=action_embedding_dim, ) # This is what we pass as input in the first step of decoding, when we don't have a # previous action. self._first_action_embedding = torch.nn.Parameter(torch.FloatTensor(action_embedding_dim)) torch.nn.init.normal_(self._first_action_embedding)
Example #16
Source File: simple_tagger.py From allennlp with Apache License 2.0 | 4 votes |
def __init__( self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, calculate_span_f1: bool = None, label_encoding: Optional[str] = None, label_namespace: str = "labels", verbose_metrics: bool = False, initializer: InitializerApplicator = InitializerApplicator(), **kwargs, ) -> None: super().__init__(vocab, **kwargs) self.label_namespace = label_namespace self.text_field_embedder = text_field_embedder self.num_classes = self.vocab.get_vocab_size(label_namespace) self.encoder = encoder self._verbose_metrics = verbose_metrics self.tag_projection_layer = TimeDistributed( Linear(self.encoder.get_output_dim(), self.num_classes) ) check_dimensions_match( text_field_embedder.get_output_dim(), encoder.get_input_dim(), "text field embedding dim", "encoder input dim", ) self.metrics = { "accuracy": CategoricalAccuracy(), "accuracy3": CategoricalAccuracy(top_k=3), } # We keep calculate_span_f1 as a constructor argument for API consistency with # the CrfTagger, even it is redundant in this class # (label_encoding serves the same purpose). if calculate_span_f1 is None: calculate_span_f1 = label_encoding is not None self.calculate_span_f1 = calculate_span_f1 if calculate_span_f1: if not label_encoding: raise ConfigurationError( "calculate_span_f1 is True, but no label_encoding was specified." ) self._f1_metric = SpanBasedF1Measure( vocab, tag_namespace=label_namespace, label_encoding=label_encoding ) else: self._f1_metric = None initializer(self)
Example #17
Source File: util.py From ARC-Solvers with Apache License 2.0 | 4 votes |
def embed_encode_and_aggregate_list_text_field(texts_list: Dict[str, torch.LongTensor], text_field_embedder, embeddings_dropout, encoder: Seq2SeqEncoder, aggregation_type, init_hidden_states=None): """ Given a batched list of token ids (3D) runs embeddings lookup with dropout, context encoding and aggregation on :param texts_list: List of texts :param text_field_embedder: The embedder to be used for embedding lookup :param embeddings_dropout: Dropout :param encoder: Context encoder :param aggregation_type: The type of aggregation - max, sum, avg, last :param get_last_states: If it should return the last states. :param init_hidden_states: Hidden states initialization :return: """ embedded_texts = text_field_embedder(texts_list) embedded_texts = embeddings_dropout(embedded_texts) batch_size, choices_cnt, choice_tokens_cnt, d = tuple(embedded_texts.shape) embedded_texts_flattened = embedded_texts.view([batch_size * choices_cnt, choice_tokens_cnt, -1]) # masks texts_mask_dim_3 = get_text_field_mask(texts_list).float() texts_mask_flatened = texts_mask_dim_3.view([-1, choice_tokens_cnt]) # context encoding multiple_texts_init_states = None if init_hidden_states is not None: if init_hidden_states.shape[0] == batch_size and init_hidden_states.shape[1] != choices_cnt: if init_hidden_states.shape[1] != encoder.get_output_dim(): raise ValueError("The shape of init_hidden_states is {0} but is expected to be {1} or {2}".format(str(init_hidden_states.shape), str([batch_size, encoder.get_output_dim()]), str([batch_size, choices_cnt, encoder.get_output_dim()]))) # in this case we passed only 2D tensor which is the default output from question encoder multiple_texts_init_states = init_hidden_states.unsqueeze(1).expand([batch_size, choices_cnt, encoder.get_output_dim()]).contiguous() # reshape this to match the flattedned tokens multiple_texts_init_states = multiple_texts_init_states.view([batch_size * choices_cnt, encoder.get_output_dim()]) else: multiple_texts_init_states = init_hidden_states.view([batch_size * choices_cnt, encoder.get_output_dim()]) encoded_texts_flattened = encoder(embedded_texts_flattened, texts_mask_flatened, hidden_state=multiple_texts_init_states) aggregated_choice_flattened = seq2vec_seq_aggregate(encoded_texts_flattened, texts_mask_flatened, aggregation_type, encoder, 1) # bs*ch X d aggregated_choice_flattened_reshaped = aggregated_choice_flattened.view([batch_size, choices_cnt, -1]) return aggregated_choice_flattened_reshaped
Example #18
Source File: model.py From HGL-pytorch with MIT License | 4 votes |
def __init__(self, vocab: Vocabulary, span_encoder: Seq2SeqEncoder, reasoning_encoder: Seq2SeqEncoder, input_dropout: float = 0.3, hidden_dim_maxpool: int = 1024, class_embs: bool = True, reasoning_use_obj: bool = True, reasoning_use_answer: bool = True, reasoning_use_question: bool = True, pool_reasoning: bool = True, pool_answer: bool = True, pool_question: bool = False, initializer: InitializerApplicator = InitializerApplicator(), ): super(HGL_Model, self).__init__(vocab) self.detector = SimpleDetector(pretrained=True, average_pool=True, semantic=class_embs, final_dim=512) ################################################################################################### self.rnn_input_dropout = TimeDistributed(InputVariationalDropout(input_dropout)) if input_dropout > 0 else None self.span_encoder = TimeDistributed(span_encoder) self.reasoning_encoder = TimeDistributed(reasoning_encoder) self.Graph_reasoning = Graph_reasoning(512) self.QAHG = BilinearMatrixAttention( matrix_1_dim=span_encoder.get_output_dim(), matrix_2_dim=span_encoder.get_output_dim(), ) self.VAHG = BilinearMatrixAttention( matrix_1_dim=span_encoder.get_output_dim(), matrix_2_dim=self.detector.final_dim, ) self.reasoning_use_obj = reasoning_use_obj self.reasoning_use_answer = reasoning_use_answer self.reasoning_use_question = reasoning_use_question self.pool_reasoning = pool_reasoning self.pool_answer = pool_answer self.pool_question = pool_question dim = sum([d for d, to_pool in [(reasoning_encoder.get_output_dim(), self.pool_reasoning), (span_encoder.get_output_dim(), self.pool_answer), (span_encoder.get_output_dim(), self.pool_question)] if to_pool]) self.final_mlp = torch.nn.Sequential( torch.nn.Dropout(input_dropout, inplace=False), torch.nn.Linear(dim, hidden_dim_maxpool), torch.nn.ReLU(inplace=True), torch.nn.Dropout(input_dropout, inplace=False), torch.nn.Linear(hidden_dim_maxpool, 1), ) self._accuracy = CategoricalAccuracy() self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #19
Source File: knowledge.py From OpenBookQA with Apache License 2.0 | 4 votes |
def embedd_encode_and_aggregate_list_text_field(texts_list: Dict[str, torch.LongTensor], text_field_embedder, embeddings_dropout, encoder: Seq2SeqEncoder, aggregation_type, init_hidden_states=None): embedded_texts = text_field_embedder(texts_list) embedded_texts = embeddings_dropout(embedded_texts) bs, ch_cnt, ch_tkn_cnt, d = tuple(embedded_texts.shape) embedded_texts_flattened = embedded_texts.view([bs * ch_cnt, ch_tkn_cnt, -1]) # masks texts_mask_dim_3 = get_text_field_mask(texts_list, num_wrapping_dims=1).float() texts_mask_flatened = texts_mask_dim_3.view([-1, ch_tkn_cnt]) # context encoding multiple_texts_init_states = None if init_hidden_states is not None: if init_hidden_states.shape[0] == bs and init_hidden_states.shape[1] != ch_cnt: if init_hidden_states.shape[1] != encoder.get_output_dim(): raise ValueError("The shape of init_hidden_states is {0} but is expected to be {1} or {2}".format(str(init_hidden_states.shape), str([bs, encoder.get_output_dim()]), str([bs, ch_cnt, encoder.get_output_dim()]))) # in this case we passed only 2D tensor which is the default output from question encoder multiple_texts_init_states = init_hidden_states.unsqueeze(1).expand([bs, ch_cnt, encoder.get_output_dim()]).contiguous() # reshape this to match the flattedned tokens multiple_texts_init_states = multiple_texts_init_states.view([bs * ch_cnt, encoder.get_output_dim()]) else: multiple_texts_init_states = init_hidden_states.view([bs * ch_cnt, encoder.get_output_dim()]) encoded_texts_flattened = encoder(embedded_texts_flattened, texts_mask_flatened, hidden_state=multiple_texts_init_states) aggregated_choice_flattened = seq2vec_seq_aggregate(encoded_texts_flattened, texts_mask_flatened, aggregation_type, encoder.is_bidirectional(), 1) # bs*ch X d aggregated_choice_flattened_reshaped = aggregated_choice_flattened.view([bs, ch_cnt, -1]) return aggregated_choice_flattened_reshaped
Example #20
Source File: knowledge.py From OpenBookQA with Apache License 2.0 | 4 votes |
def embed_encode_and_aggregate_list_text_field_with_feats(texts_list: Dict[str, torch.LongTensor], text_field_embedder, embeddings_dropout, encoder: Seq2SeqEncoder, aggregation_type, token_features=None, init_hidden_states=None): embedded_texts = text_field_embedder(texts_list) embedded_texts = embeddings_dropout(embedded_texts) if token_features is not None: embedded_texts = torch.cat([embedded_texts, token_features], dim=-1) bs, ch_cnt, ch_tkn_cnt, d = tuple(embedded_texts.shape) embedded_texts_flattened = embedded_texts.view([bs * ch_cnt, ch_tkn_cnt, -1]) # masks texts_mask_dim_3 = get_text_field_mask(texts_list, num_wrapping_dims=1).float() texts_mask_flatened = texts_mask_dim_3.view([-1, ch_tkn_cnt]) # context encoding multiple_texts_init_states = None if init_hidden_states is not None: if init_hidden_states.shape[0] == bs and init_hidden_states.shape[1] != ch_cnt: if init_hidden_states.shape[1] != encoder.get_output_dim(): raise ValueError("The shape of init_hidden_states is {0} but is expected to be {1} or {2}".format(str(init_hidden_states.shape), str([bs, encoder.get_output_dim()]), str([bs, ch_cnt, encoder.get_output_dim()]))) # in this case we passed only 2D tensor which is the default output from question encoder multiple_texts_init_states = init_hidden_states.unsqueeze(1).expand([bs, ch_cnt, encoder.get_output_dim()]).contiguous() # reshape this to match the flattedned tokens multiple_texts_init_states = multiple_texts_init_states.view([bs * ch_cnt, encoder.get_output_dim()]) else: multiple_texts_init_states = init_hidden_states.view([bs * ch_cnt, encoder.get_output_dim()]) encoded_texts_flattened = encoder(embedded_texts_flattened, texts_mask_flatened, hidden_state=multiple_texts_init_states) aggregated_choice_flattened = seq2vec_seq_aggregate(encoded_texts_flattened, texts_mask_flatened, aggregation_type, encoder.is_bidirectional(), 1) # bs*ch X d aggregated_choice_flattened_reshaped = aggregated_choice_flattened.view([bs, ch_cnt, -1]) return aggregated_choice_flattened_reshaped
Example #21
Source File: knowledge.py From OpenBookQA with Apache License 2.0 | 4 votes |
def embed_encode_and_aggregate_list_text_field_with_feats_only(texts_list: Dict[str, torch.LongTensor], text_field_embedder, embeddings_dropout, encoder: Seq2SeqEncoder, aggregation_type, token_features=None, init_hidden_states=None): embedded_texts = text_field_embedder(texts_list) embedded_texts = embeddings_dropout(embedded_texts) if token_features is not None: embedded_texts = torch.cat([token_features], dim=-1) bs, ch_cnt, ch_tkn_cnt, d = tuple(embedded_texts.shape) embedded_texts_flattened = embedded_texts.view([bs * ch_cnt, ch_tkn_cnt, -1]) # masks texts_mask_dim_3 = get_text_field_mask(texts_list, num_wrapping_dims=1).float() texts_mask_flatened = texts_mask_dim_3.view([-1, ch_tkn_cnt]) # context encoding multiple_texts_init_states = None if init_hidden_states is not None: if init_hidden_states.shape[0] == bs and init_hidden_states.shape[1] != ch_cnt: if init_hidden_states.shape[1] != encoder.get_output_dim(): raise ValueError("The shape of init_hidden_states is {0} but is expected to be {1} or {2}".format(str(init_hidden_states.shape), str([bs, encoder.get_output_dim()]), str([bs, ch_cnt, encoder.get_output_dim()]))) # in this case we passed only 2D tensor which is the default output from question encoder multiple_texts_init_states = init_hidden_states.unsqueeze(1).expand([bs, ch_cnt, encoder.get_output_dim()]).contiguous() # reshape this to match the flattedned tokens multiple_texts_init_states = multiple_texts_init_states.view([bs * ch_cnt, encoder.get_output_dim()]) else: multiple_texts_init_states = init_hidden_states.view([bs * ch_cnt, encoder.get_output_dim()]) encoded_texts_flattened = encoder(embedded_texts_flattened, texts_mask_flatened, hidden_state=multiple_texts_init_states) aggregated_choice_flattened = seq2vec_seq_aggregate(encoded_texts_flattened, texts_mask_flatened, aggregation_type, encoder.is_bidirectional(), 1) # bs*ch X d aggregated_choice_flattened_reshaped = aggregated_choice_flattened.view([bs, ch_cnt, -1]) return aggregated_choice_flattened_reshaped
Example #22
Source File: summarunner.py From summarus with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, source_embedder: TextFieldEmbedder, sentence_encoder: Seq2VecEncoder, sentence_accumulator: Seq2SeqEncoder, use_salience: bool, use_pos_embedding: bool, use_output_bias: bool, use_novelty: bool, dropout: float = 0.3, pos_embedding_num: int = 50, pos_embedding_size: int = 128) -> None: super(SummaRuNNer, self).__init__(vocab) self._source_embedder = source_embedder self._sentence_encoder = sentence_encoder self._se_output_dim = self._sentence_encoder.get_output_dim() self._sentence_accumulator = sentence_accumulator self._h_sentence_dim = self._sentence_accumulator.get_output_dim() self._dropout_layer = Dropout(dropout) self._content_projection_layer = Linear(self._h_sentence_dim, 1) self._use_salience = use_salience if use_salience: self._document_linear_layer = Linear(self._h_sentence_dim, self._h_sentence_dim, bias=True) self._salience_linear_layer = Linear(self._h_sentence_dim, self._h_sentence_dim, bias=False) self._use_pos_embedding = use_pos_embedding if use_pos_embedding: self._pos_embedding_num = pos_embedding_num self._pos_embedding_size = pos_embedding_size self._pos_embedding_layer = Embedding(pos_embedding_num, pos_embedding_size) self._pos_projection_layer = Linear(pos_embedding_size, 1) self._use_output_bias = use_output_bias if use_output_bias: self._output_bias = Parameter(torch.zeros(1).uniform_(-0.1,0.1), requires_grad=True) self._use_novelty = use_novelty if use_novelty: self._novelty_linear_layer = Linear(self._h_sentence_dim, self._h_sentence_dim, bias=False)
Example #23
Source File: seq2seq.py From summarus with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, source_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, max_decoding_steps: int, attention: Attention = None, beam_size: int = None, target_namespace: str = "tokens", target_embedding_dim: int = None, scheduled_sampling_ratio: float = 0., use_projection: bool = False, projection_dim: int = None, tie_embeddings: bool = False) -> None: super(Seq2Seq, self).__init__( vocab, source_embedder, encoder, max_decoding_steps, attention, None, beam_size, target_namespace, target_embedding_dim, scheduled_sampling_ratio ) use_projection = use_projection or projection_dim is not None self._tie_embeddings = tie_embeddings if self._tie_embeddings: assert "token_embedder_tokens" in dict(self._source_embedder.named_children()) source_token_embedder = dict(self._source_embedder.named_children())["token_embedder_tokens"] self._target_embedder.weight = source_token_embedder.weight num_classes = self.vocab.get_vocab_size(self._target_namespace) self._use_projection = use_projection if self._use_projection: self._projection_dim = projection_dim or self._source_embedder.get_output_dim() self._hidden_projection_layer = Linear(self._decoder_output_dim, self._projection_dim) self._output_projection_layer = Linear(self._projection_dim, num_classes) else: self._output_projection_layer = Linear(self._decoder_output_dim, num_classes) self._bleu = False
Example #24
Source File: slqa.py From SLQA with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, phrase_layer: Seq2SeqEncoder, projected_layer: Seq2SeqEncoder, contextual_passage: Seq2SeqEncoder, contextual_question: Seq2SeqEncoder, dropout: float = 0.2, regularizer: Optional[RegularizerApplicator] = None, initializer: InitializerApplicator = InitializerApplicator(), ): super(MultiGranularityHierarchicalAttentionFusionNetworks, self).__init__(vocab, regularizer) self._text_field_embedder = text_field_embedder self._phrase_layer = phrase_layer self._encoding_dim = self._phrase_layer.get_output_dim() self.projected_layer = torch.nn.Linear(self._encoding_dim + 1024, self._encoding_dim) self.fuse_p = FusionLayer(self._encoding_dim) self.fuse_q = FusionLayer(self._encoding_dim) self.fuse_s = FusionLayer(self._encoding_dim) self.projected_lstm = projected_layer self.contextual_layer_p = contextual_passage self.contextual_layer_q = contextual_question self.linear_self_align = torch.nn.Linear(self._encoding_dim, 1) # self.bilinear_self_align = BilinearSelfAlign(self._encoding_dim) # self._self_attention = LinearMatrixAttention(self._encoding_dim, self._encoding_dim, 'x,y,x*y') self._self_attention = BilinearMatrixAttention(self._encoding_dim, self._encoding_dim) self.bilinear_layer_s = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.bilinear_layer_e = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.yesno_predictor = torch.nn.Linear(self._encoding_dim, 3) self.relu = torch.nn.ReLU() self._max_span_length = 30 self._span_start_accuracy = CategoricalAccuracy() self._span_end_accuracy = CategoricalAccuracy() self._span_accuracy = BooleanAccuracy() self._squad_metrics = SquadEmAndF1() self._span_yesno_accuracy = CategoricalAccuracy() self._official_f1 = Average() self._variational_dropout = InputVariationalDropout(dropout) self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #25
Source File: seperate_slqa.py From SLQA with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, elmo_embedder: TextFieldEmbedder, tokens_embedder: TextFieldEmbedder, features_embedder: TextFieldEmbedder, phrase_layer: Seq2SeqEncoder, projected_layer: Seq2SeqEncoder, contextual_passage: Seq2SeqEncoder, contextual_question: Seq2SeqEncoder, dropout: float = 0.2, regularizer: Optional[RegularizerApplicator] = None, initializer: InitializerApplicator = InitializerApplicator(), ): super(MultiGranularityHierarchicalAttentionFusionNetworks, self).__init__(vocab, regularizer) self.elmo_embedder = elmo_embedder self.tokens_embedder = tokens_embedder self.features_embedder = features_embedder self._phrase_layer = phrase_layer self._encoding_dim = self._phrase_layer.get_output_dim() self.projected_layer = torch.nn.Linear(self._encoding_dim + 1024, self._encoding_dim) self.fuse_p = FusionLayer(self._encoding_dim) self.fuse_q = FusionLayer(self._encoding_dim) self.fuse_s = FusionLayer(self._encoding_dim) self.projected_lstm = projected_layer self.contextual_layer_p = contextual_passage self.contextual_layer_q = contextual_question self.linear_self_align = torch.nn.Linear(self._encoding_dim, 1) # self._self_attention = LinearMatrixAttention(self._encoding_dim, self._encoding_dim, 'x,y,x*y') self._self_attention = BilinearMatrixAttention(self._encoding_dim, self._encoding_dim) self.bilinear_layer_s = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.bilinear_layer_e = BilinearSeqAtt(self._encoding_dim, self._encoding_dim) self.yesno_predictor = FeedForward(self._encoding_dim, self._encoding_dim, 3) self.relu = torch.nn.ReLU() self._max_span_length = 30 self._span_start_accuracy = CategoricalAccuracy() self._span_end_accuracy = CategoricalAccuracy() self._span_accuracy = BooleanAccuracy() self._squad_metrics = SquadEmAndF1() self._span_yesno_accuracy = CategoricalAccuracy() self._official_f1 = Average() self._variational_dropout = InputVariationalDropout(dropout) self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #26
Source File: basic_classifier.py From allennlp with Apache License 2.0 | 4 votes |
def __init__( self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, seq2vec_encoder: Seq2VecEncoder, seq2seq_encoder: Seq2SeqEncoder = None, feedforward: Optional[FeedForward] = None, dropout: float = None, num_labels: int = None, label_namespace: str = "labels", namespace: str = "tokens", initializer: InitializerApplicator = InitializerApplicator(), **kwargs, ) -> None: super().__init__(vocab, **kwargs) self._text_field_embedder = text_field_embedder if seq2seq_encoder: self._seq2seq_encoder = seq2seq_encoder else: self._seq2seq_encoder = None self._seq2vec_encoder = seq2vec_encoder self._feedforward = feedforward if feedforward is not None: self._classifier_input_dim = self._feedforward.get_output_dim() else: self._classifier_input_dim = self._seq2vec_encoder.get_output_dim() if dropout: self._dropout = torch.nn.Dropout(dropout) else: self._dropout = None self._label_namespace = label_namespace self._namespace = namespace if num_labels: self._num_labels = num_labels else: self._num_labels = vocab.get_vocab_size(namespace=self._label_namespace) self._classification_layer = torch.nn.Linear(self._classifier_input_dim, self._num_labels) self._accuracy = CategoricalAccuracy() self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #27
Source File: tag_decoder.py From udify with MIT License | 4 votes |
def __init__(self, vocab: Vocabulary, task: str, encoder: Seq2SeqEncoder, label_smoothing: float = 0.0, dropout: float = 0.0, adaptive: bool = False, features: List[str] = None, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super(TagDecoder, self).__init__(vocab, regularizer) self.task = task self.encoder = encoder self.output_dim = encoder.get_output_dim() self.label_smoothing = label_smoothing self.num_classes = self.vocab.get_vocab_size(task) self.adaptive = adaptive self.features = features if features else [] self.metrics = { "acc": CategoricalAccuracy(), # "acc3": CategoricalAccuracy(top_k=3) } if self.adaptive: # TODO adaptive_cutoffs = [round(self.num_classes / 15), 3 * round(self.num_classes / 15)] self.task_output = AdaptiveLogSoftmaxWithLoss(self.output_dim, self.num_classes, cutoffs=adaptive_cutoffs, div_value=4.0) else: self.task_output = TimeDistributed(Linear(self.output_dim, self.num_classes)) self.feature_outputs = torch.nn.ModuleDict() self.features_metrics = {} for feature in self.features: self.feature_outputs[feature] = TimeDistributed(Linear(self.output_dim, vocab.get_vocab_size(feature))) self.features_metrics[feature] = { "acc": CategoricalAccuracy(), } initializer(self)
Example #28
Source File: udify_model.py From udify with MIT License | 4 votes |
def __init__(self, vocab: Vocabulary, tasks: List[str], text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, decoders: Dict[str, Model], post_encoder_embedder: TextFieldEmbedder = None, dropout: float = 0.0, word_dropout: float = 0.0, mix_embedding: int = None, layer_dropout: int = 0.0, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None) -> None: super(UdifyModel, self).__init__(vocab, regularizer) self.tasks = sorted(tasks) self.vocab = vocab self.bert_vocab = BertTokenizer.from_pretrained("config/archive/bert-base-multilingual-cased/vocab.txt").vocab self.text_field_embedder = text_field_embedder self.post_encoder_embedder = post_encoder_embedder self.shared_encoder = encoder self.word_dropout = word_dropout self.dropout = torch.nn.Dropout(p=dropout) self.decoders = torch.nn.ModuleDict(decoders) if mix_embedding: self.scalar_mix = torch.nn.ModuleDict({ task: ScalarMixWithDropout(mix_embedding, do_layer_norm=False, dropout=layer_dropout) for task in self.decoders }) else: self.scalar_mix = None self.metrics = {} for task in self.tasks: if task not in self.decoders: raise ConfigurationError(f"Task {task} has no corresponding decoder. Make sure their names match.") check_dimensions_match(text_field_embedder.get_output_dim(), encoder.get_input_dim(), "text field embedding dim", "encoder input dim") initializer(self) self._count_params()
Example #29
Source File: model.py From r2c with MIT License | 4 votes |
def __init__(self, vocab: Vocabulary, span_encoder: Seq2SeqEncoder, reasoning_encoder: Seq2SeqEncoder, input_dropout: float = 0.3, hidden_dim_maxpool: int = 1024, class_embs: bool=True, reasoning_use_obj: bool=True, reasoning_use_answer: bool=True, reasoning_use_question: bool=True, pool_reasoning: bool = True, pool_answer: bool = True, pool_question: bool = False, initializer: InitializerApplicator = InitializerApplicator(), ): super(AttentionQA, self).__init__(vocab) self.detector = SimpleDetector(pretrained=True, average_pool=True, semantic=class_embs, final_dim=512) ################################################################################################### self.rnn_input_dropout = TimeDistributed(InputVariationalDropout(input_dropout)) if input_dropout > 0 else None self.span_encoder = TimeDistributed(span_encoder) self.reasoning_encoder = TimeDistributed(reasoning_encoder) self.span_attention = BilinearMatrixAttention( matrix_1_dim=span_encoder.get_output_dim(), matrix_2_dim=span_encoder.get_output_dim(), ) self.obj_attention = BilinearMatrixAttention( matrix_1_dim=span_encoder.get_output_dim(), matrix_2_dim=self.detector.final_dim, ) self.reasoning_use_obj = reasoning_use_obj self.reasoning_use_answer = reasoning_use_answer self.reasoning_use_question = reasoning_use_question self.pool_reasoning = pool_reasoning self.pool_answer = pool_answer self.pool_question = pool_question dim = sum([d for d, to_pool in [(reasoning_encoder.get_output_dim(), self.pool_reasoning), (span_encoder.get_output_dim(), self.pool_answer), (span_encoder.get_output_dim(), self.pool_question)] if to_pool]) self.final_mlp = torch.nn.Sequential( torch.nn.Dropout(input_dropout, inplace=False), torch.nn.Linear(dim, hidden_dim_maxpool), torch.nn.ReLU(inplace=True), torch.nn.Dropout(input_dropout, inplace=False), torch.nn.Linear(hidden_dim_maxpool, 1), ) self._accuracy = CategoricalAccuracy() self._loss = torch.nn.CrossEntropyLoss() initializer(self)
Example #30
Source File: esim_pair2vec.py From pair2vec with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, encoder_keys: List[str], mask_key: str, pair2vec_config_file: str, pair2vec_model_file: str, text_field_embedder: TextFieldEmbedder, encoder: Seq2SeqEncoder, similarity_function: SimilarityFunction, projection_feedforward: FeedForward, inference_encoder: Seq2SeqEncoder, output_feedforward: FeedForward, output_logit: FeedForward, initializer: InitializerApplicator = InitializerApplicator(), dropout: float = 0.5, pair2vec_dropout: float = 0.0, bidirectional_pair2vec: bool = True, regularizer: Optional[RegularizerApplicator] = None) -> None: super().__init__(vocab, regularizer) self._vocab = vocab self.pair2vec = util.get_pair2vec(pair2vec_config_file, pair2vec_model_file) self._encoder_keys = encoder_keys self._mask_key = mask_key self._text_field_embedder = text_field_embedder self._projection_feedforward = projection_feedforward self._encoder = encoder from allennlp.modules.matrix_attention import DotProductMatrixAttention self._matrix_attention = DotProductMatrixAttention() self._inference_encoder = inference_encoder self._pair2vec_dropout = torch.nn.Dropout(pair2vec_dropout) self._bidirectional_pair2vec = bidirectional_pair2vec if dropout: self.dropout = torch.nn.Dropout(dropout) self.rnn_input_dropout = VariationalDropout(dropout) else: self.dropout = None self.rnn_input_dropout = None self._output_feedforward = output_feedforward self._output_logit = output_logit self._num_labels = vocab.get_vocab_size(namespace="labels") self._accuracy = CategoricalAccuracy() self._loss = torch.nn.CrossEntropyLoss() initializer(self)