def get_co2_whole_duration_social_indicators(self, user_features): with open('cfg/cnn_config.json') as f: training_config = json.load(f) train_x, train_y = utils.generate_time_series_df( self.train_features, self.train_labels, training_config['time_steps']) test_x, test_y = utils.generate_time_series_df( self.test_features, self.test_labels, training_config['time_steps']) tss = TimeSeriesSplit() _, n_features = train_x.shape model = DeepLearningModel(training_config, num_features=n_features, num_outputs=1) for train_idx, test_idx in tss.split(train_x): X, X_val = train_x.iloc[train_idx], train_x.iloc[test_idx] Y, Y_val = train_y.iloc[train_idx], train_y.iloc[test_idx] features, labels = utils.data_sequence_generator( X, Y, training_config['time_steps']) val_f, val_l = utils.data_sequence_generator( X_val, Y_val, training_config['time_steps']) model.train_with_validation_provided(features, labels, val_f, val_l) co2_pred = model.generate_future_prediction(user_features, test_x, test_y, self.pred_steps) co2_data_pred = np.array(co2_pred).reshape(-1, 1) co2_total_duration = np.concatenate( (self.co2_data_avlbl.to_numpy().reshape(-1, 1), co2_data_pred)) return co2_total_duration
print(train_labels.shape) print(test_features.shape) print(test_labels.shape) # Train model with 5 fold cross validation tss = TimeSeriesSplit() _, n_features = train_features.shape cnn = DeepLearningModel(training_config, num_features=n_features, num_outputs=1) print(cnn.model.summary()) losses = [] start = time.time() for train_idx, test_idx in tss.split(train_features): X, X_val = train_features.iloc[train_idx], train_features.iloc[test_idx] Y, Y_val = train_labels.iloc[train_idx], train_labels.iloc[test_idx] features, labels = utils.data_sequence_generator( X, Y, training_config['time_steps']) val_f, val_l = utils.data_sequence_generator(X_val, Y_val, training_config['time_steps']) h = cnn.train_with_validation_provided(features, labels, val_f, val_l) losses.append(h.history['loss']) end = time.time() print("TRAINING TIME: {}".format(end - start)) # Plot training loss loss_arr = np.zeros((50, 1)) for loss_per_fold in losses: for j, loss in enumerate(loss_per_fold): loss_arr[j] = loss_arr[j] + loss loss_arr = loss_arr / 5 fig1, ax1 = plt.subplots() ax1.plot(range(len(loss_arr)), loss_arr, label='Training Loss')
# 4. Evaluate the trained model. Calculate prediction latency. Get prediction MSE, MAE and Soft Accuracy. test_start = time.time_ns() co2 = train_y.values.tolist() + test_y.values.tolist() forecast = naive_forecast(co2, 1) test_end = time.time_ns() mse = np.mean(np.square(np.array(co2[1:]) - np.array(forecast))) mae = np.mean(np.abs(np.array(co2[1:]) - np.array(forecast))) soft_acc = soft_accuracy(np.array(co2[1:]), np.array(forecast)) print( "------------------------------------- NAIVE FORECAST ------------------------------------" ) print("TESTING TIME: {}".format(test_end - test_start)) print("\n\nTesting MSE: {}\nTesting Soft Accuracy: {}\nTesting MAE: {}". format(mse, soft_acc, mae)) test_f, test_l = utils.data_sequence_generator( features, labels, training_config_cnn['time_steps']) test_start = time.time() model_eval = dnn.model.evaluate(test_f, test_l) test_end = time.time() print( "------------------------------------- DNN MODEL ------------------------------------" ) print(dnn.model.summary()) print("\n\nPREDICTION LATENCY: {}s".format(test_end - test_start)) print("\n\nMSE: {}\nMAE: {}\nR2: {}\nSoft Accuracy: {}".format( model_eval[0], model_eval[1], model_eval[2], model_eval[3])) test_start = time.time() model_eval = lstm.model.evaluate(test_f, test_l) test_end = time.time() print(