深度学习

您所在的位置:网站首页 lstm多输入 深度学习

深度学习

2023-11-26 13:36| 来源: 网络整理| 查看: 265

#存储真实值和预测值的相关信息 y_pred_df = pd.DataFrame(index=y_pred) y_pred_df.to_csv(r'./{}/LSTM_pred.csv'.format(DIR), encoding='gbk', sep=',') y_real_df = pd.DataFrame(index=y_real) y_real_df.to_csv(r'./{}/LSTM_real.csv'.format(DIR), encoding='gbk', sep=',') # 画出真实值和预测值的图像并计算相关指标 model_plot(y_real, y_pred, savename) RMSE = np.sqrt(mean_squared_error(y_pred, y_real)) R2_SCORE = r2_score(y_pred, y_real) print('RMSE: {}\nR2_SCORE: {}\n'.format(RMSE, R2_SCORE)) return RMSE, R2_SCORE, RMSE_normal, R2_SCORE_normal ## 7.保存模型信息和绘图 这部分没什么难点,比较简单,就是存储一下运行的结果。 ```python '''保存模型信息''' def model_save(RMSE, R2_SCORE, RMSE_normal, R2_SCORE_normal, savename): with open(r'./{}/LSTM.txt'.format(DIR), 'a') as fh: fh.write('参数设置:\nTIME_STEP: {}\tDELAY: {}\n'.format(TIME_STEP, DELAY)) fh.write('RMSE: {}\nR2_SCORE: {}\n'.format(RMSE, R2_SCORE)) fh.write('RMSE_normal: {}\nR2_SCORE: {}\n\n'.format(RMSE_normal, R2_SCORE_normal)) print('%s模型信息保存成功!\n\n\n' % savename) '''绘图相关''' def model_plot(y_real, y_pred, savename): plt.cla() fig1 = plt.figure(figsize=(10, 14), dpi=80) plt.subplots_adjust(hspace=0.3) #hspace=0.3为子图之间的空间保留的高度,平均轴高度的一部分.加了这个语句,子图会稍变小,因为空间也占用坐标轴的一部分 ax1 = fig1.add_subplot(1, 1, 1) # 1行x1列的网格里第一个 ax1.plot(y_real, '-', c='blue', label='Real', linewidth=2) ax1.plot(y_pred, '-', c='red', label='Predict ', linewidth=2) ax1.legend(loc='upper right') ax1.set_xlabel('min') ax1.set_ylabel('KWH') ax1.grid() fig1.savefig('./{}/{}.png'.format(DIR, savename)) plt.close() 8.结果部分

里插入图片描述 测试集损失函数比训练集好很多的原因可能是因为加入了Dropout,训练时开启,丢掉一部分数据集,测试时Dropout关闭。

使用了10万行数据训练,1万测试,RMSE=0.03。

9.全部代码 import tensorflow as tf from tensorflow_core import keras import os import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error, r2_score from numpy import concatenate pd.set_option('display.max_columns', None) #显示所有列 '''修改数据''' SaveFile_Path = r'' # 此处加入读取和保存的文件路径 readname = '' #读取文件的名字 savename = r'' #存储文件的名字 os.chdir(SaveFile_Path) # 修改当前工作目录 DIR = 'LSTM' if not os.path.exists(DIR): #判断括号里的文件是否存在的意思,括号内的可以是文件路径。 os.makedirs(DIR) #用于递归创建目录 TIME_STEP = 360 #时间步长360min 6h DELAY = 10 #预测十分钟后 BATCHSZ = 5 #一个批处理(batch)大小 EACH_EPOCH = 3 #循环次数 LR = 0.0001 #keras.optimizers.Adam中的学习率 ''' 绘制初始数据曲线 ''' def plot_init(): df = pd.read_csv(SaveFile_Path + '\\' + readname, header=0, index_col=0, encoding='gbk') values = df.values col = df.shape[1] plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.figure() for i in range(0, col): plt.subplot(col, 1, i+1) plt.plot(values[:, i]) plt.title(df.columns[i], y=0.5, loc='right') plt.show() ''' LSTM数据准备 ''' def train_test(timestep, nextstep): #timestep时间步长, nextstep预测多长时间 # load dataset dataset = pd.read_csv(SaveFile_Path + '\\' + readname, header=0, index_col=0, encoding='gbk') values = dataset.values # ensure all data is float values = values.astype('float32') # normalize features归一化 scaler = MinMaxScaler(feature_range=(0, 1)) # sklearn 归一化函数 scaled = scaler.fit_transform(values) # fit_transform(X_train) 意思是找出X_train的均值和​​​​​​​标准差,并应用在X_train上。 # split into train and test sets 将数据集进行划分,然后将训练集和测试集划分为输入和输出变量,最终将输入(X)改造为LSTM的输入格式,即[samples,timesteps,features] n_train_hours = 2000 #20万行作训练集,11万作测试集 train = scaled[:n_train_hours, :] test = scaled[n_train_hours:3000, :] train_X = [] train_y = [] test_X = [] test_y = [] # 测试集: # 利用for循环,遍历整个测试集,提取测试集中连续360min的特征量作为输入特征test_X,第361-370min的发电量作为标签 for i in range(len(train)-timestep-nextstep+1): train_X.append(train[i:(i+timestep), :]) btemp = train[i+timestep:i+timestep+nextstep, 0] b = [] for j in range(len(btemp)): b.append(btemp[j]) train_y.append(b) # 对训练集进行打乱 np.random.seed(7) np.random.shuffle(train_X) np.random.seed(7) np.random.shuffle(train_y) tf.random.set_seed(7) # 将训练集由list格式变为array格式 train_X = np.array(train_X, dtype=np.float32) train_y = np.array(train_y, dtype=np.float32) # 使x_train符合RNN输入要求:[送入样本数, 循环核时间展开步数, 每个时间步输入特征个数]。 # 此处整个数据集送入,送入样本数为train_X.shape[0];输入360min数据,预测出第361min的发电量,循环核时间展开步数为360; 每个时间步送入的特征是某一min的运行数据,16个数据,故每个时间步输入特征个数为16 train_X = np.reshape(train_X, (train_X.shape[0], 360, 16)) # 测试集: # 利用for循环,遍历整个测试集,提取测试集中连续360min的特征量作为输入特征test_X,第361-370min的发电量作为标签 for i in range(len(test)-timestep-nextstep+1): test_X.append(test[i:(i + timestep), :]) btemp = test[i + timestep:i + timestep + nextstep, 0] b = [] for j in range(len(btemp)): b.append(btemp[j]) test_y.append(b) test_X, test_y = np.array(test_X, dtype=np.float32), np.array(test_y, dtype=np.float32) test_X = np.reshape(test_X, (test_X.shape[0], 360, 16)) #print(train_X.shape, train_y.shape, test_X.shape, test_y.shape) return train_X, train_y, test_X, test_y, scaler ''' 构造模型 ''' def model_build(train_datas): #train_datas = train_X # LSTM层 model = keras.models.Sequential() model.add(keras.layers.LSTM(40, input_shape=(train_datas.shape[1:]), return_sequences=True, )) #, return_sequences=True 400记忆体个数 model.add(keras.layers.Dropout(0.1)) model.add(keras.layers.LSTM(30, return_sequences=True)) # model.add(keras.layers.Dropout(0.5)) model.add(keras.layers.Dropout(0.1)) model.add(keras.layers.LSTM(40, return_sequences=True)) model.add(keras.layers.Dropout(0.1)) model.add(keras.layers.LSTM(40)) model.add(keras.layers.BatchNormalization()) #批标准化:对一小批数据(batch)做标准化处理(使数据符合均值为0,标准差为1分布) model.add(keras.layers.Dense(train_y.shape[1])) #全连接层 #配置训练方法 model.compile(optimizer=keras.optimizers.Adam(lr=LR, amsgrad=True), loss='mse', metrics=[rmse]) # mae: mean_absolute_error return model '''模型拟合''' def model_fit(model, train_datas, train_labels,x_test, y_test): #train_X, train_y, test_X, test_y checkpoint_save_path = "./checkpoint/LSTM_stock.ckpt" #模型保存位置 if os.path.exists(checkpoint_save_path + '.index'): print('-------------load the model-----------------') model.load_weights(checkpoint_save_path) lr_reduce = keras.callbacks.ReduceLROnPlateau('val_loss', #学习停止,模型会将学习率降低2-10倍,该hui patience=4, factor=0.7, min_lr=0.00001) best_model = keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,#保存模型 monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='min', ) early_stop = keras.callbacks.EarlyStopping(monitor='val_rmse', patience=15) history = model.fit( train_datas, train_labels, validation_data=(x_test, y_test), batch_size=BATCHSZ, epochs=EACH_EPOCH, verbose=2, callbacks=[ best_model, early_stop, lr_reduce, ] ) loss = history.history['loss'] val_loss = history.history['val_loss'] #画出训练集和测试集的损失函数 plt.plot(loss, label='Training Loss') plt.plot(val_loss, label='Validation Loss') plt.title('Training and Validation Loss') plt.legend() plt.savefig('./{}/{}.png'.format(DIR, '损失函数')) plt.close() return model, history '''评价部分''' def rmse(y_true, y_pred): #sqrt求元素平方根 mean求张量平均值 return keras.backend.sqrt(keras.backend.mean(keras.backend.square(y_pred - y_true), axis=-1)) def model_evaluation(model, test_X, test_y, savename): yhat = model.predict(test_X) yhat = yhat[:, -1] #只取最后一个时刻的进行分析,1-9min的结果不是很重要 y_normal = yhat.copy() y_normal[y_normal


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3