使用seq2seq预测多变量时序多输出回归代码

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 seq_to_seqModel

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 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))
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

seq2seq

def seq2seq_args_parser():
parser = argparse.ArgumentParser()

parser.add_argument('--epochs', type=int, default=10, help='input dimension')
parser.add_argument('--input_size', type=int, default=27, help='input dimension')
parser.add_argument('--seq_len', type=int, default=16, help='seq len')
parser.add_argument('--output_size', type=int, default=16, help='output dimension')
parser.add_argument('--hidden_size', type=int, default=64, help='hidden size')
parser.add_argument('--num_layers', type=int, default=1, help='num layers')
parser.add_argument('--lr', type=float, default=0.005857039826063427, help='learning rate')#0.005
parser.add_argument('--batch_size', type=int, default=128, help='batch size')
parser.add_argument('--optimizer', type=str, default='RMSprop', help='type of optimizer')#'adam'
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('--bidirectional', type=bool, default=False, help='LSTM direction')
parser.add_argument('--step_size', type=int, default=10, help='step size')
parser.add_argument('--pred_step_size', type=int, default=1, help='pred step size')
parser.add_argument('--gamma', type=float, default=0.1, help='gamma')

args = parser.parse_args()

return args

def nn_seq(args):
batch_size= args.batch_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

from torch import nn
import torch,random

device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

def setup_seed(seed):
os.environ[‘PYTHONHASHSEED’] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True

‘’’
class LSTM(nn.Module):
def init(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().init()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_size, self.output_size)

def forward(self, input_seq):
    batch_size, seq_len = input_seq.shape[0], input_seq.shape[1]
    h_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    c_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    # print(input_seq.size())
    # input(batch_size, seq_len, input_size)
    # output(batch_size, seq_len, num_directions * hidden_size)
    output, _ = self.lstm(input_seq, (h_0, c_0))
    pred = self.linear(output)  # pred(batch_size, seq_len, output_size)
    pred = pred[:, -1, :]

    return pred

class BiLSTM(nn.Module):
def init(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().init()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 2
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=False)
self.linear = nn.Linear(self.num_directions * self.hidden_size, self.output_size)

def forward(self, input_seq):
    batch_size, seq_len = input_seq.shape[0], input_seq.shape[1]
    h_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    c_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    output, _ = self.lstm(input_seq, (h_0, c_0))
    pred = self.linear(output)
    pred = pred[:, -1, :]

    return pred

‘’’
class Encoder(nn.Module):
def init(self, input_size, hidden_size, num_layers, batch_size):
super().init()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.num_directions = 1
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=False)

def forward(self, input_seq):
    #batch_size, seq_len = input_seq.shape[0], input_seq.shape[1]
    #h_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    #c_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    #output, (h, c) = self.lstm(input_seq, (h_0, c_0))
    output, (h, c) = self.lstm(input_seq)
    return h, c

class Decoder(nn.Module):
def init(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().init()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1
self.batch_size = batch_size
self.lstm = nn.LSTM(input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=False)
self.linear = nn.Linear(self.hidden_size, self.input_size)

def forward(self, input_seq, h, c):
    # input_seq(batch_size, input_size)
    input_seq = input_seq.unsqueeze(1)
    output, (h, c) = self.lstm(input_seq, (h, c))
    # output(batch_size, seq_len, num * hidden_size)
    pred = self.linear(output.squeeze(1))  # pred(batch_size, 1, output_size)

    return pred, h, c

class Seq2Seq(nn.Module):
def init(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().init()
self.input_size = input_size
self.output_size = output_size
self.Encoder = Encoder(input_size, hidden_size, num_layers, batch_size)
self.Decoder = Decoder(input_size, hidden_size, num_layers, output_size, batch_size)

def forward(self, input_seq):
    target_len = self.output_size  # 预测步长
    batch_size, seq_len, _ = input_seq.shape[0], input_seq.shape[1], input_seq.shape[2]
    h, c = self.Encoder(input_seq)
    outputs = torch.zeros(batch_size, self.input_size, self.output_size).to(device)
    decoder_input = input_seq[:, -1, :]
    for t in range(target_len):
        decoder_output, h, c = self.Decoder(decoder_input, h, c)
        outputs[:, :, t] = decoder_output
        decoder_input = decoder_output
    #print("outputs[:, 0, :]",outputs[:, 0, :].shape)
    return outputs[:, 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/seq_to_seq.png’)
plt.close()

def train(args, Dtr, Val):
input_size, hidden_size, num_layers = args.input_size, args.hidden_size, args.num_layers
output_size = args.output_size
batch_size = args.batch_size
model = Seq2Seq(input_size, hidden_size, num_layers, output_size, batch_size=batch_size).to(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.shape)
#print(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, path+r'/model/seq_to_seq.pth')
#torch.save(best_model, path+r'\model\seq_to_seq.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)

#model=torch.load(path+r'/model/seq_to_seq.pth', map_location=torch.device('cpu'))
model=torch.load(path+r'/model/seq_to_seq.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/transformer.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/transformer-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/seq_to_seq.pth').to(device)
#model=torch.load(path+r'/model/transformer.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/transformer16个点.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/transformer取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(‘seq_to_seq的预测结果’)
#plt.grid(True)
plt.legend(loc=‘upper center’, ncol=6)
#plt.show()
plt.savefig(path+r’/pictures/seq_to_seq.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):
input_size, hidden_size, num_layers = args.input_size, args.hidden_size, args.num_layers
output_size = args.output_size
batch_size = args.batch_size
model = Seq2Seq(input_size, hidden_size, num_layers, output_size, batch_size=batch_size).to(device)
#model =Seq2Seq(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 = seq2seq_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)
print(‘train用时’,time.time()-T1)
T2=time.time()
test(testX,testY,path,result,Cap)
test16(testX,testY,path,result,Cap)
print(‘test用时’,time.time()-T2)

if name == ‘main’:
main()

当然可以。这是一个基本的例子,使用Python的Keras库编写一个简单的多变量时序预测(Multivariate Time Series Forecasting)模型,采用LSTM(长短期记忆)算法: ```python import numpy as np from keras.models import Sequential from keras.layers import Dense, LSTM # 假设我们有三个输入变量(例如股票价格)和一个输出变量(下一个价格) num_features = 3 target_variable = 1 # 定义LSTM模型 model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(None, num_features))) model.add(LSTM(units=50)) model.add(Dense(target_variable)) # 使用mean absolute error (MAE)作为损失函数,适合回归问题 model.compile(optimizer='adam', loss='mae') # 假设X_train和y_train是训练数据集,X_test和y_test是测试数据集 # 数据预处理:填充零到保持所有样本相同的步长(如果有的样本比其他短) def pad_data(X, y): max_length = max(len(x) for x in X) padded_X = np.zeros((len(X), max_length, num_features)) padded_y = np.zeros((len(y), max_length)) for i, seq in enumerate(X): padded_X[i, :len(seq), :] = seq for i, target in enumerate(y): padded_y[i, :len(target)] = target return padded_X, padded_y # 填充数据 X_train_padded, y_train_padded = pad_data(X_train, y_train) X_test_padded, y_test_padded = pad_data(X_test, y_test) # 模型训练 model.fit(X_train_padded, y_train_padded, epochs=50, batch_size=32, validation_split=0.2) # 预测 predictions = model.predict(X_test_padded) # 将预测结果从填充后的形式转换回原始数据形状 unpadded_predictions = [predictions[i, :len(test_seq)] for i, test_seq in enumerate(X_test)] # 可视化或进一步处理预测结果 ``` 在这个例子中,你需要替换 `X_train`, `y_train`, `X_test`, 和 `y_test` 为你实际的数据集。记住,时序数据通常需要先按照时间顺序排列,并且可能需要进行一些预处理步骤,比如标准化或归一化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值