예제 #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)
예제 #2
0
파일: train.py 프로젝트: jihunsuk/peanut
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('최적화 완료!')
예제 #3
0
class chatbot:
    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)

    def run(self):
        sentences = []
        sentences.append(inputs.strip())
        reply = self.get_replay(sentences)
        return reply

    def _decode(self, enc_input, dec_input):
        enc_len = []
        dec_len = []

        enc_batch = []
        dec_batch = []

        for input in enc_input:  # 최대길이 25로 제한.
            if len(input) > 25:
                input = input[0:25]

        dec_input = enc_input

        for i in range(0, len(enc_input)):
            enc, dec, _ = self.dialogue.transform(enc_input[i], dec_input[i],
                                                  25, 25)
            enc_batch.append(enc)
            dec_batch.append(dec)
            enc_len.append(len(enc_input[i]))
            dec_len.append(len(dec_input[i]) + 1)
            # predict할떄 dec는 필요없는데 model에 placeholder로 해놔서 일단 아무거나(enc) 줬습니다.
        return self.model.predict(self.sess, [enc_batch], [enc_len],
                                  [dec_batch], [dec_len], [b], context_size)

    # msg에 대한 응답을 반환
    def get_replay(self, sentences):

        enc_input = [
            self.dialogue.tokens_to_ids(self.dialogue.tokenizer(sentence))
            for sentence in sentences
        ]
        dec_input = enc_input

        outputs = self._decode(enc_input, dec_input)
        reply = self.dialogue.decode([outputs[len(enc_input) - 1]], True)
        reply = self.dialogue.cut_eos(reply)

        return reply

    def kakao_input(self, inputs):
        self.inputs = inputs
예제 #4
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()
예제 #5
0
파일: train.py 프로젝트: jihunsuk/peanut
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]))
예제 #6
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))
예제 #7
0
파일: chat.py 프로젝트: jihunsuk/peanut
class chatbot:
    def __init__(self, voc_path):
        self.dialogue = Dialogue()
        self.dialogue.load_vocab(voc_path)
        self.model = Hred(self.dialogue.voc_size, False, 1)
        self.sess = tf.Session()

        # 모델 불러오기
        ckpt = tf.train.get_checkpoint_state('./model')
        self.model.saver.restore(self.sess, ckpt.model_checkpoint_path)

    def run(self):
        sys.stdout.write("> ")
        sys.stdout.flush()

        setences = []
        line = sys.stdin.readline()
        setences.append(line.strip())

        while line:
            reply = self.get_replay(setences)
            print(reply)  # 응답
            setences.append(reply)

            sys.stdout.write("\n> ")
            sys.stdout.flush()

            line = sys.stdin.readline()
            setences.append(line.strip())

    def _decode(self, enc_input, dec_input):
        enc_len = []
        dec_len = []

        enc_batch = []
        dec_batch = []

        for i in range(0, len(enc_input)):
            enc_in = enc_input[i]
            if len(enc_in) > 70:
                enc_in = enc_in[0:70]
            dec_in = dec_input[i]
            if len(dec_in) > 69:
                dec_in = dec_in[0:69]

            enc, dec, _ = self.dialogue.transform(enc_in, dec_in, 70, 70)
            enc_batch.append(enc)
            dec_batch.append(dec)
            enc_len.append(len(enc_in))
            dec_len.append(len(dec_in) + 1)
            # predict할떄 dec는 필요없는데 model에 placeholder로 해놔서 일단 아무거나(enc) 줬습니다.
        context_size = len(enc_input)
        b = np.max(dec_len, 0)

        return self.model.predict(self.sess, [enc_batch], [enc_len],
                                  [dec_batch], [dec_len], [b], context_size)

    def get_replay(self, sentences):

        enc_input = [
            self.dialogue.tokens_to_ids(list(sentence))
            for sentence in sentences
        ]
        dec_input = enc_input

        outputs = self._decode(enc_input, dec_input)
        reply = self.dialogue.decode([outputs[len(enc_input) - 1]], True)
        reply = self.dialogue.cut_eos(reply)

        return reply
예제 #8
0
파일: chat.py 프로젝트: jihunsuk/peanut
class chatbot:
    def __init__(self, voc_path):
        self.dialogue = Dialogue()
        self.dialogue.load_vocab(voc_path)
        self.model = Hred(self.dialogue.voc_size, False, 1)
        self.sess = tf.Session()

        # 모델 불러오기
        ckpt = tf.train.get_checkpoint_state('./model')
        self.model.saver.restore(self.sess, ckpt.model_checkpoint_path)

    def run(self):
        sys.stdout.write("> ")
        sys.stdout.flush()

        setences = []
        line = sys.stdin.readline()
        setences.append(line.strip())

        while line:
            reply = self.get_replay(setences)
            print(reply)  # 응답
            setences.append(reply)

            sys.stdout.write("\n> ")
            sys.stdout.flush()

            line = sys.stdin.readline()
            setences.append(line.strip())

    def _decode(self, enc_inputs, dec_inputs):
        enc_len = []
        dec_len = []

        enc_batch = []
        dec_batch = []

        for i in range(0, len(enc_inputs)):
            enc_input = enc_inputs[i]
            if len(enc_input) > 25:
                enc_input = enc_inputs[i][0:25]
            dec_input = dec_inputs[i]
            if len(dec_input) > 24:
                dec_input = dec_inputs[i][0:24]

            enc, dec, _ = self.dialogue.transform(enc_input, dec_input, 25, 25)
            enc_batch.append(enc)
            dec_batch.append(dec)
            enc_len.append(len(enc_input))
            dec_len.append(len(dec_input) + 1)

        context_size = len(enc_inputs)
        b = np.max(dec_len, 0)

        return self.model.predict(self.sess, [enc_batch], [enc_len],
                                  [dec_batch], [dec_len], [b], context_size)

    # msg에 대한 응답을 반환
    def get_replay(self, sentences):

        enc_input = [
            self.dialogue.tokens_to_ids(self.dialogue.tokenizer(sentence))
            for sentence in sentences
        ]
        dec_input = enc_input

        outputs = self._decode(enc_input, dec_input)
        reply = self.dialogue.decode([outputs[len(enc_input) - 1]], True)
        reply = self.dialogue.cut_eos(reply)

        return reply
예제 #9
0
class chatbot:
    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)

    def run(self):
        sys.stdout.write("> ")
        sys.stdout.flush()

        setences = []
        line = sys.stdin.readline()
        setences.append(line.strip())

        while line:
            reply = self.get_replay(setences)
            print(reply)  # 응답
            setences.append(reply)

            sys.stdout.write("\n> ")
            sys.stdout.flush()

            line = sys.stdin.readline()
            setences.append(line.strip())

    def _decode(self, enc_input, dec_input):
        enc_len = []
        dec_len = []

        enc_batch = []
        dec_batch = []

        input_len = int(math.ceil((len(enc_input) + 1) * 1.5))  # 버킷 사용

        for i in range(0, len(enc_input)):
            enc, dec, _ = self.dialogue.transform(enc_input[i], dec_input[i],
                                                  input_len, input_len)
            enc_batch.append(enc)
            dec_batch.append(dec)
            enc_len.append(len(enc_input[i]))
            dec_len.append(len(dec_input[i]) + 1)
            # predict할떄 dec는 필요없는데 model에 placeholder로 해놔서 일단 아무거나(enc) 줬습니다.
        return self.model.predict(self.sess, enc_batch, enc_len, dec_batch,
                                  dec_len, len(enc_batch))

    # msg에 대한 응답을 반환
    def get_replay(self, sentences):

        enc_input = [
            self.dialogue.tokens_to_ids(self.dialogue.tokenizer(sentence))
            for sentence in sentences
        ]
        dec_input = enc_input

        outputs = self._decode(enc_input, dec_input)
        reply = self.dialogue.decode([outputs[len(enc_input) - 1]], True)
        reply = self.dialogue.cut_eos(reply)

        return reply