ラベル伝播を用いたアクティブラーニング

Machine LearningMachine LearningBeginner
オンラインで実践に進む

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、ラベル伝播を使って手書き数字を学習するアクティブラーニング技術を示します。ラベル伝播は、グラフベースのアプローチを使ってデータポイント間でラベルを伝播させる半教師あり学習法です。アクティブラーニングは、ラベル付けするデータポイントを反復的に選択し、これらのラベル付きポイントを使ってモデルを再学習するプロセスです。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習します。

Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証を自動化できません。

学習中に問題がある場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

数字のデータセットを読み込む

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]

最も不確定なポイントを選択する

予測されたラベル分布に基づいて、最も不確定な上位 5 つのポイントを選択し、それらに対する人間によるラベルを要求します。

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)

繰り返し

最も不確定な上位 5 つのポイントを選択し、それらのラベルをラベル付きのデータポイントに追加し、30 個のラベル付きのデータポイントが得られるまでモデルを学習するというプロセスを繰り返します。

max_iterations = 3

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

    ## select top five uncertain points
    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]

    ## add labels to labeled data points
    y_train[uncertainty_index] = y[uncertainty_index]

    ## train the model
    lp_model.fit(X, y_train)

    ## remove labeled data points from the unlabeled set
    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 個のラベル付きデータポイントが得られるまで、最も不確定な上位 5 つのポイントを選択してラベル付けを繰り返しました。このアクティブラーニング手法は、モデルを学習するために必要なラベル付きデータポイントの数を最小限に抑えながら、その性能を最大化するのに役立ちます。