LoginSignup
1
0

More than 3 years have passed since last update.

ADALINEのソースコードメモ書き(Python機械学習プログラミング第2章)

Posted at

はじめに

前回と同じ部分は、はしょりました!
ADALINEのソースコードについてメモ書きを残します。

ADALINE(ADaptive LInear Neuron)

単層パーセブトロンと違う点は、連続値のコスト関数の定義と、その最小値の求め方
(学習過程で最適化するもの目的関数といい、中でも最小化したいものはコスト関数)

AdalineGDクラスの冒頭


class AdalineGD(object):
    """ADAptive LInear NEuron classifier.
    cost_ : list
      Sum-of-squares cost function value in each epoch.
    """
    def __init__(self, eta=0.01, n_iter=50, random_state=1):
        self.eta = eta
        self.n_iter = n_iter
        self.random_state = random_state

fit関数


    def fit(self, X, y):
        rgen = np.random.RandomState(self.random_state)
        self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            net_input = self.net_input(X)
            # Please note that the "activation" method has no effect
            # in the code since it is simply an identity function. We
            # could write `output = self.net_input(X)` directly instead.
            # The purpose of the activation is more conceptual, i.e.,  
            # in the case of logistic regression (as we will see later), 
            # we could change it to
            # a sigmoid function to implement a logistic regression classifier.
            output = self.activation(net_input)
            errors = (y - output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta * errors.sum()
            cost = (errors**2).sum() / 2.0
            self.cost_.append(cost)
        return self

net_input:総出力 W.T * X = z
output:総出力と恒等 φ(W.T * X) = φ(z)
errors:正解ラベルと、総出力の差 yi - φ(zi) 2次元かも?

self.w[1:] += self.eta * X.T.dot(errors)

X.T.dot(errors)はXの転置行列とerrorsの積。Xが100*2行列、errorsが2*1行列

▼X.T.dot(errors)
https://deeplearning-benkyotyu.hatenablog.com/entry/2018/04/14/181441

self.w[0:] += self.eta * errors.sum()

self.w[1:]とself.w[0:]の役割は単純パーセプトロンと同じであるが、
更新タイミングが違う。重みの更新は(サンプル毎に重みを小刻みに計算するのではなく)
トレーニングデータセットの全てのサンプルに基づいて計算される。
このアプローチが「バッチ」勾配降下法


    def net_input(self, X):
        """Calculate net input"""
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def activation(self, X):
        """Compute linear activation"""
        return X

    def predict(self, X):
        """Return class label after unit step"""
        return np.where(self.activation(self.net_input(X)) >= 0.0, 1, -1)

activation関数;恒等関数

▼端的要約
http://forgetzimbnot.com/2014/03/post-134.html

▼i=3ver
https://thinkit.co.jp/article/10666?nopaging=1

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0