Beispiel #1
0
 def test(self):
     """
     Test Data set
     :param X:
     :param y:
     :return:
     """
     return Validator.get_accuracy_with_confusion_matrix(self.model, self.X_test, self.Y_test)
Beispiel #2
0
    def train(self):
        iterations_count, iterations_per_epoch = Worker.get_iterations_count(self.X_train.shape[0], self.batch_size,
                                                                             self.epochs_count)
        for t in range(iterations_count):
            self._send_pulse()

            # Maybe print training loss
            if self.debug and t % self.debug_every == 0:
                print('loss: %f after (Iteration %d / %d)' % (
                    self.loss,t + 1, iterations_count,))

            #Update learning rate after an epoch
            epoch_end = (t + 1) % iterations_per_epoch == 0
            if epoch_end:
                self.epoch += 1
                for k in range(len(self.model.layer_objs)):
                    self.W_configs[k].decay(self.lr_decay)
                    self.b_configs[k].decay(self.lr_decay)

            #Print accuracy after each epoch
            if epoch_end:
                train_acc = Validator.get_accuracy(self.model, self.X_train, self.Y_train,
                                                   sample_size=1000)
                val_acc = Validator.get_accuracy(self.model, self.X_val, self.Y_val)

                if self.debug:
                    print('(Epoch %d / %d) train_acc: %f; val_acc: %f' % (
                        self.epoch, self.epochs_count, train_acc, val_acc))

                if val_acc > self.best_val_acc:
                    self.best_val_acc = val_acc

                    self.best_Ws = []
                    self.best_bs = []
                    for layer_obj in self.model.layer_objs:
                        self.best_Ws.append(layer_obj.W)
                        self.best_bs.append(layer_obj.b)

        for layer_id in range(len(self.model.layer_objs)):
            self.model.layer_objs[layer_id].W = self.best_Ws[layer_id]
            self.model.layer_objs[layer_id].b = self.best_bs[layer_id]