def train_model(self, all_sequences, all_responses_true, use_pre_trained=False, pre_trained_modelpath='./model/pre-trained-model'):
    config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    with tf.Session(config=config) as sess:
        timestamp = str(int(time.time()))
        out_dir = os.path.abspath(os.path.join(os.curdir, 'log', timestamp))
        print('Writing log to {}\n'.format(out_dir))
        for var in tf.trainable_variables():
            tf.summary.histogram(name=var.name, values=var)
        loss_summary = tf.summary.scalar('summary_loss', self.loss)
        acc_summary = tf.summary.scalar('summary_accuracy', self.accuracy)
        train_summary_op = tf.summary.merge_all()
        train_summary_dir = os.path.join(out_dir, 'summaries', 'train')
        train_summary_writer = tf.summary.FileWriter(train_summary_dir, tf.get_default_graph())
        dev_summary_op = tf.summary.merge_all()
        dev_summary_dir = os.path.join(out_dir, 'summaries', 'dev')
        dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, tf.get_default_graph())
        checkpoint_dir = os.path.join(out_dir, 'checkpoints')
        checkpoint_prefix = os.path.join(checkpoint_dir, 'model')
        if (not os.path.exists(checkpoint_dir)):
            os.makedirs(checkpoint_dir)
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=self.num_checkpoints)
        sess.run(tf.global_variables_initializer())
        if use_pre_trained:
            print('reloading model parameters...')
            saver.restore(sess, pre_trained_modelpath)
        actions = all_responses_true[:]
        (history, history_len) = self.utils.multi_sequences_padding(all_sequences, self.max_sentence_len)
        true_utt_len = np.array(self.utils.get_sequences_length(all_responses_true, maxlen=self.max_sentence_len))
        true_utt = np.array(pad_sequences(all_responses_true, padding='post', maxlen=self.max_sentence_len))
        actions_len = np.array(self.utils.get_sequences_length(actions, maxlen=self.max_sentence_len))
        actions = np.array(pad_sequences(actions, padding='post', maxlen=self.max_sentence_len))
        (history, history_len) = (np.array(history), np.array(history_len))
        low = 0
        epoch = 1
        while (epoch <= self.max_epoch):
            n_sample = (min((low + self.batch_size), history.shape[0]) - low)
            negative_indices = [np.random.randint(0, actions.shape[0], n_sample) for _ in range(self.negative_samples)]
            negs = [actions[negative_indices[i], :] for i in range(self.negative_samples)]
            negs_len = [actions_len[negative_indices[i]] for i in range(self.negative_samples)]
            feed_dict = {self.utterance_ph: np.concatenate(([history[low:(low + n_sample)]] * (self.negative_samples + 1)), axis=0), self.all_utterance_len_ph: np.concatenate(([history_len[low:(low + n_sample)]] * (self.negative_samples + 1)), axis=0), self.response_ph: np.concatenate(([true_utt[low:(low + n_sample)]] + negs), axis=0), self.response_len: np.concatenate(([true_utt_len[low:(low + n_sample)]] + negs_len), axis=0), self.y_true: np.concatenate(([np.ones(n_sample)] + ([np.zeros(n_sample)] * self.negative_samples)), axis=0)}
            (_, step, summaries, loss, accuracy, y_logits, y_pred, y_true) = sess.run([self.train_op, self.global_step, train_summary_op, self.loss, self.accuracy, self.logits, self.y_pred, self.y_true], feed_dict)
            y_pred_proba = y_logits[:, 1]
            timestr = datetime.datetime.now().isoformat()
            print('{}: => epoch {} | step {} | loss {:.6f} | acc {:.6f}'.format(timestr, epoch, step, loss, accuracy))
            train_summary_writer.add_summary(summaries, step)
            current_step = tf.train.global_step(sess, self.global_step)
            low += n_sample
            if ((current_step % self.evaluate_every) == 0):
                pass
            if ((current_step % self.checkpoint_every) == 0):
                path = saver.save(sess=sess, save_path=checkpoint_prefix, global_step=self.global_step)
                print('\nSaved model checkpoint to {}\n'.format(path))
            if (low >= history.shape[0]):
                low = 0
                epoch += 1
def train_model(self, all_sequences, all_responses_true, use_pre_trained=False, pre_trained_modelpath='./model/pre-trained-model'):
    config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    with tf.Session(config=config) as sess:
        timestamp = str(int(time.time()))
        out_dir = os.path.abspath(os.path.join(os.curdir, 'log', timestamp))
        print('Writing log to {}\n'.format(out_dir))
        for var in tf.trainable_variables():
            tf.summary.histogram(name=var.name, values=var)
        loss_summary = tf.summary.scalar('summary_loss', self.loss)
        acc_summary = tf.summary.scalar('summary_accuracy', self.accuracy)
        train_summary_op = tf.summary.merge_all()
        train_summary_dir = os.path.join(out_dir, 'summaries', 'train')
        train_summary_writer = tf.summary.FileWriter(train_summary_dir, tf.get_default_graph())
        dev_summary_op = tf.summary.merge_all()
        dev_summary_dir = os.path.join(out_dir, 'summaries', 'dev')
        dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, tf.get_default_graph())
        checkpoint_dir = os.path.join(out_dir, 'checkpoints')
        checkpoint_prefix = os.path.join(checkpoint_dir, 'model')
        if (not os.path.exists(checkpoint_dir)):
            os.makedirs(checkpoint_dir)
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=self.num_checkpoints)
        sess.run(tf.global_variables_initializer())
        if use_pre_trained:
            print('reloading model parameters...')
            saver.restore(sess, pre_trained_modelpath)
        actions = all_responses_true[:]
        (history, history_len) = self.utils.multi_sequences_padding(all_sequences, self.max_sentence_len)
        true_utt_len = np.array(self.utils.get_sequences_length(all_responses_true, maxlen=self.max_sentence_len))
        true_utt = np.array(pad_sequences(all_responses_true, padding='post', maxlen=self.max_sentence_len))
        actions_len = np.array(self.utils.get_sequences_length(actions, maxlen=self.max_sentence_len))
        actions = np.array(pad_sequences(actions, padding='post', maxlen=self.max_sentence_len))
        (history, history_len) = (np.array(history), np.array(history_len))
        low = 0
        epoch = 1
        while (epoch <= self.max_epoch):
            n_sample = (min((low + self.batch_size), history.shape[0]) - low)
            negative_indices = [np.random.randint(0, actions.shape[0], n_sample) for _ in range(self.negative_samples)]
            negs = [actions[negative_indices[i], :] for i in range(self.negative_samples)]
            negs_len = [actions_len[negative_indices[i]] for i in range(self.negative_samples)]
            feed_dict = {self.utterance_ph: np.concatenate(([history[low:(low + n_sample)]] * (self.negative_samples + 1)), axis=0), self.all_utterance_len_ph: np.concatenate(([history_len[low:(low + n_sample)]] * (self.negative_samples + 1)), axis=0), self.response_ph: np.concatenate(([true_utt[low:(low + n_sample)]] + negs), axis=0), self.response_len: np.concatenate(([true_utt_len[low:(low + n_sample)]] + negs_len), axis=0), self.y_true: np.concatenate(([np.ones(n_sample)] + ([np.zeros(n_sample)] * self.negative_samples)), axis=0)}
            (_, step, summaries, loss, accuracy, y_logits, y_pred, y_true) = sess.run([self.train_op, self.global_step, train_summary_op, self.loss, self.accuracy, self.logits, self.y_pred, self.y_true], feed_dict)
            y_pred_proba = y_logits[:, 1]
            timestr = datetime.datetime.now().isoformat()
            print('{}: => epoch {} | step {} | loss {:.6f} | acc {:.6f}'.format(timestr, epoch, step, loss, accuracy))
            train_summary_writer.add_summary(summaries, step)
            current_step = tf.train.global_step(sess, self.global_step)
            low += n_sample
            if ((current_step % self.evaluate_every) == 0):
                pass
            if ((current_step % self.checkpoint_every) == 0):
                path = saver.save(sess=sess, save_path=checkpoint_prefix, global_step=self.global_step)
                print('\nSaved model checkpoint to {}\n'.format(path))
            if (low >= history.shape[0]):
                low = 0
                epoch += 1