コード例 #1
0
def set_measured_flow(rnn_input, pred_forward, labels, mon_ratio):
    """

    :param rnn_input: shape(#n_flows, #time-steps)
    :param pred_forward: shape(#n_flows, #time-steps)
    :param labels: shape(n_flows, #time-steps)
    :return:
    """

    n_flows = rnn_input.shape[0]

    fw_losses = []
    for flow_id in range(rnn_input.shape[0]):
        idx_fw = labels[flow_id, 1:]

        fw_losses.append(
            error_ratio(y_true=rnn_input[flow_id, 1:][idx_fw == 1.0],
                        y_pred=pred_forward[flow_id, :-1][idx_fw == 1.0],
                        measured_matrix=np.zeros(idx_fw[idx_fw == 1.0].shape)))

    fw_losses = np.array(fw_losses)
    fw_losses[fw_losses == 0.] = np.max(fw_losses)

    w = calculate_flows_weights(fw_losses=fw_losses, measured_matrix=labels)

    sampling = np.zeros(shape=n_flows)
    m = int(mon_ratio * n_flows)

    w = w.flatten()
    sorted_idx_w = np.argsort(w)
    sampling[sorted_idx_w[:m]] = 1

    return sampling
コード例 #2
0
def calculate_forward_backward_loss(labels, pred_forward, pred_backward,
                                    rnn_input):
    l_fw, l_bw = [], []
    for flow_id in range(rnn_input.shape[0]):
        idx_fw = labels[flow_id, 1:]

        l_fw.append(
            error_ratio(y_true=rnn_input[flow_id, 1:][idx_fw == 1.0],
                        y_pred=pred_forward[flow_id, :-1][idx_fw == 1.0],
                        measured_matrix=np.zeros(idx_fw[idx_fw == 1.0].shape)))
        idx_bw = labels[flow_id, 0:-1]

        l_bw.append(
            error_ratio(y_true=rnn_input[flow_id, :-1][idx_bw == 1.0],
                        y_pred=pred_backward[flow_id, 1:][idx_bw == 1.0],
                        measured_matrix=np.zeros(idx_bw[idx_bw == 1.0].shape)))

    l_fw = np.array(l_fw)
    l_fw[l_fw == 0.] = np.max(l_fw)
    l_bw = np.array(l_bw)
    l_bw[l_bw == 0.] = np.max(l_bw)

    return l_fw, l_bw
コード例 #3
0
    def _test(self, sess, **kwargs):

        global_step = sess.run(tf.train.get_or_create_global_step())

        results_summary = pd.DataFrame(index=range(self._run_times))
        results_summary['No.'] = range(self._run_times)

        n_metrics = 4
        # Metrics: MSE, MAE, RMSE, MAPE, ER
        metrics_summary = np.zeros(shape=(self._run_times, self._horizon * n_metrics + 1))

        for i in range(self._run_times):
            self._logger.info('|--- Run time: {}'.format(i))
            # y_test = self._prepare_test_set()

            test_results = self._run_tm_prediction(sess, model=self._test_model)

            # y_preds:  a list of (batch_size, horizon, num_nodes, output_dim)
            test_loss, y_preds = test_results['loss'], test_results['y_preds']
            utils.add_simple_summary(self._writer, ['loss/test_loss'], [test_loss], global_step=global_step)

            y_preds = test_results['y_preds']
            y_preds = np.concatenate(y_preds, axis=0)

            y_truths = test_results['y_truths']
            y_truths = np.concatenate(y_truths, axis=0)
            scaler = self._data['scaler']
            predictions = []

            for horizon_i in range(self._horizon):
                y_truth = scaler.inverse_transform(y_truths[:, horizon_i, :, 0])

                y_pred = scaler.inverse_transform(y_preds[:, horizon_i, :, 0])
                predictions.append(y_pred)

                mse = metrics.masked_mse_np(preds=y_pred, labels=y_truth, null_val=0)
                mae = metrics.masked_mae_np(preds=y_pred, labels=y_truth, null_val=0)
                mape = metrics.masked_mape_np(preds=y_pred, labels=y_truth, null_val=0)
                rmse = metrics.masked_rmse_np(preds=y_pred, labels=y_truth, null_val=0)
                self._logger.info(
                    "Horizon {:02d}, MSE: {:.2f}, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}".format(
                        horizon_i + 1, mse, mae, rmse, mape
                    )
                )
                metrics_summary[i, horizon_i * n_metrics + 0] = mse
                metrics_summary[i, horizon_i * n_metrics + 1] = mae
                metrics_summary[i, horizon_i * n_metrics + 2] = rmse
                metrics_summary[i, horizon_i * n_metrics + 3] = mape

            tm_pred = scaler.inverse_transform(test_results['tm_pred'])
            g_truth = scaler.inverse_transform(self._data['test_data_norm'][self._seq_len:-self._horizon])
            m_indicator = test_results['m_indicator']
            er = error_ratio(y_pred=tm_pred,
                             y_true=g_truth,
                             measured_matrix=m_indicator)
            metrics_summary[i, -1] = er

            self._save_results(g_truth=g_truth, pred_tm=tm_pred, m_indicator=m_indicator, tag=str(i))

            print('ER: {}'.format(er))

        for horizon_i in range(self._horizon):
            results_summary['mse_{}'.format(horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 0]
            results_summary['mae_{}'.format(horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 1]
            results_summary['rmse_{}'.format(horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 2]
            results_summary['mape_{}'.format(horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 3]

        results_summary['er'] = metrics_summary[:, -1]
        results_summary.to_csv(self._log_dir + 'results_summary.csv', index=False)

        return
コード例 #4
0
def run_test(test_data2d, test_data_normalized2d, fwbw_conv_lstm_net, scalers,
             results_summary):
    err, r2_score, rmse = [], [], []
    err_ims, r2_score_ims, rmse_ims = [], [], []

    for i in range(Config.FWBW_CONV_LSTM_TESTING_TIME):
        print('|--- Run time {}'.format(i))

        test_data_normalize, init_data_normalize, test_data = prepare_test_set_last_5days(
            test_data2d, test_data_normalized2d)
        # test_data_normalize, init_data_normalize, test_data = prepare_test_set(test_data2d, test_data_normalized2d)

        init_data_normalize = np.reshape(
            init_data_normalize,
            newshape=(init_data_normalize.shape[0], Config.FWBW_CONV_LSTM_WIDE,
                      Config.FWBW_CONV_LSTM_HIGH))
        test_data_normalize = np.reshape(
            test_data_normalize,
            newshape=(test_data_normalize.shape[0], Config.FWBW_CONV_LSTM_WIDE,
                      Config.FWBW_CONV_LSTM_HIGH))

        measured_matrix_ims2d = np.zeros(
            (test_data.shape[0] - Config.FWBW_CONV_LSTM_IMS_STEP + 1,
             Config.FWBW_CONV_LSTM_WIDE * Config.FWBW_CONV_LSTM_HIGH))

        pred_tm, measured_matrix, ims_tm = predict_fwbw_conv_lstm(
            initial_data=init_data_normalize,
            test_data=test_data_normalize,
            model=fwbw_conv_lstm_net.model)

        pred_tm2d = np.reshape(pred_tm,
                               newshape=(pred_tm.shape[0],
                                         pred_tm.shape[1] * pred_tm.shape[2]))

        measured_matrix2d = np.reshape(
            measured_matrix,
            newshape=(measured_matrix.shape[0],
                      measured_matrix.shape[1] * measured_matrix.shape[2]))

        pred_tm_invert2d = scalers.inverse_transform(pred_tm2d)

        if np.any(np.isinf(pred_tm_invert2d)):
            raise ValueError('Value is infinity!')
        elif np.any(np.isnan(pred_tm_invert2d)):
            raise ValueError('Value is NaN!')

        err.append(
            error_ratio(y_true=test_data,
                        y_pred=pred_tm_invert2d,
                        measured_matrix=measured_matrix2d))
        r2_score.append(
            calculate_r2_score(y_true=test_data, y_pred=pred_tm_invert2d))
        rmse.append(
            calculate_rmse(y_true=test_data / 1000000,
                           y_pred=pred_tm_invert2d / 1000000))

        if Config.FWBW_CONV_LSTM_IMS:
            # Calculate error for multistep-ahead-prediction

            ims_tm2d = np.reshape(np.copy(ims_tm),
                                  newshape=(ims_tm.shape[0],
                                            ims_tm.shape[1] * ims_tm.shape[2]))

            ims_tm_invert2d = scalers.inverse_transform(ims_tm2d)

            ims_ytrue2d = ims_tm_test_data(test_data=test_data)

            err_ims.append(
                error_ratio(y_pred=ims_tm_invert2d,
                            y_true=ims_ytrue2d,
                            measured_matrix=measured_matrix_ims2d))

            r2_score_ims.append(
                calculate_r2_score(y_true=ims_ytrue2d, y_pred=ims_tm_invert2d))
            rmse_ims.append(
                calculate_rmse(y_true=ims_ytrue2d / 1000000,
                               y_pred=ims_tm_invert2d / 1000000))
        else:
            err_ims.append(0)
            r2_score_ims.append(0)
            rmse_ims.append(0)

        print('Result: err\trmse\tr2 \t\t err_ims\trmse_ims\tr2_ims')
        print('        {}\t{}\t{} \t\t {}\t{}\t{}'.format(
            err[i], rmse[i], r2_score[i], err_ims[i], rmse_ims[i],
            r2_score_ims[i]))
    results_summary['No.'] = range(Config.FWBW_CONV_LSTM_TESTING_TIME)
    results_summary['err'] = err
    results_summary['r2'] = r2_score
    results_summary['rmse'] = rmse
    results_summary['err_ims'] = err_ims
    results_summary['r2_ims'] = r2_score_ims
    results_summary['rmse_ims'] = rmse_ims

    print('Test: {}-{}-{}-{}'.format(Config.DATA_NAME, Config.ALG, Config.TAG,
                                     Config.SCALER))

    print('avg_err: {} - avg_rmse: {} - avg_r2: {}'.format(
        np.mean(np.array(err)), np.mean(np.array(rmse)),
        np.mean(np.array(r2_score))))

    return results_summary
コード例 #5
0
def run_test(test_data2d, test_data_normalized2d, lstm_net, scalers,
             results_summary):
    err, r2_score, rmse = [], [], []
    err_ims, r2_score_ims, rmse_ims = [], [], []

    for i in range(Config.RES_LSTM_TESTING_TIME):
        print('|--- Running time: {}'.format(i))

        # test_data_normalize, init_data_normalize, test_data = prepare_test_set(test_data2d, test_data_normalized2d)
        test_data_normalize, init_data_normalize, test_data = prepare_test_set_last_5days(
            test_data2d, test_data_normalized2d)

        ims_test_data = ims_tm_test_data(test_data=test_data)
        measured_matrix_ims = np.zeros(shape=ims_test_data.shape)

        pred_tm2d, measured_matrix2d, ims_tm2d = predict_lstm_nn(
            init_data=init_data_normalize,
            test_data=test_data_normalize,
            model=lstm_net.model)

        pred_tm_invert2d = scalers.inverse_transform(pred_tm2d)

        err.append(
            error_ratio(y_true=test_data,
                        y_pred=pred_tm_invert2d,
                        measured_matrix=measured_matrix2d))
        r2_score.append(
            calculate_r2_score(y_true=test_data, y_pred=pred_tm_invert2d))
        rmse.append(
            calculate_rmse(y_true=test_data / 1000000,
                           y_pred=pred_tm_invert2d / 1000000))

        if Config.RES_LSTM_IMS:
            ims_tm_invert2d = scalers.inverse_transform(ims_tm2d)

            err_ims.append(
                error_ratio(y_pred=ims_tm_invert2d,
                            y_true=ims_test_data,
                            measured_matrix=measured_matrix_ims))

            r2_score_ims.append(
                calculate_r2_score(y_true=ims_test_data,
                                   y_pred=ims_tm_invert2d))
            rmse_ims.append(
                calculate_rmse(y_true=ims_test_data / 1000000,
                               y_pred=ims_tm_invert2d / 1000000))

        else:
            err_ims.append(0)
            r2_score_ims.append(0)
            rmse_ims.append(0)

        print('Result: err\trmse\tr2 \t\t err_ims\trmse_ims\tr2_ims')
        print('        {}\t{}\t{} \t\t {}\t{}\t{}'.format(
            err[i], rmse[i], r2_score[i], err_ims[i], rmse_ims[i],
            r2_score_ims[i]))

    results_summary['No.'] = range(Config.RES_LSTM_TESTING_TIME)
    results_summary['err'] = err
    results_summary['r2'] = r2_score
    results_summary['rmse'] = rmse
    results_summary['err_ims'] = err_ims
    results_summary['r2_ims'] = r2_score_ims
    results_summary['rmse_ims'] = rmse_ims

    print('Test: {}-{}-{}-{}'.format(Config.DATA_NAME, Config.ALG, Config.TAG,
                                     Config.SCALER))

    print('avg_err: {} - avg_rmse: {} - avg_r2: {}'.format(
        np.mean(np.array(err)), np.mean(np.array(rmse)),
        np.mean(np.array(r2_score))))
    return results_summary
コード例 #6
0
def run_test(test_data2d, test_data_normalized2d, fwbw_net, scalers,
             results_summary):
    err, r2_score, rmse = [], [], []
    err_ims, r2_score_ims, rmse_ims = [], [], []

    # per_gain = []

    for i in range(Config.RES_FWBW_LSTM_TESTING_TIME):
        print('|--- Run time {}'.format(i))
        # test_data_normalize, init_data_normalize, test_data = prepare_test_set(test_data2d, test_data_normalized2d)
        test_data_normalize, init_data_normalize, test_data = prepare_test_set_last_5days(
            test_data2d, test_data_normalized2d)
        ims_test_data = ims_tm_test_data(test_data=test_data)
        measured_matrix_ims = np.zeros(shape=ims_test_data.shape)

        pred_tm2d, measured_matrix2d, ims_tm2d = predict_fwbw_lstm_v2(
            initial_data=init_data_normalize,
            test_data=test_data_normalize,
            model=fwbw_net.model)

        pred_tm_invert2d = scalers.inverse_transform(pred_tm2d)
        # pred_tm_wo_invert2d = scalers.inverse_transform(pred_tm2d_wo)
        if np.any(np.isinf(pred_tm_invert2d)):
            raise ValueError('Value is infinity!')
        elif np.any(np.isnan(pred_tm_invert2d)):
            raise ValueError('Value is NaN!')

        err.append(
            error_ratio(y_true=test_data,
                        y_pred=pred_tm_invert2d,
                        measured_matrix=measured_matrix2d))
        r2_score.append(
            calculate_r2_score(y_true=test_data, y_pred=pred_tm_invert2d))
        rmse.append(
            calculate_rmse(y_true=test_data / 1000000,
                           y_pred=pred_tm_invert2d / 1000000))

        # err_wo = error_ratio(y_true=test_data, y_pred=pred_tm_wo_invert2d, measured_matrix=measured_matrix2d)
        # r2_score_wo = calculate_r2_score(y_true=test_data, y_pred=pred_tm_wo_invert2d)
        # rmse_wo = calculate_rmse(y_true=test_data / 1000000, y_pred=pred_tm_wo_invert2d / 1000000)

        if Config.RES_FWBW_LSTM_IMS:
            # Calculate error for multistep-ahead-prediction
            ims_tm_invert2d = scalers.inverse_transform(ims_tm2d)

            err_ims.append(
                error_ratio(y_pred=ims_tm_invert2d,
                            y_true=ims_test_data,
                            measured_matrix=measured_matrix_ims))

            r2_score_ims.append(
                calculate_r2_score(y_true=ims_test_data,
                                   y_pred=ims_tm_invert2d))
            rmse_ims.append(
                calculate_rmse(y_true=ims_test_data / 1000000,
                               y_pred=ims_tm_invert2d / 1000000))
        else:
            err_ims.append(0)
            r2_score_ims.append(0)
            rmse_ims.append(0)

        print('Result: err\trmse\tr2 \t\t err_ims\trmse_ims\tr2_ims')
        print('        {}\t{}\t{} \t\t {}\t{}\t{}'.format(
            err[i], rmse[i], r2_score[i], err_ims[i], rmse_ims[i],
            r2_score_ims[i]))
        # print('Result without data correction: mape \t err\trmse\tr2')
        # print('        {}\t{}\t{}\t{}'.format(mape_wo, err_wo, rmse_wo, r2_score_wo))
        #
        # if err[i] < err_wo:
        #     per_gain.append(np.abs(err[i] - err_wo) * 100.0 / err_wo)
        #     print('|-----> Performance gain: {}'.format(np.abs(err[i] - err_wo) * 100.0 / err_wo))
        # else:
        #     per_gain.append(-np.abs(err[i] - err_wo) * 100.0 / err[i])
        #     print('|-----> Performance gain: {}'.format(-np.abs(err[i] - err_wo) * 100.0 / err[i]))

    results_summary['No.'] = range(Config.RES_FWBW_LSTM_TESTING_TIME)
    results_summary['err'] = err
    results_summary['r2'] = r2_score
    results_summary['rmse'] = rmse
    results_summary['err_ims'] = err_ims
    results_summary['r2_ims'] = r2_score_ims
    results_summary['rmse_ims'] = rmse_ims

    print('Test: {}-{}-{}-{}'.format(Config.DATA_NAME, Config.ALG, Config.TAG,
                                     Config.SCALER))

    print('avg_err: {} - avg_rmse: {} - avg_r2: {}'.format(
        np.mean(np.array(err)), np.mean(np.array(rmse)),
        np.mean(np.array(r2_score))))

    # print('Avg_per_gain: {} - Confidence: {}'.format(np.mean(np.array(per_gain)),
    #                                                  calculate_confident_interval(per_gain)))

    return results_summary
コード例 #7
0
    def test(self):
        scaler = self._data['scaler']
        results_summary = pd.DataFrame(index=range(self._run_times))
        results_summary['No.'] = range(self._run_times)

        n_metrics = 4
        # Metrics: MSE, MAE, RMSE, MAPE, ER
        metrics_summary = np.zeros(shape=(self._run_times,
                                          self._horizon * n_metrics + 1))

        for i in range(self._run_times):
            self._logger.info('|--- Running time: {}/{}'.format(
                i, self._run_times))

            outputs = self._run_tm_prediction()

            tm_pred, m_indicator, y_preds = outputs['tm_pred'], outputs[
                'm_indicator'], outputs['y_preds']

            y_preds = np.concatenate(y_preds, axis=0)
            predictions = []
            y_truths = outputs['y_truths']
            y_truths = np.concatenate(y_truths, axis=0)

            for horizon_i in range(self._horizon):
                y_truth = scaler.inverse_transform(y_truths[:, horizon_i, :])

                y_pred = scaler.inverse_transform(y_preds[:, horizon_i, :])
                predictions.append(y_pred)

                mse = metrics.masked_mse_np(preds=y_pred,
                                            labels=y_truth,
                                            null_val=0)
                mae = metrics.masked_mae_np(preds=y_pred,
                                            labels=y_truth,
                                            null_val=0)
                mape = metrics.masked_mape_np(preds=y_pred,
                                              labels=y_truth,
                                              null_val=0)
                rmse = metrics.masked_rmse_np(preds=y_pred,
                                              labels=y_truth,
                                              null_val=0)
                self._logger.info(
                    "Horizon {:02d}, MSE: {:.2f}, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}"
                    .format(horizon_i + 1, mse, mae, rmse, mape))
                metrics_summary[i, horizon_i * n_metrics + 0] = mse
                metrics_summary[i, horizon_i * n_metrics + 1] = mae
                metrics_summary[i, horizon_i * n_metrics + 2] = rmse
                metrics_summary[i, horizon_i * n_metrics + 3] = mape

            tm_pred = scaler.inverse_transform(tm_pred)
            g_truth = scaler.inverse_transform(
                self._data['test_data_norm'][self._seq_len:-self._horizon])
            er = error_ratio(y_pred=tm_pred,
                             y_true=g_truth,
                             measured_matrix=m_indicator)
            metrics_summary[i, -1] = er

            self._save_results(g_truth=g_truth,
                               pred_tm=tm_pred,
                               m_indicator=m_indicator,
                               tag=str(i))

            self._logger.info('ER: {}'.format(er))

        for horizon_i in range(self._horizon):
            results_summary['mse_{}'.format(
                horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 0]
            results_summary['mae_{}'.format(
                horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 1]
            results_summary['rmse_{}'.format(
                horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 2]
            results_summary['mape_{}'.format(
                horizon_i)] = metrics_summary[:, horizon_i * n_metrics + 3]

        results_summary['er'] = metrics_summary[:, -1]
        results_summary.to_csv(self._log_dir + 'results_summary.csv',
                               index=False)
コード例 #8
0
def run_test(test_data2d, test_data_normalized2d, init_data2d, net, params, scalers):
    alg_name = Config.ALG
    tag = Config.TAG
    data_name = Config.DATA_NAME

    results_summary = pd.DataFrame(index=range(Config.FWBW_CONVLSTM_TESTING_TIME),
                                   columns=['No.', 'err', 'r2', 'rmse', 'err_ims', 'r2_ims', 'rmse_ims'])

    err, r2_score, rmse = [], [], []
    err_ims, r2_score_ims, rmse_ims = [], [], []

    measured_matrix_ims2d = np.zeros((test_data2d.shape[0] - Config.FWBW_CONVLSTM_IMS_STEP + 1,
                                      Config.FWBW_CONVLSTM_WIDE * Config.FWBW_CONVLSTM_HIGH))
    # if not os.path.isfile(Config.RESULTS_PATH + 'ground_true_{}.npy'.format(data_name)):
    #     np.save(Config.RESULTS_PATH + 'ground_true_{}.npy'.format(data_name),
    #             test_data2d)

    # if not os.path.isfile(Config.RESULTS_PATH + 'ground_true_scaled_{}_{}.npy'.format(data_name, Config.SCALER)):
    #     np.save(Config.RESULTS_PATH + 'ground_true_scaled_{}_{}.npy'.format(data_name, Config.SCALER),
    #             test_data_normalized2d)

    if not os.path.exists(Config.RESULTS_PATH + '{}-{}-{}-{}/'.format(data_name,
                                                                      alg_name, tag, Config.SCALER)):
        os.makedirs(Config.RESULTS_PATH + '{}-{}-{}-{}/'.format(data_name, alg_name, tag, Config.SCALER))

    for i in range(Config.FWBW_CONVLSTM_TESTING_TIME):
        print('|--- Run time {}'.format(i))

        init_data = np.reshape(init_data2d, newshape=(init_data2d.shape[0],
                                                      Config.FWBW_CONVLSTM_WIDE,
                                                      Config.FWBW_CONVLSTM_HIGH))
        test_data_normalized = np.reshape(test_data_normalized2d, newshape=(test_data_normalized2d.shape[0],
                                                                            Config.FWBW_CONVLSTM_WIDE,
                                                                            Config.FWBW_CONVLSTM_HIGH))

        pred_tm, measured_matrix, ims_tm = predict_fwbw_convlstm(initial_data=init_data,
                                                                 test_data=test_data_normalized,
                                                                 model=net.model)

        pred_tm2d = np.reshape(np.copy(pred_tm), newshape=(pred_tm.shape[0], pred_tm.shape[1] * pred_tm.shape[2]))
        measured_matrix2d = np.reshape(np.copy(measured_matrix),
                                       newshape=(measured_matrix.shape[0],
                                                 measured_matrix.shape[1] * measured_matrix.shape[2]))
        # np.save(Config.RESULTS_PATH + '{}-{}-{}-{}/pred_scaled-{}.npy'.format(data_name, alg_name, tag,
        #                                                                       Config.SCALER, i),
        #         pred_tm2d)

        pred_tm_invert2d = scalers.inverse_transform(pred_tm2d)

        if np.any(np.isinf(pred_tm_invert2d)):
            raise ValueError('Value is infinity!')
        elif np.any(np.isnan(pred_tm_invert2d)):
            raise ValueError('Value is NaN!')

        err.append(error_ratio(y_true=test_data2d, y_pred=pred_tm_invert2d, measured_matrix=measured_matrix2d))
        r2_score.append(calculate_r2_score(y_true=test_data2d, y_pred=pred_tm_invert2d))
        rmse.append(calculate_rmse(y_true=test_data2d / 1000000, y_pred=pred_tm_invert2d / 1000000))

        if Config.FWBW_IMS:
            # Calculate error for multistep-ahead-prediction

            ims_tm2d = np.reshape(np.copy(ims_tm), newshape=(ims_tm.shape[0], ims_tm.shape[1] * ims_tm.shape[2]))

            ims_tm_invert2d = scalers.inverse_transform(ims_tm2d)

            ims_ytrue2d = ims_tm_test_data(test_data=test_data2d)

            err_ims.append(error_ratio(y_pred=ims_tm_invert2d,
                                       y_true=ims_ytrue2d,
                                       measured_matrix=measured_matrix_ims2d))

            r2_score_ims.append(calculate_r2_score(y_true=ims_ytrue2d, y_pred=ims_tm_invert2d))
            rmse_ims.append(calculate_rmse(y_true=ims_ytrue2d / 1000000, y_pred=ims_tm_invert2d / 1000000))
        else:
            err_ims.append(0)
            r2_score_ims.append(0)
            rmse_ims.append(0)

        print('Result: err\trmse\tr2 \t\t err_ims\trmse_ims\tr2_ims')
        print('        {}\t{}\t{} \t\t {}\t{}\t{}'.format(err[i], rmse[i], r2_score[i],
                                                          err_ims[i], rmse_ims[i],
                                                          r2_score_ims[i]))
        # np.save(Config.RESULTS_PATH + '{}-{}-{}-{}/pred-{}.npy'.format(data_name, alg_name, tag,
        #                                                                Config.SCALER, i),
        #         pred_tm_invert2d)
        # np.save(Config.RESULTS_PATH + '{}-{}-{}-{}/measure-{}.npy'.format(data_name, alg_name, tag,
        #                                                                   Config.SCALER, i),
        #         measured_matrix2d)

    results_summary['No.'] = range(Config.FWBW_CONVLSTM_TESTING_TIME)
    results_summary['err'] = err
    results_summary['r2'] = r2_score
    results_summary['rmse'] = rmse
    results_summary['err_ims'] = err_ims
    results_summary['r2_ims'] = r2_score_ims
    results_summary['rmse_ims'] = rmse_ims

    results_summary.to_csv(Config.RESULTS_PATH +
                           '{}-{}-{}-{}/results.csv'.format(data_name, alg_name, tag, Config.SCALER),
                           index=False)

    print('Test: {}-{}-{}-{}'.format(data_name, alg_name, tag, Config.SCALER))
    print('avg_err: {} - avg_rmse: {} - avg_r2: {}'.format(np.mean(np.array(err)),
                                                           np.mean(np.array(rmse)),
                                                           np.mean(np.array(r2_score))))

    return
コード例 #9
0
def predict_fwbw_convlstm(initial_data, test_data, model):
    tf_a = np.array([1.0, 0.0])

    tm_labels = np.zeros(shape=(initial_data.shape[0] + test_data.shape[0], test_data.shape[1], test_data.shape[2]))
    tm_labels[0:initial_data.shape[0], :, :] = initial_data

    _tm_labels = np.zeros(shape=(initial_data.shape[0] + test_data.shape[0], test_data.shape[1], test_data.shape[2]))
    _tm_labels[0:initial_data.shape[0], :, :] = initial_data

    labels = np.zeros(shape=(initial_data.shape[0] + test_data.shape[0], test_data.shape[1], test_data.shape[2]))
    labels[0:initial_data.shape[0], :, :] = np.ones(shape=initial_data.shape)

    ims_tm = np.zeros(
        shape=(test_data.shape[0] - Config.FWBW_CONVLSTM_IMS_STEP + 1, test_data.shape[1], test_data.shape[2]))

    raw_data = np.zeros(shape=(initial_data.shape[0] + test_data.shape[0], test_data.shape[1], test_data.shape[2]))

    raw_data[0:initial_data.shape[0]] = initial_data
    raw_data[initial_data.shape[0]:] = test_data

    for ts in tqdm(range(test_data.shape[0])):

        # if Config.FWBW_IMS and (ts <= test_data.shape[0] - Config.FWBW_CONVLSTM_IMS_STEP):
        #     ims_tm[ts] = ims_tm_prediction(init_data_labels=tm_labels[ts:ts + Config.FWBW_CONVLSTM_STEP, :, :, :],
        #                                    forward_model=forward_model,
        #                                    backward_model=backward_model)
        rnn_input = np.zeros(
            shape=(Config.FWBW_CONVLSTM_STEP,
                   Config.FWBW_CONVLSTM_WIDE, Config.FWBW_CONVLSTM_HIGH, Config.FWBW_CONVLSTM_CHANNEL))

        rnn_input[:, :, :, 0] = tm_labels[ts:(ts + Config.FWBW_CONVLSTM_STEP)]
        rnn_input[:, :, :, 1] = labels[ts:(ts + Config.FWBW_CONVLSTM_STEP)]

        rnn_input_backward = np.flip(np.copy(rnn_input), axis=0)

        rnn_input_forward = np.expand_dims(rnn_input, axis=0)

        # Prediction results from forward network
        predict_tm, corr_data = model.predict(rnn_input_forward)  # shape(1, timesteps, od, od , 1)
        predict_tm = np.reshape(predict_tm, newshape=(predict_tm.shape[0], test_data.shape[1], test_data.shape[2]))

        # if ts == 20:
        #     plot_test_data('Before_update', raw_data[ts + 1:ts + Config.FWBW_CONVLSTM_STEP - 1],
        #                    predictX[:-2],
        #                    predictX_backward[2:],
        #                    tm_labels[ts + 1:ts + Config.FWBW_CONVLSTM_STEP - 1])

        # before_ = np.copy(tm_labels[ts + 1:ts + Config.FWBW_CONVLSTM_STEP - 1])

        # _err_1 = error_ratio(y_pred=tm_labels[ts:ts + Config.FWBW_CONVLSTM_STEP],
        #                      y_true=raw_data[ts:ts + Config.FWBW_CONVLSTM_STEP],
        #                      measured_matrix=labels[ts: ts + Config.FWBW_CONVLSTM_STEP])

        # Correcting the imprecise input data

        corr_data = np.reshape(corr_data, (Config.FWBW_CONVLSTM_STEP - 2,
                                           Config.FWBW_CONVLSTM_WIDE, Config.FWBW_CONVLSTM_HIGH))
        _labels = 1 - labels[(ts + 1):(ts + Config.FWBW_LSTM_STEP - 1)]

        corr_data = corr_data * _labels
        tm_labels[(ts + 1):(ts + Config.FWBW_CONVLSTM_STEP - 1)] = \
            tm_labels[(ts + 1):(ts + Config.FWBW_CONVLSTM_STEP - 1)] * \
            labels[(ts + 1):(ts + Config.FWBW_CONVLSTM_STEP - 1)] + \
            corr_data

        if Config.FWBW_CONVLSTM_RANDOM_ACTION:
            sampling = np.random.choice(tf_a, size=(test_data.shape[1], test_data.shape[2]),
                                        p=(Config.FWBW_CONVLSTM_MON_RAIO, 1 - Config.FWBW_CONVLSTM_MON_RAIO))
        # else:
        #     sampling = set_measured_flow_3d(rnn_input=tm_labels[ts:ts + Config.FWBW_CONVLSTM_STEP],
        #                                     labels=labels[ts:ts + Config.FWBW_CONVLSTM_STEP],
        #                                     forward_pred=predictX,
        #                                     backward_pred=predictX_backward)

        # Selecting next monitored flows randomly
        inv_sampling = 1 - sampling

        pred_tm = predict_tm * inv_sampling
        corrected_data = test_data[ts]
        ground_truth = corrected_data * sampling

        # Calculating the true value for the TM
        new_tm = pred_tm + ground_truth

        tm_labels[ts + Config.FWBW_CONVLSTM_STEP] = new_tm
        _tm_labels[ts + Config.FWBW_CONVLSTM_STEP] = new_tm
        labels[ts + Config.FWBW_CONVLSTM_STEP] = sampling

    _err_1 = error_ratio(y_pred=tm_labels[Config.FWBW_CONVLSTM_STEP:],
                         y_true=raw_data[Config.FWBW_CONVLSTM_STEP:],
                         measured_matrix=labels[Config.FWBW_CONVLSTM_STEP:])
    _err_2 = error_ratio(y_pred=_tm_labels[Config.FWBW_CONVLSTM_STEP:],
                         y_true=raw_data[Config.FWBW_CONVLSTM_STEP:],
                         measured_matrix=labels[Config.FWBW_CONVLSTM_STEP:])

    print('Err_w: {} -- Err_wo: {}'.format(_err_1, _err_2))

    return tm_labels[Config.FWBW_CONVLSTM_STEP:], labels[Config.FWBW_CONVLSTM_STEP:], ims_tm