ranker.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:deeplearning4chatbot 作者: liangjz92 项目源码 文件源码
def test2vec(self,history):
        #?????????????
        #???????????
        history_inputs =[]
        candidate_inputs =[]
        if (history ==None) or len(history)==0 : #??????
            return None,None
        #print(history)
        candidate_size = len(history[1])
        #print('candidate_size',candidate_size)
        cache = []
        for j in range(self.max_dialogue_size): #????????????????
            if j< len(history):
                encoder_pad = [data_utils.PAD_ID]*(self.max_sentence_size-len(history[j][0]))   #0??????
                cache.append(list(reversed(history[j][0]+encoder_pad))) #????
            else:
                cache.append(list([data_utils.PAD_ID]*self.max_sentence_size))
        history_inputs = cache
        #print(history_inputs)
        true_cache =[]
        for i in range(self.max_dialogue_size):   #candidate part
            if i %2==0: #?0,2,4,..??????
                continue
            if i<len(history):  #????????
                for j in range(candidate_size):
                    true_pad = [data_utils.PAD_ID]*(self.max_sentence_size-len(history[i][j]))
                    true_cache.append(list(reversed(history[i][j] + true_pad)))# true candidate
            else:
                for j in range(candidate_size):
                    true_cache.append(list([data_utils.PAD_ID]*self.max_sentence_size))
            candidate_inputs.append(true_cache)
            true_cache =[]


        ######################################################
        batch_history, batch_candidate = [], []

        for sent_index in range(self.max_dialogue_size):
            history_cache = []
            for length_index in range(self.max_sentence_size):
                history_cache.append(np.array( [history_inputs[sent_index][length_index]]))
            batch_history.append(history_cache)

            if sent_index % 2 != 0:
                candidate_cache = []
                for length_index in range(self.max_sentence_size):
                    candidate_cache.append(np.array([candidate_inputs[int(sent_index/2)][batch_index][length_index] for batch_index in range(candidate_size)]))
                batch_candidate.append(candidate_cache)
        return batch_history, batch_candidate

############################################################################
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号