【问题标题】:How to save and restore Keras LSTM model?如何保存和恢复 Keras LSTM 模型?
【发布时间】:2019-02-01 01:36:16
【问题描述】:

我已经训练了一个 LSTM 网络来预测股票价格。但我不知道如何保存和恢复它。

以下是我的代码:

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')

现在预测出来的都是历史数据。但是我想要的是用这个模型来预测未来的价格。具体代码哪位朋友可以帮忙。

任何朋友都可以帮助我,非常感谢!

【问题讨论】:

    标签: tensorflow keras lstm prediction


    【解决方案1】:

    Keras docs 中描述了保存模型及其权重的过程。这里给你一个总结:

    • 为了保存模型和权重,请使用模型的save() 函数。
        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')
    
    
    • 对于仅存储模型定义,您可以获取其描述为 JSON 或 YAML:
        # save as JSON
        json_string = model.to_json()
        
        # save as YAML
        yaml_string = model.to_yaml()
    

    要再次加载,只需应用

        # 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)
    
    • 如果您只想存储权重,请使用
        model.save_weights('my_model_weights.h5')  # to store
        model.load_weights('my_model_weights.h5')  # to load
    

    再次加载模型后,您可以通过将其应用于先前加载的数据来使用它,例如

    predicted_output = model.predict(input_data, batch_size=BS)
    

    【讨论】:

    • 嗨朋友们,当尝试 predict_output = model.predict(newdata, batch_size=512)
    • 你好亲爱的朋友,你能帮我解决这个相关的问题吗?非常感谢!stackoverflow.com/questions/54509570/…
    • @Martin 我们需要将所有参数存储在 LSTM 中吗?当我有 1K 大的 LSTM 单元时,还有其他选择吗?
    • 您必须存储所有内容。否则,您会丢失从培训中获得的信息。
    【解决方案2】:

    这很简单。首先你必须保存模型的 json,然后是模型的权重。保存您的权重、结构和完整的 keras 模型后,删除您之前创建的模型。

    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'])
    

    【讨论】:

    • 非常感谢你的朋友!你能告诉我'通过编译来初始化你的模型'的更具体的步骤或代码我在深度学习方面真的很新。例如我如何预测ibm通过'通过编译来初始化你的模型'来储存它真的非常珍贵!
    • 欢迎您。我也是新人。您需要调用“model.predict()”,这是文档:keras.io/models/model
    • 你好亲爱的朋友,你能帮我解决这个相关的问题吗?非常感谢!stackoverflow.com/questions/54509570/…
    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多