(self,sent_encoder,sent_decoder,src_sents, input_step)
return mmr_among_words
# sent_encoder: size (sent_len=9,batch=2,dim=512)
        # sent_decoder: size (sent_len=1,batch=2,dim=512)
        # src_sents: size (batch=2,sent_len=9)
        function to calculate mmr
        :param sent_encoder:
        :param sent_decoder:
        :param src_sents:
        :return:
# sent_encoder: size (sent_len=9,batch=2,dim=512)
        # sent_decoder: size (sent_len=1,batch=2,dim=512)
        # src_sents: size (batch=2,sent_len=9)
        function to calculate mmr
        :param sent_encoder:
        :param sent_decoder:
        :param src_sents:
        :return:
# sent_encoder : size ( sent_len = 9 batch = 2 dim = 512 ) # sent_decoder : size ( sent_len = 1 batch = 2 dim = 512 ) # src_sents : size ( batch = 2 sent_len = 9 ) function to calculate mmr : param sent_encoder : : param sent_decoder : : param src_sents : : return :
def _run_mmr(self,sent_encoder,sent_decoder,src_sents, input_step):
        '''
        # sent_encoder: size (sent_len=9,batch=2,dim=512)
        # sent_decoder: size (sent_len=1,batch=2,dim=512)
        # src_sents: size (batch=2,sent_len=9)
        function to calculate mmr
        :param sent_encoder:
        :param sent_decoder:
        :param src_sents:
        :return:
        '''
        pdist = nn.PairwiseDistance(p=2)
        sent_decoder=sent_decoder.permute(1,0,2) # (2,1,512)

        scores =[]
        # define sent matrix and current vector distance as the Euclidean distance
        for sent in sent_encoder: # iterate over each batch sample
            # distance: https://pytorch.org/docs/stable/_modules/torch/nn/modules/distance.html

            # import pdb;
            # pdb.set_trace()

            # sim1=torch.sum(pdist(sent_encoder.permute(1,0,2),sent.unsqueeze(1)),1).unsqueeze(1)  # -> this is sim2 on my equation, note this is distance!

            sim1 = 1 - torch.mean(pdist(sent_encoder.permute(1, 0, 2), sent.unsqueeze(1)), 1).unsqueeze(1) # this is a similarity function
            # sim1 shape: (batch_size,1)

            sim2=torch.bmm(self.mmr_W(sent_decoder),sent.unsqueeze(2)).squeeze(2) # (2,1) -> this is sim1 on my equation

            # scores.append(sim1-sim2)
            scores.append(sim2 - sim1)


        sent_ranking_att = torch.t(torch.cat(scores,1)) #(sent_len=9,batch_size)
        sent_ranking_att = torch.softmax(sent_ranking_att, dim=0).permute(1,0)  #(sent_len=9,batch_size)
        # scores is a list of score (sent_len=9, tensor shape (batch_size, 1))
        mmr_among_words = [] # should be (batch=2,input_step=200)
        for batch_id in range(sent_ranking_att.size()[0]):
            # iterate each batch, create zero weight on the input steps
            # mmr= torch.zeros([input_step], dtype=torch.float32).cuda()

            tmp = []
            for id,position in enumerate(src_sents[batch_id]):

                for x in range(position):
                    tmp.append(sent_ranking_att[batch_id][id])


            mmr = torch.stack(tmp) # make to 1-d


            if len(mmr) < input_step: # pad with 0
                tmp = torch.zeros(input_step - len(mmr)).float().cuda()
                # for x in range(input_step-len(mmr)):
                mmr = torch.cat((mmr, tmp), 0)
            else:
                mmr = mmr[:input_step]

            mmr_among_words.append(mmr.unsqueeze(0))

        mmr_among_words = torch.cat(mmr_among_words,0)

        # shape: (batch=2, input_step=200)

        return mmr_among_words
def _run_mmr ( self , sent_encoder , sent_decoder , src_sents , input_step ) : pdist = nn . PairwiseDistance ( p = 2 ) sent_decoder = sent_decoder . permute ( 1 , 0 , 2 ) # (2,1,512) scores = [ ] # define sent matrix and current vector distance as the Euclidean distance for sent in sent_encoder : # iterate over each batch sample # distance: https://pytorch.org/docs/stable/_modules/torch/nn/modules/distance.html # import pdb; # pdb.set_trace() # sim1=torch.sum(pdist(sent_encoder.permute(1,0,2),sent.unsqueeze(1)),1).unsqueeze(1)  # -> this is sim2 on my equation, note this is distance! sim1 = 1 - torch . mean ( pdist ( sent_encoder . permute ( 1 , 0 , 2 ) , sent . unsqueeze ( 1 ) ) , 1 ) . unsqueeze ( 1 ) # this is a similarity function # sim1 shape: (batch_size,1) sim2 = torch . bmm ( self . mmr_W ( sent_decoder ) , sent . unsqueeze ( 2 ) ) . squeeze ( 2 ) # (2,1) -> this is sim1 on my equation # scores.append(sim1-sim2) scores . append ( sim2 - sim1 ) sent_ranking_att = torch . t ( torch . cat ( scores , 1 ) ) #(sent_len=9,batch_size) sent_ranking_att = torch . softmax ( sent_ranking_att , dim = 0 ) . permute ( 1 , 0 ) #(sent_len=9,batch_size) # scores is a list of score (sent_len=9, tensor shape (batch_size, 1)) mmr_among_words = [ ] # should be (batch=2,input_step=200) for batch_id in range ( sent_ranking_att . size ( ) [ 0 ] ) : # iterate each batch, create zero weight on the input steps # mmr= torch.zeros([input_step], dtype=torch.float32).cuda() tmp = [ ] for id , position in enumerate ( src_sents [ batch_id ] ) : for x in range ( position ) : tmp . append ( sent_ranking_att [ batch_id ] [ id ] ) mmr = torch . stack ( tmp ) # make to 1-d if len ( mmr ) < input_step : # pad with 0 tmp = torch . zeros ( input_step - len ( mmr ) ) . float ( ) . cuda ( ) # for x in range(input_step-len(mmr)): mmr = torch . cat ( ( mmr , tmp ) , 0 ) else : mmr = mmr [ : input_step ] mmr_among_words . append ( mmr . unsqueeze ( 0 ) ) mmr_among_words = torch . cat ( mmr_among_words , 0 ) # shape: (batch=2, input_step=200) return mmr_among_words