Example #1
0
 def forward(self, input, hidden_state):
     f_output, h_output = self.gru(input, hidden_state)
     output = self.weight(f_output)
     output = self.context(output).squeeze().permute(1, 0)
     output = F.softmax(output, dim=-1)
     output = element_wise_mul(f_output, output.permute(1, 0)).squeeze(0)
     output = self.fc(output)
     return output, h_output
Example #2
0
    def forward(self, x, hidden_state):
        # output = self.attention_model(x,hidden_state)
        # return output 

        f_output, h_output = self.gru(x.float(),hidden_state)
        output = matrix_mul(f_output, self.sent_weight, self.sent_bias)        
        output = matrix_mul(output, self.context_weight).permute(1,0)
        output = F.softmax(output)
        output = element_wise_mul(f_output,output.permute(1,0))
        return output,h_output
Example #3
0
    def forward(self, x, hidden_state):

        f_output, h_output = self.gru(x, hidden_state)
        output = matrix_mul(f_output, self.comment_weight, self.comment_bias)
        output = matrix_mul(output, self.context_weight).permute(1, 0)
        output = F.softmax(output)
        output = element_wise_mul(f_output, output.permute(1, 0)).squeeze(0)
        output = self.fc(output)

        return output, h_output
Example #4
0
  def forward(self, input, hidden_state):

    output = self.lookup(input)
    # feature output and hidden state output
    f_output, h_output = self.gru(output.float(), hidden_state)
    output = matrix_mul(f_output, self.word_weight, self.word_bias)
    output = matrix_mul(output, self.context_weight).permute(1, 0)
    output = F.softmax(output, dim=-1)
    output = element_wise_mul(f_output, output.permute(1, 0))

    return output, h_output
Example #5
0
    def forward(self, input, hidden_state):

        f_output, h_output = self.gru(input, hidden_state)
        output = matrix_mul(f_output, self.sent_weight, self.sent_bias)
        output_2 = matrix_mul(output, self.context_weight).permute(1, 0)
        if torch.isnan(output_2[0][0]):
            print('nan detected')
            return None

        output = F.softmax(output_2, dim=-1)
        output = element_wise_mul(f_output, output.permute(1, 0)).squeeze(0)
        output = self.dropout(self.fc(output))

        return output, h_output
 def forward(self, input, hidden_state,
             doc_len):  # input [Doc_len,batch_size,2*word_hidden_size]
     mask = torch.arange(input.shape[0]).expand(
         len(doc_len), input.shape[0]).to(
             self.device) < doc_len.unsqueeze(1)
     f_output, h_output = self.gru(
         input,
         hidden_state)  # f_output [Doc_len,batch_size,2*sent_hidden_size]
     output = self.sent_weight(f_output)
     output = self.context_weight(output).squeeze().permute((1, 0))
     #output = mat_mul(f_output,self.sent_weight,self.sent_bias) # out_put [Doc_len,batch_size,2*hidden_size]
     #output = mat_mul(output,self.context_weight).permute((1,0)) # [batch_size,Doc_len]
     exps = torch.exp(output) * mask.float()
     output = exps / (torch.sum(exps, dim=1).unsqueeze(1)
                      )  #[batch_size,Doc_len]
     #output[~mask] = float('-inf')
     #output = F.softmax(output,dim=1) #[batch_size,Doc_len]
     output = element_wise_mul(f_output, output.permute(
         (1, 0))).squeeze(0)  # [batch_size,2*sent_hidden_size]
     output = self.fc(output)  # [batch_size,num_classes]
     return output, h_output
Example #7
0
    def forward(self, input, hidden_state, sent_len):
        mask = torch.arange(input.shape[0]).expand(
            len(sent_len), input.shape[0]).to(
                self.device) < sent_len.unsqueeze(1)
        output = self.lookup(input)  #[Seq_len,batch_size,emb_size]
        f_output, h_output = self.gru(
            output.float(),
            hidden_state)  # f_output [Seq_len,batch_size,2*hidden_size]
        output = self.word_weight(f_output)
        output = self.context_weight(output).squeeze().permute((1, 0))
        #output = mat_mul(f_output,self.word_weight,self.word_bias) # out_put [Seq_len,batch_size,2*hidden_size]
        #output = mat_mul(output,self.context_weight).permute((1,0)) # [batch_size,Seq_len]
        #print(torch.isnan(output).any())
        exps = torch.exp(output) * mask.float()
        output = exps / (torch.sum(exps, dim=1).unsqueeze(1) + 1e-8
                         )  #[batch_size,Seq_len]
        #output[~mask] = float('-inf')
        #output = F.softmax(output,dim=1) #[batch_size,Seq_len]
        output = element_wise_mul(f_output, output.permute(
            (1, 0)))  # [1,batch_size,2*hidden_size]

        return output, h_output