gpt4 book ai didi

java - 即使有偏差,感知器也找不到正确的线(处理)

转载 作者:行者123 更新时间:2023-11-30 06:04:53 26 4
gpt4 key购买 nike

即使我添加了偏差,我的感知器也找不到正确的 y 截距。坡度是正确的。这是我第二次尝试从头开始编写感知器,并且两次遇到相同的错误。感知器评估 Canvas 上的点是否高于或低于截取线。输入为 x 坐标、y 坐标和 1(表示偏差)。

Perceptron class:

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;
}
}

This is the testing code:

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);
}

最佳答案

我发现了问题

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

应该是

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

即使答案是正确的,输出也永远不会恰好是一矿石零。因此,即使是正确的点也会产生一个小错误,阻止感知器找到正确的答案。首先计算答案的符号,如果答案正确,则错误为 0。

关于java - 即使有偏差,感知器也找不到正确的线(处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com