Javascript NOT函数的布尔感知器

Javascript NOT函数的布尔感知器,javascript,machine-learning,neural-network,boolean-logic,perceptron,Javascript,Machine Learning,Neural Network,Boolean Logic,Perceptron,我试图为基本布尔表达式实现一个简单的感知器。但是我不能正确地训练非感知器 我成功地训练了和以及或感知器返回给定输入集的正确值。但是当我试图训练时,却不是 我就是这样做的: 和以及或感知机有两个输入,两个权重和一个偏差(偏差输入固定为1) 所有感知器在所有权重上均以0开始。 然后我生成随机值(介于0和1之间)来训练感知机,并保持循环,直到我得到10个正确的猜测 他们的学习率为0.1 这是培训过程: 要猜测值: 对于每个输入,我将输入乘以权重,并将所有值相加,包括偏差 sum = (weight1

我试图为基本布尔表达式实现一个简单的感知器。但是我不能正确地训练非感知器

我成功地训练了以及感知器返回给定输入集的正确值。但是当我试图训练时,却不是

我就是这样做的:

以及感知机有两个输入,两个权重和一个偏差(偏差输入固定为1

所有感知器在所有权重上均以0开始。 然后我生成随机值(介于0和1之间)来训练感知机,并保持循环,直到我得到10个正确的猜测

他们的学习率为0.1

这是培训过程:

要猜测值:
对于每个输入,我将输入乘以权重,并将所有值相加,包括偏差

sum = (weight1 * input1) + (weight2 * input2) + (biasWeight * biasInput)--Bias input is fixed to 1
return = if (sum > 0) then 1 else 0
培训感知机:
我从感知机那里得到了猜测

val = and.guess(1,0) --This will return 0 or 1
error = answer - val
对于每个输入,我执行此计算

weight = weight + (input * error * rate)
然后我对偏差也做了同样的处理

biasWeight = biasWeight + (input * error * rate)--Bias input is fixed to 1
通过这个过程,我可以成功地训练以及感知机

/感知机之间的唯一区别是输入的数量(只有1个用于

但是非感知器只是通过学习速率中的数字不断增加权重

有时,根据感知器的训练顺序,当它达到0.5时,会得到正确的值

当我回家发布代码时,有一些代码(html、javascript)。我真的找到了这个bug。CALC函数应该返回weight*输入,返回的是weight+输入,它实际上对以及训练有效

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="jquery-3.2.1.js"></script>

    <script type="text/javascript">
        function Show(text) {
            if (!text) {
                text = '';
            }

            document.writeln(text + '<br />');
        }

        //return random value from 0 ~ 1
        function getRandom() {
            return Math.floor(Math.random() * 2);
        };

        function PerceptronData(input, weight) {
            this.input = input;
            this.weight = weight;
        }
        PerceptronData.prototype.calc = function () {
            var result = this.input + this.weight;
            return result;
        };
        PerceptronData.prototype.adjust = function (error, rate) {
            this.weight += (this.input * error * rate);
        };
        PerceptronData.prototype.print = function () {
            return '(' + this.input + ', ' + this.weight + ')';
        }

        function Perceptron(n) {
            this.data = [];//Data array [input, weight]
            this.bias = new PerceptronData(1, 0);
            this.rate = 0.1;//learning rate

            //initial data
            for (var index = 0; index < n; index++) {
                this.data.push(new PerceptronData(0, 0));
            }
        }
        //called from "guess" function in the final perceptron
        Perceptron.prototype.process = function (inputs) {
            var data = this.data;

            if (inputs.length != data.length) {
                throw "The number os inputs [" + inputs.length + "] doesn't match with the start value [" + data.length + "] of the Perceptron.";
            }

            var dataSum = 0;
            for (var index = 0; index < data.length; index++) {
                data[index].input = parseInt(inputs[index]);
                dataSum += data[index].calc();
            }

            dataSum += this.bias.calc();

            return dataSum;
        };
        //twick the weight for every data
        Perceptron.prototype.adjust = function (value, answer) {
            var data = this.data;
            var error = answer - value;

            for (var index = 0; index < data.length; index++) {
                data[index].adjust(error, this.rate);
            }

            this.bias.adjust(error, this.rate);
        };
        Perceptron.prototype.print = function () {
            var data = this.data;
            var result = '';
            for (var index = 0; index < data.length; index++) {
                result += 'data[' + index + ']' + data[index].print() + ' > ';
            }

            return result + 'bias' + this.bias.print();
        };

        function NotPerceptron() {
            Perceptron.call(this, 1);
        }
        NotPerceptron.prototype = Object.create(Perceptron.prototype);
        NotPerceptron.prototype.guess = function (value) {
            var data = this.process([value]);

            //activation function
            return ((data > 0) ? 1 : 0);
        };
        NotPerceptron.prototype.train = function (value, answer) {
            var result = this.guess([value]);
            this.adjust(result, answer);
        };

        function AndPerceptron() {
            Perceptron.call(this, 2);
        }
        AndPerceptron.prototype = Object.create(Perceptron.prototype);
        AndPerceptron.prototype.guess = function (valueA, valueB) {
            var data = this.process([valueA, valueB]);

            //activation function
            return ((data > 0) ? 1 : 0);
        };
        AndPerceptron.prototype.train = function (valueA, valueB, answer) {
            var result = this.guess(valueA, valueB);

            this.adjust(result, answer);
        };

        function OrPerceptron() {
            Perceptron.call(this, 2);
        }
        OrPerceptron.prototype = Object.create(Perceptron.prototype);
        OrPerceptron.prototype.guess = function (valueA, valueB) {
            var data = this.process([valueA, valueB]);

            //activation function
            return ((data > 0) ? 1 : 0);
        };
        OrPerceptron.prototype.train = function (valueA, valueB, answer) {
            var result = this.guess(valueA, valueB);

            this.adjust(result, answer);
        };
    </script>
</head>
<body>
    <script type="text/javascript">
        Show('Training AND...');
        Show();
        var and = new AndPerceptron();

        var count = 0;
        var total = 0;
        var max = 100;

        while (count < 10 && total < max) {
            total++;
            var a = getRandom();
            var b = getRandom();
            var answer = ((a === 1 && b === 1) ? 1 : 0);

            and.train(a, b, answer);

            a = getRandom();
            b = getRandom();
            answer = ((a === 1 && b === 1) ? 1 : 0);

            var guess = and.guess(a, b);

            if (guess === answer) {
                count++;
            } else {
                count = 0;
            }

            Show(' > AND(' + a + ', ' + b + ') = ' + guess + ' > [' + and.print() + ']');

            if (count == 10) {
                //final test
                if (and.guess(0, 0) == 1) {
                    count = 0;
                }

                if (and.guess(0, 1) == 1) {
                    count = 0;
                }

                if (and.guess(1, 0) == 1) {
                    count = 0;
                }

                if (and.guess(1, 1) == 0) {
                    count = 0;
                }
            }
        }
        Show();

        if (total >= max) {
            Show('AND training failed...');
        } else {
            Show('AND trained with [' + total + '] interactions. [' + and.print() + ']');
        }

        Show();
        Show('AND(0, 0) = ' + and.guess(0, 0));
        Show('AND(0, 1) = ' + and.guess(0, 1));
        Show('AND(1, 0) = ' + and.guess(1, 0));
        Show('AND(1, 1) = ' + and.guess(1, 1));

        Show();
        Show('Training OR...');
        Show();
        var or = new OrPerceptron();

        count = 0;
        total = 0;
        max = 100;

        while (count < 10 && total < max) {
            total++;
            var a = getRandom();
            var b = getRandom();
            var answer = ((a === 1 || b === 1) ? 1 : 0);

            or.train(a, b, answer);

            a = getRandom();
            b = getRandom();
            answer = ((a === 1 || b === 1) ? 1 : 0);

            var guess = or.guess(a, b);

            if (guess === answer) {
                count++;
            } else {
                count = 0;
            }

            Show(' > OR(' + a + ', ' + b + ') = ' + guess + ' > [' + or.print() + ']');

            if (count == 10) {
                //final test
                if (or.guess(0, 0) == 1) {
                    count = 0;
                }

                if (or.guess(0, 1) == 0) {
                    count = 0;
                }

                if (or.guess(1, 0) == 0) {
                    count = 0;
                }

                if (or.guess(1, 1) == 0) {
                    count = 0;
                }
            }
        }
        Show();

        if (total >= max) {
            Show('OR training failed...');
        } else {
            Show('OR trained with [' + total + '] interactions. [' + or.print() + ']');
        }

        Show();
        Show('OR(0, 0) = ' + or.guess(0, 0));
        Show('OR(0, 1) = ' + or.guess(0, 1));
        Show('OR(1, 0) = ' + or.guess(1, 0));
        Show('OR(1, 1) = ' + or.guess(1, 1));

        Show();
        Show('Training NOT...');
        Show();
        var not = new NotPerceptron();
        not.rate = 0.1;

        count = 0;
        total = 0;
        max = 100;

        while (count < 10 && total < max) {
            total++;
            var test = getRandom();
            var answer = ((test === 1) ? 0 : 1);

            not.train(test, answer);

            test = getRandom();
            answer = ((test === 1) ? 0 : 1);

            var guess = not.guess(test);

            if (guess === answer) {
                count++;
            } else {
                count = 0;
            }

            Show(' > NOT(' + test + ') = ' + guess + ' > [' + not.print() + ']');

            if (count == 10) {
                //final test
                if (not.guess(0) == 0) {
                    count = 0;
                }

                if (not.guess(1) == 1) {
                    count = 0;
                }
            }
        }
        Show();

        if (total >= max) {
            Show('NOT training failed...');
        } else {
            Show('NOT trained with [' + total + '] interactions. [' + not.print() + ']');
        }

        Show();
        Show('NOT(1) = ' + not.guess(1));
        Show('NOT(0) = ' + not.guess(0));
    </script>
</body>
</html>

在@Stanislav Kralin sugestion之后,我再次更新了这个问题,因此它显示了问题。这就是解决办法

问题在于CALC函数应该乘以输入的权重值。但我正在添加它

不幸的是,我非常关注是否应该使用sigmoid函数或其他函数,关注学习率以及线性和非线性函数,以至于我没有看到这个错误

事实上,与以及感知器配合良好,这确实让我走错了方向

PerceptronData.prototype.calc = function () {
    //var result = this.input + this.weight;//This was wrong... :(
    var result = this.input * this.weight;
    return result;
};

请同时添加代码。当我回家编辑问题并发布代码时,我发现了错误。无论如何谢谢你。我已经用所有代码更新了这个问题。那么问题出在哪里?现在它工作得很好。张贴你的答案并接受它。我认为你的问题很有启发性,不应该以“一个无法再复制的问题或一个简单的印刷错误”来结束。好的,我会这么做
PerceptronData.prototype.calc = function () {
    //var result = this.input + this.weight;//This was wrong... :(
    var result = this.input * this.weight;
    return result;
};