Skip to content

Instantly share code, notes, and snippets.

@jknthn jknthn/nnFit.swift
Created Sep 26, 2017

Embed
What would you like to do?
class DeepNeuralNetwork {
func fit(X: Matrix, y: Matrix) {
for i in 1...iterations {
let AL = layersForward(X: X)
let c = cost(yHat: AL, y: y)
layersBackward(y: y)
layersUpdate()
if i % 100 == 0 {
print("Iteration: \(i)")
print("Cost: \(c)")
var goodClassification = 0
for (yHat, y) in zip(AL.values, y.values) {
let yHatt = yHat > 0.5 ? 1.0 : 0.0
if y == yHatt {
goodClassification += 1
}
}
print("Accuracy: \(Double(goodClassification) / Double(y.values.count))")
}
}
}
func test(X: Matrix, y: Matrix) {
print("Testing")
let yHat = layersForward(X: X)
var goodClassification = 0
for (yHat, y) in zip(yHat.values, y.values) {
let prediction = yHat > 0.5 ? 1.0 : 0.0
print("Prediction: \(prediction), Real: \(y)")
if y == prediction {
goodClassification += 1
}
}
print("Accuracy: \(Double(goodClassification) / Double(y.values.count))")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.