读完需要
速读仅需 2 分钟
请尊重原创劳动成果
转载请注明本文链接
及文章作者:机器学习之心
摘要:畅销作品!LSTM专栏! | 一次订阅,永久更新
1
购买后可加博主QQ1153460737咨询交流。注意:其他非官方渠道购买的盗版代码不含模型咨询交流服务,大家注意甄别,谢谢。
2
专栏目录
完整专栏扫描二维码:
3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
import time
import math, time
from sklearn.metrics import mean_squared_error,r2_score
import plotly.express as px
import plotly.graph_objects as go
from utils import split_data
import argparse
import os
from model import *
from utils import *
from pathlib import Path
parser = argparse.ArgumentParser()
# data
parser.add_argument('--path', type=str, default='../data/rlData.csv')
parser.add_argument('--vision', type=bool, default=True)
parser.add_argument('--input_features', type=list, default=['Open', 'High', 'Low', 'Volume', 'Close'])
parser.add_argument('--output_features', type=list, default=['Close','Open','High','Low'])
parser.add_argument('--window_size', type=int, default=20)
parser.add_argument('--train_test_ratio', type=float, default=0.2)
parser.add_argument('--random_state', type=int, default=34)
# model
parser.add_argument('--model_name', type=str, default='LSTM-ekan',help='selection from LSTM,LSTM-ekan')
parser.add_argument('--dropout', type=float, default=0.2)
parser.add_argument('--hidden_dim', type=int, default=32)
##开始时间统计和结果保存
start_time = time.time()
result = []
##循环训练
for t in range(args.num_epochs):
y_train_pred = model(x_train)
##损失计算
loss = criterion(y_train_pred, y_train)
##R2计算
R2 = r2_score(y_train_pred.detach().numpy(), y_train.detach().numpy())
print("Epoch ", t, "MSE: ", loss.item(), 'R2', R2.item())
# 统计每个时间步的损失和R2指标
MSE_hist[t] = loss.item()
if R2 < 0:
R2 = 0
R2_hist[t] = R2
optimiser.zero_grad()
loss.backward()
optimiser.step()
training_time = time.time() - start_time
print("Training time: {}".format(training_time))
## 反缩放-恢复正常值
predict = pd.DataFrame(target_scaler.inverse_transform(y_train_pred.detach().numpy()))
original = pd.DataFrame(target_scaler.inverse_transform(y_train.detach().numpy()))
##对每个预测单元进行绘图
for i in range(len(args.output_features)):
sns.set_style("white")
# 解决画图中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
fig.subplots_adjust(hspace=0.2, wspace=0.2)
# 计算当前组的行数和列数
ax = sns.lineplot(x=range(len(original.index)), y=original.loc[:, i], label=f"{param_index_dict[i]}",
color='royalblue')
ax = sns.lineplot(x=range(len(predict.index)), y=predict.loc[:, i], label=f"{args.model_name} ",
color='tomato')
# print(predict.index)
# print(predict[0])
ax.set_title(f'{param_index_dict[i]}', size=14, fontweight='bold')
ax.set_xlabel("时间", size=14)
ax.set_ylabel("价格", size=14)
ax.set_xticklabels('', size=10)
Fitting_path = f'../{args.model_name}/train'
if not os.path.exists(Fitting_path):
# 使用 os.makedirs() 创建目录
os.makedirs(Fitting_path)
print(f"Directory '{Fitting_path}' was created.")
else:
print(f"Directory '{Fitting_path}' already exists.")
plt.savefig(os.path.join(Fitting_path, f'{param_index_dict[i]}'))
##绘制loss曲线和R2决定系数
indication_path=f'../{args.model_name}/train'
plt.plot()
ax = sns.lineplot(data=MSE_hist, color='royalblue')
ax.set_xlabel("Epoch", size = 14)
ax.set_ylabel("Loss", size = 14)
ax.set_title("训练损失", size = 14, fontweight='bold')
fig.set_figheight(6)
fig.set_figwidth(16)
plt.savefig(os.path.join(indication_path,'loss'))
plt.show()
plt.plot()
ax = sns.lineplot(data=R2_hist, color='red')
ax.set_xlabel("Epoch", size = 14)
ax.set_ylabel("R2", size = 14)
ax.set_title("R2", size = 14, fontweight='bold')
fig.set_figheight(6)
fig.set_figwidth(16)
plt.savefig(os.path.join(indication_path,'R2'))
plt.show()
y_test_pred = model(x_test)
trainScore = mean_squared_error(y_train.detach().numpy(), y_train_pred.detach().numpy(),squared=False)
r2_train = r2_score(y_train.detach().numpy(), y_train_pred.detach().numpy())
print('Train Score: %.2f RMSE' % (trainScore))
print('Train R^2: %.2f' % (r2_train))
testScore = math.sqrt(mean_squared_error(y_test.detach().numpy(), y_test_pred.detach().numpy()))
r2_test = r2_score(y_test.detach().numpy(), y_test_pred.detach().numpy())
print('Test Score: %.2f RMSE' % (testScore))
print('Test R^2: %.2f' % (r2_test))
result.append(trainScore)
result.append(testScore)
result.append(training_time)
## 反缩放-恢复正常值
y_train_pred = target_scaler.inverse_transform(y_train_pred.detach().numpy())
y_train = target_scaler.inverse_transform(y_train.detach().numpy())
y_test_pred = target_scaler.inverse_transform(y_test_pred.detach().numpy())
y_test = target_scaler.inverse_transform(y_test.detach().numpy())
file_path = 'model_scores.csv'
# 检查文件是否存在
if Path(file_path).exists():
# 文件存在,追加数据
df.to_csv(file_path, mode='a', header=False)
else:
# 文件不存在,创建文件并写入数据
df.to_csv(file_path)
for i in range(len(args.output_features)):
trainPredictPlot = np.empty_like(target_scaled[:,i]).reshape(-1,1)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[args.window_size:len(y_train_pred)+args.window_size, :] = y_train_pred[:,i].reshape(-1,1)
4
嗯,细心的你会发现:https://mbd.pub/o/slowtrain/work
博客专家认证,机器学习领域创作者,2023博客之星TOP50,主做机器学习和深度学习时序、回归、分类、聚类和降维等程序设计和案例分析。科研课题模型定制/横向项目模型仿真/职称学术论文辅导/模型程序讲解均可联系本人唯一QQ1153460737(其他均为盗版,注意甄别)
技术交流群:购买博主任意代码或分享博主博文到任意三方平台后即可添加博主QQ进群