選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

訓練感知器

  • 建立 感知器物件
  • 建立 訓練函式
  • 訓練感知器以得到正確答案

訓練任務

想象一個空間中有分散的 x y 點,以及一條直線。

訓練一個感知器來區分直線之上和之下的點。


建立感知器物件

建立一個感知器物件。可以命名為任何名稱(例如 Perceptron)。

讓感知器接受兩個引數

  1. 輸入數量(no)
  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;
}

// 線條函式
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);
}

自己動手試一試 »


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援