Ejemplo n.º 1
0
 def infer(self, text):
     if not self.init_infer:
         raise Exception('Infer graph is not inited!')
     with self.infer_graph.as_default():
         in_seq = reader.encode_text(text.split(' ') + ['</s>',],
                 self.infer_vocab_indices)
         in_seq_len = len(in_seq)
         outputs = self.infer_session.run(self.infer_output,
                 feed_dict={
                     self.infer_in_seq: [in_seq],
                     self.infer_in_seq_len: [in_seq_len]})
         output = outputs[0]
         output_text = reader.decode_text(output, self.infer_vocabs)
         return output_text
Ejemplo n.º 2
0
    def infer(self, texts):
        if not self.init_infer:
            raise Exception('Infer graph is not inited!')
        with self.infer_graph.as_default():
            if not isinstance(texts, list):
                texts = [texts]
            in_seq_list = []
            in_seq_len_list = []
            for text in texts:
                in_seq = reader.encode_text(
                    list(text) + [
                        '</s>',
                    ], self.infer_vocab_indices)
                in_seq_len = len(in_seq)
                in_seq_list.append(in_seq)
                in_seq_len_list.append(in_seq_len)
            import time
            pre_time = time.time()
            outputs = self.infer_session.run(self.infer_output,
                                             feed_dict={
                                                 self.infer_in_seq:
                                                 in_seq_list,
                                                 self.infer_in_seq_len:
                                                 in_seq_len_list
                                             })
            pre_time = time.time()
            outputs_final = []
            for output in outputs:
                output_text = []
                if self.decode_method == 'beam':
                    for i in range(len(output[0])):
                        output_x = [x[i] for x in output]
                        _text = reader.decode_text(output_x, self.infer_vocabs)
                        output_text.append(_text)
                else:
                    _text = reader.decode_text(output, self.infer_vocabs)
                    output_text.append(_text)

                outputs_final.append(output_text)

            return outputs_final
Ejemplo n.º 3
0
 def infer(self, text):
     if not self.init_infer:
         raise Exception('Infer graph is not inited!')
     with self.infer_graph.as_default():
         in_seq = reader.encode_text(
             text.split(' ') + [
                 '</s>',
             ], self.infer_vocab_indices)
         in_seq_len = len(in_seq)
         (outputs,
          scores) = self.infer_session.run(self.infer_output,
                                           feed_dict={
                                               self.infer_in_seq: [in_seq],
                                               self.infer_in_seq_len:
                                               [in_seq_len]
                                           })
         output = outputs[0]
         score = np.average(scores[0].T, axis=1)
         output_text = reader.decode_multi_text(output, self.infer_vocabs)
         output_without_space = [''.join(s.split(' ')) for s in output_text]
         return (output_without_space, score)
Ejemplo n.º 4
0
 def head_infer(self, text):
     # input_words = reader.encode_text(text.split(' '), self.infer_vocab_indices)
     input_words = []
     if not self.init_infer:
         print("=====infer 289===== 模型未初始化")
     with self.infer_graph.as_default():
         print("===text==", text.split(' ') + ['</s>'])
         for i in range(5):
             input_words.append(random.randint(4, self.batch_size * 10))
         random.shuffle(input_words)
         print("===input_words297==", input_words)
         words_in_seq = input_words
         words_in_seq_len = len(input_words)
         print("===infer_in_seq_len 300====", self.infer_in_seq_len)
         outputs = self.infer_session.run(self.infer_output,
                                          feed_dict={
                                              self.infer_in_seq:
                                              [words_in_seq],
                                              self.infer_in_seq_len:
                                              [words_in_seq_len]
                                          })
         print("=====outputs 306====", outputs)
         input_words.append(3)
         input_words.extend(outputs[0])
         in_seq_len = len(input_words)
         output = self.infer_session.run(self.infer_output,
                                         feed_dict={
                                             self.infer_in_seq:
                                             [input_words],
                                             self.infer_in_seq_len:
                                             [in_seq_len]
                                         })
         head = reader.encode_text(text.split(' '),
                                   self.infer_vocab_indices)
         output_one, output_two = reader.headrandom(input_words, output[0],
                                                    head)
         output_top = reader.decode_text(output_one, self.infer_vocabs)
         output_text = reader.decode_text(output_two, self.infer_vocabs)
         return output_top, output_text
Ejemplo n.º 5
0
    def infer(self, text):
        input_words = reader.encode_text(text.split(' '),
                                         self.infer_vocab_indices)
        if not self.init_infer:
            print("模型未初始化")
        with self.infer_graph.as_default():
            # print("===text==", text.split(' ') + ['</s>'])
            len_text = len(text.split(' '))
            input_words = reader.handleData(input_words, len_text,
                                            self.batch_size)
            words_in_seq = input_words
            words_in_seq_len = len(input_words)
            print("===infer_in_seq_len====", self.infer_in_seq_len)

            outputs = self.infer_session.run(self.infer_output,
                                             feed_dict={
                                                 self.infer_in_seq:
                                                 [words_in_seq],
                                                 self.infer_in_seq_len:
                                                 [words_in_seq_len]
                                             })
            input_words.append(3)
            input_words.extend(outputs[0])
            in_seq_len = len(input_words)

            output = self.infer_session.run(self.infer_output,
                                            feed_dict={
                                                self.infer_in_seq:
                                                [input_words],
                                                self.infer_in_seq_len:
                                                [in_seq_len]
                                            })
            output_top = reader.decode_text(input_words, self.infer_vocabs)
            output_text = reader.decode_text(output[0], self.infer_vocabs)
            # print("return=====",output[0])
            return output_top, output_text
Ejemplo n.º 6
0
    def train(self, epochs, start=0):
        if not self.init_train:
            raise Exception('Train graph is not inited!')
        with self.train_graph.as_default():
            if path.isfile(self.model_file + '.meta') and self.restore_model:
                print("Reloading model file before training.")
                self.train_saver.restore(self.train_session, self.model_file)
            else:
                self.train_session.run(self.train_init)
            total_loss = 0
            for step in range(start, epochs):
                data = next(self.train_data)
                in_seq = data['in_seq_ids']
                in_seq_len = data['in_seq_len']
                target_seq = data['target_seq_ids']
                target_seq_len = data['target_seq_len']
                in_seq_mask = data['in_seq_mask']
                in_seq_segment = data['in_seq_segment']
                target_seq_mask = data['target_seq_mask']
                target_seq_segment = data['target_seq_segment']
                output, loss, train, summary,output_ ,_cost= self.train_session.run(
                        [self.train_output, self.loss, self.train_op, self.train_summary,self.output,self._cost],
                        feed_dict={
                            self.train_in_seq: in_seq,
                            self.train_in_seq_mask: in_seq_mask,
                            self.train_in_seq_segment: in_seq_segment,
                            self.train_in_seq_len: in_seq_len,
                            self.train_target_seq: target_seq,
                            self.train_target_seq_mask: target_seq_mask,
                            self.train_target_seq_segment: target_seq_segment,
                            self.train_target_seq_len: target_seq_len})
                # seq_output=
                [str(i) for i in output_.shape]
                print('output_.get_shape()',' '.join([str(i) for i in output_.shape]))
                print('_cost.get_shape()', ' '.join([str(i) for i in _cost.shape]))
                total_loss += loss
                self.log_writter.add_summary(summary, step)
                if step % self.save_step == 0:
                    self.train_saver.save(self.train_session, self.model_file)
                    print("Saving model. Step: %d, loss: %f" % (step,
                        total_loss / self.save_step))
                    # print sample output
                    sid = random.randint(0, self.batch_size-1)

                    # input_text = reader.decode_text(in_seq[sid],
                    #     self.eval_reader.vocabs)
                    # output_text = reader.decode_text(output[sid],
                    #         self.train_reader.vocabs)
                    # target_text = reader.decode_text(target_seq[sid],
                    #         self.train_reader.vocabs).split(' ')[1:]
                    # target_text = ' '.join(target_text)
                    input_text=' '.join(reader.encode_text(in_seq[sid],self.train_reader.vocabs))
                    # input_text=in_seq[sid]
                    output_text=' '.join(reader.encode_text(output[sid],self.train_reader.vocabs))
                    target_text=' '.join(reader.encode_text(target_seq[sid],self.train_reader.vocabs))

                    print('******************************')
                    print('src: ' + input_text)
                    print('output: ' + output_text)
                    print('target: ' + target_text)
                if step % self.eval_step == 0:
                    bleu_score = self.eval(step)
                    print("Evaluate model. Step: %d, score: %f, loss: %f" % (
                        step, bleu_score, total_loss / self.save_step))
                    logging.debug("Evaluate model. Step: , score: , loss: "+str(step)+str(bleu_score)+str(total_loss / self.save_step))
                    # eval_summary = tf.Summary(value=[tf.Summary.Value(
                    #     tag='bleu', simple_value=bleu_score)])
                    # self.log_writter.add_summary(eval_summary, step)
                if step % self.save_step == 0:
                    total_loss = 0