使用标签传播的主动学习

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

本实验展示了一种使用标签传播来学习手写数字的主动学习技术。标签传播是一种半监督学习方法,它使用基于图的方法在数据点之间传播标签。主动学习是一个允许我们迭代选择数据点进行标注,并使用这些标注点重新训练模型的过程。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问Jupyter Notebook进行练习。

有时,你可能需要等待几秒钟让Jupyter Notebook完成加载。由于Jupyter Notebook的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向Labby提问。课程结束后提供反馈,我们会及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/AdvancedDataAnalysisandDimensionalityReductionGroup(["Advanced Data Analysis and Dimensionality Reduction"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/AdvancedDataAnalysisandDimensionalityReductionGroup -.-> sklearn/semi_supervised("Semi-Supervised Learning") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/semi_supervised -.-> lab-49184{{"使用标签传播的主动学习"}} ml/sklearn -.-> lab-49184{{"使用标签传播的主动学习"}} end

加载数字数据集

我们将从scikit-learn库中加载数字数据集开始。

from sklearn import datasets

digits = datasets.load_digits()

打乱并分割数据

接下来,我们将打乱数据集并将其分割为有标签和无标签的部分。我们将从仅10个有标签的点开始。

import numpy as np

rng = np.random.RandomState(0)
indices = np.arange(len(digits.data))
rng.shuffle(indices)

X = digits.data[indices[:330]]
y = digits.target[indices[:330]]
images = digits.images[indices[:330]]

n_total_samples = len(y)
n_labeled_points = 10
unlabeled_indices = np.arange(n_total_samples)[n_labeled_points:]

训练标签传播模型

现在我们将使用有标签的数据点训练一个标签传播模型,并使用它来预测其余无标签数据点的标签。

from sklearn.semi_supervised import LabelSpreading

lp_model = LabelSpreading(gamma=0.25, max_iter=20)
lp_model.fit(X, y_train)
predicted_labels = lp_model.transduction_[unlabeled_indices]

选择最不确定的点

我们将根据预测的标签分布选择最不确定的前五个点,并请求人工为它们标注标签。

from scipy import stats

pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T)
uncertainty_index = np.argsort(pred_entropies)[::-1]
uncertainty_index = uncertainty_index[np.in1d(uncertainty_index, unlabeled_indices)][:5]

标注最不确定的点

我们将把人工标注的标签添加到有标签的数据点中,并用它们来训练模型。

y_train[uncertainty_index] = y[uncertainty_index]
lp_model.fit(X, y_train)

重复操作

我们将重复选择最不确定的前五个点、将它们的标签添加到有标签的数据点中以及训练模型的过程,直到我们有30个有标签的数据点。

max_iterations = 3

for i in range(max_iterations):
    if len(unlabeled_indices) == 0:
        print("No unlabeled items left to label.")
        break

    ## 选择前五个不确定的点
    pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T)
    uncertainty_index = np.argsort(pred_entropies)[::-1]
    uncertainty_index = uncertainty_index[np.in1d(uncertainty_index, unlabeled_indices)][:5]

    ## 将标签添加到有标签的数据点中
    y_train[uncertainty_index] = y[uncertainty_index]

    ## 训练模型
    lp_model.fit(X, y_train)

    ## 从未标记集中移除已标记的数据点
    delete_indices = np.array([], dtype=int)
    for index, image_index in enumerate(uncertainty_index):
        (delete_index,) = np.where(unlabeled_indices == image_index)
        delete_indices = np.concatenate((delete_indices, delete_index))
    unlabeled_indices = np.delete(unlabeled_indices, delete_indices)
    n_labeled_points += len(uncertainty_index)

总结

总之,本实验展示了一种使用标签传播的主动学习技术来识别手写数字。我们首先仅用10个有标签的点训练了一个标签传播模型,然后迭代地选择最不确定的前五个点进行标注,直到我们有30个有标签的数据点。这种主动学习技术有助于在最大化模型性能的同时,最小化训练模型所需的有标签数据点的数量。