基于pytorch代价敏感平衡一维ICIIDS-2017数据集并用cnn训练的代码
时间: 2024-01-12 07:05:30 浏览: 157
通过CNN-pytorch训练识别苹果树叶病害识别-含数据集.zip
5星 · 资源好评率100%
以下是基于PyTorch代价敏感平衡一维ICIIDS-2017数据集并使用CNN训练的代码示例:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import numpy as np
import pandas as pd
#加载数据集
train_data = pd.read_csv('train_data.csv')
test_data = pd.read_csv('test_data.csv')
#将数据集转换为numpy数组
train_data = train_data.values
test_data = test_data.values
#将数据集按标签分为攻击和正常
train_attack = train_data[train_data[:, -1] == 1]
train_normal = train_data[train_data[:, -1] == 0]
test_attack = test_data[test_data[:, -1] == 1]
test_normal = test_data[test_data[:, -1] == 0]
#计算代价敏感权重
num_attack_train = train_attack.shape[0]
num_normal_train = train_normal.shape[0]
num_total_train = num_attack_train + num_normal_train
weight_normal = num_total_train / (2 * num_normal_train)
weight_attack = num_total_train / (2 * num_attack_train)
class_weight = torch.FloatTensor([weight_normal, weight_attack])
#将数据集转换为PyTorch张量
train_data_tensor = torch.FloatTensor(train_data[:, :-1])
train_label_tensor = torch.LongTensor(train_data[:, -1])
test_data_tensor = torch.FloatTensor(test_data[:, :-1])
test_label_tensor = torch.LongTensor(test_data[:, -1])
#创建数据集和数据加载器
train_dataset = TensorDataset(train_data_tensor, train_label_tensor)
test_dataset = TensorDataset(test_data_tensor, test_label_tensor)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
#定义CNN模型
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, stride=1)
self.conv2 = nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, stride=1)
self.pool = nn.MaxPool1d(kernel_size=2)
self.fc1 = nn.Linear(64*23, 128)
self.fc2 = nn.Linear(128, 2)
self.dropout = nn.Dropout(p=0.5)
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = self.relu(x)
x = self.pool(x)
x = x.view(-1, 64*23)
x = self.fc1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return x
#实例化模型、损失函数和优化器
model = CNN()
criterion = nn.CrossEntropyLoss(weight=class_weight)
optimizer = optim.Adam(model.parameters(), lr=0.001)
#训练模型
for epoch in range(50):
model.train()
train_loss = 0.0
train_correct = 0
for data, label in train_loader:
optimizer.zero_grad()
output = model(data.unsqueeze(1))
loss = criterion(output, label)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, pred = torch.max(output, 1)
train_correct += (pred == label).sum().item()
train_acc = train_correct / num_total_train
train_loss /= len(train_loader)
#在测试集上评估模型
model.eval()
test_loss = 0.0
test_correct = 0
with torch.no_grad():
for data, label in test_loader:
output = model(data.unsqueeze(1))
loss = criterion(output, label)
test_loss += loss.item()
_, pred = torch.max(output, 1)
test_correct += (pred == label).sum().item()
test_acc = test_correct / test_data.shape[0]
test_loss /= len(test_loader)
#输出训练和测试结果
print('Epoch: {}, Train Loss: {:.4f}, Train Acc: {:.4f}, Test Loss: {:.4f}, Test Acc: {:.4f}'.format(epoch+1, train_loss, train_acc, test_loss, test_acc))
```
在这个示例中,我们首先加载数据集并按标签分为攻击和正常。然后,我们计算代价敏感权重,将数据集转换为PyTorch张量,并创建数据集和数据加载器。接下来,我们定义了一个简单的CNN模型,并实例化了模型、损失函数和优化器。最后,我们使用训练和测试数据集训练模型,并输出训练和测试结果。
阅读全文