当前位置:   article > 正文

完整代码CNN-LSTM多变量时间序列预测_cnn-lstm时间序列模型预测代码

cnn-lstm时间序列模型预测代码
  1. import torch
  2. from torch import nn
  3. import pandas as pd
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from sklearn.preprocessing import MinMaxScaler
  7. from torch.nn import MaxPool2d, Conv2d, Dropout, ReLU
  8. from torch.utils.data import DataLoader, Dataset
  9. #准备数据集
  10. df=pd.read_csv("train.csv",parse_dates=["Date"],index_col=[0])
  11. print(df.shape)
  12. train_data_size=round(len(df)*0.8)
  13. test_data_size=round(len(df)*0.2)
  14. print("训练数据集的长度为:{}".format(train_data_size))
  15. print("测试数据集的长度为:{}".format(test_data_size))
  16. df[['Open']].plot()
  17. plt.ylabel("stock price")
  18. plt.xlabel("times")
  19. plt.show()
  20. sel_col = ['Open', 'High', 'Low', 'Close']
  21. df=df[sel_col]
  22. df_close_max=df['Close'].max()
  23. df_close_min=df['Close'].min()
  24. print("最高价=", df_close_max)
  25. print("最低价=", df_close_min)
  26. print("波动值=", df_close_max-df_close_min)
  27. print("上涨率=", (df_close_max-df_close_min)/df_close_min)
  28. print("下跌率=", (df_close_max-df_close_min)/df_close_max)
  29. df=df.apply(lambda x:(x-min(x))/(max(x)-min(x)))
  30. print(df)
  31. total_len=df.shape[0]
  32. print("df.shape=",df.shape)
  33. print("df_len=", total_len)
  34. sequence=10
  35. x=[]
  36. y=[]
  37. for i in range(total_len-sequence):
  38. x.append(np.array(df.iloc[i:(i+sequence),].values,dtype=np.float32))
  39. y.append(np.array(df.iloc[(i+sequence),1],dtype=np.float32))
  40. print("train data of item 0: \n", x[0])
  41. print("train label of item 0: \n", y[0])
  42. print("\n序列化后的数据形状:")
  43. X = np.array(x)
  44. Y = np.array(y)
  45. Y = np.expand_dims(Y, 1)
  46. print("X.shape =",X.shape)
  47. print("Y.shape =",Y.shape)
  48. x_tensor=torch.from_numpy(X)
  49. y_tensor=torch.from_numpy(Y)
  50. train_x = x_tensor[:int(0.7 * total_len)]
  51. train_y = y_tensor[:int(0.7 * total_len)]
  52. # 数据集前70%后的数据(30%)作为验证集
  53. valid_x = x_tensor[int(0.7 * total_len):]
  54. valid_y = y_tensor[int(0.7 * total_len):]
  55. print("训练集x的形状是:",train_x.shape)
  56. print("测试集y的形状是:",train_y.shape)
  57. print("测试集x的形状是:",valid_x.shape)
  58. print("测试集y的形状是:",valid_y.shape)
  59. class Mydataset(Dataset):
  60. def __init__(self, x, y, transform=None):
  61. self.x = x
  62. self.y = y
  63. def __getitem__(self, index):
  64. x1 = self.x[index]
  65. y1 = self.y[index]
  66. return x1, y1
  67. def __len__(self):
  68. return len(self.x)
  69. dataset_train = Mydataset(train_x, train_y)
  70. dataset_valid = Mydataset(valid_x, valid_y)
  71. train_dataloader=DataLoader(dataset_train,batch_size=64)
  72. valid_dataloader=DataLoader(dataset_valid,batch_size=64)
  73. class cnn_lstm(nn.Module):
  74. def __init__(self,window_size,feature_number):
  75. super(cnn_lstm, self).__init__()
  76. self.window_size=window_size
  77. self.feature_number=feature_number
  78. self.conv1 = Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1)
  79. self.relu1 = ReLU()
  80. self.maxpooling1 = MaxPool2d(3, stride=1,padding=1)
  81. self.dropout1 = Dropout(0.3)
  82. self.lstm1 = nn.LSTM(input_size=64 * feature_number, hidden_size=128, num_layers=1, batch_first=True)
  83. self.lstm2 = nn.LSTM(input_size=128, hidden_size=64, num_layers=1, batch_first=True)
  84. self.fc = nn.Linear(in_features=64, out_features=32)
  85. self.relu2 = nn.ReLU()
  86. self.head = nn.Linear(in_features=32, out_features=1)
  87. def forward(self, x):
  88. x = x.reshape([x.shape[0], 1, self.window_size, self.feature_number])
  89. # x = x.transpose(-1, -2)
  90. x = self.conv1(x)
  91. x = self.relu1(x)
  92. x = self.maxpooling1(x)
  93. x = self.dropout1(x)
  94. x = x.reshape([x.shape[0], self.window_size, -1])
  95. # x = x.transpose(-1, -2) #
  96. x, (h, c) = self.lstm1(x)
  97. x, (h, c) = self.lstm2(x)
  98. x = x[:, -1, :] # 最后一个LSTM只要窗口中最后一个特征的输出
  99. x = self.fc(x)
  100. x = self.relu2(x)
  101. x = self.head(x)
  102. return x
  103. #创建网络模型
  104. cnn_lstm=cnn_lstm(window_size=10,feature_number=4)
  105. print(cnn_lstm)
  106. #定义损失函数
  107. loss_fn=nn.MSELoss(size_average=True)
  108. #定义优化器
  109. learning_rate=0.01
  110. opitmizer=torch.optim.Adam(cnn_lstm.parameters(),learning_rate)
  111. #设置训练网络参数
  112. total_train_step=0
  113. total_valid_step=0
  114. #训练论数
  115. epoch=100
  116. hist = np.zeros(epoch)
  117. for i in range(epoch):
  118. #print("______第{}轮训练开始________".format((i + 1)))
  119. y_train_pred=cnn_lstm(train_x)
  120. loss=loss_fn(y_train_pred,train_y)
  121. if i % 10 == 0 and i != 0: # 每训练十次,打印一次均方差
  122. print("Epoch ", i, "MSE: ", loss.item())
  123. hist[i] = loss.item()
  124. #优化器优化模型
  125. opitmizer.zero_grad()
  126. loss.backward()
  127. opitmizer.step()
  128. y_train_pred=cnn_lstm(train_x)
  129. loss_fn(y_train_pred,train_y).item()
  130. y_test_pred=cnn_lstm(valid_x)
  131. loss_fn(y_test_pred,valid_y)
  132. plt.grid()
  133. plt.xlabel("iters")
  134. plt.ylabel("")
  135. plt.title("loss", fontsize = 12)
  136. plt.plot(hist, "r")
  137. plt.show()
  138. data_loader = valid_dataloader
  139. # 存放测试序列的预测结果
  140. predicts = []
  141. # 存放测试序列的实际发生的结果
  142. labels = []
  143. for idx, (x, label) in enumerate(data_loader):
  144. if (x.shape[0] != 64):
  145. continue
  146. # 对测试集样本进行批量预测,把结果保存到predict Tensor中
  147. # 开环预测:即每一次序列预测与前后的序列无关。
  148. predict= cnn_lstm(x)
  149. # 把保存在tensor中的批量预测结果转换成list
  150. predicts.extend(predict.data.squeeze(1).tolist())
  151. # 把保存在tensor中的批量标签转换成list
  152. labels.extend(label.data.squeeze(1).tolist())
  153. predicts = np.array(predicts)
  154. labels = np.array(labels)
  155. print(predicts.shape)
  156. print(labels.shape)
  157. predicts_unnormalized = df_close_min + (df_close_max - df_close_min) * predicts
  158. labels_unnormalized = df_close_min + (df_close_max - df_close_min) * labels
  159. print("shape:", predicts_unnormalized.shape)
  160. print("正则化后的预测数据:\n", predicts)
  161. print("")
  162. print("正则化前的预测数据:\n", predicts_unnormalized)
  163. plt.plot(predicts_unnormalized,"r",label="pred")
  164. plt.plot(labels_unnormalized, "b",label="real")
  165. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/237511
推荐阅读
相关标签
  

闽ICP备14008679号