Last active
June 6, 2017 04:25
-
-
Save klgraham/8d7bdb6b150094bd9c81009fbea266f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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