块引用
我正在制作一个单层感知器,它学习如何检测一个点是高于还是低于给定线(它将结果输出为 1 或 -1)。当线的 y 截距为 0 并且没有偏差时,它工作正常,但是当我合并一个偏差并更改 y 截距(在本例中为 -150)时,偏差不断减小到 -150 并且感知器可以'解决不了。我该如何解决?
(不要担心 JFrame 和点的细节,一些我知道可以正常工作的方法已从这篇文章中删除以简化代码)
主要类:
public class Runner {
ArrayList<JLabel> dots = new ArrayList<JLabel>();
JFrame frame = new JFrame();
Brain brain = new Brain();
public Runner() {
frame.setBounds(0, 0, 1400, 740);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addDots(1000, 1400, 740); //1000 dots are added with x from 0-1400 and y from 0-740
}
public static void main(String[] args) {
new Runner();
}
public void trainBrain(int dotsInd) {
JLabel dot = dots.get(dotsInd);
brain.guess(dot.getX(), dot.getY());
float er = brain.getError(getTarget(dot));
brain.changeWeights(er);
if(er == 0) {dots.get(dotsInd).setBackground(Color.GREEN);}
else {dots.get(dotsInd).setBackground(Color.RED);}
}
//Used to set the target line (y = x/2 - 150)
public int getTarget(JLabel dot) {
if(dot.getX() > (2 * dot.getY() + 300)) {return 1;}
return -1;
}
}
大脑类(感知器):
public class Brain {
int inputOne = 0; //x of a dot
int inputTwo = 0; //y of a dot
float weightOne = (float) (Math.random() * 3 - 1);
float weightTwo = (float) (Math.random() * 3 - 1);
int output = 0;
float biasWeight = (float) (Math.random() * 3 - 1);
public int guess(int iOne, int iTwo) {
inputOne = iOne;
inputTwo = iTwo;
output = activationFunc(iOne * weightOne + iTwo * weightTwo + biasWeight);
return output;
}
public float getError(int target) {
return target - output;
}
public void changeWeights(float error) {
weightOne += error * inputOne * 1;
weightTwo += error * inputTwo * 1;
biasWeight += error * 1;
}
public int activationFunc(float d) {
if(d > 0) return 1;
return -1;
}
}