BiLSTM_Attention用于多变量时序回归

#!/usr/bin/env python

import argparse

from ast import arg

import torch

import math

import numpy as np

import pandas as pd

import torch.nn as nn

from torch.utils.data import Dataset, DataLoader

import copy

import os

import sys

import torch.utils.data as Data

#from optuna import TrialState

import optuna

#from get_data import get_mape

#from model import BiLSTM_AttentionModel

from itertools import chain

#from sklearn.preprocessing import MinMaxScaler

import torch

from scipy.interpolate import make_interp_spline

from torch import nn

import numpy as np

import matplotlib.pyplot as plt

from torch.utils.data import DataLoader

import optuna

from matplotlib import pyplot

from tqdm import tqdm

from torch.optim.lr_scheduler import StepLR

from tqdm import tqdm

#from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error,mean_absolute_percentage_error

import torch

from torch import nn

'''

from sklearn.metrics import mean_squared_error # 均方误差

from sklearn.metrics import mean_absolute_error # 平方绝对误差

from sklearn.metrics import r2_score # R square

'''

#from get_data import nn_seq

#from args import args_parser

#from util import train, test, get_best_parameters

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签

plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 #有中文出现的情况,需要u'内容'

import os

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

path = os.path.dirname(os.path.realpath(file ))

#filename=r'/data/森源张鼓匠数据2022-7至2023-6删除限电.csv'

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def args_parser():

parser = argparse.ArgumentParser()

parser.add_argument('--epochs', type=int, default=10, help='epochs')

parser.add_argument('--seq_len', type=int, default=16, help='seq len')

parser.add_argument('--hidden_size', type=int, default=16, help='hidden_size')

parser.add_argument('--input_size', type=int, default=27, help='input dimension')

parser.add_argument('--num_layers', type=int, default=1, help='num_layers')

parser.add_argument('--d_model', type=int, default=8, help='input dimension')

parser.add_argument('--output_size', type=int, default=16, help='output dimension')

parser.add_argument('--lr', type=float, default=0.0008, help='learning rate')

parser.add_argument('--batch_size', type=int, default=64, help='batch size')

parser.add_argument('--optimizer', type=str, default='adam', help='type of optimizer')

parser.add_argument('--device', default=torch.device("cuda" if torch.cuda.is_available() else "cpu"))

parser.add_argument('--weight_decay', type=float, default=1e-4, help='weight decay')

parser.add_argument('--step_size', type=int, default=1, help='step size')

parser.add_argument('--gamma', type=float, default=0.25, help='gamma')

复制代码
args = parser.parse_args()

return args

def nn_seq(args):

seq_len, batch_size, pred_step_size= args.seq_len, args.batch_size, args.output_size

filename = '/data/data.csv'

filepath = os.path.dirname(os.path.realpath(file )) + filename

data = pd.read_csv(filepath, parse_dates=True, index_col=r'时间')

data = data.fillna('0')

data["cos预测风向"]=np.cos(data["预测风向(°)"].values.astype('float'))

data["sin预测风向"]=np.sin(data["预测风向(°)"].values.astype('float'))

data["实际功率"] = data["实际功率(MW)"]

#实际功率(MW),平均风速1(m/s),最大风速1(m/s),极大风速1(m/s),平均风向1(°),平均风速2(m/s),最大风速2(m/s),极大风速2(m/s),平均风向2(°),平均风速3(m/s),最大风速3(m/s),极大风速3(m/s),平均风向3(°),平均风速4(m/s),最大风速4(m/s),极大风速4(m/s),平均风向4(°),平均风速5(m/s),最大风速5(m/s),极大风速5(m/s),平均风向5(°),平均风速6(m/s),最大风速6(m/s),极大风速6(m/s),平均风向6(°),平均气温(℃),平均湿度(%),平均气压(hPa),平均空气密度(kg/m3),预测风速(m/s),预测风向(°),预测气温(℃),

data = data[["实际功率","平均风速1(m/s)","最大风速1(m/s)","极大风速1(m/s)","平均风向1(°)","平均风速2(m/s)","最大风速2(m/s)","极大风速2(m/s)","平均风向2(°)","平均风速3(m/s)","最大风速3(m/s)","极大风速3(m/s)","平均风向3(°)","平均风速4(m/s)","最大风速4(m/s)","极大风速4(m/s)","平均风向4(°)","平均风速5(m/s)","最大风速5(m/s)","极大风速5(m/s)","平均风向5(°)","预测风速(m/s)","预测风向(°)","预测气温(℃)"]]

#data = data[["实际功率","预测风速(m/s)","预测风向(°)","预测气温(℃)","cos预测风向","sin预测风向"]]#

train = data[:'2024-01-01 00:15:00']

val = data['2023-12-01 00:15:00':'2024-01-01 00:15:00']

test = data['2023-12-31 16:30:00':]

复制代码
result=pd.DataFrame(columns=['实际功率', '真实值', '预测值'])
result['实际功率']=test['实际功率']['2024-01-01 00:15:00':]#[96+96-1:]
print(train.shape,val.shape,test.shape)
print(train.shape,test.shape)

num_nodes=24#3
''' 
scaler1 = MinMaxScaler()
train.iloc[:,0] = scaler1.fit_transform(train.iloc[:,0].values.reshape([-1,1])).reshape([-1])
scaler2 = MinMaxScaler()
train.iloc[:,1] = scaler2.fit_transform(train.iloc[:,1].values.reshape([-1,1])).reshape([-1])
scaler3 = MinMaxScaler()
train.iloc[:,2] = scaler3.fit_transform(train.iloc[:,2].values.reshape([-1,1])).reshape([-1])
scaler4 = MinMaxScaler()
train.iloc[:,3] = scaler4.fit_transform(train.iloc[:,3].values.reshape([-1,1])).reshape([-1])

val.iloc[:,0] = scaler1.transform(val.iloc[:,0].values.reshape([-1,1])).reshape([-1])
val.iloc[:,1] = scaler2.transform(val.iloc[:,1].values.reshape([-1,1])).reshape([-1])
val.iloc[:,2] = scaler3.transform(val.iloc[:,2].values.reshape([-1,1])).reshape([-1])
val.iloc[:,3] = scaler4.transform(val.iloc[:,3].values.reshape([-1,1])).reshape([-1])

test.iloc[:,0] = scaler1.transform(test.iloc[:,0].values.reshape([-1,1])).reshape([-1])
test.iloc[:,1] = scaler2.transform(test.iloc[:,1].values.reshape([-1,1])).reshape([-1])
test.iloc[:,2] = scaler3.transform(test.iloc[:,2].values.reshape([-1,1])).reshape([-1])
test.iloc[:,3] = scaler4.transform(test.iloc[:,3].values.reshape([-1,1])).reshape([-1])
'''   
def process(dataset,shuffle):
    #观看过去时间窗口 过去多少天
    past_history_size = 16
    #预测未来值n天
    future_target = 16 
    x = []
    y = []
    x1= []
    x2= []
    x3= []
    dataset=dataset.values
    for i in tqdm(range(len(dataset)-past_history_size-future_target+1)):
        x.append(dataset[i:i+past_history_size,:])
        x1.append(dataset[i+past_history_size:i+past_history_size+future_target,-1])
        x2.append(dataset[i+past_history_size:i+past_history_size+future_target,-2])
        x3.append(dataset[i+past_history_size:i+past_history_size+future_target,-3])
        y.append(dataset[i+past_history_size:i+past_history_size+future_target,0])
    x,x1,x2,x3 = np.array(x),np.array(x1),np.array(x2),np.array(x3)
    x = x.reshape([-1,past_history_size,num_nodes])
    x2=x2.reshape(-1,future_target,1)
    x1=x1.reshape([-1,future_target,1])
    x3=x3.reshape([-1,future_target,1])
    
    x = np.array(x).astype(float)
    x1 = np.array(x1).astype(float)
    x2 = np.array(x2).astype(float)
    x3 = np.array(x3).astype(float)
    y=np.array(y).astype(float)
    # 将x1, x2, x3拼接到x上
    x = torch.tensor(x, dtype=torch.float)
    x1 = torch.tensor(x1, dtype=torch.float)
    x2 = torch.tensor(x2, dtype=torch.float)
    x3 = torch.tensor(x3, dtype=torch.float)  # 转换x3为Tensor
    x = torch.cat((x, x1, x2, x3), dim=2)  # 在最后一个维度上拼接x1, x2, x3
    
    y=torch.tensor(y,dtype = torch.float)
    
    #print('y'*100)
    print(x.shape,y.shape)
    loader = Data.DataLoader(dataset=Data.TensorDataset(x, y),batch_size=batch_size,
                                               shuffle=shuffle, drop_last=False,num_workers=0)
    return loader

def process_test(dataset):
    #观看过去时间窗口 过去多少天
    past_history_size = 16
    #预测未来值n天
    future_target = 16 
    x = []
    y = []
    x1= []
    x2= []
    x3= []
    dataset=dataset.values
    for i in tqdm(range(len(dataset)-past_history_size-future_target+1)):
        x.append(dataset[i:i+past_history_size,:])
        x1.append(dataset[i+past_history_size:i+past_history_size+future_target,-1])
        x2.append(dataset[i+past_history_size:i+past_history_size+future_target,-2])
        x3.append(dataset[i+past_history_size:i+past_history_size+future_target,-3])
        y.append(dataset[i+past_history_size:i+past_history_size+future_target,0])
    x,x1,x2,x3 = np.array(x),np.array(x1),np.array(x2),np.array(x3)
    x = x.reshape([-1,past_history_size,num_nodes])
    x2=x2.reshape(-1,future_target,1)
    x1=x1.reshape([-1,future_target,1])
    x3=x3.reshape([-1,future_target,1])
    
    x = np.array(x).astype(float)
    x1 = np.array(x1).astype(float)
    x2 = np.array(x2).astype(float)
    x3 = np.array(x3).astype(float)
    y=np.array(y).astype(float)
    # 将x1, x2, x3拼接到x上
    x = torch.tensor(x, dtype=torch.float)
    x1 = torch.tensor(x1, dtype=torch.float)
    x2 = torch.tensor(x2, dtype=torch.float)
    x3 = torch.tensor(x3, dtype=torch.float)  # 转换x3为Tensor
    x = torch.cat((x, x1, x2, x3), dim=2)  # 在最后一个维度上拼接x1, x2, x3
    
    y=torch.tensor(y,dtype = torch.float)
    
    print(x.shape,y.shape)
    return x,y


Dtr = process(dataset=train,shuffle=True)
Val = process(dataset=val,shuffle=True)
testX,testY = process_test(dataset=test)
#Y_test  = torch.tensor(Y_test,dtype = torch.float)
#print(x_train.shape, y_train.shape,x_test.shape,y_test.shape)
return  Dtr, Val, testX,testY,result#, scaler1

class BiLSTM_Attention(nn.Module):

def init (self, args):

super(BiLSTM_Attention, self).init ()

self.args = args

#def init (self, input_size, hidden_size, output_size, num_layers=1):

self.hidden_size = args.hidden_size

self.num_layers = args.num_layers

self.lstm = nn.LSTM(args.input_size, args.hidden_size, args.num_layers, batch_first=True, bidirectional=True)

self.fc = nn.Linear(args.hidden_size * 2, args.output_size) # 双向LSTM,输出尺寸翻倍

self.attention_layer = nn.Sequential(

nn.Linear(args.hidden_size * 2, args.hidden_size), # 注意力层的全连接层

nn.Tanh(),

nn.Linear(args.hidden_size, 1, bias=False)

)

复制代码
def attention(self, lstm_output):
    # 将lstm_output看作是query和key,简化注意力机制
    attn_weights = torch.matmul(lstm_output, lstm_output.transpose(1, 2))  # 点积注意力,不涉及permute
    attn_weights = attn_weights / torch.sqrt(torch.tensor(self.hidden_size * 2).float())  # 缩放因子
    attn_weights = torch.softmax(attn_weights, dim=-1)
    # 加权求和
    context_vector = torch.bmm(attn_weights, lstm_output)
    
    return context_vector

def forward(self, x):
    h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device)
    c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device)
    out, _ = self.lstm(x, (h0, c0))  # LSTM输出,形状为[batch_size, sequence_length, hidden_size * 2]
    # 应用注意力机制
    context_vector = self.attention(out)  # 经过注意力机制处理后的向量,形状应为[batch_size, hidden_size * 2]
    # 确保全连接层的输出维度与output_size匹配
    # 注意:如果context_vector需要被进一步处理以适应多输出(例如,如果是多任务回归),这里可能需要调整
    output = self.fc(context_vector)  # 经过全连接层后的输出,形状应为[batch_size, output_size]
    #print("outputs[:, 0, :]",output[:, 0, :].shape)
    return output[:, 0, :]

def get_val_loss(args, model, Val):

model.eval()

loss_function = nn.MSELoss().to(args.device)

val_loss = []

for (seq, label) in Val:

with torch.no_grad():

seq, label = seq.to(args.device), label.to(args.device)

label = label.to(args.device)

y_pred = model(seq)

loss = loss_function(y_pred, label)

val_loss.append(loss.item())

复制代码
return np.mean(val_loss)

def print_history(train_loss_,val_loss_):

plt.plot(train_loss_,color='Orange',label="train loss")

#print("history.history['loss']",len(history.history['loss']))

plt.plot(val_loss_,color='b',label="validation loss")

plt.title('train_validation loss')

plt.xlabel('epoch')

plt.ylabel('loss')

plt.legend()

#plt.show()

plt.savefig(path+r'/loss/BiLSTM_Attention.png')

plt.close()

def train(args, Dtr, Val):

model = BiLSTM_Attention(args).to(args.device)

loss_function = nn.MSELoss().to(args.device)

optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

print('training...')

epochs = args.epochs

min_epochs = 10

best_model = model#None

min_val_loss = 5

final_val_loss = []

train_loss_,val_loss_=[],[]

scheduler = StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)

for epoch in range(epochs):

train_loss = []

for batch_idx, (seq, target) in enumerate(Dtr, 0):

seq, target = seq.to(args.device), target.to(args.device)

#target=target.reshape(-1,1)

optimizer.zero_grad()

y_pred = model(seq)

#print("y_pred",y_pred.shape)

#print("target",target.shape)

loss = loss_function(y_pred, target)

train_loss.append(loss.item())

loss.backward()

optimizer.step()

scheduler.step()

validation

val_loss = get_val_loss(args, model, Val)

复制代码
    if epoch + 1 >= min_epochs and val_loss < min_val_loss:
        min_val_loss = val_loss
        best_model = copy.deepcopy(model)

    print('epoch {:03d} train_loss {:.8f} val_loss {:.8f}'.format(epoch, np.mean(train_loss), val_loss))
    final_val_loss.append(val_loss)
    model.train()
    
    train_loss_.append(np.mean(train_loss))
    val_loss_.append(val_loss )

print_history(train_loss_,val_loss_)

#state = {'model': best_model.state_dict(), 'optimizer': optimizer.state_dict()}
#torch.save(state,PATH)
#torch.save(best_model,r'D:\我的坚果云\workworkwork\202308work\光伏功率超短期预测\model\BiLSTM_Attention.pth')
#torch.save(best_model, path+r'\model\BiLSTM_Attention.pth')
torch.save(best_model,path+r'/model/BiLSTM_Attention.pth')
#torch.save(best_model, path+r'\model\BiLSTM_Attention.pth')
return np.mean(final_val_loss)

@torch.no_grad()

def test(X_test1,ys,path,result,Cap):#,scaler1):# scaler,

print('loading models...')

X_test1 = torch.tensor(X_test1,dtype = torch.float).to(device)

print(X_test1.shape)

#BiLSTM_Attention

model=torch.load(path+r'/model/BiLSTM_Attention.pth').to(device)#, map_location=torch.device('cpu'))

#model.eval()

print('predicting...')

preds = model(X_test1)#,X_test2)

preds = preds.detach().cpu().numpy()

ys = ys.numpy()

#print(ys.shape,preds.shape)

#y = scaler1.inverse_transform(ys)#.T

#pred = scaler1.inverse_transform(preds)#.T

y,pred=ys[:,-1],preds[:,-1]

for j in range(len(pred)):

pred[j] = np.round(pred[j], 3)

if pred[j] < 0:

pred[j] = float(0)

if pred[j]>Cap:

pred[j]=Cap

print(y.shape,pred.shape)

#print(type(y),type(pred))

print('mse:', get_mse(y, pred))

print('rmse:', get_rmse(y, pred))

print('mae:', get_mae(y, pred))

print('mape:', get_mape(y, pred))

print('r2score',get_r2(y,pred))

result['真实值']=y#[2192-2014:]

result['预测值']=pred#[2192-2014:]

plot_test(y, pred)

result.to_csv(path+r'/result/BiLSTM_Attention.csv', sep=',')

mse=get_mse(y, pred)

rmse=get_rmse(y, pred)

mae=get_mae(y, pred)

mape= get_mape(y, pred)

r2score=get_r2(y,pred)

df = {"mse": mse, "rmse": rmse,"mae": mae, "mape": mape,"r2score":r2score}

df = pd.DataFrame(list(df.items()))

df.to_csv(path + '/result_score/BiLSTM_Attention-result_score.csv')

@torch.no_grad()

def test16(X_test1,ys,path,result,Cap):#,scaler1):# scaler,

复制代码
print('loading models...')
X_test1 = torch.tensor(X_test1,dtype = torch.float).to(device)
print(X_test1.shape)

model=torch.load(path+r'/model/BiLSTM_Attention.pth').to(device)#, map_location=torch.device('cpu'))
#model.eval()
print('predicting...')
preds = model(X_test1)#,X_test2)
preds = preds.detach().cpu().numpy()
ys    = ys.numpy()
#print(ys.shape,preds.shape)
#y = scaler1.inverse_transform(ys)#.T
#pred = scaler1.inverse_transform(preds)#.T
y,pred=ys[:,-1],preds[:,-1]
print(y.shape,pred.shape)
#print(type(y),type(pred))
print('mse:', get_mse(y, pred))
print('rmse:', get_rmse(y, pred))
print('mae:', get_mae(y, pred))
print('mape:', get_mape(y, pred))
print('r2score',get_r2(y,pred))
result['真实值']=y#[2192-2014:]
result['预测值']=pred#[2192-2014:]
plot_test(y, pred)

def correction(jj):
    for j in range(len(preds[:,jj])):
        preds[:,jj][j] = np.round(preds[:,jj][j], 3)
        if preds[:,jj][j] < 0:
            preds[:,jj][j] = float(0)
        if preds[:,jj][j]>Cap:
            preds[:,jj][j]=Cap

for j in range(16):
    correction(j)
   
result['真实值']=y
for i in range(16):
    result['预测值'+str(i)]=preds[:,i]

result.to_csv(path+r'/result/BiLSTM_Attention16个点.csv', sep=',')
mse=get_mse(y, pred)
rmse=get_rmse(y, pred)
mae=get_mae(y, pred)
mape= get_mape(y, pred)
r2score=get_r2(y,pred)
df = {"mse": mse, "rmse": rmse,"mae": mae, "mape": mape,"r2score":r2score}
df = pd.DataFrame(list(df.items()))
df.to_csv(path + '/result_score/BiLSTM_Attention取16个点最后一个-result_score.csv')

def plot_test(y, pred):

plot

plt.plot(y, color='blue', label='true value')

plt.plot(pred, color='red', label='pred value')

plt.title('BiLSTM_Attention的预测结果')

#plt.grid(True)

plt.legend(loc='upper center', ncol=6)

#plt.show()

plt.savefig(path+r'/pictures/BiLSTM_Attention.png')

def get_mape(y_true, y_pred):

"""

计算平均绝对百分比误差 (Mean Absolute Percentage Error)

"""

防止除以零和负数开根号的情况

y_true = np.where(y_true == 0, 1e-6, y_true) # 对于0值,用一个极小正数替代以避免除法错误

return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

def get_r2(y_true, y_pred):

"""

计算决定系数 R²

"""

rss = np.sum((y_true - y_pred) ** 2) # 剩余平方和

tss = np.sum((y_true - np.mean(y_true)) ** 2) # 总平方和

return 1 - (rss / tss)

def get_mae(y_true, y_pred):

"""

计算平均绝对误差 (Mean Absolute Error)

"""

return np.mean(np.abs(y_true - y_pred))

def get_mse(y_true, y_pred):

"""

计算均方误差 (Mean Squared Error)

"""

return np.mean((y_true - y_pred) ** 2)

def get_rmse(y, pred):

return np.sqrt(get_mse(y, pred))

def get_best_parameters(args, Dtr, Val):

def objective(trial):

model = BiLSTM_Attention(args).to(args.device)

loss_function = nn.MSELoss().to(args.device)

optimizer = trial.suggest_categorical('optimizer',

torch.optim.SGD, torch.optim.RMSprop, torch.optim.Adam\])( model.parameters(), lr=trial.suggest_loguniform('lr', 5e-4, 1e-2)) print('training...') epochs = 10 val_loss = 0 for epoch in range(epochs): train_loss = \[

for batch_idx, (seq, target) in enumerate(Dtr, 0):

seq, target = seq.to(args.device), target.to(args.device)

optimizer.zero_grad()

y_pred = model(seq)

loss = loss_function(y_pred, target)

train_loss.append(loss.item())

loss.backward()

optimizer.step()

validation

val_loss = get_val_loss(args, model, Val)

复制代码
        print('epoch {:03d} train_loss {:.8f} val_loss {:.8f}'.format(epoch, np.mean(train_loss), val_loss))
        model.train()

    return val_loss

sampler = optuna.samplers.TPESampler()
study = optuna.create_study(sampler=sampler, direction='minimize')
study.optimize(func=objective, n_trials=5)
pruned_trials = study.get_trials(deepcopy=False,
                                 states=tuple([optuna.trial.TrialState.PRUNED]))
complete_trials = study.get_trials(deepcopy=False,
                                   states=tuple([optuna.trial.TrialState.COMPLETE]))
best_trial = study.best_trial
print('val_loss = ', best_trial.value)
for key, value in best_trial.params.items():
    print("{}: {}".format(key, value))

def main():

Cap=300

args = args_parser()

Dtr, Val, testX,testY,result= nn_seq(args)

get_best_parameters(args, Dtr, Val)

import time

T1 = time.time()

train(args, Dtr, Val)#, PATH

print('train用时',time.time()-T1)

T2=time.time()

test( testX,testY,path,result,Cap)

test16( testX,testY,path,result,Cap)#, scaler1)

print('test用时',time.time()-T2)

if name == 'main ':

main()

相关推荐
Alvin千里无风6 分钟前
在 Ubuntu 上从源码安装 Nanobot:轻量级 AI 助手完整指南
linux·人工智能·ubuntu
环黄金线HHJX.7 分钟前
龙虾钳足启发的AI集群语言交互新范式
开发语言·人工智能·算法·编辑器·交互
Omics Pro8 分钟前
虚拟细胞:开启HIV/AIDS治疗新纪元的关键?
大数据·数据库·人工智能·深度学习·算法·机器学习·计算机视觉
悦来客栈的老板39 分钟前
AI逆向|猿人学逆向反混淆练习平台第七题加密分析
人工智能
KOYUELEC光与电子努力加油44 分钟前
JAE日本航空端子推出支持自走式机器人的自主充电功能浮动式连接器“DW15系列“方案与应用
服务器·人工智能·机器人·无人机
萤火阳光1 小时前
13|自定义 Skill 创作:打造专属自动化利器
人工智能
我哪会这个啊1 小时前
SpringAlibaba Ai基础入门
人工智能
tianbaolc1 小时前
Claude Code 源码剖析 模块一 · 第六节:autoDream 自动记忆整合
人工智能·ai·架构·claude code
蓝色的杯子2 小时前
从 LLM 到 Agent Skill,龙虾的技术基础 · ② Token
人工智能
tq10862 小时前
AI时代的价值冲击——共识瓦解与转型阵痛
人工智能