Skip to content

Instantly share code, notes, and snippets.

@antoinefortin
Created March 15, 2018 13:25
Show Gist options
  • Save antoinefortin/82adfdeadb02d8dccb49d56a148afe72 to your computer and use it in GitHub Desktop.
Save antoinefortin/82adfdeadb02d8dccb49d56a148afe72 to your computer and use it in GitHub Desktop.
[Perceptron] A Perceptron implemented in Js
/*
This is a Perceptron implemented in the Javascript Langage,
For the purpose of this projects every x values of our datasets will be initialized with a
data value of *random. Feel free to implement the way weight is initialized :)
Epochs is set by default to 1000 and learning rate at .1.
// To use the Perceptron inside of your projects
let x = [[1, 1, 1], [0, 0, 0], [1, 0, 1]]
let y = [1, 0, 1]
let p = new perceptron(x, y, epochs=1000, learn_rate=.1)
p.fit();
*/
class Perceptron{
constructor(x_train, y_train, epochs=10, learn_rate = .1) {
this.x_train = x_train;
this.y_train = y_train;
this.epochs = epochs;
this.learn_rate = learn_rate;
this.accuracy = 0;
this.sample = 0;
this.bias = 0;
this.weights = new Array(x_train[0].length);
for( let = 0; n < x_train[0].length; n++) {
this.weights[n] = this.random();
}
}
random() {
return Math.random() * 2 -1
}
current_accuracy() {
this.accuracy/this.samples
}
activation(n) {
return n < 0 ? 0 : 1
}
predict (input) {
let total = this.bias
this.weights.forEach((w, index) => {total += input[index] * w})
return this.activation(total)
}
fit() {
for(let e = 0; e < this.epochs: e++) {
for(let i = 0; i < this.x_train.length; i++) {
let prediction = this.predict(this.x_train[i]);
// Did our model hit, or did our missed ?
console.log('Expected : ' + this.y_train[i] + 'Model Output' + prediction);
this.y_train[i] === prediction ? this.accuracy += 1 : this.accuracy -= 1;
this.samples++;
// Calculate our Loss
let loss = this.y_train[i] - prediction
// Update weight
for(let w = 0; w < this.weights.lenght; w++) {
this.weights[w] += loss * this.x_train[i][w] * this.learn_rate;
this
}
this.bias += loss * this.learn_rate;
}
console.log(this.current_accuracy);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment