import os
import json
import time
import math
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import plot_model
import pandas as pd
import warnings
from keras.models import Sequential,load_model
import datetime as dt
from keras.layers import Dense,Activation,Dropout,LSTM
from keras.utils import plot_model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from numpy import newaxis
warnings.filterwarnings("ignore")%matplotlib inline
defplot_point_result(predicted_data, true_data):fig = plt.figure(facecolor='white')sub_plot = fig.add_subplot(111)sub_plot.plot(true_data, label="True Data")plt.plot(predicted_data, label="Predict Data")plt.legend()plt.show()plt.savefig("LSTMModel_stock_price_predict_point_result.png")defplot_sequences_result(predicted_data, true_data, predict_len):fig = plt.figure(facecolor="white")sub_plot = fig.add_subplot(111)sub_plot.plot(true_data, label="True Data")plt.legend()for i, data inenumerate(predicted_data):padding =[Nonefor p inrange(i * predict_len)]plt.plot(padding+data, label="Predict Data")plt.show()plt.savefig("LSTMModel_stock_price_predict_sequences_result.png")
4.main方法
model_config = json.load(open("rnn_stock_predict_config.json",'r'))
save_dir = model_config['model']['save_dir']ifnot os.path.exists(save_dir):os.makedirs(save_dir)#读取数据
data_loader = DataLoader(os.path.join('data', model_config['data']['filename']),model_config['data']['train_test_split'],model_config['data']['columns'])#创建RNN模型
lstm_model = LSTMModel()
builded_lstm_model = lstm_model.build_model(model_config)
plot_model(builded_lstm_model, to_file="LSTMModel_stock_price_predict.png", show_shapes=True)#加载训练数据
x, y = data_loader.get_train_data(seq_len=model_config['data']['sequence_length'],normalise=model_config['data']["normalise"])print("train data x shape: ", x.shape)print("train data y shape: ", y.shape)#训练模型
lstm_model.train(x, y, epochs = model_config['training']['epochs'],batch_size = model_config['training']['batch_size'],save_dir = model_config['model']['save_dir'])#获取测试数据
x_test, y_test = data_loader.get_test_data(seq_len=model_config['data']['sequence_length'],normalise=model_config['data']['normalise'])#测试
predict_seqs = lstm_model.predict_sequences(x_test,model_config['data']['sequence_length'],model_config['data']['sequence_length'])
predict_point = lstm_model.predict_point(x_test, debug =True)
model compiled
Time taken: 0:00:00.063992
train data x shape: (3942, 49, 2)
train data y shape: (3942, 1)
model train started epochs 1 batch_size 32
[1m124/124[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m7s[0m 35ms/step - loss: 0.0019
model train completed. model save as saved_models\20092024-153703-e1.h5.keras
Time taken: 0:00:07.437250predict sequence multiple...
[1m1/1[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 305ms/steppredict point start
predict data shape (655, 49, 2)
[1m21/21[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 12ms/step
predited data shape (655, 1)
predited data shape (655,)
predict point completed