示例#1
0
    def train(self, sess, dw, dt, qw, qt, a, m_dw, m_qw, tt, tm, c, m_c, cl,
              fnames, dropout, learning_rate):
        """
        train model
        Args:
        - data: (object) containing training data
        """
        feed_dict = {
            self.doc: dw,
            self.qry: qw,
            self.doc_char: dt,
            self.qry_char: qt,
            self.target: a,
            self.doc_mask: m_dw,
            self.qry_mask: m_qw,
            self.token: tt,
            self.char_mask: tm,
            self.cand: c,
            self.cand_mask: m_c,
            self.cloze: cl,
            self.keep_prob: 1 - dropout,
            self.lr: learning_rate
        }
        if self.use_feat:
            feat = prepare_input(dw, qw)
            feed_dict += {self.feat: feat}

        loss, acc, _, = \
            sess.run([self.loss, self.accuracy, self.updates], feed_dict)
        return loss, acc
示例#2
0
 def validate(self, sess, data):
     """
     test the model
     """
     loss = acc = n_exmple = 0
     tr = trange(len(data),
                 desc="loss: {:.3f}, acc: {:.3f}".format(0.0, 0.0),
                 leave=False)
     for dw, dt, qw, qt, a, m_dw, m_qw, tt, \
             tm, c, m_c, cl, fnames in data:
         start = time.time()
         feed_dict = {
             self.doc: dw,
             self.qry: qw,
             self.doc_char: dt,
             self.qry_char: qt,
             self.target: a,
             self.doc_mask: m_dw,
             self.qry_mask: m_qw,
             self.token: tt,
             self.char_mask: tm,
             self.cand: c,
             self.cand_mask: m_c,
             self.cloze: cl,
             self.keep_prob: 1.,
             self.lr: 0.
         }
         if self.use_feat:
             feat = prepare_input(dw, qw)
             feed_dict += {self.feat: feat}
         _loss, _acc = sess.run([self.loss, self.accuracy], feed_dict)
         n_exmple += dw.shape[0]
         loss += _loss
         acc += _acc
         tr.set_description("loss: {:.3f}, acc: {:.3f}".format(
             _loss, _acc / dw.shape[0]))
         tr.update()
     tr.close()
     loss /= n_exmple
     acc /= n_exmple
     spend = (time.time() - start) / 60
     statement = "loss: {:.3f}, acc: {:.3f}, time: {:.1f}(m)"\
         .format(loss, acc, spend)
     logging.info(statement)
     return loss, acc
示例#3
0
 def train(self, sess, dw, dsw, dt, qw, qt, nw, a, m_dw, m_dsw, m_qw, tt,
           tm, c, m_c, cl, fnames, dropout, learning_rate):
     """
     train model
     Args:
     - data: (object) containing training data
     """
     feed_dict = {self.doc_sent: dsw, self.qry: qw,
                  self.doc: dw, self.doc_mask: m_dw,
                  self.answer: a,
                  self.doc_sent_mask: m_dsw, self.qry_mask: m_qw,
                  self.candidate: c, self.candidate_mask: m_c, self.position: cl,
                  self.lr: learning_rate}
     if self.use_feat:
         feat = prepare_input(dw, qw)
         feed_dict += {self.feat: feat}
     loss, acc, _, test = \
         sess.run([self.loss, self.accuracy, self.updates, self.test], feed_dict)
     return loss, acc, test
示例#4
0
    def validate(self, sess, data):
        """
        test the model
        """
        loss = acc_p = acc_n = n_exmple = 0
        tr = trange(
            len(data),
            desc="loss: {:.3f}, acc: {:.3f}".format(0.0, 0.0),
            leave=False)
        start = time.time()
        for dw, dsw, dt, qw, qt, nw, a, m_dw, m_dsw, m_qw, tt, \
            tm, c, m_c, cl, fnames in data:
            feed_dict = {self.doc_sent: dsw, self.qry: qw,
                     	 self.doc: dw, self.doc_mask: m_dw,
                     	 self.answer: a,
                     	 self.doc_sent_mask: m_dsw, self.qry_mask: m_qw,
                     	 self.candidate: c, self.candidate_mask: m_c, self.position: cl,
                         self.lr: 0.0}
            if self.use_feat:
                feat = prepare_input(dw, qw)
                feed_dict += {self.feat: feat}
            _acc = sess.run(self.accuracy, feed_dict)
            # _acc = sess.run(self.seq_length, feed_dict)
            n_exmple += dw.shape[0]
            acc_p += _acc
            tr.set_description("loss: {:.3f}, acc: {:.3f}".
                               format(loss, _acc / dw.shape[0]))
            tr.update()
        tr.close()
        '''
        tr = trange(
            len(data),
            desc="loss: {:.3f}, acc: {:.3f}".format(0.0, 0.0),
            leave=False)
        start = time.time()
        for dw, dsw, dt, qw, qt, nw, a, m_dw, m_dsw, m_qw, tt, \
            tm, c, m_c, cl, fnames in data:
            feed_dict = {self.doc_sent: dsw, self.qry: qw
                     	 self.doc: dw, self.doc_mask: m_dw,
                     	 self.answer: a,
                     	 self.doc_sent_mask: m_dsw, self.qry_mask: m_qw, self.neg_mask: m_qw,
                     	 self.candidate: c, self.candidate_mask: m_c, self.position: cl,
                         self.keep_prob: 1.0,
                         self.lr: 0.0}
            if self.use_feat:
                feat = prepare_input(dw, qw)
                feed_dict += {self.feat: feat}
            _acc = sess.run(self.accuracy, feed_dict)
            acc_n += _acc
            tr.set_description("loss: {:.3f}, acc: {:.3f}".
                               format(loss, _acc / dw.shape[0]))
            tr.update()
        tr.close()
        '''
        loss /= n_exmple
        acc_p /= n_exmple
        #acc_n /= n_exmple
        spend = (time.time() - start) / 60
        statement = "\nloss: {:.3f}, acc_p: {:.3f}\n" \
            .format(loss, acc_p)

        logging.info(statement)

        return loss, acc_p
示例#5
0
 def validate(self, sess, data, write_results=False):
     """
     test the model
     """
     loss = acc = n_exmple = 0
     tr = trange(len(data),
                 desc="loss: {:.3f}, acc: {:.3f}".format(0.0, 0.0),
                 leave=False)
     for dw, dt, qw, qt, a, m_dw, m_qw, tt, \
             tm, c, m_c, cl, fnames in data:
         start = time.time()
         feed_dict = {
             self.doc: dw,
             self.qry: qw,
             self.doc_char: dt,
             self.qry_char: qt,
             self.target: a,
             self.doc_mask: m_dw,
             self.qry_mask: m_qw,
             self.token: tt,
             self.char_mask: tm,
             self.cand: c,
             self.cand_mask: m_c,
             self.cloze: cl,
             self.keep_prob: 1.,
             self.lr: 0.
         }
         if self.use_feat:
             feat = prepare_input(dw, qw)
             feed_dict += {self.feat: feat}
         _loss, _acc, pred_ans, attentions = sess.run(
             [self.loss, self.accuracy, self.pred_ans, self.attentions],
             feed_dict)
         n_exmple += dw.shape[0]
         loss += _loss
         acc += _acc
         tr.set_description("loss: {:.3f}, acc: {:.3f}".format(
             _loss, _acc / dw.shape[0]))
         tr.update()
         if write_results:
             for i, fname in enumerate(fnames):
                 doc_len = np.sum(m_dw[i])
                 qry_len = np.sum(m_qw[i])
                 result = {
                     'pred_ans': pred_ans[i],
                     'attentions': attentions[i, :, 0:doc_len, 0:qry_len],
                     'ans': a[i]
                 }
                 if pred_ans[i] == a[i]:
                     correct = 'correct'
                 else:
                     correct = 'incorrect'
                 question = os.path.basename(fname).split('.')[0]
                 pickle.dump(
                     result,
                     open(
                         'results/validation/{}/{}.pkl'.format(
                             correct, question), 'wb'))
     tr.close()
     loss /= n_exmple
     acc /= n_exmple
     merged = tf.summary.merge_all()
     spend = (time.time() - start) / 60
     statement = "loss: {:.3f}, acc: {:.3f}, time: {:.1f}(m)"\
         .format(loss, acc, spend)
     logging.info(statement)
     return loss, acc
示例#6
0
    def train(self,
              sess,
              dw,
              dt,
              qw,
              qt,
              a,
              m_dw,
              m_qw,
              tt,
              tm,
              c,
              m_c,
              cl,
              fnames,
              dropout,
              learning_rate,
              summary_step=False):
        """
        train model
        Args:
        - data: (object) containing training data
        """
        feed_dict = {
            self.doc: dw,
            self.qry: qw,
            self.doc_char: dt,
            self.qry_char: qt,
            self.target: a,
            self.doc_mask: m_dw,
            self.qry_mask: m_qw,
            self.token: tt,
            self.char_mask: tm,
            self.cand: c,
            self.cand_mask: m_c,
            self.cloze: cl,
            self.keep_prob: 1 - dropout,
            self.lr: learning_rate
        }
        if self.use_feat:
            feat = prepare_input(dw, qw)
            feed_dict += {self.feat: feat}

        tf.summary.scalar('learning_rate', self.lr)
        merged = tf.summary.merge_all()
        options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
        run_metadata = tf.RunMetadata()
        if summary_step:
            loss, acc, _, summary = \
                sess.run([self.loss, self.accuracy, self.updates, merged], feed_dict, options=options, run_metadata=run_metadata)
            # Create the Timeline object, and write it to a json file
            fetched_timeline = timeline.Timeline(run_metadata.step_stats)
            chrome_trace = fetched_timeline.generate_chrome_trace_format()
            with open('timeline_01.json', 'w') as f:
                f.write(chrome_trace)
            return loss, acc, summary
        else:
            loss, acc, _, = \
                sess.run([self.loss, self.accuracy, self.updates], feed_dict, options=options, run_metadata=run_metadata)
            # Create the Timeline object, and write it to a json file
            fetched_timeline = timeline.Timeline(run_metadata.step_stats)
            chrome_trace = fetched_timeline.generate_chrome_trace_format()
            with open('timeline_01.json', 'w') as f:
                f.write(chrome_trace)
            return loss, acc