LSTM Sequence Prediction Model Generator

  • Share this:

Code introduction


This function generates a simple sequence prediction model using LSTM (Long Short-Term Memory) layers for training, and finally uses softmax activation function to output a probability distribution.


Technology Stack : Keras, Numpy

Code Type : Neural network model generating function

Code Difficulty : Intermediate


                
                    
import numpy as np
from keras.layers import LSTM, Dense
from keras.models import Sequential

def generate_sequence_model(input_shape, output_shape):
    model = Sequential()
    model.add(LSTM(50, input_shape=input_shape, return_sequences=True))
    model.add(LSTM(50, return_sequences=False))
    model.add(Dense(output_shape, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    return model                
              
Tags: