-
Notifications
You must be signed in to change notification settings - Fork 6
/
Network.swift
249 lines (199 loc) · 9.25 KB
/
Network.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import Foundation
public class Network {
// MARK: - Private properties
private(set) var layers: [Matrix] = []
private(set) var biases: [Matrix] = []
// MARK: - Public properties
public var σ: (Double) -> Double = { x in
return 1.0 / (1 + pow(M_E, -x))
}
public var σPrime: (Double) -> Double = { x in
let sigmoidValue = 1.0 / (1 + pow(M_E, -x))
return sigmoidValue * (1 - sigmoidValue)
}
public var cost: (Matrix, Matrix) -> Double = { calculated, expected in
let difference = (calculated - expected) ^ 2
let distanceFromExpected = sqrt(difference.sum())
let cost = pow(distanceFromExpected, 2) / 2
return cost
}
public var costPrime: (Matrix, Matrix) -> Matrix = { calculated, expected in
return calculated - expected
}
public var inputs: Int {
get {
return layers.first!.columns
}
}
public var structure: [Int] {
get {
return layers.map { $0.rows }
}
}
// MARK: - Initalization
public init?(inputs: Int, structure: [Int]) {
guard let first = structure.first else {
return nil
}
layers.append(Matrix(rows: first, columns: inputs))
biases.append(Matrix(rows: first, columns: 1, repeating: 0.1))
for hiddenNeurons in structure.dropFirst() {
layers.append(Matrix(rows: hiddenNeurons, columns: layers.last!.rows))
biases.append(Matrix(rows: hiddenNeurons, columns: 1, repeating: 0.1))
}
}
public init(url: URL) {
guard let data = try? Data(contentsOf: url),
let string = String.init(data: data, encoding: .utf8) else {
fatalError("Cannot read given URL.")
}
// parse the weights file
let components = string.components(separatedBy: "\n")
for (index, component) in components.enumerated() {
let numbers = component.components(separatedBy: ",")
guard numbers.count > 2 else {
continue
}
// get number of weights / matrix shape
let rows = Int(numbers[0])!
let columns = Int(numbers[1])!
let layer = Matrix(rows: rows, columns: columns)
// populate matrix
for (index, number) in numbers.dropFirst(2).enumerated() {
let row = index / columns
let column = index % columns
layer[row][column] = Double(number)!
}
// evens are considered the neurons, odds are the bias
if index % 2 == 0 {
layers.append(layer)
}
else {
biases.append(layer)
}
}
}
// MARK: - Feeding, Cost, Training
public func feed(inputs: [Double]) -> Matrix {
let (_, a) = internalFeed(inputs: inputs)
return a.last!
}
public func cost(batchInputs: [[Double]], batchExpectedOutputs: [[Double]]) -> Double {
var runningCostSum: Double = 0
// calculate cost for each sample
for sampleIndex in 0..<batchInputs.count {
// get inputs/expected outputs
let inputs = batchInputs[sampleIndex]
let expectedOutputs = batchExpectedOutputs[sampleIndex]
let expectedOutputsMatrix = Matrix(elements: [expectedOutputs]).transpose()
// calculate cost, add to runnign sum
let calculatedOutput = feed(inputs: inputs)
let sampleCost = cost(calculatedOutput, expectedOutputsMatrix)
runningCostSum += sampleCost
}
// take average cost
return runningCostSum / Double(batchInputs.count)
}
public func batchTrain(batchInputs: [[Double]], batchExpectedOutputs: [[Double]], η: Double) {
// make sure we have something to train
guard batchInputs.count > 0,
batchInputs.count == batchExpectedOutputs.count else {
return
}
var weightGradients: [Matrix]!
var biasGradients: [Matrix]!
// sum gradients from each sample
for sampleIndex in 0..<batchInputs.count {
let inputs = batchInputs[sampleIndex]
let expectedOutputs = batchExpectedOutputs[sampleIndex]
let (sampleWeightGradients, sampleBiasGradients) = calculateGradients(inputs: inputs, expectedOutputs: expectedOutputs)
if weightGradients != nil {
// add the new gradients to the existing ones (later used to take gradient avergae)
for layerIndex in 0..<sampleWeightGradients.count {
weightGradients[layerIndex] = weightGradients[layerIndex] + sampleWeightGradients[layerIndex]
biasGradients[layerIndex] = biasGradients[layerIndex] + sampleBiasGradients[layerIndex]
}
}
else {
weightGradients = sampleWeightGradients
biasGradients = sampleBiasGradients
}
}
let batchSize = Double(batchInputs.count)
for layerIndex in 0..<layers.count {
let layerWeightGradients = weightGradients[layerIndex] / batchSize
let layerBiasGradients = biasGradients[layerIndex] / batchSize
// update weights
layers[layerIndex] = layers[layerIndex] - (layerWeightGradients * η)
// update biases
biases[layerIndex] = biases[layerIndex] - (layerBiasGradients * η)
}
}
public func train(inputs: [Double], expectedOutputs: [Double], η: Double) {
let (weightGradients, biasGradients) = calculateGradients(inputs: inputs, expectedOutputs: expectedOutputs)
print(weightGradients)
for layerIndex in 0..<layers.count {
// update weights
layers[layerIndex] = layers[layerIndex] - (weightGradients[layerIndex] * η)
// update biases
biases[layerIndex] = biases[layerIndex] - (biasGradients[layerIndex] * η)
}
}
// MARK: - Back propagation calculations
private func calculateGradients(inputs: [Double], expectedOutputs: [Double]) -> (weightGradients: [Matrix], biasGradients: [Matrix]) {
// our return variables
var allWeightGradients: [Matrix] = []
var allBiasGradients: [Matrix] = []
// forward pass
let expectedOutput = Matrix(elements: [expectedOutputs]).transpose()
var (allActivations, allTransformedActivations) = internalFeed(inputs: inputs)
// backpropagate
let errors = calculateErrors(allActivations: allActivations, allTransformedActivations: allTransformedActivations, expectedOutput: expectedOutput)
allTransformedActivations.insert(Matrix(elements: [inputs]).transpose(), at: 0)
for (index, error) in errors.enumerated() {
// calculate weight gradients
let layer = layers[index]
let weightGradients = Matrix(rows: layer.rows, columns: layer.columns, repeating: 0)
for row in 0..<weightGradients.rows {
for col in 0..<weightGradients.columns {
weightGradients[row][col] = error[row][0] * allTransformedActivations[index][col][0]
}
}
// bias gradients are exactly equal to errors
let biasGradients = error
// add to array
allWeightGradients.append(weightGradients)
allBiasGradients.append(biasGradients)
}
return (allWeightGradients, allBiasGradients)
}
private func calculateErrors(allActivations: [Matrix], allTransformedActivations: [Matrix], expectedOutput: Matrix) -> [Matrix] {
// cost error
let activationDerivative = allActivations.last!.elementMap(transform: self.σPrime)
let outputError = costPrime(allTransformedActivations.last!, expectedOutput).elementwiseProduct(matrix: activationDerivative)
// layer errors
var errors = [outputError]
for index in (0..<(layers.count - 1)).reversed() {
let previousError = errors.last!
let layer = layers[index + 1].transpose()
let activation = allActivations[index]
let activationTransformationPrime = activation.elementMap(transform: self.σPrime)
let newError = (layer * previousError).elementwiseProduct(matrix: activationTransformationPrime)
errors.append(newError)
}
return errors.reversed()
}
// MARK: - Forward propagation calculator
private func internalFeed(inputs: [Double]) -> (z: [Matrix], a: [Matrix]) {
var z: [Matrix] = []
var a: [Matrix] = []
var output = Matrix(elements: [inputs]).transpose()
for (index, layer) in layers.enumerated() {
output = (layer * output) + biases[index]
z.append(output)
output = output.elementMap(transform: self.σ)
a.append(output)
}
return (z, a)
}
}