简体   繁体   中英

Keras multivariate time series forecasting model returns NaN as MAE and loss

I have multivariate time series data, collected every 5 seconds for a few days. This includes columns of standardized data, which looks like below (few example values). "P1" is the label-column.

|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
|       | P1                    | P2                    | P3                   | AI_T_MOWA             | AI_T_OEL              | AI_T_KAT_EIN           | AI_T_KAT_AUS           | P-Oel                | P-Motorwasser        |
|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 0     | 0.8631193380009695    | 0.8964414887167506    | 0.8840858759128901   | -0.523186057460264    | -0.6599697679790338   | 0.8195843978382326     | 0.6536355179773343     | 2.0167991331023862   | 1.966765280217274    |
|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 1     | 2.375731412346451     | 2.416190921505275     | 2.3921080971495456   | 1.2838015319452019    | 0.6783070711474897    | 2.204838829646018      | 2.250184559609546      | 2.752702514412287    | 2.7863834647854797   |
|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 2     | 2.375731412346451     | 2.416190921505275     | 2.3921080971495456   | 1.2838015319452019    | 1.2914092683827934    | 2.2484584825559955     | 2.2968465552769324     | 2.4571347629025726   | 2.743245665597679    |
|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 3     | 2.3933199248388406    | 2.416190921505275     | 2.3753522946913606   | 1.2838015319452019    | 1.5485166414169536    | 2.2557284247076588     | 2.3039344533529906     | 2.31839887954087     | 2.7863834647854797   |
|-------|-----------------------|-----------------------|----------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|

Corresponding graphs of the standardized data show nothing out of the ordinary.

标准化数据的小提琴图

I have split this data into train, validation and test sets, so that my training data is the first 70% of overall data, the validation are the next 20% and the test are the last 10%.

train_df_st = df[0:int(self._n*0.7)]
val_df_st = df[int(self._n*0.7):int(self._n*0.9)]
test_df_st = df[int(self._n*0.9):]

I then generate windows through the WindowGenerator class from tensorflows tutorial like here .

Using a simple Baseline model that predicts the ouput the same as the input I get actual predictions, so I assume my generated windows are fine. The shapes of my batches are

Input shape: (32, 24, 193)
Output shape: (32, 24, 1)

Now to the tricky part: I obviously want to use another model for better predictions. I have tried out using Conv1D using only one column and that worked, so I wanted to try it with this as well. My windows look like:

CONV_WIDTH = 3
LABEL_WIDTH = 24
INPUT_WIDTH = LABEL_WIDTH + (CONV_WIDTH - 1)
conv_window = WindowGenerator(
    input_width=INPUT_WIDTH,
    label_width=LABEL_WIDTH,
    shift=1,
    train_df=train_df_st, val_df=val_df_st, test_df=test_df_st, label_columns=["P1"])
Total window size: 25
Input indices: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
Label indices: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
Label column name(s): ['P1']

I then define my model and use the compile_and_fit() method as used here .

conv_model = tf.keras.Sequential([
    tf.keras.layers.Conv1D(filters=32,
                           kernel_size=(CONV_WIDTH,),
                           activation='relu'),
    tf.keras.layers.Dense(units=32, activation='relu'),
    tf.keras.layers.Dense(units=1),
])

MAX_EPOCHS = 20

def compile_and_fit(model, window, patience=2):
  early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                    patience=patience,
                                                    mode='min')

  model.compile(loss=tf.losses.MeanSquaredError(),
                optimizer=tf.optimizers.Adam(),
                metrics=[tf.metrics.MeanAbsoluteError()])

  history = model.fit(window.train, epochs=MAX_EPOCHS,
                      validation_data=window.val,
                      callbacks=[early_stopping])
  return history

history = compile_and_fit(window=conv_window, model=conv_model)

Input and Output shapes are:

Input shape: (32, 26, 193)
Output shape: (32, 24, 1)

My final output however is only two epochs that show nan as mean absolute error as well as loss:

Epoch 1/20
382/382 [==============================] - 2s 4ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 2/20
382/382 [==============================] - 1s 3ms/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan

And if I plot some example windows I see that I get labels, but no predictions:

示例窗口上的 conv 模型预测

I have tried implementing yet another model (LSTM) with slightly different windows, but a similar approach, but I get the same NaN's, so I believe it is not my models problem, but something in my data?.

事实证明我的数据标准化是错误的,将其标准化,我得到的是实际值而不是 NaN。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM