def bot_func(bot, update, args): text = " ".join(args) words = utils.tokenize(text) seq_1 = data.encode_words(words, emb_dict) input_seq = model.pack_input(seq_1, net.emb) enc = net.encode(input_seq) if prog_args.sample: _, tokens = net.decode_chain_sampling(enc, input_seq.data[0:1], seq_len=data.MAX_TOKENS, stop_at_token=end_token) else: _, tokens = net.decode_chain_argmax(enc, input_seq.data[0:1], seq_len=data.MAX_TOKENS, stop_at_token=end_token) if tokens[-1] == end_token: tokens = tokens[:-1] reply = data.decode_words(tokens, rev_emb_dict) if reply: reply_text = utils.untokenize(reply) bot.send_message(chat_id=update.message.chat_id, text=reply_text)
def bot_func(bot, update, args): text = " ".join(args) words = utils.tokenize(text) seq_1 = data.encode_words(words, emb_dict) input_seq = model.pack_input(seq_1, net.emb) enc = net.encode(input_seq) if prog_args.sample: _, tokens = net.decode_chain_sampling(enc, input_seq.data[0:1], seq_len=data.MAX_TOKENS, stop_at_token=end_token) else: _, tokens = net.decode_chain_argmax(enc, input_seq.data[0:1], seq_len=data.MAX_TOKENS, stop_at_token=end_token) if tokens[-1] == end_token: tokens = tokens[:-1] reply = data.decode_words(tokens, rev_emb_dict) if reply: reply_text = utils.untokenize(reply) bot.send_message(chat_id=update.message.chat_id, text=reply_text)
r_argmax, actions = net.decode_chain_argmax( item_enc, beg_embedding, data.MAX_TOKENS, stop_at_token=end_token) argmax_bleu = utils.calc_bleu_many(actions, ref_indices) bleus_argmax.append(argmax_bleu) if not args.disable_skip: if argmax_bleu > 0.99: skipped_samples += 1 continue if not dial_shown: w = data.decode_words(inp_idx, rev_emb_dict) log.info("Input: %s", utils.untokenize(w)) ref_words = [ utils.untokenize( data.decode_words(ref, rev_emb_dict)) for ref in ref_indices ] ref = " ~~|~~ ".join(ref_words) log.info("Refer: %s", ref) w = data.decode_words(actions, rev_emb_dict) log.info("Argmax: %s, bleu=%.4f", utils.untokenize(w), argmax_bleu) for _ in range(args.samples): r_sample, actions = \ net.decode_chain_sampling( item_enc, beg_embedding,
# Get predicted tokens. seq = torch.max(r.data, dim=1)[1] seq = seq.cpu().numpy() # argmax做训练; else: r, seq = net.decode_chain_argmax(enc_item, out_seq.data[0:1], len(ref_indices)) blue_temp = utils.calc_bleu(seq, ref_indices) bleu_sum += blue_temp net_results.append(r) net_targets.extend(ref_indices) bleu_count += 1 if not dial_shown: # data.decode_words transform IDs to tokens. ref_words = [utils.untokenize(data.decode_words(ref_indices, rev_emb_dict))] log.info("Reference: %s", " ~~|~~ ".join(ref_words)) log.info("Predicted: %s, bleu=%.4f", utils.untokenize(data.decode_words(seq, rev_emb_dict)), blue_temp) dial_shown = True results_v = torch.cat(net_results) results_v = results_v.cuda() targets_v = torch.LongTensor(net_targets).to(device) targets_v = targets_v.cuda() loss_v = F.cross_entropy(results_v, targets_v) loss_v = loss_v.cuda() loss_v.backward() optimiser.step() losses.append(loss_v.item()) bleu = bleu_sum / bleu_count bleu_test = run_test(test_data, net, end_token, device)
r_argmax, actions = net.decode_chain_argmax( item_enc, beg_embedding, data.MAX_TOKENS, stop_at_token=end_token) argmax_bleu = utils.calc_bleu_many(actions, ref_indices) bleus_argmax.append(argmax_bleu) if not args.disable_skip and argmax_bleu > 0.99: skipped_samples += 1 continue if not dial_shown: log.info( "Input: %s", utils.untokenize( data.decode_words(inp_idx, rev_emb_dict))) ref_words = [ utils.untokenize( data.decode_words(ref, rev_emb_dict)) for ref in ref_indices ] log.info("Refer: %s", " ~~|~~ ".join(ref_words)) log.info( "Argmax: %s, bleu=%.4f", utils.untokenize( data.decode_words(actions, rev_emb_dict)), argmax_bleu) for _ in range(args.samples): r_sample, actions = net.decode_chain_sampling( item_enc,
logging.basicConfig(format="%(asctime)-15s %(levelname)s %(message)s", level=logging.INFO) parser = argparse.ArgumentParser() parser.add_argument("-m", "--model", required=True, help="Model name to load") parser.add_argument("-s", "--string", help="String to process, otherwise will loop") parser.add_argument("--sample", default=False, action="store_true", help="Enable sampling generation instead of argmax") parser.add_argument("--self", type=int, default=1, help="Enable self-loop mode with given amount of phrases.") args = parser.parse_args() emb_dict = data.load_emb_dict(os.path.dirname(args.model)) net = model.PhraseModel(emb_size=model.EMBEDDING_DIM, dict_size=len(emb_dict), hid_size=model.HIDDEN_STATE_SIZE) net.load_state_dict(torch.load(args.model)) rev_emb_dict = {idx: word for word, idx in emb_dict.items()} while True: if args.string: input_string = args.string else: input_string = input(">>> ") if not input_string: break words = utils.tokenize(input_string) for _ in range(args.self): words = words_to_words(words, emb_dict, rev_emb_dict, net, use_sampling=args.sample) print(utils.untokenize(words)) if args.string: break pass
seq = seq.cpu().numpy() # argmax做训练; else: r, seq = net.decode_chain_argmax(enc_item, out_seq.data[0:1], len(ref_indices)) blue_temp = utils.calc_bleu(seq, ref_indices) bleu_sum += blue_temp net_results.append(r) net_targets.extend(ref_indices) bleu_count += 1 if not dial_shown: # data.decode_words transform IDs to tokens. ref_words = [ utils.untokenize( data.decode_words(ref_indices, rev_emb_dict)) ] log.info("Reference: %s", " ~~|~~ ".join(ref_words)) log.info( "Predicted: %s, bleu=%.4f", utils.untokenize(data.decode_words(seq, rev_emb_dict)), blue_temp) dial_shown = True results_v = torch.cat(net_results) results_v = results_v.cuda() targets_v = torch.LongTensor(net_targets).to(device) targets_v = targets_v.cuda() loss_v = F.cross_entropy(results_v, targets_v) loss_v = loss_v.cuda() loss_v.backward() optimiser.step()
args = parser.parse_args() emb_dict = data.load_emb_dict(os.path.dirname(args.model)) net = model.PhraseModel(emb_size=model.EMBEDDING_DIM, dict_size=len(emb_dict), hid_size=model.HIDDEN_STATE_SIZE) net.load_state_dict(torch.load(args.model)) rev_emb_dict = {idx: word for word, idx in emb_dict.items()} while True: if args.string: input_string = args.string else: input_string = input(">>> ") if not input_string: break words = utils.tokenize(input_string) for _ in range(args.self): words = words_to_words(words, emb_dict, rev_emb_dict, net, use_sampling=args.sample) print(utils.untokenize(words)) if args.string: break pass
ref_indices = [ indices[1:] for indices in output_batch[idx] ] item_enc = net.get_encoded_item(enc, idx) r_argmax, actions = net.decode_chain_argmax(item_enc, beg_embedding, data.MAX_TOKENS, stop_at_token=end_token) argmax_bleu = utils.calc_bleu_many(actions, ref_indices) bleus_argmax.append(argmax_bleu) if not args.disable_skip and argmax_bleu > 0.99: skipped_samples += 1 continue if not dial_shown: log.info("Input: %s", utils.untokenize(data.decode_words(inp_idx, rev_emb_dict))) ref_words = [utils.untokenize(data.decode_words(ref, rev_emb_dict)) for ref in ref_indices] log.info("Refer: %s", " ~~|~~ ".join(ref_words)) log.info("Argmax: %s, bleu=%.4f", utils.untokenize(data.decode_words(actions, rev_emb_dict)), argmax_bleu) for _ in range(args.samples): r_sample, actions = net.decode_chain_sampling(item_enc, beg_embedding, data.MAX_TOKENS, stop_at_token=end_token) sample_bleu = utils.calc_bleu_many(actions, ref_indices) if not dial_shown: log.info("Sample: %s, bleu=%.4f", utils.untokenize(data.decode_words(actions, rev_emb_dict)), sample_bleu) net_policies.append(r_sample)