Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and restore Keras LSTM model?

I have trained a LSTM network to predict stock price.But I don't know how to save and restore it.

Follow is my code:

CONST_TRAINING_SEQUENCE_LENGTH = 12
CONST_TESTING_CASES = 5


def dataNormalization(data):
    return [(datum - data[0]) / data[0] for datum in data]


def dataDeNormalization(data, base):
    return [(datum + 1) * base for datum in data]


def getDeepLearningData(ticker):
    # Step 1. Load data
    data = pandas.read_csv('./data/Intraday/' + ticker + '.csv')[
        'close'].tolist()
    # Step 2. Building Training data
    dataTraining = []
    for i in range(len(data) - CONST_TESTING_CASES * CONST_TRAINING_SEQUENCE_LENGTH):
        dataSegment = data[i:i + CONST_TRAINING_SEQUENCE_LENGTH + 1]
        dataTraining.append(dataNormalization(dataSegment))

    dataTraining = numpy.array(dataTraining)
    numpy.random.shuffle(dataTraining)
    X_Training = dataTraining[:, :-1]
    Y_Training = dataTraining[:, -1]

    # Step 3. Building Testing data
    X_Testing = []
    Y_Testing_Base = []
    for i in range(CONST_TESTING_CASES, 0, -1):
        dataSegment = data[-(i + 1) * CONST_TRAINING_SEQUENCE_LENGTH:-i * CONST_TRAINING_SEQUENCE_LENGTH]
        Y_Testing_Base.append(dataSegment[0])
        X_Testing.append(dataNormalization(dataSegment))

    Y_Testing = data[-CONST_TESTING_CASES * CONST_TRAINING_SEQUENCE_LENGTH:]

    X_Testing = numpy.array(X_Testing)
    Y_Testing = numpy.array(Y_Testing)

    # Step 4. Reshape for deep learning
    X_Training = numpy.reshape(X_Training, (X_Training.shape[0], X_Training.shape[1], 1))
    X_Testing = numpy.reshape(X_Testing, (X_Testing.shape[0], X_Testing.shape[1], 1))

    return X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base


def predict(model, X):
    predictionsNormalized = []

    for i in range(len(X)):
        data = X[i]
        result = []

        for j in range(CONST_TRAINING_SEQUENCE_LENGTH):
            predicted = model.predict(data[numpy.newaxis, :, :])[0, 0]
            result.append(predicted)
            data = data[1:]
            data = numpy.insert(data, [CONST_TRAINING_SEQUENCE_LENGTH - 1], predicted, axis=0)

        predictionsNormalized.append(result)

    return predictionsNormalized


def plotResults(Y_Hat, Y):
    plt.plot(Y)

    for i in range(len(Y_Hat)):
        padding = [None for _ in range(i * CONST_TRAINING_SEQUENCE_LENGTH)]
        plt.plot(padding + Y_Hat[i])

    plt.show()


def predictLSTM(ticker):
    # Step 1. Load data
    X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base = getDeepLearningData(ticker)

    # Step 2. Build model
    model = Sequential()

    model.add(LSTM(
        input_shape=(None, 1),
        units=50,
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        200,
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(units=1))
    model.add(Activation('linear'))

    model.compile(loss='mse', optimizer='rmsprop')

    # Step 3. Train model
    model.fit(X_Training, Y_Training,
              batch_size=512,
              epochs=27,
              validation_split=0.05)

    # Step 4. Predict
    predictionsNormalized = predict(model, X_Testing)

    # Step 5. De-nomalize
    predictions = []
    for i, row in enumerate(predictionsNormalized):
        predictions.append(dataDeNormalization(row, Y_Testing_Base[i]))

    # Step 6. Plot
    plotResults(predictions, Y_Testing)


predictLSTM(ticker='IBM')

Now all the come out of the prediction are all history data.But what I want is to use this model to predict the future price.Any friend can help with specific code.

Any friend can help me with this,really appreciate!

like image 608
William Avatar asked Feb 01 '19 01:02

William


People also ask

How do you save a keras model?

There are two formats you can use to save an entire model to disk: the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel. It is the default when you use model. save() .

How do I save keras model and load it?

Keras provides the ability to describe any model using JSON format with a to_json() function. This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification.


Video Answer


2 Answers

The procedure on saving a model and its weights is described in the Keras docs. Here a summary for you:

  • In order to save the model and the weights use the model's save() function.
    from keras.models import load_model
    
    model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
    del model  # deletes the existing model
    
    # returns a compiled model
    # identical to the previous one
    model = load_model('my_model.h5')

  • For only storing the model definition, you can obtain its description as a JSON or YAML:
    # save as JSON
    json_string = model.to_json()
    
    # save as YAML
    yaml_string = model.to_yaml()

to load it again, simply apply

    # model reconstruction from JSON:
    from keras.models import model_from_json
    model = model_from_json(json_string)
    
    # model reconstruction from YAML:
    from keras.models import model_from_yaml
    model = model_from_yaml(yaml_string)
  • In case you only want to store the weights, use
    model.save_weights('my_model_weights.h5')  # to store
    model.load_weights('my_model_weights.h5')  # to load

Once tho model is loaded again, you can use it by applying it on previously loaded data, like

predicted_output = model.predict(input_data, batch_size=BS)
like image 89
Martin Avatar answered Oct 19 '22 01:10

Martin


It is very simple. First you have to save the model's json, then the model's weights. After saving your weights,structure and full keras model delete your previously created model.

from pathlib import Path
# Save neural network structure
model_structure = model.to_json()
f = Path("C:\\----yourfolderpath.json")
f.write_text(model_structure)
print('done')

# Save neural network's trained weights
your_model.save_weights("C:\\---------yourfolderpath_weights.h5")
print('done')

# or you can save the full model via:
your_model.save('C:\\---------yourfolderpath_fullkeras_model.h5')

#delete your model in memory
del your_model

#Know to load your model use:
my_new_model = tf.keras.models.load_model("path to model")


#compile my_new_model:
my_new_model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
like image 37
Mohamoud Mohamed Avatar answered Oct 19 '22 01:10

Mohamoud Mohamed