基于Pytorch实现多层感知机多输入的回归预测,数据从本地导入,数据集一共有15000个数据点、13个特征,并评估其在测试集上的预测结果,可视化损失曲线及r2变化值
时间: 2024-04-08 07:35:58 浏览: 113
多层感知机 python pytorch
下面是基于PyTorch实现多层感知机(MLP)多输入的回归预测,并评估其在测试集上的预测结果,并可视化损失曲线和R2变化值的示例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
# 导入数据
data = np.loadtxt('data.csv', delimiter=',') # 根据实际数据文件名修改
X = data[:, :-1]
y = data[:, -1]
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 转换为张量
X_train = torch.tensor(X_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.float32)
X_test = torch.tensor(X_test, dtype=torch.float32)
y_test = torch.tensor(y_test, dtype=torch.float32)
# 定义多层感知机模型
class MLP(nn.Module):
def __init__(self, input_size, hidden_size, output_size, num_hidden_layers):
super(MLP, self).__init__()
layers = []
layers.append(nn.Linear(input_size, hidden_size))
layers.append(nn.ReLU())
for _ in range(num_hidden_layers - 1):
layers.append(nn.Linear(hidden_size, hidden_size))
layers.append(nn.ReLU())
layers.append(nn.Linear(hidden_size, output_size))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
# 定义模型参数
input_size = 13
hidden_size = 64
output_size = 1
num_hidden_layers = 4
# 创建MLP模型实例
model = MLP(input_size, hidden_size, output_size, num_hidden_layers)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练模型
num_epochs = 100
train_losses = []
for epoch in range(num_epochs):
model.train()
optimizer.zero_grad()
# 前向传播
outputs = model(X_train)
loss = criterion(outputs, y_train.unsqueeze(1))
# 反向传播和优化
loss.backward()
optimizer.step()
train_losses.append(loss.item())
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Train Loss: {loss.item():.4f}')
# 在测试集上进行预测
model.eval()
with torch.no_grad():
y_pred = model(X_test).squeeze().numpy()
# 计算R2分数
r2 = r2_score(y_test, y_pred)
print(f'R2 Score: {r2:.4f}')
# 可视化损失曲线
plt.plot(train_losses)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training Loss Curve')
plt.show()
# 可视化预测结果
plt.scatter(y_test, y_pred, alpha=0.5)
plt.xlabel('Actual Value')
plt.ylabel('Predicted Value')
plt.title('Regression Predictions')
plt.show()
```
请确保将数据保存在名为`data.csv`的文件中,并将其放置在与代码文件相同的目录下。你可以根据自己的需求修改模型的参数和训练的轮数。运行代码后,将会输出训练过程中的损失值,以及在测试集上计算得到的R2分数。此外,还会显示损失曲线和预测结果的散点图。
阅读全文