Ejemplo n.º 1
0
 def __init__(self, voc_path):
     self.dialogue = Dialogue()
     self.dialogue.load_vocab(voc_path)
     self.model = Hred(self.dialogue.voc_size, False)
     self.sess = tf.Session()
     # 모델 불러오기
     ckpt = tf.train.get_checkpoint_state('./model')
     self.model.saver.restore(self.sess, ckpt.model_checkpoint_path)
Ejemplo n.º 2
0
def train(dialog, batch_size=10, epoch=100):

    model = Hred(dialog.voc_size, True, 10)
    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./model')
        if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
            print("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path)
            model.saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            print("새로운 모델을 생성하는 중 입니다.")
            sess.run(tf.global_variables_initializer())

        # 학습시작.
        total_batch = int(
            math.ceil(len(dialog.seq_data) / (float(batch_size) * 10)))
        for step in range(total_batch * epoch):

            enc_inputs = []
            enc_lengths = []
            dec_inputs = []
            dec_lengths = []
            targets = []

            for i in range(10):
                enc_input, enc_length, dec_input, dec_length, target = dialog.next_batch(
                    batch_size)

                enc_inputs.append(enc_input)
                enc_lengths.append(enc_length)
                dec_inputs.append(dec_input)
                dec_lengths.append(dec_length)
                targets.append(target)

            max_dec_lengths = np.max(dec_lengths, 1)
            context_size = len(enc_input)

            _, loss = sess.run(
                [model.train_op, model.cost],
                feed_dict={
                    model.enc_input_idx: enc_inputs,
                    model.dec_input_idx: dec_inputs,
                    model.enc_length: enc_lengths,
                    model.dec_length: dec_lengths,
                    model.targets: targets,
                    model.dec_max_length: max_dec_lengths,
                    model.context_size: context_size
                })

            if (step + 1) % 100 == 0:
                print('Step:', '%06d' % model.global_step.eval(), 'cost =',
                      '{:.6f}'.format(loss))

            if (step + 1) % 500 == 0:
                model.saver.save(sess,
                                 './model/conversation.ckpt',
                                 global_step=model.global_step)

        print('최적화 완료!')
Ejemplo n.º 3
0
def test(batch_size=10):

    data_path = './data/dict_idx_all_test.npy'
    vocab_path = './data/words.npy'

    dialog = Dialogue()

    dialog.load_vocab(vocab_path)
    dialog.load_data(data_path)

    print("\n=== 예측 테스트 ===")
    model = Hred(dialog.voc_size, dialog.embedding_matrix, False, 1)

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./model')
        print("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path)

        model.saver.restore(sess, ckpt.model_checkpoint_path)

        all_expect = []
        all_predict = []

        total_batch = int(math.ceil(len(dialog.seq_data) / float(batch_size)))
        for step in range(total_batch):
            enc_input, enc_length, dec_input, dec_length, targets = dialog.next_batch(
                batch_size)

            expects, outputs = sess.run(
                [model.targets, model.outputs],
                feed_dict={
                    model.enc_input_idx: [enc_input],
                    model.dec_input_idx: [dec_input],
                    model.enc_length: [enc_length],
                    model.dec_length: [dec_length],
                    model.targets: [targets],
                    model.dec_max_length: [np.max(dec_length, 0)],
                    model.context_size: len(enc_input)
                })

            for i in range(len(outputs)):
                e = dialog.cut_eos(dialog.decode([expects[0][i]], True))
                all_expect.append(e)
                o = dialog.cut_eos(dialog.decode([outputs[i]], True))
                all_predict.append(o)

        with open('./expect.txt', 'w') as f:
            for i in range(len(all_predict)):
                f.write(all_expect[i] + "\n")
        f.close()

        with open('./predict.txt', 'w') as f2:
            for i in range(len(all_predict)):
                f2.write(all_predict[i] + "\n")
        f2.close()
Ejemplo n.º 4
0
def test(batch_size=15):

    data_path = './data/dict_idx_shuffleAll_test.npy'
    vocab_path = './data/dictionary.txt'

    dialog = Dialogue()

    dialog.load_vocab(vocab_path)
    dialog.load_data(data_path)

    print("\n=== 예측 테스트 ===")
    model = Hred(dialog.voc_size, False, 1)

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./model')
        print("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path)

        model.saver.restore(sess, ckpt.model_checkpoint_path)

        all_expect = []
        all_predict = []

        total_batch = int(math.ceil(len(dialog.seq_data) / float(batch_size)))
        print(total_batch)
        for step in range(total_batch):
            enc_input, enc_length, dec_input, dec_length, targets = dialog.next_batch(
                batch_size)

            expects, outputs = sess.run(
                [model.targets, model.outputs],
                feed_dict={
                    model.enc_input_idx: [enc_input],
                    model.dec_input_idx: [dec_input],
                    model.enc_length: [enc_length],
                    model.dec_length: [dec_length],
                    model.targets: [targets],
                    model.dec_max_length: [np.max(dec_length, 0)],
                    model.context_size: len(enc_input)
                })

            for i in range(len(outputs)):
                all_expect.append(
                    dialog.cut_eos(dialog.decode([expects[0][i]], True)))
                all_predict.append(
                    dialog.cut_eos(dialog.decode([outputs[i]], True)))

        for i in range(len(all_predict)):
            print("    실제값:", all_expect[i])
            print("    예측값:", ''.join(all_predict[i]))
Ejemplo n.º 5
0
def test(dialog, batch_size=20):
    print("\n=== 예측 테스트 ===")
    model = Hred(dialog.voc_size, False)

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./model')
        print("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path)

        model.saver.restore(sess, ckpt.model_checkpoint_path)

        enc_input, enc_length, dec_input, dec_length, targets = dialog.next_batch(
            batch_size)

        expect, outputs, accuracy = sess.run(
            [model.targets, model.outputs, model.accuracy],
            feed_dict={
                model.enc_input_idx: enc_input,
                model.dec_input_idx: dec_input,
                model.enc_length: enc_length,
                model.dec_length: dec_length,
                model.targets: targets,
                model.batch_size: len(enc_input)
            })

        expect = dialog.decode(expect)
        outputs = dialog.decode(outputs)

        pick = random.randrange(0, len(expect) / 2)
        input = dialog.decode([dialog.seq_data[pick * 2]], True)
        expect = dialog.decode([dialog.seq_data[pick * 2 + 1]], True)
        outputs = dialog.cut_eos(outputs[pick])

        print("\n정확도:", accuracy)
        print("랜덤 결과\n")
        print("    입력값:", input)
        print("    실제값:", expect)
        print("    예측값:", ' '.join(outputs))