Perceptron即使有偏差也找不到正确的线(处理中)

时间:2018-07-26 12:50:34

标签: java neural-network processing perceptron

即使我增加了偏见,我的感知器也找不到正确的y截距。斜率是正确的。这是我第二次尝试从头开始编码感知器,并且两次遇到相同的错误。 感知器评估画布上的某个点是高于还是低于拦截线。输入为x坐标,y坐标和1作为偏差。

  

感知器类:

class Perceptron
{
  float[] weights;

  Perceptron(int layerSize)
  {
    weights = new float[layerSize];
    for (int i = 0; i < layerSize; i++)
    {
      weights[i] = random(-1.0,1.0);
    }
  }

  float Evaluate(float[] input)
  {
    float sum = 0;
    for (int i = 0; i < weights.length; i++)
    {
      sum += weights[i] * input[i];
    }
    return sum;
  }

  float Learn(float[] input, int expected)
  {
    float guess = Evaluate(input);
    float error = expected - guess;

    for (int i = 0; i < weights.length; i++)
    {
      weights[i] += error * input[i] * 0.01;
    }

    return guess;
  }
}
  

这是测试代码:

PVector[] points;

float m = 1; // y = mx+q  (in canvas space)
float q = 0; //

Perceptron brain;

void setup()
{
  size(600,600);

  points = new PVector[100];
  for (int i = 0; i < points.length; i++)
  {
    points[i] = new PVector(random(0,width),random(0,height));
  }

  brain = new Perceptron(3);
}

void draw()
{
  background(255);
  DrawGraph();
  DrawPoints();
  //noLoop();
}

void DrawPoints()
{
  for (int i = 0; i < points.length; i++)
  {
    float[] input = new float[] {points[i].x / width, points[i].y / height, 1};

    int expected = ((m * points[i].x + q) < points[i].y) ? 1 : 0; // is point above line

    float output = brain.Learn(input, expected);

    fill(sign(output) * 255);

    stroke(expected*255,100,100);
    strokeWeight(3);
    ellipse(points[i].x, points[i].y, 20, 20);
  }
}

int sign(float x)
{
  return x >= 0 ? 1 : 0;
}

void DrawGraph()
{
  float y1 = 0 * m + q;
  float y2 = width * m + q;

  stroke(255,100,100);
  strokeWeight(3);

  line(0,y1,width,y2);
}

1 个答案:

答案 0 :(得分:1)

我发现了问题

float guess = Evaluate(input);
float error = expected - guess;

应该是

float guess = sign(Evaluate(input));
float error = expected - guess;

即使答案是正确的,输出也永远不会精确地为零或零。因此,即使是正确的点也给出了一个小错误,使感知器无法找到正确的答案。首先,通过计算答案的符号,如果答案正确,则错误为0。