I have an LSTM network and I use it to predict. My whole data is an array with 10 rows and 1000 columns (10, 1000). I want to divide the data to train with size (10, 600), validate (10, 200) and test (10, 200). When I have the train data, I want to change the data to a an array with size (10*600, 1) and then train the model. However, in the tensorflow, we have validation_splitand I am not sure that this validation is same as the method that I want. Here is a simple example:

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
from keras.layers.recurrent import LSTM

X_total = np.randm.rand(10, 1000)
#here is an example with a sample data
X_train = np.random.rand(10,5,2)
Y_train = np.random.rand(10,2)
X_test = np.random.rand(3, 5, 2)

model = Sequential()
model.add(LSTM(64, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True))
model.add(LSTM(32, activation='relu', return_sequences=False))
model.add(Dense(Y_train.shape[1], kernel_regularizer='l2'))
opt = keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=opt, loss='mse')
history = model.fit(X_train, Y_train, epochs=1, batch_size=200, validation_split=0.1,verbose=1)

prediction = model.predict(X_test) 

2 Answers 2


Based on your explanation, I think that this is what you want. Same as the train and test, you can create the the x_val and y_val and then in the model.fit, you can use this line of code : validation_data= (X_val, Y_val). In this way, the model will validated on this set that you provide.


tf Keras documentation says that explicitly

validation_split: Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling.

Meaning they take the last samples out of your batches (before shuffling). If you want to explicitly choose validation data youc an by the key validation_data.

  • $\begingroup$ Does this automatic validation_split=0.1 option take into account the right way of splitting time series? (leaving always the future data for validation) $\endgroup$
    – skan
    Feb 26 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.