Hi,
I have created a multi-output, multi-step time-series model from the tensorflow time-series window generator example at URL Time series forecasting | TensorFlow Core but I keep getting errors on the loss function and metric function. You will see that I am using “MeanSquaredError” as per recommendation from Google Bard and extensive research. I am using Python 3.8 and Tensorflow 2.10.
My feature engineering, model build, and model training scripts are below, and I have provided comments to help myself and others understand what I am trying to accomplish. All help and advice are welcomed. I added multiple successive loss functions from a solution that I found on another website tensorflow - How the MSE loss calculated for multiple neurons in output layer - Stack Overflow , and make some sense as there are multiple sets of prediction values it still did not work and Google Bard, suggested directly naming the labels (y_true) but with additional question, how would the model know to properly map the “y_true” values with the “y_pred” values?
I was able to confirm the output shape of the model by running
sample_input = np.random.rand(90, 30, 315)
sample_output = CNN_Multi_LSTM_model.predict(sample_input)
print(sample_output.shape)
3/3 [==============================] - 5s 30ms/step
(90, 4, 315)
code:
Num_of_features = 315
OUT_STEPS = 4
INPUT_STEPS = 30
weather_df_multi_window = WindowGenerator(input_width = INPUT_STEPS,
label_width = OUT_STEPS,
shift = OUT_STEPS,
train_df = train_weather_df,
val_df = validation_weather_df,
test_df = test_weather_df,
label_columns=[‘daily_weather_morning’, ‘daily_weather_afternoon’, ‘daily_weather_evening’, ‘daily_weather_night’])
output of feature and label shapes after processed with the window generator
#Inputs shape (batch, time, features): (90, 30, 315)
#Labels shape (batch, time, features): (90, 4, 4)
MAX_EPOCHS = 300
method does the following:
monitor the validation loss
compile using tf.losses.MeanAbsolutePercentageError()
fit the model with the training dataset, epochs, validation dataset and callback method
def compile_and_fit(model, window, patience=60):
early_stopping = tf.keras.callbacks.EarlyStopping(monitor=‘val_loss’,
patience=patience,
mode=‘min’)
Get the y_true values
y_true = window.example[1]
Reshape the y_true argument
#y_true = tf.reshape(y_true, (y_pred.shape[0], y_pred.shape[1], y_pred.shape[2]))
model.compile(loss=[tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError(),tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError()],
optimizer=tf.keras.optimizers.Adam(),
metrics=[tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError(),tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError()],
run_eagerly=True)
history = model.fit(window.train, epochs=MAX_EPOCHS,
validation_data=window.val,
callbacks=[early_stopping])
return history
Add a CNN layer
cnn_layer = tf.keras.layers.Conv1D(filters=32, kernel_size=(INPUT_STEPS,), activation=‘relu’)
Build the Multi-Stacked LSTM model
CNN_Multi_LSTM_model = tf.keras.models.Sequential([
cnn_layer,
tf.keras.layers.LSTM(128, activation=‘tanh’, return_sequences=True),
tf.keras.layers.LSTM(64, activation=‘tanh’, return_sequences=True),
tf.keras.layers.LSTM(32, activation=‘tanh’),
tf.keras.layers.Dense(OUT_STEPS*num_of_features,kernel_initializer=tf.initializers.zeros(), kernel_regularizer=tf.keras.regularizers.l2(0.001)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Reshape([OUT_STEPS, num_of_features])
])
history = compile_and_fit(CNN_Multi_LSTM_model, weather_df_multi_window)
The error being generated is as follows:
InvalidArgumentError Traceback (most recent call last)
Cell In[92], line 1
----> 1 history = compile_and_fit(CNN_Multi_LSTM_model, weather_df_multi_window)
Cell In[90], line 29, in compile_and_fit(model, window, patience)
21 # Reshape the y_true argument
22 #y_true = tf.reshape(y_true, (y_pred.shape[0], y_pred.shape[1], y_pred.shape[2]))
24 model.compile(loss=[tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError(),tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError()],
25 optimizer=tf.keras.optimizers.Adam(),
26 metrics=[tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError(),tf.keras.losses.MeanSquaredError(), tf.keras.losses.MeanSquaredError()],
27 run_eagerly=True)
—> 29 history = model.fit(window.train, epochs=MAX_EPOCHS,
30 validation_data=window.val,
31 callbacks=[early_stopping])
32 return history
File ~\anaconda3\envs\tf_210_gpu\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~\anaconda3\envs\tf_210_gpu\lib\site-packages\keras\losses.py:1486, in mean_squared_error(y_true, y_pred)
1484 y_pred = tf.convert_to_tensor(y_pred)
1485 y_true = tf.cast(y_true, y_pred.dtype)
→ 1486 return backend.mean(tf.math.squared_difference(y_pred, y_true), axis=-1)
InvalidArgumentError: {{function_node _wrapped__SquaredDifference_device/job:localhost/replica:0/task:0/device:GPU:0}} required broadcastable shapes [Op:SquaredDifference]