def doc_to_tensor(self, cand_names, get_doc_id=None, hierarchical=False): """ convert the documents on natural language to tensor :param get_doc_id: function for getting documents id by names :param hierarchical: :param cand_names: :return: """ if get_doc_id is None: get_doc_id = self.doc_reader.get_doc_id if hierarchical: cand_docs_id_tensor = [] for name in cand_names: # padding sentences cur_doc_id = get_doc_id(name, hierarchical) cur_doc_id_array = pad_sequences(cur_doc_id, maxlen=self._max_sent_length, padding='post', value=Vocabulary.PAD_IDX) cur_doc_id_tensor = to_long_tensor(cur_doc_id_array) # padding to sentences number if cur_doc_id_tensor.size()[0] > self._max_sent_num: cur_doc_id_tensor_pad = cur_doc_id_tensor[:self. _max_sent_num, :] else: padding_tensor = torch.zeros( self._max_sent_num - cur_doc_id_tensor.shape[0], self._max_sent_length).long() cur_doc_id_tensor_pad = torch.cat( [cur_doc_id_tensor, padding_tensor], dim=0) cand_docs_id_tensor.append(cur_doc_id_tensor_pad) cand_docs_id_tensor = torch.stack(cand_docs_id_tensor, dim=0) else: cand_docs_id = list( map(lambda x: get_doc_id(x, hierarchical), cand_names)) cand_docs_id_array = pad_sequences(cand_docs_id, maxlen=self._max_doc_length, padding='post', value=Vocabulary.PAD_IDX) cand_docs_id_tensor = to_long_tensor(cand_docs_id_array) return cand_docs_id_tensor
def mrcqa(nlp, context, question, model, dataset, metadata, global_config, device): # preprocess context_doc = DocText(nlp, context, global_config['preprocess']) question_doc = DocText(nlp, question, global_config['preprocess']) context_doc.update_em(question_doc) question_doc.update_em(context_doc) context_token = context_doc.token question_token = question_doc.token context_id_char = to_long_tensor(dataset.sentence_char2id(context_token)) question_id_char = to_long_tensor(dataset.sentence_char2id(question_token)) context_id, context_f = context_doc.to_id(metadata) question_id, question_f = question_doc.to_id(metadata) bat_input = [ context_id, question_id, context_id_char, question_id_char, context_f, question_f ] bat_input = [ x.unsqueeze(0).to(device) if x is not None else x for x in bat_input ] out_ans_prop, out_ans_range, vis_param = model.forward(*bat_input) out_ans_range = out_ans_range.cpu().data.numpy() start = out_ans_range[0][0] end = out_ans_range[0][1] + 1 out_answer_id = context_id[start:end] out_answer = dataset.sentence_id2word(out_answer_id) space = context_doc.right_space[start:end] result_answer = '' for i, token in enumerate(out_answer): if space[i]: result_answer += (token + ' ') else: result_answer += token return result_answer.strip()
def qa_to_tensor(self, qa, padding=True): qa_split = Tokenizer.lower_tokenize(qa) qa_id = self.vocab.sentence_to_id(qa_split) # padding qa_id = qa_id[:self._max_qa_length] if len( qa_id) > self._max_qa_length else qa_id if padding: qa_id = qa_id + [ 0 for _ in range(self._max_qa_length - len(qa_id)) ] qa_tensor = to_long_tensor(qa_id) return qa_tensor
def _encode_sample(self, idx): batch_states = [] batch_actions = [] batch_rewards = [] batch_next_states = [] batch_terminals = [] for i in idx: data = self.buffer[i] batch_states.append(data.state) batch_actions.append(data.action) batch_rewards.append(data.reward) batch_next_states.append(data.next_state) batch_terminals.append(data.term) batch_states_tensor = self._statck_state(batch_states) batch_actions_tensor = to_long_tensor(batch_actions) batch_rewards_tensor = to_float_tensor(batch_rewards) batch_next_states_tensor = self._statck_state(batch_next_states) batch_terminals_tensor = to_long_tensor(batch_terminals) return Transition(batch_states_tensor, batch_actions_tensor, batch_rewards_tensor, batch_next_states_tensor, batch_terminals_tensor)
def main(): logger.info('------------MODEL TEST INPUT--------------') logger.info('loading config file...') global_config = read_config() # set random seed seed = global_config['global']['random_seed'] torch.manual_seed(seed) torch.set_grad_enabled(False) # make sure all tensors below have require_grad=False logger.info('reading squad dataset...') dataset = SquadDataset(global_config) logger.info('constructing model...') model_choose = global_config['global']['model'] dataset_h5_path = global_config['data']['dataset_h5'] if model_choose == 'base': model = BaseModel(dataset_h5_path, model_config=read_config('config/base_model.yaml')) elif model_choose == 'match-lstm': model = MatchLSTM(dataset_h5_path) elif model_choose == 'match-lstm+': model = MatchLSTMPlus(dataset_h5_path) elif model_choose == 'r-net': model = RNet(dataset_h5_path) elif model_choose == 'm-reader': model = MReader(dataset_h5_path) else: raise ValueError('model "%s" in config file not recoginized' % model_choose) model.eval() # let training = False, make sure right dropout logging.info('model parameters count: %d' % count_parameters(model)) # load model weight logger.info('loading model weight...') model_weight_path = global_config['data']['model_path'] is_exist_model_weight = os.path.exists(model_weight_path) assert is_exist_model_weight, "not found model weight file on '%s'" % model_weight_path weight = torch.load(model_weight_path, map_location=lambda storage, loc: storage) model.load_state_dict(weight, strict=False) # manual input qa context = "In 1870, Tesla moved to Karlovac, to attend school at the Higher Real Gymnasium, where he was " \ "profoundly influenced by a math teacher Martin Sekuli\u0107.:32 The classes were held in German, " \ "as it was a school within the Austro-Hungarian Military Frontier. Tesla was able to perform integral " \ "calculus in his head, which prompted his teachers to believe that he was cheating. He finished a " \ "four-year term in three years, graduating in 1873.:33 " question1 = "What language were classes held in at Tesla's school?" answer1 = ["German"] question2 = "Who was Tesla influenced by while in school?" answer2 = ["Martin Sekuli\u0107"] question3 = "Why did Tesla go to Karlovac?" answer3 = ["attend school at the Higher Real Gymnasium", 'to attend school'] # change here to select questions question = question1 answer = answer1[0] # preprocess nlp = spacy.load('en') context_doc = DocText(nlp, context, global_config['preprocess']) question_doc = DocText(nlp, question, global_config['preprocess']) context_doc.update_em(question_doc) question_doc.update_em(context_doc) context_token = context_doc.token question_token = question_doc.token context_id_char = to_long_tensor(dataset.sentence_char2id(context_token)) question_id_char = to_long_tensor(dataset.sentence_char2id(question_token)) context_id, context_f = context_doc.to_id(dataset.meta_data) question_id, question_f = question_doc.to_id(dataset.meta_data) bat_input = [context_id, question_id, context_id_char, question_id_char, context_f, question_f] bat_input = [x.unsqueeze(0) if x is not None else x for x in bat_input] out_ans_prop, out_ans_range, vis_param = model.forward(*bat_input) out_ans_range = out_ans_range.numpy() start = out_ans_range[0][0] end = out_ans_range[0][1] + 1 out_answer_id = context_id[start:end] out_answer = dataset.sentence_id2word(out_answer_id) logging.info('Predict Answer: ' + ' '.join(out_answer)) # to show on visdom s = 0 e = 48 x_left = vis_param['match']['left']['alpha'][0, :, s:e].numpy() x_right = vis_param['match']['right']['alpha'][0, :, s:e].numpy() x_left_gated = vis_param['match']['left']['gated'][0, :, s:e].numpy() x_right_gated = vis_param['match']['right']['gated'][0, :, s:e].numpy() draw_heatmap_sea(x_left, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-left.png', bottom=0.45) draw_heatmap_sea(x_right, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-right.png', bottom=0.45) enable_self_match = False if enable_self_match: x_self_left = vis_param['self']['left']['alpha'][0, s:e, s:e].numpy() x_self_right = vis_param['self']['right']['alpha'][0, s:e, s:e].numpy() draw_heatmap_sea(x_self_left, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-left.png', inches=(11, 11), bottom=0.2) draw_heatmap_sea(x_self_right, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-right.png', inches=(11, 11), bottom=0.2)
def main(): logger.info('------------MODEL TEST INPUT--------------') logger.info('loading config file...') # manual set global_config = read_config('config/global_config.yaml') # set random seed seed = global_config['global']['random_seed'] torch.manual_seed(seed) torch.set_grad_enabled( False) # make sure all tensors below have require_grad=False logger.info('reading dataset...') dataset = Dataset(global_config) logger.info('constructing model...') dataset_h5_path = global_config['data']['dataset_h5'] model = MatchLSTMPlus(dataset_h5_path) model.eval() # let training = False, make sure right dropout logging.info('model parameters count: %d' % count_parameters(model)) model_rerank = None rank_k = global_config['global']['rank_k'] if global_config['global']['enable_rerank']: model_rerank = ReRanker(dataset_h5_path) model_rerank.eval() logging.info('rerank model parameters count: %d' % count_parameters(model_rerank)) # load model weight logger.info('loading model weight...') model_weight_path = global_config['data']['model_path'] is_exist_model_weight = os.path.exists(model_weight_path) assert is_exist_model_weight, "not found model weight file on '%s'" % model_weight_path weight = torch.load(model_weight_path, map_location=lambda storage, loc: storage) model.load_state_dict(weight, strict=False) if global_config['global']['enable_rerank']: rerank_weight_path = global_config['data']['rerank_model_path'] assert os.path.exists( rerank_weight_path ), "not found rerank model weight file on '%s'" % rerank_weight_path logger.info('loading rerank model weight...') weight = torch.load(rerank_weight_path, map_location=lambda storage, loc: storage) model_rerank.load_state_dict(weight, strict=False) context = "《战国无双3》()是由光荣和ω-force开发的战国无双系列的正统第三续作。本作以三大故事为主轴,分别是以武田信玄等人为主的《关东三国志》,织田信长等人为主的《战国三杰》,石田三成等人为主的《关原的年轻武者》,丰富游戏内的剧情。此部份专门介绍角色,欲知武器情报、奥义字或擅长攻击类型等,请至战国无双系列1.由于乡里大辅先生因故去世,不得不寻找其他声优接手。从猛将传 and Z开始。2.战国无双 编年史的原创男女主角亦有专属声优。此模式是任天堂游戏谜之村雨城改编的新增模式。本作中共有20张战场地图(不含村雨城),后来发行的猛将传再新增3张战场地图。但游戏内战役数量繁多,部分地图会有兼用的状况,战役虚实则是以光荣发行的2本「战国无双3 人物真书」内容为主,以下是相关介绍。(注:前方加☆者为猛将传新增关卡及地图。)合并本篇和猛将传的内容,村雨城模式剔除,战国史模式可直接游玩。主打两大模式「战史演武」&「争霸演武」。系列作品外传作品" question1 = "《战国无双3》是由哪两个公司合作开发的?" answer1 = ['光荣和ω-force'] question2 = '男女主角亦有专属声优这一模式是由谁改编的?' answer2 = ['村雨城', '任天堂游戏谜之村雨城'] question3 = '战国史模式主打哪两个模式?' answer3 = ['「战史演武」&「争霸演武」'] # change here to select questions question = question2 answer = answer2[0] # preprocess preprocess_config = global_config['preprocess'] context_doc = DocTextCh(context, preprocess_config) question_doc = DocTextCh(question, preprocess_config) link_char = '' # mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] mpl.rcParams['font.sans-serif'] = ['SimHei'] context_doc.update_em(question_doc) question_doc.update_em(context_doc) context_token = context_doc.token question_token = question_doc.token context_id_char = None question_id_char = None if preprocess_config['use_char']: context_id_char = to_long_tensor( dataset.sentence_char2id(context_token)) question_id_char = to_long_tensor( dataset.sentence_char2id(question_token)) context_id, context_f = context_doc.to_id(dataset.meta_data) question_id, question_f = question_doc.to_id(dataset.meta_data) bat_input = [ context_id, question_id, context_id_char, question_id_char, context_f, question_f ] bat_input = [x.unsqueeze(0) if x is not None else x for x in bat_input] # predict out_ans_prop, out_ans_range, vis_param = model.forward(*bat_input) if model_rerank is not None: cand_ans_range = beam_search(out_ans_prop, k=rank_k) cand_score, out_ans_range = model_rerank(bat_input[0], bat_input[1], cand_ans_range) out_ans_range = out_ans_range.numpy() start = out_ans_range[0][0] end = out_ans_range[0][1] + 1 out_answer_id = context_id[start:end] out_answer = dataset.sentence_id2word(out_answer_id) logging.info('Predict Answer: ' + link_char.join(out_answer)) # to show on visdom s = 0 e = 48 x_left = vis_param['match']['left']['alpha'][0, :, s:e].numpy() x_right = vis_param['match']['right']['alpha'][0, :, s:e].numpy() x_left_gated = vis_param['match']['left']['gated'][0, :, s:e].numpy() x_right_gated = vis_param['match']['right']['gated'][0, :, s:e].numpy() draw_heatmap_sea(x_left, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-left.png', bottom=0.2) draw_heatmap_sea(x_right, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-right.png', bottom=0.2)
def main(): logger.info('------------MODEL TEST INPUT--------------') logger.info('loading config file...') global_config = read_config() # set random seed seed = global_config['global']['random_seed'] torch.manual_seed(seed) torch.set_grad_enabled( False) # make sure all tensors below have require_grad=False logger.info('reading squad dataset...') dataset = SquadDataset(global_config) logger.info('constructing model...') model_choose = global_config['global']['model'] dataset_h5_path = global_config['data']['dataset_h5'] if model_choose == 'base': model = BaseModel(dataset_h5_path, model_config=read_config('config/base_model.yaml')) elif model_choose == 'match-lstm': model = MatchLSTM(dataset_h5_path) elif model_choose == 'match-lstm+': model = MatchLSTMPlus(dataset_h5_path) elif model_choose == 'r-net': model = RNet(dataset_h5_path) elif model_choose == 'm-reader': model = MReader(dataset_h5_path) else: raise ValueError('model "%s" in config file not recoginized' % model_choose) model.eval() # let training = False, make sure right dropout logging.info('model parameters count: %d' % count_parameters(model)) # load model weight logger.info('loading model weight...') model_weight_path = global_config['data']['model_path'] is_exist_model_weight = os.path.exists(model_weight_path) assert is_exist_model_weight, "not found model weight file on '%s'" % model_weight_path weight = torch.load(model_weight_path, map_location=lambda storage, loc: storage) model.load_state_dict(weight, strict=False) # manual input qa context = "In 1870, Tesla moved to Karlovac, to attend school at the Higher Real Gymnasium, where he was " \ "profoundly influenced by a math teacher Martin Sekuli\u0107.:32 The classes were held in German, " \ "as it was a school within the Austro-Hungarian Military Frontier. Tesla was able to perform integral " \ "calculus in his head, which prompted his teachers to believe that he was cheating. He finished a " \ "four-year term in three years, graduating in 1873.:33 " question1 = "What language were classes held in at Tesla's school?" answer1 = ["German"] question2 = "Who was Tesla influenced by while in school?" answer2 = ["Martin Sekuli\u0107"] question3 = "Why did Tesla go to Karlovac?" answer3 = [ "attend school at the Higher Real Gymnasium", 'to attend school' ] # change here to select questions question = question1 answer = answer1[0] # preprocess nlp = spacy.load('en') context_doc = DocText(nlp, context, global_config['preprocess']) question_doc = DocText(nlp, question, global_config['preprocess']) context_doc.update_em(question_doc) question_doc.update_em(context_doc) context_token = context_doc.token question_token = question_doc.token context_id_char = to_long_tensor(dataset.sentence_char2id(context_token)) question_id_char = to_long_tensor(dataset.sentence_char2id(question_token)) context_id, context_f = context_doc.to_id(dataset.meta_data) question_id, question_f = question_doc.to_id(dataset.meta_data) bat_input = [ context_id, question_id, context_id_char, question_id_char, context_f, question_f ] bat_input = [x.unsqueeze(0) if x is not None else x for x in bat_input] out_ans_prop, out_ans_range, vis_param = model.forward(*bat_input) out_ans_range = out_ans_range.numpy() start = out_ans_range[0][0] end = out_ans_range[0][1] + 1 out_answer_id = context_id[start:end] out_answer = dataset.sentence_id2word(out_answer_id) logging.info('Predict Answer: ' + ' '.join(out_answer)) # to show on visdom s = 0 e = 48 x_left = vis_param['match']['left']['alpha'][0, :, s:e].numpy() x_right = vis_param['match']['right']['alpha'][0, :, s:e].numpy() x_left_gated = vis_param['match']['left']['gated'][0, :, s:e].numpy() x_right_gated = vis_param['match']['right']['gated'][0, :, s:e].numpy() draw_heatmap_sea(x_left, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-left.png', bottom=0.45) draw_heatmap_sea(x_right, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-right.png', bottom=0.45) enable_self_match = False if enable_self_match: x_self_left = vis_param['self']['left']['alpha'][0, s:e, s:e].numpy() x_self_right = vis_param['self']['right']['alpha'][0, s:e, s:e].numpy() draw_heatmap_sea(x_self_left, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-left.png', inches=(11, 11), bottom=0.2) draw_heatmap_sea(x_self_right, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-right.png', inches=(11, 11), bottom=0.2)
def main(): logger.info('------------Match-LSTM TEST INPUT--------------') logger.info('loading config file...') global_config = read_config() # set random seed seed = global_config['model']['global']['random_seed'] torch.manual_seed(seed) torch.no_grad() # make sure all tensors below have require_grad=False logger.info('reading squad dataset...') dataset = SquadDataset(global_config) logger.info('constructing model...') model = MatchLSTMModel(global_config) model.eval() # let training = False, make sure right dropout logging.info('model parameters count: %d' % count_parameters(model)) # load model weight logger.info('loading model weight...') model_weight_path = global_config['data']['model_path'] is_exist_model_weight = os.path.exists(model_weight_path) assert is_exist_model_weight, "not found model weight file on '%s'" % model_weight_path weight = torch.load(model_weight_path, map_location=lambda storage, loc: storage) model.load_state_dict(weight, strict=False) # manual input qa context = "In 1870, Tesla moved to Karlovac, to attend school at the Higher Real Gymnasium, where he was " \ "profoundly influenced by a math teacher Martin Sekuli\u0107.:32 The classes were held in German, " \ "as it was a school within the Austro-Hungarian Military Frontier. Tesla was able to perform integral " \ "calculus in his head, which prompted his teachers to believe that he was cheating. He finished a " \ "four-year term in three years, graduating in 1873.:33 " question1 = "What language were classes held in at Tesla's school?" answer1 = ["German"] question2 = "Who was Tesla influenced by while in school?" answer2 = ["Martin Sekuli\u0107"] question3 = "Why did Tesla go to Karlovac?" answer3 = [ "attend school at the Higher Real Gymnasium", 'to attend school' ] # change here to select questions question = question1 answer = answer1[0] # preprocess context_token = nltk.word_tokenize(context) question_token = nltk.word_tokenize(question) a = np.array(context_token) context_id = dataset.sentence_word2id(context_token) question_id = dataset.sentence_word2id(question_token) context_id_char = dataset.sentence_char2id(context_token) question_id_char = dataset.sentence_char2id(question_token) context_var, question_var, context_var_char, question_var_char = [ to_long_tensor(x).unsqueeze(0) for x in [context_id, question_id, context_id_char, question_id_char] ] out_ans_prop, out_ans_range, vis_param = model.forward( context_var, question_var, context_var_char, question_var_char) out_ans_range = out_ans_range.cpu().data.numpy() start = out_ans_range[0][0] end = out_ans_range[0][1] + 1 out_answer_id = context_id[start:end] out_answer = dataset.sentence_id2word(out_answer_id) logging.info('Predict Answer: ' + ' '.join(out_answer)) # to show on visdom s = 0 e = 48 x_left = vis_param['match']['left'][0, :, s:e].cpu().data.numpy() x_right = vis_param['match']['right'][0, :, s:e].cpu().data.numpy() draw_heatmap_sea(x_left, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-left.png', bottom=0.45) draw_heatmap_sea(x_right, xlabels=context_token[s:e], ylabels=question_token, answer=answer, save_path='data/test-right.png', bottom=0.45) if global_config['model']['interaction']['enable_self_match']: x_self_left = vis_param['self']['left'][0, s:e, s:e].cpu().data.numpy() x_self_right = vis_param['self']['right'][0, s:e, s:e].cpu().data.numpy() draw_heatmap_sea(x_self_left, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-left.png', inches=(11, 11), bottom=0.2) draw_heatmap_sea(x_self_right, xlabels=context_token[s:e], ylabels=context_token[s:e], answer=answer, save_path='data/test-self-right.png', inches=(11, 11), bottom=0.2)