import numpy as np import pandas as pd import matplotlib.pyplot as plt import torch from sklearn import preprocessing from torch.utils.data import DataLoader,TensorDataset data = pd.read_excel(r'C:\python-project\pytorch3\入模数据\杭州数据.xlsx',index_col='dtdate') print(data.columns) x = np.array(data.drop(columns=['售电量','city_name']).loc['2021-1':'2023-6']) y = np.array(data['售电量'].loc['2021-1':'2023-6']) # 数据标准化操作:(x-均值μ) / 标准差σ ,使数据关于原点对称,提升训练效率 input_features = preprocessing.StandardScaler().fit_transform(np.array(x)) # fit:求出均值和标准差 transform:求解 # y归一化 min = np.min(y) max = np.max(y) y = (y - min)/(max - min) x_eval = torch.from_numpy(data.drop(columns=['售电量','city_name']).loc['2023-7'].values).type(torch.float32) y_eval = torch.from_numpy(data['售电量'].loc['2023-7'].values).type(torch.float32) ds = TensorDataset(torch.from_numpy(x),torch.from_numpy(y)) dl = DataLoader(ds,batch_size=12,shuffle=True,drop_last=True) # 设定神经网络的输入参数、隐藏层神经元、输出参数的个数 input_size = input_features.shape[1] # 设定输入特征个数 hidden_size = 64 output_size =1 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # 选择使用GPU训练 my_nn = torch.nn.Sequential( torch.nn.Linear(input_size, hidden_size).to(device), # 输入层 → 第一层 torch.nn.ReLU().to(device), torch.nn.Linear(hidden_size, hidden_size).to(device), # 第一层 → 第二层 torch.nn.ReLU().to(device), torch.nn.Linear(hidden_size, hidden_size).to(device), # 第二层 → 第三层 torch.nn.ReLU().to(device), torch.nn.Linear(hidden_size, output_size) ).to(device) cost = torch.nn.MSELoss().to(device) optimizer = torch.optim.Adam(my_nn.parameters(), lr=0.0001) # 训练网络 losses = [] for i in range(1000): batch_loss = [] # 采用MINI-Batch的方法进行训练 for X,y in dl: X,y = X.to(device).type(torch.float32),y.to(device).type(torch.float32) prediction = my_nn(X) loss = cost(y, prediction) optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() batch_loss.append(loss.data.cpu().numpy()) if i % 10 == 0: losses.append(np.mean(batch_loss)) print(i, np.mean(batch_loss)) # 保存模型 # torch.save(my_nn, 'BP.pt') # 绘制图像 # dev_x = [i * 10 for i in range(20)] # plt.xlabel('step count') # plt.ylabel('loss') # plt.xlim((0, 200)) # plt.ylim((0, 1000)) # plt.plot(dev_x, losses) # plt.show()