get 1 year ago
parent cbe2d0a68a
commit 8034d7d92c

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="C:\anaconda\envs\pytorch" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="pytorch_gpu" project-jdk-type="Python SDK" />
</project>

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="C:\anaconda\envs\pytorch" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="pytorch_gpu" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,81 @@
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from sklearn import preprocessing
data = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\杭州数据.xlsx',index_col='dtdate')
print(data.columns)
y = np.array(data['售电量']) # 制作标签,用于比对训练结果
x = data.drop(columns=['售电量','city_name']) # 在特征数据集中去掉label
# df.drop(label, axis=0)
# label要删除的列或者行如果要删除多个传入列表
# axis:轴的方向0为行1为列默认为0
fea_train = np.array(x) # 转换为ndarray格式
# 数据标准化操作:(x-均值μ) / 标准差σ ,使数据关于原点对称,提升训练效率
input_features = preprocessing.StandardScaler().fit_transform(fea_train) # fit求出均值和标准差 transform求解
# y归一化 防止梯度爆炸
y = (y - np.min(y))/(np.max(y) - np.min(y))
print(y)
# 设定神经网络的输入参数、隐藏层神经元、输出参数的个数
input_size = input_features.shape[1] # 设定输入特征个数
# np.shape[1]
# 0为行1为列默认为0
# 在此表格中因为每行为各样本的值每列为不同的特征分类所以此处0表示样本数1表示特征数
hidden_size = 64 # 设定隐藏层包含64个神经元
output_size = 1 # 设定输出特征个数为1
batch_size = 32 # 每一批迭代的特征数量
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, hidden_size).to(device), # 第三层 → 第四层
torch.nn.ReLU().to(device),
torch.nn.Linear(hidden_size, output_size).to(device) # 第四层 → 输出层
).to(device)
cost = torch.nn.MSELoss().to(device)
optimizer = torch.optim.Adam(my_nn.parameters(), lr=0.001)
# 训练网络
losses = []
for i in range(300):
batch_loss = []
# 采用MINI-Batch的方法进行训练
for start in range(0, len(input_features), batch_size):
end = start + batch_size if start + batch_size < len(input_features) else len(input_features)
x_train = torch.tensor(input_features[start:end], dtype=torch.float32, requires_grad=True).to(device)
y_train = torch.tensor(y[start:end], dtype=torch.float32, requires_grad=True).to(device)
prediction = my_nn(x_train)
loss = cost(y_train, 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(losses)
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()

@ -87,14 +87,14 @@ def run(file_dir,excel):
train_loss = []
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
for i in range(1500):
for i in range(2500):
out = model(train_x)
loss = loss_function(out, train_y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_loss.append(loss.item())
# print(loss)
print(loss)
# 保存模型
# torch.save(model.state_dict(),save_filename)
# torch.save(model.state_dict(),os.path.join(model_save_dir,model_file))
@ -121,7 +121,7 @@ def run(file_dir,excel):
# result_list = []
# 以x为基础实际数据滚动预测未来3天
x = torch.from_numpy(df[-14:-4]).to(device)
pred = model(x.reshape(-1,1,DAYS_FOR_TRAIN)).view(-1).detach().numpy()
pred = model(x.reshape(-1,1,DAYS_FOR_TRAIN)).view(-1).cpu().detach().numpy()
# for i in range(3):
@ -147,7 +147,7 @@ def run(file_dir,excel):
print(target)
print(result_eight)
final_df = pd.concat(list_app,ignore_index=True)
final_df.to_csv('市行业电量.csv',encoding='gbk')
# final_df.to_csv('市行业电量.csv',encoding='gbk')
print(final_df)
# result_eight.to_csv(f'./月底预测结果/9月{excel[:2]}.txt', sep='\t', mode='a')
@ -155,7 +155,7 @@ def run(file_dir,excel):
# f.write(f'{excel[:2]}{industry}:{round(target, 5)}\n')
if __name__ == '__main__':
file_dir = r'C:\python-project\pytorch3\浙江行业电量\浙江所有地市133行业数据'
file_dir = r'C:\Users\user\PycharmProjects\pytorch2\浙江行业电量\浙江所有地市133行业数据'
run(file_dir,'丽水133行业数据.xlsx')
# p = Pool(4)

Loading…
Cancel
Save