使用pytorch构建一个三层12个神经元的BP神经网络多输入多输出预测模型,以0.7:0.15:0.15划分训练集、验证集、测试集,以RMSE评价模型的有效性,梯度下降法作为优化器,给每一行代码标出注释
时间: 2024-06-04 09:12:53 浏览: 101
好的,我会按照您的要求进行回答。
以下是代码实现:
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from sklearn.metrics import mean_squared_error
# 定义多输入多输出BP神经网络模型
class BPNN(nn.Module):
def __init__(self, input_size, output_size, hidden_size):
super().__init__()
self.fc1 = nn.Linear(input_size, hidden_size) # 第一层全连接层
self.fc2 = nn.Linear(hidden_size, hidden_size) # 第二层全连接层
self.fc3 = nn.Linear(hidden_size, output_size) # 第三层全连接层
def forward(self, x):
x = torch.relu(self.fc1(x)) # 使用ReLU激活函数
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# 定义训练函数
def train(model, train_loader, criterion, optimizer, device):
model.train() # 模型设为训练状态
train_loss = 0
for inputs, targets in train_loader:
inputs = inputs.to(device)
targets = targets.to(device)
optimizer.zero_grad() # 梯度清零
outputs = model(inputs) # 正向传播
loss = criterion(outputs, targets) # 计算损失
loss.backward() # 反向传播
optimizer.step() # 更新权重
train_loss += loss.item() * inputs.size(0) # 累计训练损失
return train_loss
# 定义验证函数
def validate(model, val_loader, criterion, device):
model.eval() # 模型设为评估状态
val_loss = 0
with torch.no_grad():
for inputs, targets in val_loader:
inputs = inputs.to(device)
targets = targets.to(device)
outputs = model(inputs) # 正向传播
loss = criterion(outputs, targets) # 计算损失
val_loss += loss.item() * inputs.size(0) # 累计验证损失
return val_loss
# 定义测试函数
def test(model, test_loader, device):
model.eval() # 模型设为评估状态
y_true = []
y_pred = []
with torch.no_grad():
for inputs, targets in test_loader:
inputs = inputs.to(device)
targets = targets.to(device)
outputs = model(inputs) # 正向传播
y_true.extend(targets.cpu().numpy().tolist()) # 累计测试真实值
y_pred.extend(outputs.cpu().numpy().tolist()) # 累计测试预测值
rmse = np.sqrt(mean_squared_error(y_true, y_pred)) # 计算RMSE
return rmse
# 定义主函数
def main():
# 数据准备
X = np.random.randn(100, 5) # 生成100个5维的输入特征
Y = X[:,0]*2 + X[:,1]*(-3) + X[:,2]*0.5 + np.random.randn(100)*0.5 # 生成随机的输出值
X_train, X_val, X_test = np.split(X, [int(0.7*len(X)), int(0.85*len(X))]) # 数据集划分
Y_train, Y_val, Y_test = np.split(Y, [int(0.7*len(Y)), int(0.85*len(Y))])
train_dataset = torch.utils.data.TensorDataset(torch.Tensor(X_train), torch.Tensor(Y_train)) # 将数据转化为torch.Tensor
val_dataset = torch.utils.data.TensorDataset(torch.Tensor(X_val), torch.Tensor(Y_val))
test_dataset = torch.utils.data.TensorDataset(torch.Tensor(X_test), torch.Tensor(Y_test))
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=10, shuffle=True) # 数据加载器
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=10)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=10)
# 模型训练
input_size = 5 # 输入特征维度
output_size = 1 # 输出维度
hidden_size = 12 # 隐层神经元数量
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 判断是否有可用的GPU
model = BPNN(input_size, output_size, hidden_size).to(device) # 初始化模型
criterion = nn.MSELoss() # 定义损失函数
optimizer = optim.SGD(model.parameters(), lr=0.01) # 定义优化器
for epoch in range(100): # 遍历训练集100次
train_loss = train(model, train_loader, criterion, optimizer, device)
val_loss = validate(model, val_loader, criterion, device)
print('Epoch {}: Train Loss: {:.4f}, Val Loss: {:.4f}'.format(epoch+1, train_loss, val_loss))
# 模型测试
rmse = test(model, test_loader, device)
print('Test RMSE: {:.4f}'.format(rmse))
# 运行主函数
if __name__ == "__main__":
main()
代码注释已标出,希望能够帮助您解决问题。如果您还有其他问题,可以继续向我提问。