パーセプトロン

中級

2.2.3

パーセプトロン

最終更新 2020-02-26 読了時間 3 分
まとめ
  • パーセプトロンは線形に分離可能なデータで有限回の更新で収束する、古典的なオンライン学習アルゴリズムです。
  • 重み付き和 \(\mathbf{w}^\top \mathbf{x} + b\) の符号だけで予測でき、誤分類したサンプルでのみ重みを更新します。
  • 更新則が単純なため勾配法の直感をつかみやすく、学習率を調整しながら決定境界を少しずつ動かします。
  • 非線形データにはそのままでは対応できないため、特徴量拡張やカーネルトリックと組み合わせます。

直感 #

パーセプトロンは「誤分類したら境界をサンプルのある側へ少し動かす」という素朴な考え方で学習します。重みベクトル \(\mathbf{w}\) は決定境界の法線方向を表し、バイアス \(b\) がオフセットを表します。学習率 \(\eta\) を調整しながら境界を動かすと、誤りが減る方向へ徐々に収束します。

flowchart LR A["入力x"] --> B["重み付き和\nw·x + b"] B --> C["sign関数"] C --> D["予測ŷ"] D --> E{"誤分類?"} E -->|Yes| F["重みを更新\nw ← w + ηyx"] F --> B E -->|No| G["次のサンプルへ"] style A fill:#2563eb,color:#fff style C fill:#1e40af,color:#fff style G fill:#10b981,color:#fff

数式で見る #

予測は

$$ \hat{y} = \operatorname{sign}(\mathbf{w}^\top \mathbf{x} + b) $$

で行います。サンプル \((\mathbf{x}_i, y_i)\) を誤分類した場合、以下のように更新します。

$$ \mathbf{w} \leftarrow \mathbf{w} + \eta\, y_i\, \mathbf{x}_i,\qquad b \leftarrow b + \eta\, y_i $$

データが線形に分離可能であれば、この更新を繰り返すだけで有限回で収束することが知られています。

Pythonによる実験 #

次のコードは人工データにパーセプトロンを適用し、学習の進み具合と決定境界を可視化する例です。誤分類が 0 になった時点で更新を打ち切ります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.metrics import accuracy_score

def run_perceptron_demo(
    n_samples: int = 200,
    lr: float = 0.1,
    n_epochs: int = 20,
    random_state: int = 0,
    title: str = "パーセプトロンの決定境界",
    xlabel: str = "特徴量1",
    ylabel: str = "特徴量2",
    label_boundary: str = "決定境界",
) -> dict[str, object]:
    """Train a perceptron on synthetic blobs and plot the decision boundary."""
    X, y = make_blobs(
        n_samples=n_samples,
        centers=2,
        cluster_std=1.0,
        random_state=random_state,
    )
    y_signed = np.where(y == 0, -1, 1)

    w = np.zeros(X.shape[1])
    b = 0.0
    history: list[int] = []

    for _ in range(n_epochs):
        errors = 0
        for xi, target in zip(X, y_signed):
            update = lr * target if target * (np.dot(w, xi) + b) <= 0 else 0.0
            if update != 0.0:
                w += update * xi
                b += update
                errors += 1
        history.append(int(errors))
        if errors == 0:
            break

    preds = np.where(np.dot(X, w) + b >= 0, 1, -1)
    accuracy = float(accuracy_score(y_signed, preds))

    xx = np.linspace(X[:, 0].min() - 1, X[:, 0].max() + 1, 200)
    yy = -(w[0] * xx + b) / w[1]

    fig, ax = plt.subplots(figsize=(6, 5))
    ax.scatter(X[:, 0], X[:, 1], c=y, cmap="coolwarm", edgecolor="k")
    ax.plot(xx, yy, color="black", linewidth=2, label=label_boundary)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    ax.legend(loc="best")
    fig.tight_layout()
    plt.show()

    return {"weights": w, "bias": b, "errors": history, "accuracy": accuracy}

metrics = run_perceptron_demo(
    title="パーセプトロンの決定境界",
    xlabel="特徴量1",
    ylabel="特徴量2",
    label_boundary="決定境界",
)
print(f"訓練精度: {metrics['accuracy']:.3f}")
print("重み:", metrics['weights'])
print(f"バイアス: {metrics['bias']:.3f}")
print("各エポックの誤り数:", metrics['errors'])

パーセプトロンの決定境界

パーセプトロンはデータが線形分離可能でない場合、収束せずに振動し続けます。実データでは線形分離可能性を事前に確認できないため、max_iterでエポック数を制限してください。非線形データにはSVMやニューラルネットワークを検討してください。

scikit-learnのPerceptronクラスを使えばpenaltyでL1/L2正則化を追加でき、early_stopping=Trueで検証スコアが改善しなくなった時点で学習を打ち切れます。パーセプトロンはSGDClassifierのloss="perceptron"と等価です。

エポック数と決定境界 #

学習のエポック数を増やすと決定境界がどう変化するか確認できます。

まとめ #

  • パーセプトロンは誤分類したサンプルでのみ重みを更新する、もっともシンプルな線形分類器です。
  • データが線形分離可能であれば有限回の更新で収束することが保証されています。
  • 更新則が単純なため、勾配法やオンライン学習の直感を掴むのに適した入門的手法です。
  • 非線形データにはそのまま対応できないため、SVMやニューラルネットワークへの発展が必要です。

参考文献 #

  • Rosenblatt, F. (1958). The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain. Psychological Review, 65(6), 386 E08.
  • Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.