x_train shape=( y_train shape=( ,x_test shape=( y_test shape=( ,the x_train and x_test tensors conform to the (nwfformat we discussed earlier and is required for input to our rnn we have sequences in our training seteach with six time steps and one value to forecast similarlywe have sequences in our test set now that we have our datasets preprocessed and readywe build up an rnn network using keras the keras framework provides us high level abstractions to work with neural networks over theano and tensorflow backends the following snippet showcases the model prepared using the get_reg_model(function in [ ]lstm_model get_reg_model(layer_units=[ , ]window_size=windowthe generated lstm model architecture has two hidden lstm layers stacked over each other with first one having lstm units and the second one having the output layer is dense layer with linear activation function we use mean squared error as our loss function to optimize upon since we are stacking lstm layerswe need to set return_sequences to true in order for the subsequent layer to get the required values as is evidentkeras abstracts most of the heavy lifting and makes it pretty intuitive to build even complex architectures with just few lines of code the next step is to train our lstm network we use batch size of with epochs and validation set of the following snippet uses the fit(function to train the model in [ ]use early stopping to avoid overfitting callbacks [keras callbacks earlystopping(monitor='val_loss'patience= verbose= )lstm_model fit(x_trainy_trainepochs= batch_size= verbose= ,validation_split= callbacks=callbacksthe model generates information regarding training and validation loss for every epoch it runs the callback for stopping enables us to stop the training if there is no further improvement observed for two consecutive epochs we start with batch size of you may experiment with larger batch sizes and observe the difference once the model is fitthe next step is to forecast using the predict(function since we have modeled this as regression problem with fixed window sizewe would generate forecasts for every sequence to do sowe write another utility function called predict_reg_multiple(this function takes the lstm modelwindowed datasetwindow and prediction lengths as input parameters to return list of predictions for every input window the predict_reg_multiple(function works as follows for every sequence in the list of windowed sequencesrepeat steps -ca use keras' predict(function to generate one output value append this output value to the end of the input sequence and remove the first value to maintain the window size then repeat this process (steps and buntil the required prediction length is achieved the function utilizes predicted values to forecast subsequent ones