[Deep Learning] Exploration of classification problems (multi-label classification converted to multiple binary classifications, etc.)

[Deep Learning] Exploration of classification problems (multi-label classification converted to multiple binary classifications, etc.)

1 Introduction

In machine learning and deep learning, there are many types of classification problems. The following lists some common classification types and provides corresponding examples:

  • Binary classification: Divide samples into two mutually exclusive categories. For example, a spam classifier can classify emails as spam or not spam.
  • Multiclass classification: Divide samples into multiple mutually exclusive categories. For example, handwritten digit recognition can classify handwritten digit images into ten categories from 0 to 9.
  • Multilabel classification: For each sample, multiple labels can be assigned at the same time. For example, image label classification can divide an image into multiple labels, such as "car", "tree" and "sky".
  • Hierarchical classification: Divide samples into multiple categories according to the hierarchical structure. There are nested relationships between these categories, forming a tree structure. For example, a classification system in biology is an example of hierarchical classification, from first-level classification to more specific classifications.
  • Rule-based classification: uses a set of rules to classify. Rules can be based on characteristics value thresholds, logical judgments and other conditions. For example, develop rules for transportation selection based on weather conditions and traffic conditions.
  • Sequence classification: Classify sequence data and divide it into different categories based on the characteristics of the input sequence. For example, speaker recognition in speech recognition can attribute input speech sequences to different people.
  • Anomaly detection: Identifying abnormal samples that are different from the majority of samples in the data set. For example, in network intrusion detection, identifying network traffic that may be an attack.

These are some common types of classification problems, each with its own unique characteristics and application scenarios. Choosing the appropriate classification type depends on the specific problem and data set. The most typical ones are undoubtedly the first three.

2. Some analysis

2.1 About multi-label classification to multiple two-class classification

Multi-classification problems can be transformed into multiple two-classification problems. Compared with multi-classification models, the recognition accuracy of the two-classification model will be improved (the more categories, the higher the probability of misidentification). However, when multi-classification is converted into two-classification, the complexity of the model will become higher. If the recognition is accurate, If the accuracy requirement is very high, multiple two-class classification can be used for identification. If the accuracy requirement is not so high, a multi-classification model can be used. Therefore, you can choose according to specific scenarios and convert multi-classification into multiple two-classifications.

Multi-label classification can be transformed into multiple binary classification tasks, each binary classification task corresponding to a label. The effectiveness depends on the specific problem and data set.

  • The advantage of converting multi-label classification into multiple binary classification tasks is that each task is relatively independent and different models or algorithms can be used to process different labels. This can make full use of the characteristics and correlation of each label and improve classification accuracy. In addition, since each task is binary classification, it may be easier to find suitable models and optimization methods.
  • However, there are also some challenges in converting multi-label classification into multiple binary classification tasks. First, there may be correlations between labels, and binary classification of each label alone may not fully consider the correlation between labels. Secondly, if some label categories are unbalanced, that is, when one category has a small number of samples, the binary classification task may face the problem of sample imbalance. Furthermore, transforming the problem into multiple binary classification tasks increases computational and storage overhead.
  • Therefore, which method to choose depends on the specific problem and data set. Sometimes, the model or algorithm of multi-label classification itself may already be able to achieve good results. In other cases, transforming multi-label classification into multiple binary classification tasks may provide better performance. Experimentation and evaluation are required on a case-by-case basis to find the most suitable method.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset

# 自定义数据集类
class MultiLabelDataset(Dataset):
    def __init__(self, X, y):
        self.X = torch.tensor(X, dtype=torch.float32)
        self.y = torch.tensor(y, dtype=torch.float32)

    def __len__(self):
        return len(self.X)
    
    def __getitem__(self, idx):
        return self.X[idx], self.y[idx]

# 自定义模型类
class BinaryClassifier(nn.Module):
    def __init__(self, input_size):
        super(BinaryClassifier, self).__init__()
        self.fc = nn.Linear(input_size, 1)
        
    def forward(self, x):
        x = self.fc(x)
        return x

# 训练函数
def train_model(model, train_loader, criterion, optimizer, num_epochs):
    model.train()
    for epoch in range(num_epochs):
        running_loss = 0.0
        for inputs, labels in train_loader:
            optimizer.zero_grad()
            outputs = model(inputs)
            loss = criterion(outputs.squeeze(), labels)
            loss.backward()
            optimizer.step()
            running_loss += loss.item()
        print(f"Epoch {
      
      epoch+1}/{
      
      num_epochs}, Loss: {
      
      running_loss / len(train_loader)}")

# 测试函数
def test_model(model, test_loader, threshold=0.5):
    model.eval()
    with torch.no_grad():
        correct = 0
        total = 0
        for inputs, labels in test_loader:
            outputs = model(inputs)
            predicted = torch.sigmoid(outputs.squeeze()) > threshold
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    print(f"Accuracy: {
      
      correct / total}")

# 生成示例的多标签分类数据集
X, y = make_multilabel_classification(n_samples=100, n_features=10, n_labels=3, random_state=1)

# 数据集划分为训练集和测试集
train_dataset = MultiLabelDataset(X[:80], y[:80])
test_dataset = MultiLabelDataset(X[80:], y[80:])

# 创建数据加载器
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=16, shuffle=False)

# 创建模型、损失函数和优化器
model = BinaryClassifier(input_size=10)
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 训练并测试模型
train_model(model, train_loader, criterion, optimizer, num_epochs=10)
test_model(model, test_loader)

In the above code,

  • First, a custom data set class MultiLabelDataset is defined for loading data. Then, a simple binary classification model BinaryClassifier is defined, which uses a linear fully connected layer to perform binary classification tasks. Next, the training function train_model and the test function test_model are defined for training and evaluating the model.
  • In the main program, use make_multilabel_classification to generate a multi-label classification dataset for the example. Then, divide the data set into a training set and a test set, and create corresponding data loaders. Next, create the model, loss function, and optimizer. Finally, call train_model to train the model, and call test_model to evaluate the accuracy of the model on the test set.
  • The code only provides a simple example, and more complex models and optimization strategies may be required for actual use. In addition, hyperparameters can also be adjusted according to specific circumstances to obtain better results.

2.2 continue

Guess you like

Origin blog.csdn.net/qq_51392112/article/details/133417461