读取准备好的数据,分为训练集与测试集
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
import torch
import torch.optim as Opt
import warnings
from sklearn import metrics
warnings.filterwarnings("ignore")
# 从excel文件中读取数据,读取后数据形式为Dataframe
data0 = pd.read_excel("D:\\Machine Learning\\ML_train\\2000.xls", 0)
data0 = data0.iloc[:, :] # 将指针读取
# 数据归一化 适用Relu和sigmoid,sk对dataframe操作
min_max_scaler = preprocessing.MinMaxScaler()
data1 = min_max_scaler.fit_transform(data0)
# 预处理后数据变为ndarray格式
all_data_one = pd.DataFrame(data1, columns=data0.columns)
x = all_data_one.iloc[:, :-1]
y = all_data_one.iloc[:, -1]
# 划分训练集与测试集
cut = 300 # 取最后cut行数据为测试集
# DataFrame切片操作
x_train, x_test = x.iloc[:-cut], x.iloc[-cut:]
y_train, y_test = y.iloc[:-cut], y.iloc[-cut:]
# DataFrame格式转换为ndarray用.values
x_train, x_test = x_train.values, x_test.values
y_train, y_test = y_train.values, y_test.values
# ndarray格式转换为Tensor 或 torch.from_numpy()
x_train = torch.tensor(x_train, dtype=torch.float, requires_grad=True)
x_test = torch.tensor(x_test, dtype=torch.float)
y_train = torch.tensor(y_train, dtype=torch.float, requires_grad=True)
y_test = torch.tensor(y_test, dtype=torch.float)
构建网络模型
# 构建网络模型
Epochs = 60000
"""
辅助设计:
structure = [3, 5, 1]
def Layer_setting(structure):
for layer_set in range(len(structure)-2):
print(f"torch.nn.Linear({structure[layer_set]}, {structure[layer_set+1]}),")
print("torch.nn.Sigmoid()")
print(f"torch.nn.Linear({structure[-2]}, {structure[-1]})")
"""
class nn_model(torch.nn.Module):
def __init__(self):
super(nn_model, self).__init__()
self.model = torch.nn.Sequential(
torch.nn.Linear(3, 12),
torch.nn.Sigmoid(),
torch.nn.Linear(12, 8),
torch.nn.Sigmoid(),
torch.nn.Linear(8, 4),
torch.nn.Sigmoid(),
torch.nn.Linear(4, 1),
)
def forward(self,x):
x = self.model(x)
return x
nn_m = nn_model()
cost = torch.nn.MSELoss(reduction='mean')
optimizer = Opt.Adam(nn_model().parameters(), lr=0.2)
模型训练
# 训练网络
losses = []
for epoch in range(Epochs+1):
optimizer.zero_grad()
training_prediction = nn_m(x_train)
loss = cost(training_prediction, y_train)
losses.append(loss.data.numpy()) # .numpy将tensor转换为ndarray
loss.backward()
optimizer.step()
if epoch % 1000 == 0:
print(f"epoch{epoch},the loss is {loss}.")
验证学习效果
# 验证学习效果
test_pre = nn_m(x_test)
test_pre = test_pre.detach().numpy()
y_test = y_test.detach().numpy()
绘图对比
# 绘图对比
draw = pd.concat([pd.DataFrame(y_test), pd.DataFrame(test_pre)], axis=1) # axis为0时逐列 为1时逐行 concat对象为DataFrame
draw.iloc[:, 0].plot(figsize=(12, 6))
draw.iloc[:, 1].plot(figsize=(12, 6))
plt.legend(('real', 'predict'), loc='upper right', fontsize='15')
plt.title("Predicting Outcomes", fontsize='30')
plt.show()
plt.figure(figsize=(12, 6))
plt.plot(range(len(losses)), losses)
plt.title("Trend of Losses")
plt.xlabel("epoch")
plt.ylabel("loss function")
plt.xlim([0, len(losses)])
plt.show()
print("R2 = ", metrics.r2_score(y_test, test_pre))
这损失完全不带变的
求大佬帮忙看看