Skip to content

Instantly share code, notes, and snippets.

@smakosh
Created March 14, 2018 07:31
Show Gist options
  • Save smakosh/eb9d577271cd32322d1a25c68ad73129 to your computer and use it in GitHub Desktop.
Save smakosh/eb9d577271cd32322d1a25c68ad73129 to your computer and use it in GitHub Desktop.
Perceptron from scratch with vanilla Js
// Sigmoid
const sigmoid = x => 1 / (1 + Math.exp(-x))
const D_sigmoid = x => sigmoid(x) * (1-sigmoid(x))
// data
const data = [
[ 5.1, 3.5, 0 ],
[ 4.9, 3. , 0 ],
[ 4.7, 3.2, 0 ],
[ 4.6, 3.1, 0 ],
[ 5. , 3.6, 0 ],
[ 5.4, 3.9, 0 ],
[ 4.6, 3.4, 0 ],
[ 5. , 3.4, 0 ],
[ 4.4, 2.9, 0 ],
[ 4.9, 3.1, 0 ],
[ 5.4, 3.7, 0 ],
[ 4.8, 3.4, 0 ],
[ 4.8, 3. , 0 ],
[ 4.3, 3. , 0 ],
[ 5.8, 4. , 0 ],
[ 5.7, 4.4, 0 ],
[ 5.4, 3.9, 0 ],
[ 5.1, 3.5, 0 ],
[ 5.7, 3.8, 0 ],
[ 5.1, 3.8, 0 ],
[ 7. , 3.2, 1 ],
[ 6.4, 3.2, 1 ],
[ 6.9, 3.1, 1 ],
[ 5.5, 2.3, 1 ],
[ 6.5, 2.8, 1 ],
[ 5.7, 2.8, 1 ],
[ 6.3, 3.3, 1 ],
[ 4.9, 2.4, 1 ],
[ 6.6, 2.9, 1 ],
[ 5.2, 2.7, 1 ],
[ 5. , 2. , 1 ],
[ 5.9, 3. , 1 ],
[ 6. , 2.2, 1 ],
[ 6.1, 2.9, 1 ],
[ 5.6, 2.9, 1 ],
[ 6.7, 3.1, 1 ],
[ 5.6, 3. , 1 ],
[ 5.8, 2.7, 1 ],
[ 6.2, 2.2, 1 ],
[ 5.6, 2.5, 1 ],
[ 5.9, 3.2, 1 ],
[ 6.1, 2.8, 1 ],
[ 6.3, 2.5, 1 ],
[ 6.1, 2.8, 1 ],
[ 6.4, 2.9, 1 ]
]
// training time
const learning_rate = 0.2
let costs = []
// generating the weights & the bias
let w1 = Math.random()*.2-.1
let w2 = Math.random()*.2-.1
let b = Math.random()*.2-.1
// training loop
for(let x = 0; x < 1000; x++){
let ri = Math.floor(Math.random() * data.length)
let point = data[ri]
// result
let target = point[2]
// prediction
let z = point[0] * w1 + point[1] * w2 + b
let prediction = sigmoid(z)
// cost function
let cost = (prediction - target) ** 2
// derivative of the cost function
let dcost_prediction = 2 * (prediction -target)
let d_prediction = D_sigmoid(z)
// derivatives of the weights & the bias
let dz_dw1 = point[0]
let dz_dw2 = point[1]
let dz_db = 1
// the slope of the cost function
let dcost_dz = dcost_prediction * d_prediction
// the slope of the weights & bias
let dcost_dw1 = dcost_dz * dz_dw1
let dcost_dw2 = dcost_dz * dz_dw2
let dcost_db = dcost_dz * dz_db
// Getting new weights & bias
w1 = w1 - learning_rate * dcost_dw1
w2 = w2 - learning_rate * dcost_dw2
b = b - learning_rate * dcost_db
}
const guess_flower = (SepalLength, SepalWidth) => {
const test = SepalLength * w1 + SepalWidth * w2 + b
const new_prediction = sigmoid(test)
if(new_prediction < .5) console.log('Iris-setosa')
else console.log('Iris-versicolor')
}
guess_flower(5.1, 3.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment