퍼셉트론 훈련

  • 퍼셉트론 객체 생성
  • 훈련 함수 생성
  • 원하는 답변에 대해 퍼셉트론 훈련

교육 과제

xy 점이 흩어져 있는 공간에서 직선을 상상해 보십시오.

선의 위와 아래에 있는 점을 분류하도록 퍼셉트론을 훈련시킵니다.


퍼셉트론 객체 생성

퍼셉트론 개체를 만듭니다. 이름을 아무거나 지정하세요(예: Perceptron).

퍼셉트론이 두 개의 매개변수를 허용하도록 하십시오.

  1. 입력 수(없음)
  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
}

무작위 가중치

Perceptron은 각 입력에 대해 임의의 가중치 로 시작합니다.

학습률

각 실수에 대해 Perceptron을 훈련하는 동안 가중치는 작은 부분으로 조정됩니다.

이 작은 부분이 " 퍼셉트론의 학습률 "입니다.

Perceptron 객체에서 우리는 그것을 learnc 라고 부릅니다 .

편견

때로는 두 입력이 모두 0이면 퍼셉트론이 올바른 출력을 생성할 수 있습니다.

이를 피하기 위해 퍼셉트론에 값 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);
}