Skip to content

Instantly share code, notes, and snippets.

@dbalduini
Created February 28, 2020 23:37
Show Gist options
  • Save dbalduini/9c20d97507c4dfce6aeaf3a6fbfc6a02 to your computer and use it in GitHub Desktop.
Save dbalduini/9c20d97507c4dfce6aeaf3a6fbfc6a02 to your computer and use it in GitHub Desktop.
My first perceptron that models the AND logical gate
// AND logic
// @see https://medium.com/@stanleydukor/neural-representation-of-and-or-not-xor-and-xnor-logic-gates-perceptron-algorithm-b0275375fea1
// The perceptron model is x1+x2–1 where weights is 1
// Classify into 1 or -1 where
// 1 = true
// -1 = false
const trainingData = [
[1, 1, 1],
[1, 0, -1],
[0, 1, -1],
[0, 0, -1]
]
function random(min, max) {
return Math.random() * (max - min) + min;
}
// activation function = the sign function
function activation(sum) {
if (sum > 0) return 1
else return -1
}
// feed forward
// x1, x2 -> activation -> output guessed
function feedForward(inputs, weights) {
let sum = 0
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * weights[i];
}
return activation(sum);
}
function train(epochs, learningRate) {
// x1, x2 and the bias
const weights = [
random(-1, 1),
random(-1, 1),
1
];
let i = 0;
const bias = -1
while (i < epochs) {
i++;
for ([x1, x2, y] of trainingData) {
const inputs = [x1, x2, bias];
const guess = feedForward(inputs, weights);
const err = y - guess;
// update the weights
for (let i = 0; i < weights.length; i++) {
weights[i] += learningRate * err * inputs[i];
}
}
}
return weights;
}
function predict(input, weights) {
const [x1, x2, y] = input;
const output = feedForward(input, weights);
console.log(`${x1} AND ${x2} = guessed ${output}, label is ${y}`)
}
// Test the feedForward and activation function based on the expected result with fixed weights
const fixedModel = [1, 1, 1];
console.log('got', feedForward([1, 1, -1], fixedModel), 'expected', 1);
console.log('got', feedForward([1, 0, -1], fixedModel), 'expected', -1);
console.log('got', feedForward([0, 1, -1], fixedModel), 'expected', -1);
console.log('got', feedForward([0, 0, -1], fixedModel), 'expected', -1);
// Train the model
let model = train(1000, 0.01);
console.log(model);
for (let input of trainingData) {
predict(input, model);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment