基于pytorch代价敏感平衡一维ICIIDS-2017数据集进行15分类并用cnn训练的代码
时间: 2024-01-02 21:04:33 浏览: 156
使用PyTorch实现CNN训练一个针对于CIFAR10数据集的分类识别网络,Python源码.zip
5星 · 资源好评率100%
以下是一个基于 PyTorch 的代价敏感平衡一维ICIIDS-2017数据集进行15分类并用CNN训练的代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
import numpy as np
import pandas as pd
# 定义数据集类
class ICIIDSDataset(Dataset):
def __init__(self, data_path):
data = pd.read_csv(data_path, header=None)
self.x = data.iloc[:, :-1].values
self.y = data.iloc[:, -1].values
self.y = self.y.astype(np.int64)
self.class_weight = torch.tensor([0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05])
def __len__(self):
return len(self.y)
def __getitem__(self, idx):
return torch.tensor(self.x[idx]), torch.tensor(self.y[idx])
# 定义CNN模型
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2)
self.pool = nn.MaxPool1d(kernel_size=2, stride=2)
self.conv2 = nn.Conv1d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(32 * 50, 128)
self.fc2 = nn.Linear(128, 15)
def forward(self, x):
x = x.unsqueeze(1)
x = self.conv1(x)
x = torch.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = torch.relu(x)
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = torch.relu(x)
x = self.fc2(x)
return x
# 定义训练函数
def train(model, device, train_loader, optimizer, criterion):
model.train()
total_loss = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data.float())
loss = criterion(output, target)
loss.backward()
optimizer.step()
total_loss += loss.item()
return total_loss / len(train_loader)
# 定义测试函数
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data.float())
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader)
accuracy = 100. * correct / len(test_loader.dataset)
return test_loss, accuracy
# 加载数据集
train_dataset = ICIIDSDataset('train.csv')
test_dataset = ICIIDSDataset('test.csv')
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# 初始化模型、优化器、损失函数
model = CNN()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
weights = train_dataset.class_weight.to(device)
criterion = nn.CrossEntropyLoss(weight=weights)
# 训练模型
for epoch in range(20):
train_loss = train(model, device, train_loader, optimizer, criterion)
test_loss, accuracy = test(model, device, test_loader, criterion)
print('Epoch: {} Train Loss: {:.6f} Test Loss: {:.6f} Accuracy: {:.2f}%'.format(
epoch, train_loss, test_loss, accuracy))
```
需要注意的是,这只是一个简单的示例代码,实际使用时需要根据具体数据集和任务进行调整。
阅读全文