def train_epoch(self, epochs):
        if not self.init_train:
            raise Exception('Train graph is not inited')
        with self.train_graph.as_default():
            if os.path.isfile(cfg.save_path + '.meta'):
                print("##########################")
                print('#     Model restore..    #')
                print("##########################")
                self.train_saver.restore(self.train_session, cfg.save_path)
            else:
                self.train_session.run(self.train_init)
            total_loss = 0
            total_step = 0
            start_time =time.time()
            for e in range(epochs):
                for step in range(self.total_iter// self.batch_size):
                    data = next(self.train_data)
                    week_stock = data['week_stock']
                    month_stock = data['month_stock']
                    t_month_stock = data['t_month_stock']
                    decoder_input = data['decoder_input']
                    decoder_target = data['decoder_target']
                    batch_seq = batch_seq_len(data['decoder_target'])
                    _, loss, sample_id = self.train_session.run([self.optimizer, self.loss, self.sample_id], 
                                                            feed_dict = {self.week_input : week_stock,
                                                                         self.month_input : month_stock,
                                                                         self.t_month_input : t_month_stock,
                                                                         self.decoder_input : decoder_input,
                                                                         self.decoder_target : decoder_target,
                                                                        self.decoder_targets_length : batch_seq})
#                     total_loss += loss
#                 total_step += self.total_iter
#                 loss = total_loss/total_step
                end = time.time()
                print('epoch: {}|{}  minibatch loss: {:.6f}   Time: {:.1f} min'.format(e+1, epochs, loss, (end-start_time)/60 ))
                
                if e % 50 ==0:
                    self.train_saver.save(self.train_session, cfg.save_path)
                    #랜덤 sid 선택, training output_text
                    sid = random.randint(0, self.batch_size-1)
                    target_text = decode_text(decoder_target[sid],self.idx2word_dict)
                    output_text = decode_text(sample_id[sid],self.idx2word_dict)
                    print('============ training sample text =============')
                    print('training_target :' + target_text)
                    print('training_output :' + output_text)
                    print('===============================================')
                    self.eval()
Beispiel #2
0
def get_output():
    data = request.get_json()

    input_sample = data["sample"]
    model_name = data["model"]
    sup_ids = input_sample["sup_ids"] if "sup_ids" in input_sample else None

    answer_text = decode_text(input_sample["answer"])
    context = decode_text(input_sample["context"])

    answer_start = context.lower().find(answer_text.lower())

    answer_dict = {"text": answer_text, "answer_start": answer_start}

    print(sup_ids)

    if not sup_ids:
        if answer_start != -1 and answer_text != "":
            sup_ids = [find_sup_char_ids(context, answer_start)]

    sample = {
        "id": decode_text(input_sample["id"]),
        "question": decode_text(input_sample["question"]),
        "context": context,
        "answer": answer_dict,
        "sup_ids": sup_ids
    }

    prediction, layers, token_indices = generate_model_output(
        sample, model_name)

    output = {
        'hidden_states': layers,
        'prediction': prediction,
        'token_indices': token_indices
    }

    return jsonify(output)
    def eval(self):
        with self.eval_graph.as_default():
            self.eval_saver.restore(self.eval_session, cfg.save_path)
            all_bleu = [0] * 4
            eval_mask_weights = tf.ones(shape=[self.batch_size, self.max_length],dtype=tf.float64)
            for step in range(self.val_total_iter//self.batch_size):
                data = next(self.val_data)
                week_stock = data['week_stock']
                month_stock = data['month_stock']
                t_month_stock = data['t_month_stock']
                batch_seq = batch_seq_len(data['decoder_target'])
                #beam search_output
                beam_output = self.eval_session.run([self.predicted_ids], 
                                                    feed_dict = {self.eval_week_input : week_stock,
                                                                 self.eval_month_input : month_stock,
                                                                 self.eval_t_month_input : t_month_stock,
                                                                 self.eval_decoder_targets_length : batch_seq
                                                                })   
                
                target_text = idx_to_text(data['decoder_input'][:,1:],self.idx2word_dict) 
                target_text = remove_sent_pad(target_text)
                
                beam_output = np.squeeze(np.array(beam_output),axis=0)
                output_text = idx_to_text(beam_output[:,:,0], self.idx2word_dict)
                bleu_score = self.cal_metrics(target_text, output_text)

                for idx,score in enumerate(bleu_score):
                    all_bleu[idx] += score
            print('================ BLEU score ================')
            for idx, bleu in enumerate(bleu_score):#2,3,4,5
                print('BLEU-{} : {}'.format(idx+2, bleu))
            sid = random.randint(0, self.batch_size-1)
            target_text = decode_text(data['decoder_target'][sid],self.idx2word_dict)
            output_text = decode_text(beam_output[sid,:,0],self.idx2word_dict)
            print('============= Beam search text =============')
            print('infer_target : ' + target_text)
            print('beam_search  : ' + output_text)
            print('============================================')
Beispiel #4
0
 def search(entry, level):
     entry = dict_value(entry)
     if 'Title' in entry:
         if 'A' in entry or 'Dest' in entry:
             title = decode_text(str_value(entry['Title']))
             dest = entry.get('Dest')
             action = entry.get('A')
             se = entry.get('SE')
             yield (level, title, dest, action, se)
     if 'First' in entry and 'Last' in entry:
         for x in search(entry['First'], level + 1):
             yield x
     if 'Next' in entry:
         for x in search(entry['Next'], level):
             yield x
     return
Beispiel #5
0
 def search(entry, level):
     entry = dict_value(entry)
     if 'Title' in entry:
         if 'A' in entry or 'Dest' in entry:
             title = decode_text(str_value(entry['Title']))
             dest = entry.get('Dest')
             action = entry.get('A')
             se = entry.get('SE')
             yield (level, title, dest, action, se)
     if 'First' in entry and 'Last' in entry:
         for x in search(entry['First'], level+1):
             yield x
     if 'Next' in entry:
         for x in search(entry['Next'], level):
             yield x
     return