This file contains hidden or 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
| func sign(z: Double) -> Int { | |
| // Note that 0 is not considered positive or negative | |
| return (z > 0) ? 1 : -1 | |
| } |
This file contains hidden or 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
| let testData = createData(100) | |
| func evaluatePerceptron(p: Perceptron, testData: [PerceptronDataPair]) -> Double { | |
| var correct = 0 | |
| for d in testData { | |
| let prediction = p.feedForward(d.input) | |
| if (prediction == d.output) { | |
| correct += 1 | |
| } | |
| } |
This file contains hidden or 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
| func randomDouble() -> Double { | |
| return Double(arc4random()) / Double(UINT32_MAX) | |
| } | |
| func createData(numPoints: Int) -> [PerceptronDataPair] { | |
| var data = [PerceptronDataPair]() | |
| for _ in 0..<numPoints { | |
| let x = [2.0 * (randomDouble() - 0.5)] | |
| let y = line(x[0]) |
This file contains hidden or 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
| struct PerceptronTrainer { | |
| let data: [PerceptronDataPair] | |
| func train(inout p: Perceptron) -> Int { | |
| var error: Int = 0 | |
| for d in data { | |
| error = p.backProp(d.input, output: d.output) | |
| } | |
| return error | |
| } |
This file contains hidden or 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
| mutating func backProp(input: [Double], output: Int) { | |
| let prediction = feedForward(input) | |
| let error = output - prediction | |
| for i in 0..<weights.count { | |
| weights[i] += learningRate * Double(error) * input[i] | |
| } | |
| } |
This file contains hidden or 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]() |
This file contains hidden or 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
| func activate(bias: Double, bW: Double, x: [Double], w: [Double]) -> Int { | |
| var sum = 0.0 | |
| for i in 0..<x.count { | |
| sum += x[i] * w[i] | |
| } | |
| sum += bias * bW | |
| return step(sum) | |
| } |
This file contains hidden or 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
| func activate(bias: Double, bW: Double, x: [Double], w: [Double]) -> Int { | |
| var sum = 0.0 | |
| for i in 0..<x.count { | |
| sum += x[i] * w[i] | |
| } | |
| sum += bias * bW | |
| return step(sum) | |
| } |
This file contains hidden or 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
| func flatMap<B>(f: A -> Distribution<B>) -> Distribution<B> { | |
| var d = Distribution<B>(get: {() -> Optional<B> in return nil}) | |
| d.get = { | |
| (Void) -> B in return f(self.get()!).get()! | |
| } | |
| return d | |
| } |
This file contains hidden or 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
| struct Distribution<A> { | |
| var get: () -> A? | |
| func sample(n: Int) -> [A] { | |
| return (1...n).map { x in get()! } | |
| } | |
| func map<B>(f: A -> B) -> Distribution<B> { | |
| var d = Distribution<B>(get: {() -> Optional<B> in return nil}) | |
| d.get = { |