def metric(y_true, y_pred):
        """
        Parameters
        ----------
        y_true : Keras tensor
            Keras tensor including the ground truth. Since the keras tensor includes an extra column to store the index of the data sample in the training set this column is ignored.
        y_pred : Keras tensor
            Keras tensor with the predictions of the contamination model (no data index).
        """

        return mean_absolute_error(y_true[:, :nout], y_pred[:, :nout])
Esempio n. 2
0
def mae(y_true, y_pred):  #array di N array di probabilità, N=batchsize
    y_true = tf.map_fn(lambda element: tf.math.argmax(element),
                       y_true,
                       dtype=tf.dtypes.int64)
    y_pred = tf.map_fn(lambda element: tf.math.argmax(element),
                       y_pred,
                       dtype=tf.dtypes.int64)
    #tf.print(tf.shape(y_true)) # [128 101]

    #ages_true = tf.map_fn(lambda true: K.argmax(true), y_true, dtype=tf.int64)
    #ages_pred = tf.map_fn(lambda pred: K.argmax(pred), y_pred, dtype=tf.int64)

    return mean_absolute_error(tf.dtypes.cast(y_true, dtype=tf.dtypes.float64),
                               tf.dtypes.cast(y_pred, dtype=tf.dtypes.float64))
    def metric(y_true, y_pred):
        """
        Parameters
        ----------
        y_true : Keras tensor
            Keras tensor including the ground truth
        y_pred : Keras tensor
            Keras tensor including the predictions of a heteroscedastic model. The predictions follow the order: (mean_0, S_0, mean_1, S_1, ...) with S_i the log of the variance for the ith output.
        """
        if nout > 1:
            y_out = K.reshape(y_pred[:, 0::nout], K.shape(y_true))
        else:
            y_out = K.reshape(y_pred[:, 0], K.shape(y_true))

        return mean_absolute_error(y_true, y_out)
Esempio n. 4
0
plt.title('Loss vs Epochs')
plt.xlabel('Epochs')
plt.ylabel('Loss')

#%%
# Predictions
forecast = []
for time in range(len(series) - window_size):
    forecast.append(model.predict(series[time:time + window_size][np.newaxis]))

forecast = forecast[split_time-window_size:]
results = np.array(forecast)[:, 0, 0]

plot_series(time_valid, x_valid)
plot_series(time_valid, results)
mae = mean_absolute_error(x_valid, results).numpy()
print(f'MAE: {mae}')

#%%
# Learning Rate Scheduler
dataset = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)

model_name = f'seq_dnn_lrsch-1010'

model = Sequential(layers=[
                            Dense(10, input_shape=[window_size], activation="relu"),
                            Dense(10, activation="relu"),
                            Dense(1)
                            ],
                    name=model_name
                            )
Esempio n. 5
0
def mae(y_true, y_pred):
    return mean_absolute_error(y_true, y_pred)
Esempio n. 6
0
                         _phase=0,
                         _level=5,
                         _auto_amplitude=0,
                         _seed=42)
    x_train = series[0:SPLIT_TIME]
    x_valid = series[SPLIT_TIME:]
    time = np.arange(0, len(series), 1)
    time_train = time[0:SPLIT_TIME]
    time_valid = time[SPLIT_TIME:]
    series.plot()
    data_train: tf.raw_ops.PrefetchDataset = TimeSeriesUtils.split_window(
        series=x_train.get_values(),
        window_size=WINDOW_SIZE,
        batch_size=32,
        buffer_size=1000)
    reg = TimeSeriesRegressor(_input_length=WINDOW_SIZE,
                              _model_weights="./models/TimeSeriesRegressor.h5",
                              _logs="./logs/TimeSeriesRegressor")
    reg.build_model()
    reg.fit(data_train)
    y: np.ndarray = reg.predict(
        TimeSeriesUtils.window(series.get_values(), WINDOW_SIZE, 32))
    y: np.ndarray = y.reshape((y.shape[0], ))
    # 最后一个时间窗口没有真值进行比对,所以截止到此
    y_valid = y[SPLIT_TIME - WINDOW_SIZE:-1]
    print("mae =", mean_absolute_error(x_valid.get_values(), y_valid).numpy())
    plt.figure(1)
    plt.plot(time_valid, x_valid.get_values())
    plt.plot(time_valid, y_valid)
    plt.show()
Esempio n. 7
0
                  (epoch, iteration, mse_train, mse_valid))

    model.summary()

    # Out-of-sample test (Evaluation)
    y_test_pred = model(x_test)

    true_price = sc.inverse_transform(y_test)
    pred_price = sc.inverse_transform(y_test_pred)

    true_open = true_price[:, 0]
    pred_open = pred_price[:, 0]

    mse = metrics.mean_squared_error(true_open, pred_open).numpy().mean()
    rmse = math.sqrt(mse)
    mae = metrics.mean_absolute_error(true_open, pred_open).numpy().mean()
    mape = metrics.mean_absolute_percentage_error(true_open,
                                                  pred_open).numpy().mean()
    print('MSE - %.6f' % mse)
    print('RMSE - %.6f' % rmse)
    print('MAE - %.6f' % mae)
    print('MAPE - %.6f' % mape)

    # Loss plotting
    plt.figure(figsize=(12, 5))
    plt.subplot(1, 2, 1)
    plot_loss_history(loss_history, n_epochs)

    plt.subplot(1, 2, 2)
    plt.plot(true_open, label='test true', color='black')
    plt.plot(pred_open, label='test prediction', color='green')
Esempio n. 8
0
def mae_in_months(x_p, y_p):
    '''function to return mae in months'''
    return mean_absolute_error((std_bone_age * x_p + mean_bone_age),
                               (std_bone_age * y_p + mean_bone_age))