RBM 特徴を用いた数字分類

Machine LearningMachine LearningBeginner
今すぐ練習

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

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

はじめに

この実験では、手書き数字の分類に対するベルヌーイ制限ボルツマンマシン(RBM)の使用に焦点を当てています。RBM 特徴抽出器は、ロジスティック回帰分類器と組み合わされて数字を予測します。使用されるデータセットは、グレースケール画像データであり、画素値は白背景上の黒さの度合いとして解釈できます。

VM のヒント

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

時々、Jupyter Notebook が読み込み終了するまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題に直面した場合は、Labby にお尋ねください。セッション後にフィードバックを提供してください。そうすれば、迅速に問題を解決します。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/linear_model("Linear Models") sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/neural_network("Neural Network Models") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/preprocessing("Preprocessing and Normalization") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/pipeline("Pipeline") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/model_selection("Model Selection") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/metrics("Metrics") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/base("Base Classes and Utility Functions") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/linear_model -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/neural_network -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/preprocessing -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/pipeline -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/model_selection -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/metrics -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/base -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} sklearn/datasets -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} ml/sklearn -.-> lab-49259{{"RBM 特徴を用いた数字分類"}} end

データの準備

このステップでは、学習とテスト用のデータを準備します。sklearn.datasets から load_digits 関数を使用してデータセットを取得します。その後、学習データを各方向に 1 ピクセルの線形シフトで乱して、人工的により多くのラベル付きデータを生成します。データを 0 から 1 の間でスケーリングします。

import numpy as np
from scipy.ndimage import convolve
from sklearn import datasets
from sklearn.preprocessing import minmax_scale
from sklearn.model_selection import train_test_split

def nudge_dataset(X, Y):
    """
    これは、8x8 の画像を X で左右上下に 1px ずつ移動させることで、
    元のデータセットの 5 倍のサイズのデータセットを生成します
    """
    direction_vectors = [
        [[0, 1, 0], [0, 0, 0], [0, 0, 0]],
        [[0, 0, 0], [1, 0, 0], [0, 0, 0]],
        [[0, 0, 0], [0, 0, 1], [0, 0, 0]],
        [[0, 0, 0], [0, 0, 0], [0, 1, 0]],
    ]

    def shift(x, w):
        return convolve(x.reshape((8, 8)), mode="constant", weights=w).ravel()

    X = np.concatenate(
        [X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]
    )
    Y = np.concatenate([Y for _ in range(5)], axis=0)
    return X, Y

X, y = datasets.load_digits(return_X_y=True)
X = np.asarray(X, "float32")
X, Y = nudge_dataset(X, y)
X = minmax_scale(X, feature_range=(0, 1))  ## 0-1 スケーリング

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

モデルの定義

このステップでは、ベルヌーイRBM特徴抽出器とロジスティック回帰分類器を使用して分類パイプラインを定義します。それぞれ sklearn.neural_networksklearn.linear_model モジュールから BernoulliRBMLogisticRegression クラスを使用します。その後、2つのモデルを結合するためのパイプラインオブジェクト rbm_features_classifier を作成します。

from sklearn import linear_model
from sklearn.neural_network import BernoulliRBM
from sklearn.pipeline import Pipeline

logistic = linear_model.LogisticRegression(solver="newton-cg", tol=1)
rbm = BernoulliRBM(random_state=0, verbose=True)

rbm_features_classifier = Pipeline(steps=[("rbm", rbm), ("logistic", logistic)])

学習

このステップでは、前のステップで定義したパイプラインモデルを学習します。モデルのハイパーパラメータ(学習率、隠れ層のサイズ、正則化)を設定した後、学習データをモデルに適合させます。

from sklearn.base import clone

## ハイパーパラメータ。これらはGridSearchCVを使った交差検証によって設定されました。
## ここでは時間を節約するために交差検証を行っていません。
rbm.learning_rate = 0.06
rbm.n_iter = 10

## より多くのコンポーネントがより良い予測性能をもたらす傾向がありますが、
## 学習時間は長くなります
rbm.n_components = 100
logistic.C = 6000

## RBM-Logisticパイプラインの学習
rbm_features_classifier.fit(X_train, Y_train)

評価

このステップでは、テストデータセット上でモデルの性能を評価します。sklearn.metrics モジュールの classification_report 関数を使用して、パイプラインモデルとロジスティック回帰モデルの両方に対する分類レポートを生成します。

from sklearn import metrics

Y_pred = rbm_features_classifier.predict(X_test)
print(
    "Logistic regression using RBM features:\n%s\n"
    % (metrics.classification_report(Y_test, Y_pred))
)

## 画素値を直接使ってロジスティック回帰分類器を学習させる
raw_pixel_classifier = clone(logistic)
raw_pixel_classifier.C = 100.0
raw_pixel_classifier.fit(X_train, Y_train)

Y_pred = raw_pixel_classifier.predict(X_test)
print(
    "Logistic regression using raw pixel features:\n%s\n"
    % (metrics.classification_report(Y_test, Y_pred))
)

プロット

このステップでは、RBMによって抽出された100個のコンポーネントをプロットします。画像をプロットするために matplotlib.pyplot モジュールを使用します。

import matplotlib.pyplot as plt

plt.figure(figsize=(4.2, 4))
for i, comp in enumerate(rbm.components_):
    plt.subplot(10, 10, i + 1)
    plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation="nearest")
    plt.xticks(())
    plt.yticks(())
plt.suptitle("100 components extracted by RBM", fontsize=16)
plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)

plt.show()

まとめ

この実験では、手書き数字の分類に対して、ベルヌーイ制限ボルツマンマシン(RBM)とロジスティック回帰をどのように使用するかを学びました。また、分類レポートを使用してモデルの性能を評価する方法と、RBMによって抽出されたコンポーネントをプロットする方法についても学びました。