第六讲 循环神经网络--GRU--stock

  1 !pip install tushare
  2 import tushare as ts
  3 import numpy as np
  4 import tensorflow as tf
  5 from tensorflow.keras.layers import Dropout, Dense, GRU
  6 import matplotlib.pyplot as plt
  7 import os
  8 import pandas as pd
  9 from sklearn.preprocessing import MinMaxScaler
 10 from sklearn.metrics import mean_squared_error, mean_absolute_error
 11 import math
 12 
 13 
 14 df1 = ts.get_k_data('600519', ktype='D', start='2003-01-01', end='2020-05-14')
 15 datapath1 = "./SH600519.csv"
 16 df1.to_csv(datapath1)
 17 
 18 
 19 maotai = pd.read_csv('./SH600519.csv')
 20 
 21 maotai.head()
 22 maotai.tail()
 23 
 24 training_set = maotai.iloc[:3000, 2:3].values
 25 test_set = maotai.iloc[3000:, 2:3].values
 26 
 27 sc = MinMaxScaler(feature_range=(0, 1))
 28 training_set_scaled = sc.fit_transform(training_set)
 29 test_set = sc.transform(test_set)
 30 
 31 x_train = []
 32 y_train = []
 33 
 34 x_test = []
 35 y_test = []
 36 
 37 
 38 for i in range(60, len(training_set_scaled)):
 39   x_train.append(training_set_scaled[i - 60:i, 0])
 40   y_train.append(training_set_scaled[i, 0])
 41 
 42 
 43 np.random.seed(7)
 44 np.random.shuffle(x_train)
 45 np.random.seed(7)
 46 np.random.shuffle(y_train)
 47 tf.random.set_seed(7)
 48 
 49 
 50 x_train, y_train = np.array(x_train), np.array(y_train)
 51 x_train = np.reshape(x_train, (x_train.shape[0], 60, 1))
 52 
 53 for i in range(60, len(test_set)):
 54   x_test.append(test_set[i-60:i, 0])
 55   y_test.append(test_set[i, 0])
 56 
 57 x_test, y_test = np.array(x_test), np.array(y_test)
 58 
 59 x_test = np.reshape(x_test, (x_test.shape[0], 60, 1))
 60 
 61 
 62 model = tf.keras.Sequential([
 63         GRU(80, return_sequences=True),
 64         Dropout(0.2),
 65         GRU(100),
 66         Dropout(0.2),
 67         Dense(1)
 68 ])
 69 
 70 model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
 71               loss='mean_squared_error')
 72 
 73 checkpoint_save_path = "./checkpoint/stock.ckpt"
 74 
 75 if os.path.exists(checkpoint_save_path + '.index'):
 76   print("-----------load the model-----------")
 77   model.load_weights(checkpoint_save_path)
 78 
 79 cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
 80                                                  save_weights_only=True,
 81                                                  save_best_only=True,
 82                                                  monitor='val_loss')
 83 
 84 history = model.fit(x_train, y_train, batch_size=64, epochs=50, validation_data=(x_test, y_test), 
 85                     validation_freq=1, callbacks=[cp_callback])
 86 
 87 model.summary()
 88 
 89 
 90 with open("./weights.txt", "w") as f:
 91   for v in model.trainable_variables:
 92     f.write(str(v.name) + '\n')
 93     f.write(str(v.shape) + '\n')
 94     f.write(str(v.numpy()) + '\n')
 95 
 96 
 97 loss = history.history['loss']
 98 val_loss = history.history['val_loss']
 99 
100 plt.plot(loss, label='Training Loss')
101 plt.plot(val_loss, label='Validation Loss')
102 plt.title('Training and Validation Loss')
103 plt.legend()
104 plt.show()
105 
106 
107 
108 predicted_stock_price = model.predict(x_test)
109 predicted_stock_price = sc.inverse_transform(predicted_stock_price)
110 
111 real_stock_price = sc.inverse_transform(test_set[60:])
112 
113 plt.plot(real_stock_price, color='red', label='real_stock_price')
114 plt.plot(predicted_stock_price, color='blue', label='predicted_stock_price')
115 plt.title('MaoTai Stock Price Prediction')
116 plt.xlabel('Time')
117 plt.ylabel('Stock Price')
118 plt.legend()
119 plt.show()
120 
121 
122 mse = mean_squared_error(predicted_stock_price, real_stock_price)
123 rmse = math.sqrt(mse)
124 
125 mae = mean_absolute_error(predicted_stock_price, real_stock_price)
126 print("均方误差: %.6f"%mse)
127 print("均方根误差:%.6f"%rmse)
128 print("平均绝对误差: %.6f"%mae)

 

posted @ 2020-05-14 22:47  WWBlog  阅读(337)  评论(0编辑  收藏  举报