菜单
×
   ❮     
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 提供支持