[英]Simple Perceptron In Javascript for XOR gate
我試圖使用單個感知器來預測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]) )
}
您似乎沒有隱藏的圖層。 神經網絡至少有一個“中間”層,該中間層也傳播值。 像這樣
這是制作簡單神經網絡的好地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.