機械学習

学習はループしています

MLモデルは、データを複数回ループすることでトレーニングされます。

反復ごとに、重み値が調整されます。

反復がコストの削減に失敗すると、トレーニングは完了します。

最適なラインを見つけるために私を訓練してください:


最急降下法

最急降下法は、AIの問題を解決するための一般的なアルゴリズムです。

単純な線形回帰モデルを使用して、最急降下法を示すことができます。

線形回帰の目的は、線形グラフを一連の(x、y)点に適合させることです。これは数式で解くことができます。しかし、機械学習アルゴリズムもこれを解決できます。

これは上記の例が行うことです。

散布図と線形モデル(y = wx + b)から始まります。

次に、モデルをトレーニングして、プロットに適合する線を見つけます。これは、線の太さ(傾き)とバイアス(切片)を変更することによって行われます。

以下は、この問題(および他の多くの問題)を解決できるトレーナーオブジェクトのコードです。


トレーナーオブジェクト

2つの配列(xArr、yArr)で任意の数の(x、y)値をとることができるTrainerオブジェクトを作成します。

重みとバイアスの両方をゼロに設定します。

学習定数(learnc)を設定し、コスト変数を定義する必要があります。

function Trainer(xArray, yArray) {
  this.xArr = xArray;
  this.yArr = yArray;
  this.points = this.xArr.length;
  this.learnc = 0.00001;
  this.weight = 0;
  this.bias = 1;
  this.cost;

コスト関数

回帰問題を解決するための標準的な方法は、解決策がどれだけ優れているかを測定する「コスト関数」を使用することです。

この関数は、モデルの重みとバイアス(y = wx + b)を使用し、線がプロットにどの程度適合しているかに基づいてエラーを返します。

このエラーを計算する方法は、プロット内のすべての(x、y)ポイントをループし、各ポイントのy値と線の間の二乗距離を合計することです。

最も一般的な方法は、距離を2乗し(正の値を確保するため)、誤差関数を微分可能にすることです。

this.costError = function() {
  total = 0;
  for (let i = 0; i < this.points; i++) {
    total += (this.yArr[i] - (this.weight * this.xArr[i] + this.bias)) **2;
  }
  return total / this.points;
}

コスト関数の別名エラー関数です。

関数で使用される式は、実際には次のとおりです。

方式
  • Eはエラー(コスト)です
  • Nは、観測値(ポイント)の総数です。
  • yは、各観測値(ラベル)です。
  • xは、各観測値(特徴)です。
  • mは勾配(重量)です
  • bは切片(バイアス)です
  • mx + bは予測です
  • 1/N * N∑1 is the squared mean value

The Train Function

We will now run a gradient descent.

The gradient descent algorithm should walk the cost function towards the best line.

Each iteration should update both m and b towards a line with a lower cost (error).

To do that, we add a train function that loops over all the data many times:

this.train = function(iter) {
  for (let i = 0; i < iter; i++) {
    this.updateWeights();
  }
  this.cost = this.costError();
}

An Update Weights Function

The train function above should update the weights and biases in each iteration.

The direction to move is calculated using two partial derivatives:

this.updateWeights = function() {
  let wx;
  let w_deriv = 0;
  let b_deriv = 0;
  for (let i = 0; i < this.points; i++) {
    wx = this.yArr[i] - (this.weight * this.xArr[i] + this.bias);
    w_deriv += -2 * wx * this.xArr[i];
    b_deriv += -2 * wx;
  }
  this.weight -= (w_deriv / this.points) * this.learnc;
  this.bias -= (b_deriv / this.points) * this.learnc;
}

Create Your Own Library

Library Code

function Trainer(xArray, yArray) {
  this.xArr = xArray;
  this.yArr = yArray;
  this.points = this.xArr.length;
  this.learnc = 0.000001;
  this.weight = 0;
  this.bias = 1;
  this.cost;

// Cost Function
this.costError = function() {
  total = 0;
  for (let i = 0; i < this.points; i++) {
    total += (this.yArr[i] - (this.weight * this.xArr[i] + this.bias)) **2;
  }
  return total / this.points;
}

// Train Function
this.train = function(iter) {
  for (let i = 0; i < iter; i++) {
    this.updateWeights();
  }
  this.cost = this.costError();
}

// Update Weights Function
this.updateWeights = function() {
  let wx;
  let w_deriv = 0;
  let b_deriv = 0;
  for (let i = 0; i < this.points; i++) {
    wx = this.yArr[i] - (this.weight * this.xArr[i] + this.bias);
    w_deriv += -2 * wx * this.xArr[i];
    b_deriv += -2 * wx;
  }
  this.weight -= (w_deriv / this.points) * this.learnc;
  this.bias -= (b_deriv / this.points) * this.learnc;
}

} // End Trainer Object

Now you can include the library in HTML:

<script src="myailib.js"></script>