二元分类,但只有一个类给出结果



我正在尝试训练一个神经网络。我有两节课。其中一个类的Precision和Recall始终等于0。这是神经网络的代码。

class ConvNet(nn.Module):
def __init__(self):
super(ConvNet,self).__init__()

self.conv1 = nn.Sequential( 
nn.Conv1d(
in_channels=1,
out_channels=200,
kernel_size=4, 
stride=3, 
padding = 0) 

,nn.ReLU()
)

self.maxpool = nn.MaxPool1d(2)
random_input = torch.rand(1, 1, 1500 , requires_grad=False)
random_input = self.conv1(random_input)
random_input = self.maxpool(random_input)
maxpool_out = random_input.view(1, -1).shape[1]
self.fc1 = nn.Sequential(
nn.Linear(
in_features= maxpool_out,
out_features=200
),
nn.Dropout(p=0.05),
nn.ReLU()
)

self.fc2 = nn.Sequential(
nn.Linear(
in_features=200,
out_features=100
),
nn.Dropout(p=0.05),
nn.ReLU()
)

self.fc3 = nn.Sequential(
nn.Linear(
in_features=100,
out_features=50
),
nn.Dropout(p=0.05),
nn.ReLU()
)


self.lastlayer = nn.Linear(
in_features=50,
out_features=1
)
def forward(self,x):
#adding 1 dimention
x = x.unsqueeze(1)
#conv layers
x = self.conv1(x)
x = self.maxpool(x)
#flatten
x = x.reshape(x.shape[0], -1)
#3fc
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
#output
x = self.lastlayer(x)
return x

这是训练循环:

def binary_acc(y_pred, y_test):
y_pred_tag = torch.round(torch.sigmoid(y_pred))
correct_results_sum = (y_pred_tag == y_test).sum().float()
acc = correct_results_sum/y_test.shape[0]
acc = torch.round(acc * 100)

return acc

def Training(model, train_loader, criterion, optimizer, epochs):
train_losses  = [] 
Acc =[]
for epoch in range(epochs):
epoch_accuracy =  0
train_loss = 0
total_pcaps = 0
model.train()
for elem in train_loader:
pcap_byte = elem['feature'].to(device)

labels = elem['label'].to(device)


optimizer.zero_grad()

outputs = model(pcap_byte)

loss = criterion(outputs, labels.unsqueeze(1).float())

loss.backward()
# Updating parameters
optimizer.step()

total_pcaps += labels.size(0)
acc = binary_acc(outputs, labels.unsqueeze(1).float()
train_loss += loss.item()

epoch_accuracy += acc.item()
Acc.append(epoch_accuracy)
average_loss = train_loss / len(train_loader)
train_losses.append(train_loss)
print('epoch %d, train_loss: %.3f' % (epoch + 1, average_loss))

训练后,其中一个类的精度和召回率等于0,另一个类为pr=1和召回率=0.9。数据有问题吗?我自己收集了数据,但我不明白是数据出了问题还是我的代码出了问题。

问题出在数据上。我在预处理步骤中使用了欠采样。我删除了那个部分,模型表现良好。

最新更新