基于Pytorch实现多层感知机回归预测,一共有15000个数据样本,13个特征, 同时可视化训练集和验证集loss曲线及r2变化曲线,并最终在测试集上预测
时间: 2024-04-09 09:28:05 浏览: 217
基于pytorch的多元线性回归模型.zip
5星 · 资源好评率100%
你可以使用PyTorch来实现多层感知机(MLP)回归预测。下面是一个简单的示例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
# 创建一个多层感知机模型
class MLP(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(MLP, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# 定义模型超参数
input_size = 13
hidden_size = 64
output_size = 1
learning_rate = 0.001
num_epochs = 100
# 生成随机数据集
X = np.random.randn(15000, 13)
y = np.random.randn(15000, 1)
# 划分训练集、验证集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)
# 转换为Tensor
X_train = torch.Tensor(X_train)
y_train = torch.Tensor(y_train)
X_val = torch.Tensor(X_val)
y_val = torch.Tensor(y_val)
X_test = torch.Tensor(X_test)
y_test = torch.Tensor(y_test)
# 创建模型、损失函数和优化器
model = MLP(input_size, hidden_size, output_size)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
train_losses = []
val_losses = []
r2_scores = []
for epoch in range(num_epochs):
# 训练模式
model.train()
# 前向传播和计算损失
outputs = model(X_train)
train_loss = criterion(outputs, y_train)
# 反向传播和优化
optimizer.zero_grad()
train_loss.backward()
optimizer.step()
# 验证模式
model.eval()
# 计算验证集损失和r2分数
with torch.no_grad():
val_outputs = model(X_val)
val_loss = criterion(val_outputs, y_val)
r2 = r2_score(y_val.numpy(), val_outputs.numpy())
# 记录训练和验证集的损失以及r2分数
train_losses.append(train_loss.item())
val_losses.append(val_loss.item())
r2_scores.append(r2)
# 可视化训练集和验证集的损失曲线及r2变化曲线
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(train_losses, label='Train Loss')
plt.plot(val_losses, label='Val Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(r2_scores)
plt.xlabel('Epoch')
plt.ylabel('R2 Score')
plt.tight_layout()
plt.show()
# 在测试集上预测
model.eval()
with torch.no_grad():
test_outputs = model(X_test)
test_loss = criterion(test_outputs, y_test)
test_r2 = r2_score(y_test.numpy(), test_outputs.numpy())
print("Test Loss: {:.4f}, Test R2 Score: {:.4f}".format(test_loss.item(), test_r2))
```
这段代码首先定义了一个简单的多层感知机模型(MLP),包括一个输入层,一个隐藏层和一个输出层。然后,使用PyTorch的优化器和损失函数来训练模型,并在每个epoch结束时计算训练集和验证集的损失以及r2分数。最后,在测试集上进行预测,并计算测试集的损失和r2分数。
运行这段代码后,你将得到训练集和验证集的损失曲线以及r2变化曲线的可视化结果,并输出测试集的损失和r2分数。
阅读全文