パーセプトロンのトレーニング
- パーセプトロンオブジェクトを作成する
- トレーニング関数を作成する
- 目的の答えに対してパーセプトロンをトレーニングします
トレーニングタスク
xy点が散在している空間の直線を想像してみてください。
パーセプトロンをトレーニングして、線の上下のポイントを分類します。
パーセプトロンオブジェクトを作成する
パーセプトロンオブジェクトを作成します。パーセプトロンのように、なんでも名前を付けてください。
パーセプトロンに2つのパラメーターを受け入れさせます。
- 入力数(いいえ)
- 学習率(learningRate)。
デフォルトの学習率を0.00001に設定します。
次に、入力ごとに-1から1までのランダムな重みを作成します。
例
// Perceptron Object
function Perceptron(no, learningRate = 0.00001) {
// Set Initial Values
this.learnc = learningRate;
this.bias = 1;
// Compute Random Weights
this.weights = [];
for (let i = 0; i <= no; i++) {
this.weights[i] = Math.random() * 2 - 1;
}
// End Perceptron Object
}
ランダムウェイト
パーセプトロンは、入力ごとにランダムな重みで始まります。
学習率
ミスごとに、パーセプトロンをトレーニングしている間、重みはわずかな割合で調整されます。
この小さな部分が「パーセプトロンの学習率」です。
パーセプトロンオブジェクトでは、これをlearncと呼びます。
バイアス
場合によっては、両方の入力がゼロの場合、パーセプトロンが正しい出力を生成することがあります。
これを回避するために、パーセプトロンに値1の追加入力を与えます。
これはバイアスと呼ばれます。
活性化関数を追加する
パーセプトロンアルゴリズムを覚えておいてください。
- 各入力にパーセプトロンの重みを掛けます
- 結果を合計する
- 結果を計算する
例
this.activate = function(inputs) {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * this.weights[i];
}
if (sum > 0) {return 1} else {return 0}
}
活性化関数は以下を出力します:
- 合計が0より大きい場合は1
- 合計が0未満の場合は0
トレーニング関数を作成する
トレーニング関数は、アクティブ化関数に基づいて結果を推測します。
推測が間違っているたびに、パーセプトロンは重みを調整する必要があります。
多くの推測と調整の後、重みは正しくなります。
例
this.train = function(inputs, desired) {
inputs.push(this.bias);
let guess = this.activate(inputs);
let error = desired - guess;
if (error != 0) {
for (let i = 0; i < inputs.length; i++) {
this.weights[i] += this.learnc * error * inputs[i];
}
}
}
誤差逆伝播法
各推測の後、パーセプトロンは推測がどれほど間違っていたかを計算します。
推測が間違っている場合、パーセプトロンはバイアスと重みを調整して、次回の推測がもう少し正確になるようにします。
このタイプの学習は、バックプロパゲーションと呼ばれます。
(数千回)試した後、あなたのパーセプトロンは推測がかなり上手になります。
独自のライブラリを作成する
ライブラリコード
// Perceptron Object
function Perceptron(no, learningRate = 0.00001) {
// Set Initial Values
this.learnc = learningRate;
this.bias = 1;
// Compute Random Weights
this.weights = [];
for (let i = 0; i <= no; i++) {
this.weights[i] = Math.random() * 2 - 1;
}
// Activate Function
this.activate = function(inputs) {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * this.weights[i];
}
if (sum > 0) {return 1} else {return 0}
}
// Train Function
this.train = function(inputs, desired) {
inputs.push(this.bias);
let guess = this.activate(inputs);
let error = desired - guess;
if (error != 0) {
for (let i = 0; i < inputs.length; i++) {
this.weights[i] += this.learnc * error * inputs[i];
}
}
}
// End Perceptron Object
}
これで、ライブラリをHTMLに含めることができます。
<script src="myperceptron.js"></script>
ライブラリを使用する
例
// Initiate Values
const numPoints = 500;
const learningRate = 0.00001;
// Create a Plotter
const plotter = new XYPlotter("myCanvas");
plotter.transformXY();
const xMax = plotter.xMax;
const yMax = plotter.yMax;
const xMin = plotter.xMin;
const yMin = plotter.yMin;
// Create Random XY Points
const xPoints = [];
const yPoints = [];
for (let i = 0; i < numPoints; i++) {
xPoints[i] = Math.random() * xMax;
yPoints[i] = Math.random() * yMax;
}
// Line Function
function f(x) {
return x * 1.2 + 50;
}
//Plot the Line
plotter.plotLine(xMin, f(xMin), xMax, f(xMax), "black");
// Compute Desired Answers
const desired = [];
for (let i = 0; i < numPoints; i++) {
desired[i] = 0;
if (yPoints[i] > f(xPoints[i])) {desired[i] = 1}
}
// Create a Perceptron
const ptron = new Perceptron(2, learningRate);
// Train the Perceptron
for (let j = 0; j <= 10000; j++) {
for (let i = 0; i < numPoints; i++) {
ptron.train([xPoints[i], yPoints[i]], desired[i]);
}
}
// Display the Result
for (let i = 0; i < numPoints; i++) {
const x = xPoints[i];
const y = yPoints[i];
let guess = ptron.activate([x, y, ptron.bias]);
let color = "black";
if (guess == 0) color = "blue";
plotter.plotPoint(x, y, color);
}