About Prediction in Binary Classification

Hi guys, I can’t understand how to get the y prediction from the training model without going to testing, this would help me to get a classification_report of the model before testing it. Hope you can answer, thank you very much.

class BinaryClassification(nn.Module):
    def __init__(self):
        super(BinaryClassification, self).__init__()
        self.layer_1 = nn.Linear(feature, node)
        self.layer_2 = nn.Linear(node, node)
        self.layer_out = nn.Linear(node, 1)

        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(p=0.1)
        self.batchnorm1 = nn.BatchNorm1d(node)
        self.batchnorm2 = nn.BatchNorm1d(node)

    def forward(self, inputs):
        x = self.relu(self.layer_1(inputs))
        x = self.batchnorm1(x)
        x = self.relu(self.layer_2(x))
        x = self.batchnorm2(x)
        x = self.dropout(x)
        x = self.layer_out(x)

        return x

After this training

model.train()
flag = True
for e in range(1, EPOCHS + 1):
    epoch_loss = 0
    epoch_acc = 0
    if flag:
        for X_batch, y_batch in train_loader:
            X_batch, y_batch = X_batch.to(device), y_batch.to(device)
            optimizer.zero_grad()
            y_pred = model(X_batch)
            loss = criterion(y_pred, y_batch.unsqueeze(1))
            acc = binary_acc(y_pred, y_batch.unsqueeze(1))
            loss.backward()
            optimizer.step()
            epoch_loss += loss.item()
            epoch_acc += acc.item()
            if loss.item() < 0.00001:
                flag = False

After this training I need a classification_report, so I need a y_pred list to compare with true y.
Thanks

Since your model outputs logits, you could use a threshold e.g. at 0.0 to create your predictions as:

preds = y_pred > 0.0

You could also apply torch.sigmoid and use a threshold in the range [0, 1] instead.