def forward( self, # type: ignore source_tokens: [str, torch.LongTensor], target_tokens: [str, torch.LongTensor] = None, ending_words: [str, torch.LongTensor] = None, batch_idx: int = None, decoder_hidden: torch.FloatTensor = None, decoder_context: torch.FloatTensor = None, ending_words_mask: [str, torch.Tensor] = None, hier_mode: bool = False) -> Dict[str, torch.Tensor]: # pylint: disable=arguments-differ """ Decoder logic for producing the entire target sequence. Parameters ---------- source_tokens : Dict[str, torch.LongTensor] The output of ``TextField.as_array()`` applied on the source ``TextField``. This will be passed through a ``TextFieldEmbedder`` and then through an encoder. target_tokens : Dict[str, torch.LongTensor], optional (default = None) Output of ``Textfield.as_array()`` applied on target ``TextField``. We assume that the target tokens are also represented as a ``TextField``. """ # pdb.set_trace() # source_mask = utils.get_text_field_mask(source_tokens) source_mask = get_text_field_mask(source_tokens) embedded_input = self._target_embedder(source_tokens["tokens"]) targets = target_tokens["tokens"] # target_mask = util.get_text_field_mask(target_tokens) target_mask = get_text_field_mask(target_tokens) batch_size, time_steps, _ = embedded_input.size() embedded_ending_words = self._context_embedder(target_tokens["tokens"]) # pdb.set_trace() # Apply dropout to embeddings # embedded_input = self._dropout(embedded_input) embedded_input = self._lockdropout(x=embedded_input, dropout=self._dropout_ratio) if self._sampling_scheme == "first_word": if ((not self.training) and np.random.rand() < 0.05): i2v = self.vocab ending_words_idx = ending_words["tokens"][0].data.cpu().numpy() ending_words_str = '|'.join( [i2v[int(word)] for word in ending_words_idx]) print("Ending word sequence : ", ending_words_str) start_token_idx = source_tokens["tokens"][0][0] self._sample(start_token_idx=start_token_idx, ending_words=ending_words["tokens"][0], debug=True) if decoder_hidden is None: (decoder_hidden, decoder_context) = self.initialize_hidden(batch_size=batch_size) hiddens = [] contexts = [] p_gens = [] for t, emb_t in enumerate(embedded_input.chunk(time_steps, dim=1)): decoder_hidden, decoder_context = self._lm_cell( emb_t.squeeze(1), (decoder_hidden, decoder_context)) hiddens.append(decoder_hidden.unsqueeze(1)) contexts.append(decoder_context.unsqueeze(1)) hidden = torch.cat(hiddens, 1) # context = torch.cat(contexts, 1) output = self._lockdropout(x=hidden, dropout=self._dropout_ratio) batch_size = output.size(0) seq_len = output.size(1) hidden_dim = output.size(2) pre_decoded_output = self._intermediate_projection_layer( output.view(batch_size * seq_len, hidden_dim)) decoded_output = self._output_projection_layer(pre_decoded_output) logits = decoded_output.view(batch_size, seq_len, decoded_output.size(1)) class_probabilities = F.softmax(logits, dim=-1) _, predicted_classes = torch.max(class_probabilities, dim=-1) output_dict = { "logits": logits, "class_probabilities": class_probabilities, "predictions": predicted_classes } # This code block masks all line endings (ending words) if not self.training and hier_mode == True: # pdb.set_trace() tmp_mask = (1 - ending_words_mask["tokens"]) target_mask = target_mask.long() * tmp_mask loss = self._get_loss_custom(logits, targets, target_mask, training=self.training) output_dict["loss"] = loss target_mask = get_text_field_mask(target_tokens) source_sentence_lengths = get_lengths_from_binary_sequence_mask( mask=source_mask) target_sentence_lengths = get_lengths_from_binary_sequence_mask( mask=target_mask) output_dict["source_sentence_lengths"] = source_sentence_lengths output_dict["target_sentence_lengths"] = target_sentence_lengths # if self.training: decoder_hidden = [] decoder_context = [] for idx, length in enumerate(source_sentence_lengths): assert source_sentence_lengths[idx] == target_sentence_lengths[ idx], "Mis-match!" decoder_hidden.append(hiddens[length - 1][idx].squeeze(0)) decoder_context.append(contexts[length - 1][idx].squeeze(0)) output_dict["decoder_hidden"] = decoder_hidden output_dict["decoder_context"] = decoder_context return output_dict
def get_state(self, position): i = position[0] j = position[1] nuc = {"A": 1, "T": 2, "C": 3, "G": 4, "-": 5, "*": 6} state = [] if i != len(self.env) - 1: if j < len(self.env[i]) - 1: if j == 0: state = [ nuc[self.env[i][j]], nuc[self.env[i][j + 1]], nuc[self.env[i][j + 2]], nuc[self.env[i + 1][j]], nuc[self.env[i + 1][j + 1]], nuc[self.env[i + 1][j + 2]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) else: state = [ nuc[self.env[i][j - 1]], nuc[self.env[i][j]], nuc[self.env[i][j + 1]], nuc[self.env[i + 1][j - 1]], nuc[self.env[i + 1][j]], nuc[self.env[i + 1][j + 1]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) else: state = [ nuc[self.env[i][j - 2]], nuc[self.env[i][j - 1]], nuc[self.env[i][j]], nuc[self.env[i + 1][j - 2]], nuc[self.env[i + 1][j - 1]], nuc[self.env[i + 1][j]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) elif i == len(self.env) - 1: if j < len(self.env[i]) - 1: if j == 0: state = [ nuc[self.env[i][j]], nuc[self.env[i][j + 1]], nuc[self.env[i][j + 2]], nuc[self.env[i - 1][j]], nuc[self.env[i - 1][j + 1]], nuc[self.env[i - 1][j + 2]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) else: state = [ nuc[self.env[i][j - 1]], nuc[self.env[i][j]], nuc[self.env[i][j + 1]], nuc[self.env[i - 1][j - 1]], nuc[self.env[i - 1][j]], nuc[self.env[i - 1][j + 1]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) else: state = [ nuc[self.env[i][j - 2]], nuc[self.env[i][j - 1]], nuc[self.env[i][j]], nuc[self.env[i - 1][j - 2]], nuc[self.env[i - 1][j - 1]], nuc[self.env[i - 1][j]], ] this = self.reward(False) state.append(this) state = FloatTensor(np.reshape(state, [1, 7])) return state