Пример #1
0
    def train(self, train_words, train_chars, train_labels, test_words,
              test_chars, test_labels):
        global_test_acc, global_step, lr = 0.0, 0, self.cfg.lr
        num_batches = math.ceil(float(len(train_words) / self.cfg.batch_size))

        self.logger.info("start training...")
        for epoch in range(1, self.cfg.epochs + 1):
            self.logger.info("Epoch {}/{}:".format(epoch, self.cfg.epochs))
            train_words, train_chars, train_labels = sklearn.utils.shuffle(
                train_words, train_chars, train_labels)
            prog = Progbar(target=num_batches)

            for i, (b_words, b_seq_len, b_chars, b_char_seq_len,
                    b_labels) in enumerate(
                        batch_iter(train_words, train_chars, train_labels,
                                   self.cfg.batch_size)):
                global_step += 1
                batch_labels = []
                for j in range(self.num_classifier):
                    ecoc_array = self.nary_ecoc[:, j]
                    b_lbs = remap_labels(b_labels.copy(), ecoc_array)
                    b_lbs = dense_to_one_hot(b_lbs, self.num_class)
                    batch_labels.append(b_lbs)
                feed_dict = self.get_feed_dict(b_words,
                                               b_seq_len,
                                               b_chars,
                                               b_char_seq_len,
                                               batch_labels,
                                               lr=lr,
                                               training=True)
                _, pred_labels, loss = self.sess.run(
                    [self.train_op, self.pred_labels, self.loss],
                    feed_dict=feed_dict)
                acc = compute_ensemble_accuracy(pred_labels, self.nary_ecoc,
                                                b_labels)
                prog.update(i + 1, [("Global Step", global_step),
                                    ("Train Loss", loss),
                                    ("Train Acc", acc * 100)])
            accuracy, _ = self.test(test_words,
                                    test_chars,
                                    test_labels,
                                    batch_size=200,
                                    print_info=True,
                                    restore=False)

            if accuracy > global_test_acc:
                global_test_acc = accuracy
                self.save_session(epoch)
            lr = self.cfg.lr / (1 + epoch * self.cfg.lr_decay)
Пример #2
0
 def test(self, test_dataset, print_info=True):
     self.restore_last_session()
     test_imgs, test_labels = test_dataset.images, test_dataset.labels
     y_labels = []
     for j in range(self.num_classifier):
         ecoc_array = self.nary_ecoc[:, j]
         b_lbs = remap_labels(test_labels.copy(), ecoc_array)
         b_lbs = dense_to_one_hot(b_lbs, self.num_class)
         y_labels.append(b_lbs)
     feed_dict = self._get_feed_dict(test_imgs, y_labels)
     pred_labels, test_loss = self.sess.run([self.pred_labels, self.cost],
                                            feed_dict=feed_dict)
     test_acc = compute_ensemble_accuracy(pred_labels, self.nary_ecoc,
                                          test_dataset.labels)
     if print_info:
         self.logger.info(" -- Test Loss: {}, Test Accuracy: {}".format(
             test_loss, test_acc))
     return pred_labels
Пример #3
0
 def train(self, train_dataset, test_dataset):
     global_test_acc = 0.0
     global_step = 0
     test_imgs, test_labels = test_dataset.images, test_dataset.labels
     self.logger.info("start training...")
     for epoch in range(1, self.epochs + 1):
         self.logger.info("Epoch {}/{}:".format(epoch, self.epochs))
         num_batches = train_dataset.num_examples // self.batch_size
         prog = Progbar(target=num_batches)
         prog.update(0, [("Global Step", 0), ("Train Loss", 0.0),
                         ("Train Acc", 0.0), ("Test Loss", 0.0),
                         ("Test Acc", 0.0)])
         for i in range(num_batches):
             global_step += 1
             train_imgs, train_labels = train_dataset.next_batch(
                 self.batch_size)
             b_labels = []
             for j in range(self.num_classifier):
                 ecoc_array = self.nary_ecoc[:, j]
                 b_lbs = remap_labels(train_labels.copy(), ecoc_array)
                 b_lbs = dense_to_one_hot(b_lbs, self.num_class)
                 b_labels.append(b_lbs)
             feed_dict = self._get_feed_dict(train_imgs, b_labels, True)
             _, pred_labels, loss = self.sess.run(
                 [self.train_op, self.pred_labels, self.cost],
                 feed_dict=feed_dict)
             acc = compute_ensemble_accuracy(pred_labels, self.nary_ecoc,
                                             train_labels)
             if global_step % 100 == 0:
                 y_labels = []
                 for j in range(self.num_classifier):
                     ecoc_array = self.nary_ecoc[:, j]
                     b_lbs = remap_labels(test_labels.copy(), ecoc_array)
                     b_lbs = dense_to_one_hot(b_lbs, self.num_class)
                     y_labels.append(b_lbs)
                 feed_dict = self._get_feed_dict(test_imgs, y_labels)
                 test_pred_labels, test_loss = self.sess.run(
                     [self.pred_labels, self.cost], feed_dict=feed_dict)
                 test_acc = compute_ensemble_accuracy(
                     test_pred_labels, self.nary_ecoc, test_labels)
                 prog.update(i + 1, [("Global Step", int(global_step)),
                                     ("Train Loss", loss),
                                     ("Train Acc", acc),
                                     ("Test Loss", test_loss),
                                     ("Test Acc", test_acc)])
                 if test_acc > global_test_acc:
                     global_test_acc = test_acc
                     self.save_session(global_step)
             else:
                 prog.update(i + 1, [("Global Step", int(global_step)),
                                     ("Train Loss", loss),
                                     ("Train Acc", acc)])
         y_labels = []
         for j in range(self.num_classifier):
             ecoc_array = self.nary_ecoc[:, j]
             b_lbs = remap_labels(test_labels.copy(), ecoc_array)
             b_lbs = dense_to_one_hot(b_lbs, self.num_class)
             y_labels.append(b_lbs)
         feed_dict = self._get_feed_dict(test_imgs, y_labels)
         test_pred_labels, test_loss = self.sess.run(
             [self.pred_labels, self.cost], feed_dict=feed_dict)
         test_acc = compute_ensemble_accuracy(test_pred_labels,
                                              self.nary_ecoc, test_labels)
         self.logger.info(
             "Epoch: {}, Global Step: {}, Test Loss: {}, Test Accuracy: {}".
             format(epoch, global_step, test_loss, test_acc))