【问题标题】:Simple Perceptron In Javascript for XOR gate用于 XOR 门的 Javascript 中的简单感知器
【发布时间】:2018-07-05 00:45:23
【问题描述】:

我尝试使用单个感知器来预测 XOR 门。但是,结果似乎是完全随机的,我找不到错误。

我在这里做错了什么? - 我的训练方法错了吗? - 或者感知器模型中是否有任何错误? - 或者单个感知器不能用于这个问题?

class Perceptron {

    constructor(input_nodes, learning_rate) {
        this.nodes = input_nodes;
        this.bias = Math.random() * 2 - 1;
        this.learning_rate = learning_rate;
        this.weights = [];

        for (let i = 0; i < input_nodes; i++) {
            this.weights.push(Math.random() * 2 - 1)
        }
    }

    train (inputs, desired_output) {

        // Guess the result
        let guess = this.predict(inputs);
        let error = desired_output - guess;

        // Adjust weights and bias
        for (let i = 0; i < this.weights.length; i++) {
            this.weights[i] += this.learning_rate * error * inputs[i];         
        }
        this.bias += error * this.learning_rate;
    }

    predict (input_array) {

        if ( input_array.length != this.nodes) throw new Error({message: 'Invalid Input!'})

        let sum = this.bias;
        for (let i = 0; i < input_array.length; i++) {
            sum += this.weights[i] * input_array[i];
        }

        return this.activate(sum);
    }

    activate (num) {
        return num < 0 ? 0 : 1;
    }
}

module.exports = Perceptron;

if ( require.main === module ) {
    let p = new Perceptron(2, 0.003);

    for ( let i = 0; i < 1000; i++ ) {
        p.train([0, 0], 0);
        p.train([0, 1], 1);
        p.train([1, 0], 1);
        p.train([1, 1], 0);
    }

    console.log( p.predict([0, 1]) )
}

【问题讨论】:

  • 你不能用一个感知器进行 XOR,因为 XOR 不是线性可分的(这是在开玩笑吗?)。如果您使用多个感知器,请在纸上计算多次迭代,然后进行比较。
  • @IvanKuckir 哦,是的!谢谢

标签: javascript neural-network perceptron


【解决方案1】:

您似乎没有隐藏层。神经网络至少有一个“中间”层,它也传播这些值。像这样

Here 是制作简单神经网络的好地方。

【讨论】:

  • 是的,但我不想创建神经网络。只是想看看单个感知器能做什么。我得到了答案 - 这是因为 XOR 不是线性可分的。
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 2021-04-07
  • 2020-08-22
  • 2012-03-28
  • 2017-08-29
  • 2019-09-28
相关资源
最近更新 更多