TensorFlow 2 "Time series forecasting tutorial" (https://www.tensorflow.org/tutorials/structured_data/time_series#recurrent_neural_network) gives an example of a LSTM multi-step prediction model that given a past history predicts a range of future values:

# Prepare data
train_data_multi = tf.data.Dataset.from_tensor_slices((x_train_multi, y_train_multi))
train_data_multi = train_data_multi.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()

val_data_multi = tf.data.Dataset.from_tensor_slices((x_val_multi, y_val_multi))
val_data_multi = val_data_multi.batch(BATCH_SIZE).repeat()

# Model with two LSTM layers
multi_step_model = tf.keras.models.Sequential()
multi_step_model.add(tf.keras.layers.LSTM(16, activation='relu'))
multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')

# Train model
multi_step_history = multi_step_model.fit(train_data_multi, epochs=EPOCHS,

Tutorial also shows how to plot some values predicted by LSTM:

def multi_step_plot(history, true_future, prediction):

for x, y in val_data_multi.take(3):
     multi_step_plot(x[0], y[0], multi_step_model.predict(x)[0])

Question: How to iterate all x-s and all y-s (true values) in a validation dataset (val_data_multi)? I can not figure out how to access these values in tf.data.Dataset.from_tensor_slices. Also, how to get predictions from the model for all entries in validation dataset to be next used in calculation of mean MAE and RMSE of the model?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.