I have question,answer pair data. After embedding and lstm, I want to calculate similarity between question and answer by hidden and embedding.
My data is pad to max_length which is 100. I want to select non padding term for calculating.
    qmask = (torch.zeros_like(q) != q).float()
    amask = (torch.zeros_like(a) != a).float()


    q_embed = self.drop(self.encoder(q))
    q = q_embed.permute(1, 0, 2)
    q_embed = q_embed.masked_select(
        qmask.unsqueeze(dim=2).expand_as(q_embed)
    )
    print('q_embed', q_embed.size())
    q_lstm_out, _ = self.bilstm(q, self.hidden)
    print('q_lstm_out', q_lstm_out.shape)
    q_lstm_out = q_lstm_out.masked_select(
        qmask.transpose(0, 1).unsqueeze(dim=2).expand_as(q_lstm_out)
    )

    a_embed = self.drop(self.encoder(a))
    a = a_embed.permute(1, 0, 2)
    a_embed = a_embed.masked_select(
        amask.unsqueeze(dim=2).expand_as(a_embed)
    )
    a_lstm_out, _ = self.bilstm(a, self.hidden)
    a_lstm_out = a_lstm_out.masked_select(
        amask.transpose(0, 1).unsqueeze(dim=2).expand_as(a_lstm_out)
    )

    m1 = torch.matmul(q_embed.transpose(1, 2).contiguous(), a_embed)  # seq_length * embedding * embedding
    m2 = torch.matmul(torch.matmul(torch.t(q_lstm_out).transpose(1, 2).contiguous(), self.A), a_lstm_out.transpose(0, 1).contiguous())
    concanated = self.drop(torch.stack((m1, m2), dim=1))

I use masked_select return 1D tensor. and it cann’t view back.