Skip to content

Instantly share code, notes, and snippets.

View jknthn's full-sized avatar

Jeremi Kaczmarczyk jknthn

View GitHub Profile
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 5 columns, instead of 1. in line 3.
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
...
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
...
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
class Layer {
let size: Int
let activation: Activation
init(size: Int, activation: Activation) {
self.size = size
self.activation = activation
}
class Layer {
var A: Matrix!
var Z: Matrix!
...
func forward(X: Matrix) -> Matrix {
if activation != .none {
Z = weights.dot(X) + biases
class Layer {
...
var dW: Matrix!
var db: Matrix!
var dZ: Matrix!
func backward(m: Double, y: Matrix? = nil) {
if let nextLayer = nextLayer, let previousLayer = previousLayer {
class Layer {
...
func update(learningRate: Double) {
if previousLayer != nil {
weights = weights - (learningRate * dW)
biases = biases - (learningRate * db)
}
}
class DeepNeuralNetwork {
let iterations: Int
let learningRate: Double
private var layers = [Layer]()
init(iterations: Int, learningRate: Double) {
self.iterations = iterations
self.learningRate = learningRate
@jknthn
jknthn / nnUtils.swift
Last active September 26, 2017 11:48
class DeepNeuralNetwork {
...
private func layersForward(X: Matrix) -> Matrix {
var output = X
for layer in layers {
output = layer.forward(X: output)
}
return output
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 {
Iteration: 5000
Cost: 11.6211340518401
Accuracy: 0.958677685950413
Testing
Prediction: 0.0, Real: 0.0
Prediction: 1.0, Real: 1.0
Prediction: 0.0, Real: 0.0
Prediction: 0.0, Real: 0.0
Prediction: 1.0, Real: 1.0
import Matswift
let matrix1 = Matrix(values: [[2.0, 1.0], [3.0, 1.0]])
let matrix2 = Matrix(values: [[1.0, 3.0], [2.0, 4.0]])
let multiplication = matrix1 * matrix2
let division = matrix1 / matrix2
let addition = matrix1 + matrix2
let subtraction = matrix1 - matrix2