Search code examples
pythondeep-learningneural-networkconv-neural-network

binary classification but only one class gives results


I am trying to train a neural network. I have two classes. Precision and Recall for one of the classes equals 0 all the time. Here's the code for the neural network.

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

Here's the training loop:

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))
      

After training The precision and recall of one of the classes equals 0 and the other one is pr = 1 and recall = 0.9. Is there something wrong with the data? I self-collected the data and I can't understand whether the problem is with the data or something is wrong in my code.


Solution

  • The problem was with the data. I was using undersampling in the pre-processing step. I deleted that part and the model performed well.