Exemplo n.º 1
0
    def fit(self, x_train, y_train, x_val, y_val):
        if self.verbose > 0:
            print("[{}] Fitting the regressor with data of {}".format(
                self.name, x_train.shape))

        start_time = time.time()

        self.model = self.model.fit(x_train, y_train)

        y_pred, _ = self.predict(x_train)
        df_metrics = calculate_regression_metrics(y_train, y_pred)

        self.train_duration = time.time() - start_time
        df_metrics["duration"] = self.train_duration
        save_train_duration(self.output_directory + 'train_duration.csv',
                            self.train_duration)
        self.train_metrics = df_metrics

        if self.verbose > 0:
            print("[{}] Fitting completed, took {}s".format(
                self.name, self.train_duration))
            print("[{}] Fitting completed, best RMSE={}, MAE={}".format(
                self.name, df_metrics["rmse"][0], df_metrics["mae"][0]))

        return df_metrics
Exemplo n.º 2
0
    def fit(self, x_train, y_train, x_val, y_val):
        if self.verbose > 0:
            print("[{}] Fitting the regressor with data of {}".format(self.name, x_train.shape))

        batch_size = 16
        nb_epochs = 2000

        mini_batch_size = int(min(x_train.shape[0] / 10, batch_size))

        start_time = time.time()

        hist = self.model.fit(x_train, y_train, batch_size=mini_batch_size, epochs=nb_epochs,
                              verbose=self.verbose, validation_data=(x_val, y_val), callbacks=self.callbacks)

        self.train_duration = time.time() - start_time

        y_pred, _ = self.predict(x_train)
        df_metrics = calculate_regression_metrics(y_train, y_pred)
        df_metrics["duration"] = self.train_duration
        save_train_duration(self.output_directory + 'train_duration.csv', self.train_duration)
        self.train_metrics = df_metrics

        self.model.save(self.output_directory + 'last_model.hdf5')

        save_logs_for_regression_deep_learning(self.output_directory, hist)

        keras.backend.clear_session()

        if self.verbose > 0:
            print("[{}] Fitting completed, took {}s".format(self.name, self.train_duration))
            print("[{}] Fitting completed, RMSE={}, MAE={}".format(self.name,
                                                                   df_metrics["rmse"][0],
                                                                   df_metrics["mae"][0]))

        return df_metrics
Exemplo n.º 3
0
    def fit(self, x_train: np.array, y_train: np.array):
        """
        Fit Linear model

        Inputs:
            x_train: training data (num_examples, num_timestep, num_channels) or (num_examples, num_features)
            y_train: training target
        """
        print("[{}] Fitting the regressor with data of {}".format(
            self.name, x_train.shape))

        start_time = time.perf_counter()

        if len(x_train.shape) == 3:
            x_train = x_train.reshape(x_train.shape[0],
                                      x_train.shape[1] * x_train.shape[2])

        self.cv(x_train, y_train)

        self.model.fit(x_train, y_train)

        self.train_duration = time.perf_counter() - start_time

        save_train_duration(self.output_directory + 'train_duration.csv',
                            self.train_duration)

        print("[{}] Fitting completed, took {}s".format(
            self.name, self.train_duration))
Exemplo n.º 4
0
    def fit(self, x_train, y_train, x_val=None, y_val=None, monitor_val=False):
        print('[{}] Training'.format(self.name))

        start_time = time.perf_counter()

        self.X_train = x_train
        self.y_train = y_train
        self.X_val = x_val
        self.y_val = y_val

        epochs = self.epochs
        batch_size = self.batch_size
        mini_batch_size = int(min(x_train.shape[0] / 10, batch_size))

        file_path = self.output_directory + self.best_model_file
        if (x_val is not None) and monitor_val:
            reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(
                monitor='val_loss', factor=0.5, patience=50, min_lr=0.0001)
            model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
                filepath=file_path, monitor='val_loss', save_best_only=True)
        else:
            reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='loss',
                                                             factor=0.5,
                                                             patience=50,
                                                             min_lr=0.0001)
            model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
                filepath=file_path, monitor='loss', save_best_only=True)
        self.callbacks = [reduce_lr, model_checkpoint]

        # train the model
        if x_val is not None:
            self.hist = self.model.fit(x_train,
                                       y_train,
                                       validation_data=(x_val, y_val),
                                       verbose=self.verbose,
                                       epochs=epochs,
                                       batch_size=mini_batch_size,
                                       callbacks=self.callbacks)
        else:
            self.hist = self.model.fit(x_train,
                                       y_train,
                                       verbose=self.verbose,
                                       epochs=epochs,
                                       batch_size=mini_batch_size,
                                       callbacks=self.callbacks)

        self.train_duration = time.perf_counter() - start_time

        save_train_duration(self.output_directory + 'train_duration.csv',
                            self.train_duration)

        print('[{}] Training done!, took {}s'.format(self.name,
                                                     self.train_duration))
    def fit(self, x_train, y_train):
        print("[{}] Fitting the regressor with data of {}".format(self.name, x_train.shape))

        start_time = time.perf_counter()

        if len(x_train.shape) == 3:
            x_train = x_train.reshape(x_train.shape[0], x_train.shape[1] * x_train.shape[2])

        self.cv(x_train, y_train)

        self.model.fit(x_train, y_train)

        self.train_duration = time.perf_counter() - start_time

        save_train_duration(self.output_directory + 'train_duration.csv', self.train_duration)

        print("[{}] Fitting completed, took {}s".format(self.name, self.train_duration))
    def fit(self, x_train, y_train, x_val=None, y_val=None):
        start_time = time.perf_counter()
        print('[{}] Generating kernels'.format(self.name))
        self.kernels = generate_kernels(x_train.shape[1], self.n_kernels,
                                        x_train.shape[2])
        print('[{}] Applying kernels'.format(self.name))
        x_training_transform = apply_kernels(x_train, self.kernels)

        print('[{}] Training'.format(self.name))
        self.regressor.fit(x_training_transform, y_train)
        self.train_duration = time.perf_counter() - start_time

        save_train_duration(self.output_directory + 'train_duration.csv',
                            self.train_duration)

        print('[{}] Training done!, took {}s'.format(self.name,
                                                     self.train_duration))
Exemplo n.º 7
0
    def fit(self, x_train, y_train, x_val, y_val):
        # if len(keras.backend.tensorflow_backend._get_available_gpus()) == 0:
        #     print('error no gpu')
        #     exit()
        if self.verbose > 0:
            print("[{}] Fitting the regressor with data of {}".format(
                self.name, x_train.shape))

        # x_val and y_val are only used to monitor the test loss and NOT for training

        if self.batch_size is None:
            mini_batch_size = int(min(x_train.shape[0] / 10, 16))
        else:
            mini_batch_size = self.batch_size

        start_time = time.time()
        hist = self.model.fit(x_train,
                              y_train,
                              batch_size=mini_batch_size,
                              epochs=self.nb_epochs,
                              verbose=self.verbose,
                              validation_data=(x_val, y_val),
                              callbacks=self.callbacks)

        self.train_duration = time.time() - start_time
        y_pred, _ = self.predict(x_train)
        df_metrics = calculate_regression_metrics(y_train, y_pred)
        df_metrics["duration"] = self.train_duration
        save_train_duration(self.output_directory + 'train_duration.csv',
                            self.train_duration)
        self.train_metrics = df_metrics

        self.model.save(self.output_directory + 'last_model.hdf5')

        save_logs_for_regression_deep_learning(self.output_directory, hist)

        keras.backend.clear_session()

        if self.verbose > 0:
            print("[{}] Fitting completed, took {}s".format(
                self.name, self.train_duration))
            print("[{}] Fitting completed, RMSE={}, MAE={}".format(
                self.name, df_metrics["rmse"][0], df_metrics["mae"][0]))

        return df_metrics
Exemplo n.º 8
0
    def fit(self, x_train, y_train, x_val=None, y_val=None, monitor_val=False):
        """
        Fit DL models

        Inputs:
            x_train: training data (num_examples, num_timestep, num_channels)
            y_train: training target
            x_val: validation data (num_examples, num_timestep, num_channels)
            y_val: validation target
            monitor_val: boolean indicating if model selection should be done on validation
        """
        print('[{}] Training'.format(self.name))

        start_time = time.perf_counter()

        self.X_train = x_train
        self.y_train = y_train
        self.X_val = x_val
        self.y_val = y_val

        epochs = self.epochs
        batch_size = self.batch_size
        mini_batch_size = int(min(x_train.shape[0] / 10, batch_size))

        file_path = self.output_directory + self.best_model_file
        if (x_val is not None) and monitor_val:
            reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                             factor=0.5, patience=50,
                                                             min_lr=0.0001)
            model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=file_path,
                                                                  monitor='val_loss',
                                                                  save_best_only=True)
        else:
            reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='loss',
                                                             factor=0.5, patience=50,
                                                             min_lr=0.0001)
            model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=file_path,
                                                                  monitor='loss',
                                                                  save_best_only=True)
        self.callbacks = [reduce_lr, model_checkpoint]

        # train the model
        if x_val is not None:
            self.hist = self.model.fit(x_train, y_train,
                                       validation_data=(x_val, y_val),
                                       verbose=self.verbose,
                                       epochs=epochs,
                                       batch_size=mini_batch_size,
                                       callbacks=self.callbacks)
        else:
            self.hist = self.model.fit(x_train, y_train,
                                       verbose=self.verbose,
                                       epochs=epochs,
                                       batch_size=mini_batch_size,
                                       callbacks=self.callbacks)

        self.train_duration = time.perf_counter() - start_time

        save_train_duration(self.output_directory + 'train_duration.csv', self.train_duration)

        print('[{}] Training done!, took {}s'.format(self.name, self.train_duration))

        plot_epochs_metric(self.hist,
                           self.output_directory + 'epochs_loss.png',
                           metric='loss',
                           model=self.name)
        for m in self.metrics:
            plot_epochs_metric(self.hist,
                               self.output_directory + 'epochs_{}.png'.format(m),
                               metric=m,
                               model=self.name)