Skip to content

主動學習

使用不確定性取樣優先標註有價值的樣本。

主動學習通過優先選擇最有資訊量的樣本來幫助您更智慧地標註。不再隨機標註,而是集中於模型最不確定的實例。

工作原理

Potato 的主動學習根據機器學習預測自動重新排序標註實例:

  1. 初始收集 - 收集最少數量的標註
  2. 訓練 - 在現有標註上訓練分類器
  3. 預測 - 獲取未標註實例的不確定性分數
  4. 重新排序 - 優先選擇不確定性最高的實例
  5. 標註 - 標註者標註優先實例
  6. 重新訓練 - 定期用新標註更新模型

主動學習迴圈:在已標註資料上訓練,按不確定性為未標註池打分,標註最不確定的項目,然後重新訓練把標註精力用在模型學得最多的地方

配置

基本設定

yaml
active_learning:
  enabled: true
  schema_names:
    - sentiment  # Which annotation schemes to use
 
  min_annotations_per_instance: 1
  min_instances_for_training: 20
  update_frequency: 50  # Retrain after every 50 annotations
  max_instances_to_reorder: 1000

完整配置

yaml
active_learning:
  enabled: true
 
  # Which schemas to use for training
  schema_names:
    - sentiment
 
  # Minimum requirements
  min_annotations_per_instance: 1
  min_instances_for_training: 20
 
  # Retraining frequency
  update_frequency: 50
 
  # How many instances to reorder
  max_instances_to_reorder: 1000
 
  # Classifier configuration
  classifier:
    type: LogisticRegression
    params:
      C: 1.0
      max_iter: 1000
 
  # Feature extraction
  vectorizer:
    type: TfidfVectorizer
    params:
      max_features: 5000
      ngram_range: [1, 2]
 
  # Model persistence
  model_persistence:
    enabled: true
    save_dir: "models/"
    max_saved_models: 5

支援的分類器

分類器適用場景速度
LogisticRegression二分類/多分類
RandomForestClassifier複雜模式中等
SVC小資料集
MultinomialNB文本分類非常快

分類器示例

yaml
# Logistic Regression (recommended starting point)
classifier:
  type: LogisticRegression
  params:
    C: 1.0
    max_iter: 1000
 
# Random Forest
classifier:
  type: RandomForestClassifier
  params:
    n_estimators: 100
    max_depth: 10
 
# Support Vector Classifier
classifier:
  type: SVC
  params:
    kernel: rbf
    probability: true
 
# Naive Bayes
classifier:
  type: MultinomialNB
  params:
    alpha: 1.0

向量化器

向量化器描述
TfidfVectorizerTF-IDF 加權特徵(推薦)
CountVectorizer簡單詞頻
HashingVectorizer大詞彙量的記憶體高效方案
yaml
# TF-IDF (recommended)
vectorizer:
  type: TfidfVectorizer
  params:
    max_features: 5000
    ngram_range: [1, 2]
    stop_words: english
 
# Count Vectorizer
vectorizer:
  type: CountVectorizer
  params:
    max_features: 3000
    ngram_range: [1, 1]
 
# Hashing Vectorizer (for large datasets)
vectorizer:
  type: HashingVectorizer
  params:
    n_features: 10000

LLM 整合

主動學習可以選擇性地使用 LLM 增強實例選擇:

yaml
active_learning:
  enabled: true
  schema_names:
    - sentiment
 
  # LLM-based selection
  llm_integration:
    enabled: true
    endpoint_type: vllm
    base_url: http://localhost:8000/v1
    model: meta-llama/Llama-2-7b-chat-hf
 
    # Mock mode for testing
    mock_mode: false

多方案支援

主動學習可以迴圈使用多個標註方案:

yaml
annotation_schemes:
  - annotation_type: radio
    name: sentiment
    labels: [Positive, Negative, Neutral]
 
  - annotation_type: radio
    name: topic
    labels: [Politics, Sports, Tech, Entertainment]
 
active_learning:
  enabled: true
  schema_names:
    - sentiment
    - topic
 
  # Schema-specific settings
  schema_config:

模型持久化

在伺服器重啟時儲存和重新載入訓練好的模型:

yaml
active_learning:
  enabled: true
  schema_names:
    - sentiment
 
  model_persistence:
    enabled: true
    save_dir: "models/"
    max_saved_models: 5  # Keep last 5 models
 
    # Save to database instead of files
    use_database: false

監控進度

管理儀表板跟蹤主動學習指標:

  • 當前模型準確率
  • 訓練週期數
  • 不確定性分佈
  • 剩餘實例數
  • 重新訓練歷史

通過 /admin 使用管理員 API 金鑰訪問。

最佳實踐

1. 從隨機取樣開始

在啟用主動學習之前獲取初始標註:

yaml
active_learning:
  enabled: true
  min_instances_for_training: 50  # Wait for 50 annotations

2. 選擇合適的分類器

  • LogisticRegression:快速,大多數任務的良好預設值
  • RandomForest:適合複雜模式,較慢
  • MultinomialNB:非常快,適合簡單文本分類

3. 監控類別分佈

主動學習可能導致類別不平衡。在管理儀表板中監控並考慮分層取樣。

4. 設定合理的重新訓練頻率

過於頻繁的重新訓練浪費資源:

yaml
update_frequency: 100  # Retrain every 100 annotations

5. 啟用模型持久化

儲存模型以避免重啟後從頭訓練:

yaml
model_persistence:
  enabled: true
  save_dir: "models/"

示例:完整配置

yaml
task_name: "Sentiment Analysis with Active Learning"
task_dir: "."
port: 8000
 
data_files:
  - "data/reviews.json"
 
item_properties:
  id_key: id
  text_key: text
 
annotation_schemes:
  - annotation_type: radio
    name: sentiment
    description: "What is the sentiment?"
    labels:
      - Positive
      - Negative
      - Neutral
 
active_learning:
  enabled: true
  schema_names:
    - sentiment
 
  min_annotations_per_instance: 1
  min_instances_for_training: 30
  update_frequency: 50
  max_instances_to_reorder: 500
 
  classifier:
 
  vectorizer:
 
  model_persistence:
 
output_annotation_dir: "output/"
output_annotation_format: "json"
allow_all_users: true

與 AI 支援結合

同時使用主動學習和 LLM 輔助:

yaml
active_learning:
  enabled: true
  schema_names:
    - sentiment
  min_instances_for_training: 30
 
ai_support:
  enabled: true
  endpoint_type: openai
 
  ai_config:
    model: gpt-4
    api_key: ${OPENAI_API_KEY}
 
  features:
    label_suggestions:
      enabled: true

這種組合在優先選擇不確定實例的同時提供 AI 提示來幫助標註者。

故障排除

訓練失敗

  • 確保有足夠的標註(min_instances_for_training
  • 檢查類別分佈 - 需要所有類別的示例
  • 驗證資料格式與方案匹配

效能緩慢

  • 減少 max_instances_to_reorder
  • 增加 update_frequency
  • 對大詞彙量使用 HashingVectorizer

模型未更新

  • 檢查 update_frequency 設定
  • 驗證標註正在儲存
  • 檢視管理儀表板中的錯誤

延伸閱讀

有關實現細節,請參閱源文件