@@ -21,14 +21,13 @@ public protocol Optimizer {
21
21
associatedtype Scalar : FloatingPoint
22
22
var learningRate : Scalar { get }
23
23
mutating func update( _ variables: inout Model . AllDifferentiableVariables ,
24
- along gradient : Model . CotangentVector )
24
+ along vector : Model . CotangentVector )
25
25
}
26
26
27
27
// MARK: - Key-path based optimizers
28
28
29
29
public class Adam < Model: Layer , Scalar: TensorFlowFloatingPoint > : Optimizer
30
- where Model. AllDifferentiableVariables: AdditiveArithmetic ,
31
- Model. AllDifferentiableVariables == Model . CotangentVector {
30
+ where Model. AllDifferentiableVariables == Model . CotangentVector {
32
31
public let learningRate : Scalar
33
32
public var beta1 : Scalar
34
33
public var beta2 : Scalar
@@ -59,7 +58,7 @@ public class Adam<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
59
58
private var secondMoments = Model . AllDifferentiableVariables. zero
60
59
61
60
public func update( _ model: inout Model . AllDifferentiableVariables ,
62
- along gradient : Model . AllDifferentiableVariables ) {
61
+ along vector : Model . AllDifferentiableVariables ) {
63
62
step += 1
64
63
let learningRate = self . learningRate * 1 / ( 1 + decay * step)
65
64
let stepSize = learningRate * ( sqrt ( 1 - pow( beta2, step) ) / ( 1 - pow( beta1, step) ) )
@@ -76,8 +75,7 @@ public class Adam<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
76
75
}
77
76
78
77
public class RMSProp < Model: Layer , Scalar: TensorFlowFloatingPoint > : Optimizer
79
- where Model. AllDifferentiableVariables: AdditiveArithmetic ,
80
- Model. AllDifferentiableVariables == Model . CotangentVector {
78
+ where Model. AllDifferentiableVariables == Model . CotangentVector {
81
79
public let learningRate : Scalar
82
80
public let rho : Scalar
83
81
public let epsilon : Scalar
@@ -103,7 +101,7 @@ public class RMSProp<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
103
101
private var alpha = Model . AllDifferentiableVariables. zero
104
102
105
103
public func update( _ model: inout Model . AllDifferentiableVariables ,
106
- along gradient : Model . CotangentVector ) {
104
+ along vector : Model . CotangentVector ) {
107
105
step += 1
108
106
let learningRate = self . learningRate * 1 / ( 1 + decay * step)
109
107
for kp in model. recursivelyAllWritableKeyPaths ( to: Tensor< Scalar> . self ) {
@@ -116,8 +114,7 @@ public class RMSProp<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
116
114
}
117
115
118
116
public class SGD < Model: Layer , Scalar: TensorFlowFloatingPoint > : Optimizer
119
- where Model. AllDifferentiableVariables: AdditiveArithmetic ,
120
- Model. AllDifferentiableVariables == Model . CotangentVector {
117
+ where Model. AllDifferentiableVariables == Model . CotangentVector {
121
118
public let learningRate : Scalar
122
119
public let momentum : Scalar
123
120
public let decay : Scalar
@@ -143,7 +140,7 @@ public class SGD<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
143
140
private var velocity = Model . AllDifferentiableVariables. zero
144
141
145
142
public func update( _ model: inout Model . AllDifferentiableVariables ,
146
- along gradients : Model . CotangentVector ) {
143
+ along vectors : Model . CotangentVector ) {
147
144
step += 1
148
145
let learningRate = self . learningRate * 1 / ( 1 + decay * step)
149
146
for kp in model. recursivelyAllWritableKeyPaths ( to: Tensor< Scalar> . self ) {
@@ -170,7 +167,7 @@ public class RiemannSGD<Model: Layer, Scalar: FloatingPoint>: Optimizer
170
167
}
171
168
172
169
public func update( _ model: inout Model . AllDifferentiableVariables ,
173
- along gradient : Model . CotangentVector ) {
170
+ along vector : Model . CotangentVector ) {
174
171
model = model. moved ( along: learningRate * ( . zero - model. tangentVector ( from: gradient) ) )
175
172
}
176
173
}
0 commit comments