Exemplo n.º 1
0
def infer():
    def process(inputs):
        inputs = clear_punc(inputs)
        x = []
        for word in jieba.cut(inputs):
            x.append(token2idx.get(word, token2idx['<UNK>']))
        x.append(token2idx['</S>'])
        x = x + [token2idx['<PAD>']] * (model_params.maxlen - len(x))
        x = [x]
        return x

    model_params = Params()
    idx2token, token2idx = load_vocab(model_params.idx2token_path,model_params.token2idx_path)

    model = Transformer(model_params)
    model.eval()
    with tf.Session() as sess:
        saver = tf.train.Saver()
        last_ckpt = tf.train.latest_checkpoint(model_params.model_save)
        saver.restore(sess,last_ckpt)

        while True:
            x = input('{}:>>'.format('笑给我看'))
            #  todo 古诗模式
            if x == '对古诗':
                pass

            x = process(x)
            feed_dict = {model.xs: x}
            y_hat = sess.run(model.y_hat,
                             feed_dict=feed_dict)

            result = ''
            for word in y_hat[0]:
                if word == token2idx['<UNK>']:
                    result += '*××'
                elif word != 3:
                    result += idx2token[word]
                else:
                    break
            if result == '==':
                result = "= ="
            elif result == '<UNK>':
                result = '哎呀,我不知道啊!'


            print('傻逼一号:>>',result,'\n')
Exemplo n.º 2
0
                    feedback_size=2048,
                    num_head=8,
                    dropout=0.1,
                    device=device)
model = model.to(device)
model.load_state_dict(torch.load(args.model_path))
dataload = DataLoader(Dataload(add_(args.bin_path) + 'test',
                               src=args.src_lang,
                               trg=args.tgt_lang),
                      batch_size=32,
                      collate_fn=collate_fn)
real = []
predict = []
pbtr = tqdm(total=len(dataload))
with torch.no_grad():
    model.eval()
    for src, trg in dataload:
        src = src.to(device)
        predicts = beamsearch(model, src, 1, 100, device=device)

        for i in range(len(predicts)):
            while 0 in predicts[i]:
                predicts[i].remove(0)
        predict = predict + predicts
        real = real + trg
        tmp_bleu = corpus_bleu(list_of_references=[[each] for each in trg],
                               hypotheses=predicts)
        pbtr.set_postfix({'bleu': tmp_bleu})
        print(tmp_bleu)
        pbtr.update(1)
print("Bleu score:{}".format(