我尝试使用单个感知器来预测异或门。然而,结果似乎完全是随机的,我找不到错误。
我在这里做错了什么?
- 是我的训练方法不对吗?
- 或者感知器模型中是否存在任何错误?
- 或者单个感知器不能用于解决这个问题?
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]) )
}
You don't seem to have a hidden layer. Neural Networks have at least one 'middle' layer that also propagates the values. like so ![simple neural net](https://cdn-images-1.medium.com/max/1600/1*IjY3wFF24sK9UhiOlf36Bw.png)
Here https://medium.freecodecamp.org/how-to-create-a-neural-network-in-javascript-in-only-30-lines-of-code-343dafc50d49是制作简单神经网络的好地方。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)