(self,memory_bank,src_sents)
return sent_output
In this method we define sentence level representation. (This is the old version)
        :param memory_bank:
        :param encoder_final:
        :param src_sents:
        :return: sentence embeddings
In this method we define sentence level representation. (This is the old version)
        :param memory_bank:
        :param encoder_final:
        :param src_sents:
        :return: sentence embeddings
In this method we define sentence level representation . ( This is the old version ) : param memory_bank : : param encoder_final : : param src_sents : : return : sentence embeddings
def build_sentence_layer(self,memory_bank,src_sents):
        '''
        In this method we define sentence level representation. (This is the old version)
        :param memory_bank:
        :param encoder_final:
        :param src_sents:
        :return: sentence embeddings
        '''
        # print('Memory..', memory_bank.size()) # torch.Size([200, 2, 512]) TODO: this is the output
        # #
        # print ('encoder_final..',encoder_final) #


        if isinstance(memory_bank, torch.nn.utils.rnn.PackedSequence):

            memory_bank = nn.utils.rnn.pad_packed_sequence(memory_bank)[0] # as after unpack it is a tuple

        hidden = memory_bank.permute(1,0,2) # size: (2,200,512)

        # print ('in func...', src_sents)

        # in each case for the current batch, send the last hidden output as the input to the sent_lstm layer
        batch_input_list = []
        for output,sent_id in zip(hidden,src_sents): # so we have batch_size to be 1

            common_len = len(sent_id)

            output = output.unsqueeze(1)
            sent_input_list = []

            # firs id
            start_ind_sent_id = 0
            start_ind = sent_id[start_ind_sent_id]


            while (start_ind < output.size()[0]) and (start_ind_sent_id < sent_id.size()[0]):


                # add
                sent_input_list.append(output[start_ind])

                # both ids move to the next
                start_ind_sent_id += 1
                if start_ind_sent_id < sent_id.size()[0]:
                    start_ind += sent_id[start_ind_sent_id]
                else:
                    break


            # FEB 10, len check
            if len(sent_input_list) < common_len:
                # pad with zero
                pad_size = output[0].size()
                zeros = torch.zeros(pad_size, dtype=torch.float32).cuda()
                pad_list = [zeros]* (common_len-len(sent_input_list))

                sent_input_list = sent_input_list + pad_list


            sent_input = torch.cat(sent_input_list,0).unsqueeze(1) # (n_sent, batch_size=1,dim=512)


            batch_input_list.append(sent_input)



        # print ([x.size() for x in batch_input_list])
        # [torch.Size([18, 1, 512]), torch.Size([15, 1, 512]), torch.Size([18, 1, 512]), torch.Size([18, 1, 512]), torch.Size([18, 1, 512])]


        batch_input_list_concat = torch.cat(batch_input_list,1)

        # get the id of sent length:
        sent_output, (h_, c_) = self.sent_rnn(batch_input_list_concat)
        # LSTM(512, 256, bidirectional=True), sent_output has the same shape with batch_input_list_concat


        #sent_output: shape(number of sents or step, batch_size, dim) (9, 2, 512), number of sents or step can be different
        # print ('Encoder Sentence_output...',sent_output.size())
        return sent_output
def build_sentence_layer ( self , memory_bank , src_sents ) : # print('Memory..', memory_bank.size()) # torch.Size([200, 2, 512]) TODO: this is the output # # # print ('encoder_final..',encoder_final) # if isinstance ( memory_bank , torch . nn . utils . rnn . PackedSequence ) : memory_bank = nn . utils . rnn . pad_packed_sequence ( memory_bank ) [ 0 ] # as after unpack it is a tuple hidden = memory_bank . permute ( 1 , 0 , 2 ) # size: (2,200,512) # print ('in func...', src_sents) # in each case for the current batch, send the last hidden output as the input to the sent_lstm layer batch_input_list = [ ] for output , sent_id in zip ( hidden , src_sents ) : # so we have batch_size to be 1 common_len = len ( sent_id ) output = output . unsqueeze ( 1 ) sent_input_list = [ ] # firs id start_ind_sent_id = 0 start_ind = sent_id [ start_ind_sent_id ] while ( start_ind < output . size ( ) [ 0 ] ) and ( start_ind_sent_id < sent_id . size ( ) [ 0 ] ) : # add sent_input_list . append ( output [ start_ind ] ) # both ids move to the next start_ind_sent_id += 1 if start_ind_sent_id < sent_id . size ( ) [ 0 ] : start_ind += sent_id [ start_ind_sent_id ] else : break # FEB 10, len check if len ( sent_input_list ) < common_len : # pad with zero pad_size = output [ 0 ] . size ( ) zeros = torch . zeros ( pad_size , dtype = torch . float32 ) . cuda ( ) pad_list = [ zeros ] * ( common_len - len ( sent_input_list ) ) sent_input_list = sent_input_list + pad_list sent_input = torch . cat ( sent_input_list , 0 ) . unsqueeze ( 1 ) # (n_sent, batch_size=1,dim=512) batch_input_list . append ( sent_input ) # print ([x.size() for x in batch_input_list]) # [torch.Size([18, 1, 512]), torch.Size([15, 1, 512]), torch.Size([18, 1, 512]), torch.Size([18, 1, 512]), torch.Size([18, 1, 512])] batch_input_list_concat = torch . cat ( batch_input_list , 1 ) # get the id of sent length: sent_output , ( h_ , c_ ) = self . sent_rnn ( batch_input_list_concat ) # LSTM(512, 256, bidirectional=True), sent_output has the same shape with batch_input_list_concat #sent_output: shape(number of sents or step, batch_size, dim) (9, 2, 512), number of sents or step can be different # print ('Encoder Sentence_output...',sent_output.size()) return sent_output