Skip to content

Instantly share code, notes, and snippets.

@klgraham
Last active June 6, 2017 04:25
Show Gist options
  • Save klgraham/8d7bdb6b150094bd9c81009fbea266f0 to your computer and use it in GitHub Desktop.
Save klgraham/8d7bdb6b150094bd9c81009fbea266f0 to your computer and use it in GitHub Desktop.
class Perceptron {
var bias: Double
// weights[0] is the weight for the bias input
var weights: [Double]
init(numInputs: Int, bias: Double) {
self.bias = bias
self.weights = [Double]()
for _ in 0...numInputs{
weights.append(1.0 * (randomDouble() - 0.5))
}
}
func activate(input: [Double]) -> Int {
assert(input.count + 1 == weights.count)
var sum = 0.0
for i in 0..<input.count {
sum += input[i] * weights[i + 1]
}
sum += bias * weights[0]
return (sum > 0) ? 1 : -1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment