def LSTM_forecast(train_x, train_y, valid_x, valid_y, test_x, test_y): nodes = test_y.shape[1] print(nodes) epoch = 200 batch = 200 setup_seed(20) criterion = nn.MSELoss() for i in range(nodes): # 得到节点180的训练模型 train_loader = DataLoader(dataset=TensorDataset( train_x[:, i, :, :], train_y[:, i, :]), batch_size=batch, shuffle=True) lstm_model = FCLSTM(9, 8, 48).to(device) optimizer = torch.optim.Adam(lstm_model.parameters(), lr=1e-3) for e in range(epoch): for _, data in enumerate(train_loader): inputs, target = data if cuda_gpu: inputs, target = Variable(inputs).to(device), Variable( target).to(device) else: inputs, target = Variable(inputs), Variable(target) y_hat = lstm_model(inputs) loss = criterion(y_hat, target) optimizer.zero_grad() loss.backward() optimizer.step() if (e + 1) % 10 == 0: # print(device) valid = valid_x[:, i, :, :].to(device) print('Epoch [{}/{}], Loss:{:.4f}'.format( e + 1, epoch, loss.item())) valid_outputs = lstm_model(valid) valid_outputs = valid_outputs.cpu() valid_outputs = valid_outputs * (y_max - y_min) + y_min Valid_y = valid_y * (y_max - y_min) + y_min rmse = measure.GetRMSE(valid_outputs, Valid_y[:, i, :]) print("valid RMSE:", rmse) torch.save(lstm_model, 'FC_LSTM.pkl') print('over') test = test_x[:, i, :, :].to(device) test_outputs = lstm_model(test) if i == 0: result = test_outputs else: result = torch.cat((result, test_outputs), dim=1) result = result.cpu() * (y_max - y_min) + y_min result = result.view(test_y.shape) test_y = test_y * (y_max - y_min) + y_min print("RMSE: ", measure.GetRMSE(test_y, result)) print("MAP: ", measure.GetMAP(test_y, result)) print("SKILL: ", measure.Skill(test_y, result)) print("MAPE: ", measure.GetMAPE(test_y, result)) print("R2: ", measure.R_square(test_y, result))
def SVR_forecast(train_x, train_y, test_x, test_y, pre_len): nodes = test_y.shape[1] print(nodes) result = [] for i in range(nodes): a_X = np.array(train_x[:, i, :]) print(a_X.shape) a_Y = np.array(train_y[:, i, 0]) t_X = np.array(test_x[:, i, :]) print(t_X.shape) svr_model = GridSearchCV(SVR(), param_grid={ "kernel": ("linear", 'rbf', 'sigmoid'), "C": np.logspace(-3, 3, 7), "gamma": np.logspace(-3, 3, 7) }, cv=5) # svr_model = SVR('rbf') svr_model.fit(a_X, a_Y) for i in range(pre_len): if i == 0: pre = svr_model.predict(t_X) Predict = pre.reshape(-1, 1) # print("Predict: ", Predict.shape) else: t_X = np.concatenate((t_X[:, 1:], pre.reshape(-1, 1)), axis=1) pre = svr_model.predict(t_X) Predict = np.concatenate((Predict, pre.reshape(-1, 1)), axis=1) print(Predict.shape) result.append(Predict) result = np.array(result) result = torch.tensor(result).permute(1, 0, 2) * (y_max - y_min) + y_min test_y = test_y * (y_max - y_min) + y_min print("RMSE: ", measure.GetRMSE(test_y, result)) print("MAP: ", measure.GetMAP(test_y, result)) print("SKILL: ", measure.Skill(test_y, result)) print("MAPE: ", measure.GetMAPE(test_y, result)) print("R2: ", measure.R_square(test_y, result)) print(result.shape)
def HA_forecasr(test_x, test_y, pre_len): for i in range(len(test_x)): a = test_x[i] a1 = torch.mean(a, dim=1) for j in range(pre_len): if j == 0: temp = a1.view(-1, 1) else: temp = torch.cat((temp, a1.view(-1, 1)), dim=1) temp = temp.unsqueeze(0) if i == 0: result = temp else: result = torch.cat((result, temp), dim=0) print("result.shape: ", result.shape) result = result * (y_max - y_min) + y_min test_y = test_y * (y_max - y_min) + y_min result1 = torch.tensor(result, dtype=torch.float32) print("RMSE: ", measure.GetRMSE(test_y, result1)) print("MAP: ", measure.GetMAP(test_y, result1)) print("SKILL: ", measure.Skill(test_y, result1)) print("MAPE: ", measure.GetMAPE(test_y, result)) print("R2: ", measure.R_square(test_y, result))
def test(model, his_step, pre_step, x, y): print("device: ", device) feature_num = x.shape[-1] nodes = x.shape[1] x_ones, x_min, x_max = measure.Norm(np.array(torch.tensor(x).view(-1, feature_num), dtype=float), dim=2) y_ones, y_min, y_max = measure.Norm(np.array(torch.tensor(y).view(-1), dtype=float), dim=1) source_x = torch.tensor(x_ones).view(-1, nodes, feature_num) source_y = torch.tensor(y_ones).view(-1, nodes) hist_x, fore_y = Divide(source_x, source_y, his_step, pre_step) _, _, _, _, test_x, test_y = TrainValidTest(hist_x, fore_y, valid=0.05) model = model.to(device) with torch.no_grad(): model = model.eval() test_x = test_x.to(device) test_outputs = model(test_x) test_outputs = test_outputs.cpu() test_y = test_y * (y_max - y_min) + y_min test_outputs = test_outputs * (y_max - y_min) + y_min rmse = measure.GetRMSE(test_outputs, test_y) mae = measure.GetMAP(test_outputs, test_y) skill = measure.Skill(test_outputs, test_y) print("RMSE:", rmse) print("MAE:", mae) print("SKILL:", skill) for i in range(pre_step): print('step ', i) to = test_outputs[:, :, i] ty = test_y[:, :, i] rmse = measure.GetRMSE(ty, to) mae = measure.GetMAP(ty, to) mape = measure.GetMAPE(ty, to) r2 = measure.R_square(ty, to) print("RMSE:", rmse) print("MAE:", mae) print("MAPE:", mape) print("R2:", r2)
def Forecast(his_step, fore_step, x, y, lack=None): # x [num, city, feature] # y [num, city] RSME = [] MAE = [] SKILL = [] feature_num = x.shape[2] num_hidden = 48 num_layers = 1 encoder = model_fs.EncoderGRU(feature_num, num_hidden, num_layers, device=device) decoder = model_fs.AttnDecoderRNN(feature_num, num_hidden, output_size=1) if cuda_gpu: encoder = encoder.to(device) decoder = decoder.to(device) enandde = model_fs.EncoderDecoderAtt(encoder=encoder, decoder=decoder, time_step=fore_step) criterion = nn.MSELoss() if cuda_gpu: enandde = enandde.to(device) criterion = criterion.to(device) optimizer = torch.optim.Adam(enandde.parameters(), lr=1e-2) for city in range(x.shape[1]): x_ones, _, _ = measure.Norm(x[:, city, :], 2) y_ones, y_min, y_max = measure.Norm(y[:, city], 1) hist_x, fore_y = Divide(his_step, fore_step, x_ones, y_ones) batch_size = 100 epoch = 100 train_x, train_y, test_x, test_y = TrainValidTest(hist_x, fore_y) train_loader = DataLoader(dataset=TensorDataset(train_x, train_y), batch_size=batch_size, shuffle=True, num_workers=4) for e in range(epoch): for i, data in enumerate(train_loader): inputs, target = data if cuda_gpu: inputs, target = Variable(inputs).to(device), Variable( target).to(device) else: inputs, target = Variable(inputs), Variable(target) # print("epoch:", e, i, "inputs:", inputs.shape, "target:", target.shape) outputs, attn_weights = enandde(inputs) loss = criterion(outputs, target) optimizer.zero_grad() loss.backward() optimizer.step() if e % epoch == 0 or e + 1 == epoch: print('Epoch [{}/{}], Loss:{:.4f}'.format( e + 1, epoch, loss.item())) with torch.no_grad(): test_x = test_x.to(device) test_outputs, _ = enandde(test_x) test_outputs = test_outputs.cpu() test_y = test_y * (y_max - y_min) + y_min test_outputs = test_outputs * (y_max - y_min) + y_min rmse = measure.GetRMSE(test_outputs, test_y) mae = measure.GetMAP(test_outputs, test_y) skill = measure.Skill(test_outputs, test_y) RSME.append(rmse) MAE.append(mae) SKILL.append(skill) SaveNpy(RSME, "RMSE" + str(lack)) SaveNpy(MAE, "MAE" + str(lack)) SaveNpy(SKILL, "SKILL" + str(lack))