写一个卷积神经网络(CNN)同时实现定量回归分析和分类判别分析的详细python代码,使用pytorch,GPU最好带上数据
时间: 2024-04-30 07:17:37 浏览: 61
基于Python实现神经网络回归和分类任务【100010954】
下面是一个简单的CNN模型,可以同时进行定量回归和分类判别分析:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
# 定义CNN模型
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(32 * 7 * 7, 128)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(128, 10)
self.fc3 = nn.Linear(128, 1)
def forward(self, x):
out = self.conv1(x)
out = self.relu1(out)
out = self.pool1(out)
out = self.conv2(out)
out = self.relu2(out)
out = self.pool2(out)
out = out.view(-1, 32 * 7 * 7)
out1 = self.fc1(out)
out1 = self.relu3(out1)
out1 = self.fc2(out1)
out2 = self.fc3(out1)
return out1, out2
# 数据准备
X = np.random.rand(1000, 1, 28, 28)
y1 = np.random.randint(0, 10, size=(1000,))
y2 = np.random.rand(1000, 1) * 100
X = torch.from_numpy(X).float()
y1 = torch.from_numpy(y1).long()
y2 = torch.from_numpy(y2).float()
# 划分数据集
train_size = int(0.8 * len(X))
test_size = len(X) - train_size
train_dataset = TensorDataset(X[:train_size], y1[:train_size], y2[:train_size])
test_dataset = TensorDataset(X[train_size:], y1[train_size:], y2[train_size:])
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=True)
# 模型训练
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = CNN().to(device)
criterion1 = nn.CrossEntropyLoss()
criterion2 = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
epochs = 10
for epoch in range(epochs):
train_loss1, train_loss2 = 0, 0
for batch_idx, (data, target1, target2) in enumerate(train_loader):
data, target1, target2 = data.to(device), target1.to(device), target2.to(device)
optimizer.zero_grad()
output1, output2 = model(data)
loss1 = criterion1(output1, target1)
loss2 = criterion2(output2, target2)
loss = loss1 + loss2
loss.backward()
optimizer.step()
train_loss1 += loss1.item()
train_loss2 += loss2.item()
train_loss1 /= len(train_loader.dataset)
train_loss2 /= len(train_loader.dataset)
print('Epoch: {}, Loss1: {:.6f}, Loss2: {:.6f}'.format(epoch+1, train_loss1, train_loss2))
# 模型测试
test_loss1, test_loss2 = 0, 0
correct = 0
total = 0
with torch.no_grad():
for data, target1, target2 in test_loader:
data, target1, target2 = data.to(device), target1.to(device), target2.to(device)
output1, output2 = model(data)
loss1 = criterion1(output1, target1)
loss2 = criterion2(output2, target2)
test_loss1 += loss1.item()
test_loss2 += loss2.item()
_, predicted = torch.max(output1.data, 1)
total += target1.size(0)
correct += (predicted == target1).sum().item()
test_loss1 /= len(test_loader.dataset)
test_loss2 /= len(test_loader.dataset)
accuracy = 100 * correct / total
print('Test Loss1: {:.6f}, Test Loss2: {:.6f}, Accuracy: {:.2f}%'.format(test_loss1, test_loss2, accuracy))
```
上面的代码中,我们定义了一个包含两个分支的CNN模型,其中一个分支用于分类判别分析,另一个分支用于定量回归分析。我们使用`nn.CrossEntropyLoss()`作为分类分支的损失函数,使用`nn.MSELoss()`作为回归分支的损失函数。在训练过程中,我们将两个损失函数相加作为总损失函数。同时,我们也计算了分类分支的准确率作为模型性能的评估指标。
阅读全文