Skip to content

Instantly share code, notes, and snippets.

func sign(z: Double) -> Int {
// Note that 0 is not considered positive or negative
return (z > 0) ? 1 : -1
}
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
}
}
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])
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
}
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]
}
}
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]()
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)
}
@klgraham
klgraham / Activation.swift
Last active April 3, 2016 17:35
Sign step and activation functions
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)
}
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
}
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 = {