(self, src, src_sents=None, lengths=None)
return final_encoder_final, final_memory_bank, final_sent_output
New Forward`
New Forward`
New Forward
def forward_new(self, src, src_sents=None, lengths=None):
        "New Forward`"
        self._check_args(src, lengths)

        emb = self.embeddings(src)

        s_len, batch, emb_dim = emb.size() # (185 16 128), s_len is sequence_len.

        # 2333 TODO: change starts here


        # Feb15: we break this into sentences

        # iterate each batch..
        input_embeddings=emb.permute(1,0,2)

        final_memory_bank = []
        final_encoder_final = []

        final_sent_output = []

        for batch_id in range(batch):

            # this is the input to word-level lstm
            current_sequence = input_embeddings[batch_id] # size id (sequence_len, emb_dim)
            # break this into multiple sentences according to the sentence lengths, and input to the rnn
            # sent len check, define len_sequence to be: tensor([26, 17, 21, 23, 19, 26, 10, 42], device='cuda:0')
            if torch.sum(src_sents[batch_id]) >= s_len:
                # if exceeds the total length, then their is a bug
                len_sequence = src_sents[batch_id][:-1]
            else:
                len_sequence = src_sents[batch_id]

            counter = 0


            feeding_as_a_batch = []
            lengths_as_a_batch = []
            lengths = []
            actual_len = 0
            for idx in len_sequence:
                if (counter < s_len ) and (idx != 0):
                    actual_len += 1
                    # from the current_sequence, add to the rnn
                    feeding_sequence = current_sequence[counter:counter+idx].unsqueeze(0)
                    feeding_as_a_batch.append(feeding_sequence.permute(1,0,2)) #feeding_sequence size = [1,26,128]
                    counter += idx
                    # feed into rnn
                lengths_as_a_batch.append(idx)

            feeding_as_a_batch_padded = torch.cat([x for x in pad_sequence(feeding_as_a_batch,batch_first=True)],1)
            # feed into rnn size: torch.Size([42, 9, 128]) -> [max, batch_size, dim]
            max_dim = feeding_as_a_batch_padded.size()[0]
            lengths_as_a_batch = [max_dim for x in range(actual_len)]
            # lengths_as_a_batch = [item for sublist in lengths_as_a_batch for item in sublist]

            if lengths_as_a_batch is not None and not self.no_pack_padded_seq:
                # Lengths data is wrapped inside a Tensor.
                packed_emb_rnn_input = pack(feeding_as_a_batch_padded, lengths_as_a_batch)


            # feed into!
            memory_bank, encoder_final = self.rnn(packed_emb_rnn_input)

            # feed into sentence_level
            sent_output = self.sent_level_encoder(memory_bank)
            final_sent_output.append(sent_output.view(-1,4*emb_dim))


            if lengths is not None and not self.no_pack_padded_seq:
                memory_bank = unpack(memory_bank)[0]


            # we need to get the original output, before padded
            revised_memory_bank = memory_bank.permute(1, 0, 2)
            memory_bank_unpadded_list = []



            for idx in range(actual_len):
                memory_bank_unpadded_list.append(revised_memory_bank[idx][:len_sequence[idx]])
            unpadded_memory_bank = torch.cat(memory_bank_unpadded_list,0) # size is [sequence_len,512] # need to pad or truncate
            actual_size = unpadded_memory_bank.size()[0]
            if actual_size >= s_len:
                padded_memory_bank = unpadded_memory_bank[:s_len]
                # print ('Size is okk..', padded_memory_bank.size())
            else:
                # pad with zero
                pad_size = s_len - actual_size
                padded_memory_bank = F.pad(unpadded_memory_bank, (0,0,0,pad_size), 'constant',0.0)
                # print ('Padded...',unpadded_memory_bank.size(),pad_size,padded_memory_bank.size())
            # print (actual_size,s_len,padded_memory_bank.size())
            final_memory_bank.append(padded_memory_bank.unsqueeze(1))
            # finish processing on memory bank


            if self.use_bridge:
                encoder_final = self._bridge(encoder_final)

            final_encoder_final.append(tuple([x[:,-1,:].unsqueeze(1) for x in encoder_final]))

        # add unpacked from final_memory_bank
        final_memory_bank = torch.cat(final_memory_bank,1) # [200, 2, 512], ready to return


        # join the encoder_final
        hs = []
        cs = []
        for (h,c) in final_encoder_final:
            hs.append(h)
            cs.append(c)
        hs = torch.cat(hs,1)
        cs = torch.cat(cs,1)
        # encoder_final
        final_encoder_final = tuple([hs,cs]) # ready to return

        # sent output
        final_sent_output = pad_sequence(final_sent_output)  # size [9,2,512], ready to return

        # 2333 TODO: change finish here

        # import pdb;pdb.set_trace()

        return final_encoder_final, final_memory_bank, final_sent_output
def forward_new ( self , src , src_sents = None , lengths = None ) : self . _check_args ( src , lengths ) emb = self . embeddings ( src ) s_len , batch , emb_dim = emb . size ( ) # (185 16 128), s_len is sequence_len. # 2333 TODO: change starts here # Feb15: we break this into sentences # iterate each batch.. input_embeddings = emb . permute ( 1 , 0 , 2 ) final_memory_bank = [ ] final_encoder_final = [ ] final_sent_output = [ ] for batch_id in range ( batch ) : # this is the input to word-level lstm current_sequence = input_embeddings [ batch_id ] # size id (sequence_len, emb_dim) # break this into multiple sentences according to the sentence lengths, and input to the rnn # sent len check, define len_sequence to be: tensor([26, 17, 21, 23, 19, 26, 10, 42], device='cuda:0') if torch . sum ( src_sents [ batch_id ] ) >= s_len : # if exceeds the total length, then their is a bug len_sequence = src_sents [ batch_id ] [ : - 1 ] else : len_sequence = src_sents [ batch_id ] counter = 0 feeding_as_a_batch = [ ] lengths_as_a_batch = [ ] lengths = [ ] actual_len = 0 for idx in len_sequence : if ( counter < s_len ) and ( idx != 0 ) : actual_len += 1 # from the current_sequence, add to the rnn feeding_sequence = current_sequence [ counter : counter + idx ] . unsqueeze ( 0 ) feeding_as_a_batch . append ( feeding_sequence . permute ( 1 , 0 , 2 ) ) #feeding_sequence size = [1,26,128] counter += idx # feed into rnn lengths_as_a_batch . append ( idx ) feeding_as_a_batch_padded = torch . cat ( [ x for x in pad_sequence ( feeding_as_a_batch , batch_first = True ) ] , 1 ) # feed into rnn size: torch.Size([42, 9, 128]) -> [max, batch_size, dim] max_dim = feeding_as_a_batch_padded . size ( ) [ 0 ] lengths_as_a_batch = [ max_dim for x in range ( actual_len ) ] # lengths_as_a_batch = [item for sublist in lengths_as_a_batch for item in sublist] if lengths_as_a_batch is not None and not self . no_pack_padded_seq : # Lengths data is wrapped inside a Tensor. packed_emb_rnn_input = pack ( feeding_as_a_batch_padded , lengths_as_a_batch ) # feed into! memory_bank , encoder_final = self . rnn ( packed_emb_rnn_input ) # feed into sentence_level sent_output = self . sent_level_encoder ( memory_bank ) final_sent_output . append ( sent_output . view ( - 1 , 4 * emb_dim ) ) if lengths is not None and not self . no_pack_padded_seq : memory_bank = unpack ( memory_bank ) [ 0 ] # we need to get the original output, before padded revised_memory_bank = memory_bank . permute ( 1 , 0 , 2 ) memory_bank_unpadded_list = [ ] for idx in range ( actual_len ) : memory_bank_unpadded_list . append ( revised_memory_bank [ idx ] [ : len_sequence [ idx ] ] ) unpadded_memory_bank = torch . cat ( memory_bank_unpadded_list , 0 ) # size is [sequence_len,512] # need to pad or truncate actual_size = unpadded_memory_bank . size ( ) [ 0 ] if actual_size >= s_len : padded_memory_bank = unpadded_memory_bank [ : s_len ] # print ('Size is okk..', padded_memory_bank.size()) else : # pad with zero pad_size = s_len - actual_size padded_memory_bank = F . pad ( unpadded_memory_bank , ( 0 , 0 , 0 , pad_size ) , 'constant' , 0.0 ) # print ('Padded...',unpadded_memory_bank.size(),pad_size,padded_memory_bank.size()) # print (actual_size,s_len,padded_memory_bank.size()) final_memory_bank . append ( padded_memory_bank . unsqueeze ( 1 ) ) # finish processing on memory bank if self . use_bridge : encoder_final = self . _bridge ( encoder_final ) final_encoder_final . append ( tuple ( [ x [ : , - 1 , : ] . unsqueeze ( 1 ) for x in encoder_final ] ) ) # add unpacked from final_memory_bank final_memory_bank = torch . cat ( final_memory_bank , 1 ) # [200, 2, 512], ready to return # join the encoder_final hs = [ ] cs = [ ] for ( h , c ) in final_encoder_final : hs . append ( h ) cs . append ( c ) hs = torch . cat ( hs , 1 ) cs = torch . cat ( cs , 1 ) # encoder_final final_encoder_final = tuple ( [ hs , cs ] ) # ready to return # sent output final_sent_output = pad_sequence ( final_sent_output ) # size [9,2,512], ready to return # 2333 TODO: change finish here # import pdb;pdb.set_trace() return final_encoder_final , final_memory_bank , final_sent_output