【问题标题】:The Bias in my Single Layer Perceptron doesn't work我的单层感知器中的偏差不起作用
【发布时间】:2020-11-04 08:58:31
【问题描述】:

块引用

我正在制作一个单层感知器,它学习如何检测一个点是高于还是低于给定线(它将结果输出为 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;
}

}

【问题讨论】:

    标签: java machine-learning neural-network perceptron bias-neuron


    【解决方案1】:

    根据您的代码,只有当某个点的正错误值的相对比率等于负错误值的比率时,您才会期望偏差停止发散。

    【讨论】:

    • 我不明白。这不是应该发生的事情吗(当有 0 个正错误和 0 个负错误(即没有错误)时,它应该停止发散)?
    猜你喜欢
    • 2023-03-11
    • 2016-11-11
    • 2013-08-20
    • 2014-12-09
    • 2017-03-15
    • 2014-12-11
    • 2018-12-14
    • 2017-02-07
    • 2012-04-30
    相关资源
    最近更新 更多