关于 active-learning-system
面向机器学习从业者的主动学习系统技能,介绍样本选择策略、标注循环和性能监控。内容展示如何根据模型不确定性、委员会分歧或特征多样性挑选样本。
主要功能
- 01不确定性采样
- 02委员会分歧采样
- 03预期模型变化策略
- 04基于多样性的样本选择
- 05主动学习性能与成本监控
适用场景
- 01为人工标注挑选信息量高的样本
- 02通过集成模型分歧筛选待标注数据
- 03跟踪模型性能与标注成本
- 04规划冷启动样本和批次大小
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dengineproblem/ agents-monorepo --skill active-learning-system安装后在 Claude Code 中输入/active-learning-system确认已加载。
详细介绍
专家级主动学习系统
机器学习主动学习系统专家。
主要策略
- Uncertainty Sampling:选择模型最不确定的样本
- Query by Committee:利用集成模型的分歧
- Expected Model Change:选择信息量最大的样本
- Diversity-based Selection:覆盖特征空间
主动学习的主要循环
from modAL import ActiveLearner
from modAL.uncertainty import uncertainty_sampling
class ActiveLearningSystem:
def __init__(self, initial_labeled_pool, unlabeled_pool):
self.labeled_X, self.labeled_y = initial_labeled_pool
self.unlabeled_X = unlabeled_pool
self.learner = ActiveLearner(
estimator=RandomForestClassifier(n_estimators=100),
query_strategy=uncertainty_sampling,
X_training=self.labeled_X,
y_training=self.labeled_y
)
def query_and_update(self, batch_size=10, oracle_func=None):
query_indices = []
temp_unlabeled = self.unlabeled_X.copy()
for _ in range(min(batch_size, len(temp_unlabeled))):
query_idx, query_instance = self.learner.query(temp_unlabeled)
query_indices.append(query_idx)
temp_unlabeled = np.delete(temp_unlabeled, query_idx, axis=0)
queried_X = self.unlabeled_X[query_indices]
queried_y = oracle_func(queried_X) if oracle_func else self.simulate_oracle(queried_X)
self.learner.teach(queried_X, queried_y)
self.unlabeled_X = np.delete(self.unlabeled_X, query_indices, axis=0)
return query_indices, queried_X, queried_y
Query by Committee
class CommitteeActiveLearner:
def __init__(self, X_initial, y_initial):
self.committee = [
RandomForestClassifier(n_estimators=50),
GradientBoostingClassifier(n_estimators=50),
SVC(probability=True)
]
for clf in self.committee:
clf.fit(X_initial, y_initial)
def query_by_committee(self, X_pool, n_instances=1):
disagreements = []
for x in X_pool:
predictions = [clf.predict_proba([x])[0] for clf in self.committee]
avg_pred = np.mean(predictions, axis=0)
disagreement = -np.sum(avg_pred * np.log(avg_pred + 1e-10))
disagreements.append(disagreement)
selected_indices = np.argsort(disagreements)[-n_instances:]
return selected_indices, X_pool[selected_indices]
性能监控
class ActiveLearningMonitor:
def track_performance(self, model, X_test, y_test, n_annotations):
accuracy = accuracy_score(y_test, model.predict(X_test))
self.performance_history.append(accuracy)
self.annotation_costs.append(n_annotations)
def calculate_learning_efficiency(self):
performance_gains = np.diff(self.performance_history)
annotation_increments = np.diff(self.annotation_costs)
efficiency = performance_gains / annotation_increments
return {
'current_efficiency': efficiency[-1],
'average_efficiency': np.mean(efficiency),
'efficiency_trend': np.polyfit(range(len(efficiency)), efficiency, 1)[0]
}
def suggest_stopping_criterion(self):
efficiency = self.calculate_learning_efficiency()
if efficiency['current_efficiency'] < 0.001:
return "Consider stopping - low marginal gains"
return "Continue learning"
最佳实践
冷启动策略
- 使用分层抽样构建初始数据集
- 确保涵盖所有类别
- 每个类别至少从 5-10 个样本开始
批次大小优化
- 不确定性高时使用小批次(5-20 个)
- 标注成本高时使用大批次(50-100 个)
- 考虑标注者的疲劳程度
常见陷阱
- 忽视类别不平衡
- 未经校准便过度依赖置信度
- 忽视标注者之间的一致性
- 未考虑标注噪声
评论
支持 Markdown