-
Notifications
You must be signed in to change notification settings - Fork 87
/
DeepNetworkOperator.swift
96 lines (82 loc) · 3.18 KB
/
DeepNetworkOperator.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
//
// DeepNetworkOperator.swift
// AIToolbox
//
// Created by Kevin Coble on 6/26/16.
// Copyright © 2016 Kevin Coble. All rights reserved.
//
import Foundation
public enum DeepNetworkOperatorType : Int
{
case convolution2DOperation = 0
case poolingOperation
case feedForwardNetOperation
case nonLinearityOperation
public func getString() ->String
{
switch self {
case .convolution2DOperation:
return "2D Convolution"
case .poolingOperation:
return "Pooling"
case .feedForwardNetOperation:
return "FeedForward NN"
case .nonLinearityOperation:
return "NonLinearity"
}
}
public static func getAllTypes() -> [(name: String, type: DeepNetworkOperatorType)]
{
var raw = 0
var results : [(name: String, type: DeepNetworkOperatorType)] = []
while let type = DeepNetworkOperatorType(rawValue: raw) {
results.append((name: type.getString(), type: type))
raw += 1
}
return results
}
public static func getDeepNetworkOperatorFromDict(_ sourceDictionary: [String: AnyObject]) -> DeepNetworkOperator?
{
if let type = sourceDictionary["operatorType"] as? NSInteger {
if let opType = DeepNetworkOperatorType(rawValue: type) {
if let opDefinition = sourceDictionary["operatorDefinition"] as? [String: AnyObject] {
switch opType {
case .convolution2DOperation:
return Convolution2D(fromDictionary: opDefinition)
case .poolingOperation:
return Pooling(fromDictionary: opDefinition)
case .feedForwardNetOperation:
return DeepNeuralNetwork(fromDictionary: opDefinition)
case .nonLinearityOperation:
return DeepNonLinearity(fromDictionary: opDefinition)
}
}
}
}
return nil
}
}
public protocol DeepNetworkOperator : MLPersistence {
func getType() -> DeepNetworkOperatorType
func getDetails() -> String
func getResultingSize(_ inputSize: DeepChannelSize) -> DeepChannelSize
func initializeParameters()
func feedForward(_ inputs: [Float], inputSize: DeepChannelSize) -> [Float]
func getResults() -> [Float]
func getResultSize() -> DeepChannelSize
func getResultRange() ->(minimum: Float, maximum: Float)
func startBatch()
func backPropogateGradient(_ upStreamGradient: [Float]) -> [Float]
func updateWeights(_ trainingRate : Float, weightDecay: Float)
func gradientCheck(ε: Float, Δ: Float, network: DeepNetwork) -> Bool
}
extension DeepNetworkOperator {
public func getOperationPersistenceDictionary() -> [String : AnyObject] {
var resultDictionary : [String: AnyObject] = [:]
// Set the operator type
resultDictionary["operatorType"] = getType().rawValue as AnyObject?
// Set the definition
resultDictionary["operatorDefinition"] = getPersistenceDictionary() as AnyObject?
return resultDictionary
}
}