def run_epoch(self, sess, verbose=10): total_loss = [] total_acc_num = [] total_num = [] for step, indices in enumerate( batch_index(len(self.train_doc_y), self.config.batch_size, 1), 1): feed_dict = self.create_feed_dict(self.train_x[indices], self.train_sen_len[indices], self.train_doc_len[indices], self.train_doc_y[indices], self.config.keep_prob1, self.config.keep_prob2) _, loss, acc_num, lr = sess.run( [self.train_op, self.doc_loss, self.accuracy_num, self.lr], feed_dict=feed_dict) total_loss.append(loss) total_acc_num.append(acc_num) total_num.append(len(indices)) if verbose and step % verbose == 0: print '\n[INFO] {} : loss = {}, acc = {}, lr = {}'.format( step, np.mean(total_loss[-verbose:]), sum(total_acc_num[-verbose:]) * 1.0 / sum(total_num[-verbose:]), lr) return np.mean(total_loss), sum(total_acc_num) * 1.0 / sum(total_num)
def run_op(self, sess, op, data_x, sen_len, doc_len, doc_y=None, kp1=1.0, kp2=1.0): res_list = [] len_list = [] for indices in batch_index(len(data_x), self.config.batch_size, 1, False, False): if doc_y is not None: feed_dict = self.create_feed_dict(data_x[indices], sen_len[indices], doc_len[indices], doc_y[indices], kp1, kp2) else: feed_dict = self.create_feed_dict(data_x[indices], sen_len[indices], doc_len[indices], None, kp1, kp2) res = sess.run(op, feed_dict=feed_dict) res_list.append(res.tolist()) len_list.append(len(indices)) if type(res_list[0]) is list: res = np.concatenate(res_list, axis=1) elif op is self.accuracy_num: res = sum(res_list) elif op is self.doc_logits: res = np.asarray(res_list) else: res = sum(res_list) * 1.0 / len(len_list) return res
def get_batch_data(xo, slo, yy, batch_size, kp1, kp2, is_shuffle=True): for index in batch_index(len(yy), batch_size, 1, is_shuffle): feed_dict = { x: xo[index], y: yy[index], sen_len: slo[index], keep_prob1: kp1, keep_prob2: kp2, } yield feed_dict, len(index)
def get_batch_data(xo, slo, dlo, xr, slr, dlr, yy, batch_size, kp1, kp2, is_shuffle=True): for index in batch_index(len(yy), batch_size, 1, is_shuffle): feed_dict = { x_o: xo[index], x_r: xr[index], y: yy[index], sen_len_o: slo[index], sen_len_r: slr[index], doc_len_o: dlo[index], doc_len_r: dlr[index], keep_prob1: kp1, keep_prob2: kp2, } yield feed_dict, len(index)
def get_batch_data(x_in, y_in, sen_len_in, doc_len_in, batch_size, kp1, kp2, is_shuffle=True): for index in batch_index(len(y_in), batch_size, 1, is_shuffle): feed_dict = { x: x_in[index], y: y_in[index], sen_len: sen_len_in[index], doc_len: doc_len_in[index], keep_prob1: kp1, keep_prob2: kp2, } yield feed_dict, len(index)
def get_batch_data(x1, x2, len1, len2, yy, batch_size, kp1, kp2, is_shuffle=True): for index in batch_index(len(yy), batch_size, 1, is_shuffle): feed_dict = { x_l: x1[index], x_r: x2[index], y: yy[index], len_l: len1[index], len_r: len2[index], keep_prob1: kp1, keep_prob2: kp2, } yield feed_dict, len(index)