Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Last active August 29, 2015 14:13
Show Gist options
  • Save jonathanmarvens/dcf1975b34ef0d596a93 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/dcf1975b34ef0d596a93 to your computer and use it in GitHub Desktop.
function BasicPerceptron(weights, bias) {
this.weights = weights;
this.threshold = bias * -1;
}
BasicPerceptron.prototype.run = function run(inputs) {
var sum = 0.0;
for (var a = 0; a < inputs.length; a++) {
sum += inputs[a] * this.weights[a];
}
if (sum <= this.threshold) {
return 0;
} else {
return 1;
}
};
@jonathanmarvens
Copy link
Author

Example:

var nand = new BasicPerceptron([-2, -2], 3);

nand.run([0, 0]); // 1
nand.run([0, 1]); // 1
nand.run([1, 0]); // 1
nand.run([1, 1]); // 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment