Skip to content

Instantly share code, notes, and snippets.

@qti3e
Last active January 31, 2018 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qti3e/e9af0f8aceee1563cae078f8ac8a9c9f to your computer and use it in GitHub Desktop.
Save qti3e/e9af0f8aceee1563cae078f8ac8a9c9f to your computer and use it in GitHub Desktop.
Simple Perceptron in Propel
import { Params, T } from "propel"
let params = new Params()
let trainingData = [
[[0, 0, 1], 0],
[[0, 1, 1], 1],
[[1, 0, 1], 1],
[[1, 1, 1], 1]
]
let unit_step = x => x < 0 ? 0 : 1
let w = params.randn("W", [3])
errors = []
eta = 0.2
n = 100
for(let i = 0; i < n;i++){
let d = trainingData[i % trainingData.length]
let x = d[0]
let expected = d[1]
let result = w.dot(x)
let error = expected - unit_step(result)
w = w.add(T(x).mul(eta * error))
}
for(let x of trainingData){
result = w.dot(x[0])
console.log(x[1], result, unit_step(result))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment