使用housing_renamed.csv中的数据构建两个不同的神经网络模型预测value_per_sq_ft:使用3折交叉验证来评价这两个不同的模型的好坏,要求使用torch
时间: 2024-02-11 11:09:21 浏览: 55
神经网络做预测
好的,这是基于 PyTorch 实现的代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
# 加载数据集
data = pd.read_csv("housing_renamed.csv")
# 分为特征和目标变量
X = data.drop("value_per_sq_ft", axis=1).values
y = data["value_per_sq_ft"].values
# 定义神经网络模型
class Model1(nn.Module):
def __init__(self, input_size):
super(Model1, self).__init__()
self.fc1 = nn.Linear(input_size, 10)
self.fc2 = nn.Linear(10, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
class Model2(nn.Module):
def __init__(self, input_size):
super(Model2, self).__init__()
self.fc1 = nn.Linear(input_size, 20)
self.fc2 = nn.Linear(20, 10)
self.fc3 = nn.Linear(10, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# 定义模型训练函数
def train(model, optimizer, criterion, X_train, y_train):
model.train()
optimizer.zero_grad()
y_pred = model(torch.Tensor(X_train))
loss = criterion(y_pred, torch.Tensor(y_train).unsqueeze(1))
loss.backward()
optimizer.step()
# 定义 KFold 参数
kfold = KFold(n_splits=3, shuffle=True, random_state=42)
# 定义模型评价指标
mse_scores1 = []
mse_scores2 = []
# 对每个 fold 进行训练和评价
for train_idx, test_idx in kfold.split(X):
# 获取训练和测试数据
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
# 定义模型和优化器
model1 = Model1(X_train.shape[1])
optimizer1 = optim.Adam(model1.parameters(), lr=0.01)
criterion1 = nn.MSELoss()
model2 = Model2(X_train.shape[1])
optimizer2 = optim.Adam(model2.parameters(), lr=0.01)
criterion2 = nn.MSELoss()
# 训练第一个模型
for epoch in range(50):
train(model1, optimizer1, criterion1, X_train, y_train)
y_pred1 = model1(torch.Tensor(X_test)).detach().numpy()
mse_scores1.append(mean_squared_error(y_test, y_pred1))
# 训练第二个模型
for epoch in range(50):
train(model2, optimizer2, criterion2, X_train, y_train)
y_pred2 = model2(torch.Tensor(X_test)).detach().numpy()
mse_scores2.append(mean_squared_error(y_test, y_pred2))
# 输出模型评价指标
print("Model 1 MSE Scores:", mse_scores1)
print("Model 1 Average MSE Score:", sum(mse_scores1) / len(mse_scores1))
print("Model 2 MSE Scores:", mse_scores2)
print("Model 2 Average MSE Score:", sum(mse_scores2) / len(mse_scores2))
```
在这个版本的代码中,我们使用 PyTorch 定义了两个神经网络模型,并采用了类似的训练和评价过程。我们使用 MSE(均方误差)作为损失函数,Adam 优化器用于优化模型权重。在训练过程中,我们使用 detach() 方法分离了输出张量和计算图,以便在评价时使用 NumPy 计算 MSE。
希望这可以帮助到你!
阅读全文