• y_q
    了解作者
  • Python
    开发工具
  • 689KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 16
    下载次数
  • 2020-08-24 20:41
    上传日期
使用python实现LSTM长短期记忆网络
lstm.zip
  • lstm
  • lstm_gru_model.h5
    536.9KB
  • lstm_gru_test.py
    1.9KB
  • 201609.xlsx
    54.1KB
  • lstm_gru.py
    4.5KB
  • 201611.xlsx
    53.8KB
  • 201608.xlsx
    55.1KB
  • 201610.xlsx
    53KB
内容介绍
import pandas as pd import numpy as np from numpy import * import matplotlib.pyplot as plt from keras.models import Sequential from keras.layers import LSTM from keras.layers import Dense from keras.layers import GRU from keras.models import Model # 设置参数 PROPORTION = 0.75 # 训练集占总数据的比例 W = 28 # 空间变量数,就是预测时需要用到的车道数量 T = 6 ROAD = '第8车道' # 读取数据 col = [] col_name = [] for i in range(0,30): col.append(2 * i) col_name.append('第'+str(i)+'车道') col_name[0] = '时间' # data = data.append(pd.read_excel('201610.xlsx', sheet_name='20161001', usecols=col)) data = pd.concat([pd.read_excel('201608.xlsx', usecols=col, names=col_name), pd.read_excel('201609.xlsx', usecols=col, names=col_name), pd.read_excel('201610.xlsx', usecols=col, names=col_name), pd.read_excel('201611.xlsx', usecols=col, names=col_name)]) data.set_index(["时间"], inplace=True) print(data.shape) # # 将数据分成训练数据,训练标签,测试数据,测试标签 data = (data - data.min()) / (data.max() - data.min()) train, test = data[: 432], data[432:] print(train.shape, test.shape) # train_X, train_y = train.drop('第8车道', axis=1).drop('第30车道', axis=1), train['第8车道'] # test_X, test_y = test.drop('第8车道', axis=1).drop('第30车道', axis=1), test['第8车道'] # print("变形前的形状", train_X.shape, train_y.shape, test_X.shape, test_y.shape) # # train_X, train_y = train_X.values, train_y.values # test_X, test_y = test_X.values, test_y.values # # train_X, test_X = train_X.reshape((-1, 1, train_X.shape[1])), test_X.reshape((-1, 1, test_X.shape[1])) # print("变形后的形状", train_X.shape, train_y.shape, test_X.shape, test_y.shape) train_y = train[5::][ROAD] train = train.drop('第8车道', axis=1) train_X = np.zeros((0, train.shape[1])) for i in range(train.shape[0] - T + 1): train_X = np.append(train_X, train[i: i+6: 1], axis=0) test_y = test[5::][ROAD] test = test.drop('第8车道', axis=1) test_X = np.zeros((0, test.shape[1])) for i in range(test.shape[0] - T + 1): test_X = np.append(test_X, test[i: i+6: 1], axis=0) train_X, test_X = train_X.reshape((-1, 6, train_X.shape[1])), test_X.reshape((-1, 6, test_X.shape[1])) print(train_X.shape, train_y.shape, test_X.shape, test_y.shape) # 灰色关联分析 def GRA(Vs0:ndarray, Vsm:ndarray): # Vs0是标签,Vsm是数据 min, max = 2, -1 gamma = np.zeros((Vsm.shape[0],Vsm.shape[2])) print(Vs0.shape, Vsm.shape) for i in range(Vs0.shape[0]): for j in range(Vsm.shape[2]): if max < abs(Vs0[i] - Vsm[i][0][j]): max = abs(Vs0[i] - Vsm[i][0][j]) if min > abs(Vs0[i] - Vsm[i][0][j]): min = abs(Vs0[i] - Vsm[i][0][j]) for i in range(gamma.shape[0]): for j in range(gamma.shape[1]): gamma[i][j] = (min + 0.5 * max) / (abs(Vs0[i] - Vsm[i][0][j]) + 0.5 * max) p = np.zeros((Vsm.shape[0], Vsm.shape[2])) gamma_Sigma = np.zeros((Vsm.shape[2])) for i in range(gamma.shape[0]): for j in range(gamma.shape[1]): gamma_Sigma[j] += gamma[i][j] for i in range(gamma.shape[1]): for j in range(gamma.shape[0]): p[j][i] = gamma[j][i] / gamma_Sigma[i] p_Sigma = np.zeros((p.shape[1])) for i in range(p.shape[0]): for j in range(p.shape[1]): p_Sigma[j] += p[i][j] * log(p[i][j]) E = -p_Sigma / log(Vs0.shape[0]) GRG = np.zeros((E.shape[0])) for i in range(GRG.shape[0]): GRG[i] = E[i] * gamma_Sigma[i] / Vs0.shape[0] index = GRG.argsort()[-W:][::-1] return index # 建立模型 model = Sequential() model.add(LSTM(60, dropout=0.75, return_sequences=True, input_shape=(train_X.shape[1], train_X.shape[2]), name='lstm')) model.add(GRU(60, activation='tanh', name='gru')) model.add(Dense(1)) model.summary() model.compile(loss="mae", optimizer="adam", metrics=['accuracy']) # 结果展示 history = model.fit(train_X, train_y, epochs=80, batch_size=30, validation_data=(test_X, test_y), verbose=2, shuffle=False) model.save('lstm_gru_model.h5') print(history.history.keys()) plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.legend() plt.show() # # 画预测值与实际值的差异 # model.summary() # lstm_model = Model(inputs=model.input, outputs=model.get_layer('lstm').output)#创建的新模型 # lstm_output = lstm_model.predict(test_X)
评论
    相关推荐