引入必要的库,包括PyTorch、Pandas等。
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.datasets import fetch_california_housing
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD
import torch.utils.data as Data
import matplotlib.pyplot as plt
import seaborn as sns
这里使用sklearn自带的加利福尼亚房价数据,首次运行会下载数据集,建议下载之后,处理成csv格式单独保存,再重新读取。
后续完整代码中,数据也是采用先下载,单独保存之后,再重新读取的方式。
# 导入数据
housedata = fetch_california_housing() # 首次运行会下载数据集
data_x, data_y = housedata.data, housedata.target # 读取数据和标签
data_df = pd.DataFrame(data=data_x, columns=housedata.feature_names) # 将数据处理成dataframe格式
data_df['target'] = data_y # 添加标签列
data_df.to_csv("california_housing.csv") # 将数据输出为CSV文件
housedata_df = pd.read_csv("california_housing.csv") # 重新读取数据
# 切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(housedata[:, :-1], housedata[:, -1],test_size=0.3, random_state=42)
# 数据标准化处理
scale = StandardScaler()
x_train_std = scale.fit_transform(X_train)
x_test_std = scale.transform(X_test)
# 将数据集转为张量
X_train_t = torch.from_numpy(x_train_std.astype(np.float32))
y_train_t = torch.from_numpy(y_train.astype(np.float32))
X_test_t = torch.from_numpy(x_test_std.astype(np.float32))
y_test_t = torch.from_numpy(y_test.astype(np.float32))
# 将训练数据处理为数据加载器
train_data = Data.TensorDataset(X_train_t, y_train_t)
test_data = Data.TensorDataset(X_test_t, y_test_t)
train_loader = Data.DataLoader(dataset=train_data, batch_size=64, shuffle=True, num_workers=1)
# 搭建全连接神经网络回归
class FNN_Regression(nn.Module):
def __init__(self):
super(FNN_Regression, self).__init__()
# 第一个隐含层
self.hidden1 = nn.Linear(in_features=8, out_features=100, bias=True)
# 第二个隐含层
self.hidden2 = nn.Linear(100, 100)
# 第三个隐含层
self.hidden3 = nn.Linear(100, 50)
# 回归预测层
self.predict = nn.Linear(50, 1)
# 定义网络前向传播路径
def forward(self, x):
x = F.relu(self.hidden1(x))
x = F.relu(self.hidden2(x))
x = F.relu(self.hidden3(x))
output = self.predict(x)
# 输出一个一维向量
return output[:, 0]
# 定义优化器
optimizer = torch.optim.SGD(testnet.parameters(), lr=0.01)
loss_func = nn.MSELoss() # 均方根误差损失函数
train_loss_all = []
# 对模型迭代训练,总共epoch轮
for epoch in range(30):
train_loss = 0
train_num = 0
# 对训练数据的加载器进行迭代计算
for step, (b_x, b_y) in enumerate(train_loader):
output = testnet(b_x) # MLP在训练batch上的输出
loss = loss_func(output, b_y) # 均方根损失函数
optimizer.zero_grad() # 每次迭代梯度初始化0
loss.backward() # 反向传播,计算梯度
optimizer.step() # 使用梯度进行优化
train_loss += loss.item() * b_x.size(0)
train_num += b_x.size(0)
train_loss_all.append(train_loss / train_num)
y_pre = testnet(X_test_t)
y_pre = y_pre.data.numpy()
mae = mean_absolute_error(y_test, y_pre)
print('在测试集上的绝对值误差为:', mae)
# -*- coding: utf-8 -*-
# @Time : 2023/8/11 15:58
# @Author : huangjian
# @Email : [email protected]
# @File : FNN_demo.py
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.datasets import fetch_california_housing
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD
import torch.utils.data as Data
from torchsummary import summary
from torchviz import make_dot
import matplotlib.pyplot as plt
import seaborn as sns
# 搭建全连接神经网络回归
class FNN_Regression(nn.Module):
def __init__(self):
super(FNN_Regression, self).__init__()
# 第一个隐含层
self.hidden1 = nn.Linear(in_features=8, out_features=100, bias=True)
# 第二个隐含层
self.hidden2 = nn.Linear(100, 100)
# 第三个隐含层
self.hidden3 = nn.Linear(100, 50)
# 回归预测层
self.predict = nn.Linear(50, 1)
# 定义网络前向传播路径
def forward(self, x):
x = F.relu(self.hidden1(x))
x = F.relu(self.hidden2(x))
x = F.relu(self.hidden3(x))
output = self.predict(x)
# 输出一个一维向量
return output[:, 0]
# 导入数据
housedata_df = pd.read_csv("california_housing.csv")
housedata = housedata_df.values
# 切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(housedata[:, :-1], housedata[:, -1],test_size=0.3, random_state=42)
# 数据标准化处理
scale = StandardScaler()
x_train_std = scale.fit_transform(X_train)
x_test_std = scale.transform(X_test)
# 将训练数据转为数据表
datacor = np.corrcoef(housedata_df.values, rowvar=0)
datacor = pd.DataFrame(data=datacor, columns=housedata_df.columns, index=housedata_df.columns)
plt.figure(figsize=(8, 6))
ax = sns.heatmap(datacor, square=True, annot=True, fmt='.3f', linewidths=.5, cmap='YlGnBu',
cbar_kws={'fraction': 0.046, 'pad': 0.03})
plt.show()
# 将数据集转为张量
X_train_t = torch.from_numpy(x_train_std.astype(np.float32))
y_train_t = torch.from_numpy(y_train.astype(np.float32))
X_test_t = torch.from_numpy(x_test_std.astype(np.float32))
y_test_t = torch.from_numpy(y_test.astype(np.float32))
# 将训练数据处理为数据加载器
train_data = Data.TensorDataset(X_train_t, y_train_t)
test_data = Data.TensorDataset(X_test_t, y_test_t)
train_loader = Data.DataLoader(dataset=train_data, batch_size=64, shuffle=True, num_workers=1)
# 输出网络结构
testnet = FNN_Regression()
summary(testnet, input_size=(1, 8)) # 表示1个样本,每个样本有8个特征
# 输出网络结构
testnet = FNN_Regression()
x = torch.randn(1, 8).requires_grad_(True)
y = testnet(x)
myMLP_vis = make_dot(y, params=dict(list(testnet.named_parameters()) + [('x', x)]))
# 定义优化器
optimizer = torch.optim.SGD(testnet.parameters(), lr=0.01)
loss_func = nn.MSELoss() # 均方根误差损失函数
train_loss_all = []
# 对模型迭代训练,总共epoch轮
for epoch in range(30):
train_loss = 0
train_num = 0
# 对训练数据的加载器进行迭代计算
for step, (b_x, b_y) in enumerate(train_loader):
output = testnet(b_x) # MLP在训练batch上的输出
loss = loss_func(output, b_y) # 均方根损失函数
optimizer.zero_grad() # 每次迭代梯度初始化0
loss.backward() # 反向传播,计算梯度
optimizer.step() # 使用梯度进行优化
train_loss += loss.item() * b_x.size(0)
train_num += b_x.size(0)
train_loss_all.append(train_loss / train_num)
# 可视化训练损失函数的变换情况
plt.figure(figsize=(8, 6))
plt.plot(train_loss_all, 'ro-', label='Train loss')
plt.legend()
plt.grid()
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.show()
y_pre = testnet(X_test_t)
y_pre = y_pre.data.numpy()
mae = mean_absolute_error(y_test, y_pre)
print('在测试集上的绝对值误差为:', mae)
# 可视化测试数据的拟合情况
index = np.argsort(y_test)
plt.figure(figsize=(8, 6))
plt.plot(np.arange(len(y_test)), y_test[index], 'r', label='Original Y')
plt.scatter(np.arange(len(y_pre)), y_pre[index], s=3, c='b', label='Prediction')
plt.legend(loc='upper left')
plt.grid()
plt.xlabel('Index')
plt.ylabel('Y')
plt.show()