You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

机器学习多标签分类:Keras模型构建及超参数调整技术问询

Great question! When you're dealing with a task where each sample needs two independent classification outputs—like recognizing both a letter and its font family from a single image—this is called a multi-output classification task, and Keras has all the tools you need to build a model for it. Let's walk through how to do this, plus the key differences from standard single-label classification.

Building the Keras Multi-Output Model

The core idea is to share a common feature extraction backbone (since both tasks use the same input image) and then split into two separate classification heads, one for each label. We'll use Keras' Functional API here (since the Sequential API doesn't support multiple outputs).

Step-by-Step Code Example

from tensorflow import keras
from tensorflow.keras import layers

# Define input layer (assuming 28x28 grayscale images)
input_image = layers.Input(shape=(28, 28, 1), name="image_input")

# Shared feature extraction backbone (CNN for image data)
x = layers.Conv2D(32, (3, 3), activation='relu')(input_image)
x = layers.MaxPooling2D((2, 2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.MaxPooling2D((2, 2))(x)
x = layers.Flatten()(x)
x = layers.Dense(128, activation='relu')(x)

# First classification head: Letter category (26 classes for A-Z)
letter_pred = layers.Dense(26, activation='softmax', name='letter_output')(x)

# Second classification head: Font family category (adjust num classes to your dataset)
font_pred = layers.Dense(10, activation='softmax', name='font_output')(x)

# Assemble the model with one input and two outputs
model = keras.Model(inputs=input_image, outputs=[letter_pred, font_pred])

# Compile the model with task-specific losses and metrics
model.compile(
    optimizer='adam',
    # Use sparse categorical crossentropy if labels are integer-encoded; use categorical if one-hot
    loss={
        'letter_output': 'sparse_categorical_crossentropy',
        'font_output': 'sparse_categorical_crossentropy'
    },
    # Optional: Assign weights to prioritize one task over the other
    loss_weights={
        'letter_output': 1.0,
        'font_output': 0.8
    },
    metrics={
        'letter_output': 'accuracy',
        'font_output': 'accuracy'
    }
)

# Train the model (pass labels as a dictionary matching output names)
# Assume train_images, train_letter_labels, train_font_labels are your preprocessed data
model.fit(
    train_images,
    {'letter_output': train_letter_labels, 'font_output': train_font_labels},
    epochs=15,
    batch_size=32,
    validation_split=0.2
)

Key Hyperparameter & Setup Adjustments vs. Single-Label Classification

Compared to a standard single-label classification model, you'll need to tweak these areas:

  • Loss Configuration: Instead of one loss function, you define a loss for each output. Use loss_weights if one task is more critical (e.g., if letter accuracy matters more than font family, give it a higher weight).
  • Output Layers: You'll need separate output layers for each task, each with a number of neurons equal to the number of classes in that task, and a softmax activation (since both are multi-class tasks).
  • Training Data Handling: You'll need to prepare two separate label arrays, and pass them to model.fit() either as a list (matching the order of outputs in your model) or a dictionary (matching the output layer names for clarity).
  • Evaluation Metrics: Track metrics for each task individually (like accuracy for letter and font predictions) to monitor how each branch is performing.
  • Regularization Tweaks: If one task is prone to overfitting (e.g., fewer font family samples), you can add task-specific regularization (like Dropout or kernel_regularizer) to only that classification head, instead of applying it globally.
  • Optional: Branch-Specific Feature Extraction: If the two tasks need distinct features (unlikely in your letter/font example), you can split the backbone earlier and add separate Conv/Dense layers to each branch, instead of sharing all features.

Quick Notes

  • If your labels are one-hot encoded (instead of integers), switch from sparse_categorical_crossentropy to categorical_crossentropy.
  • Pre-trained models (like MobileNetV2) work great as the shared backbone if you have limited data—just freeze the base layers and add your two classification heads on top.

内容的提问来源于stack exchange,提问作者Bumuthu Dilshan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 22:17:34