Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 8b922f5

Browse files
eaplataniosrxwei
authored andcommitted
Rename of 'vector' to 'direction' in the optimizers. (#18)
1 parent 8bb9b1a commit 8b922f5

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Sources/DeepLearning/Optimizer.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public protocol Optimizer {
2121
associatedtype Scalar: FloatingPoint
2222
var learningRate: Scalar { get }
2323
mutating func update(_ variables: inout Model.AllDifferentiableVariables,
24-
along vector: Model.CotangentVector)
24+
along direction: Model.CotangentVector)
2525
}
2626

2727
// MARK: - Key-path based optimizers
@@ -60,16 +60,16 @@ public class Adam<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
6060
private var secondMoments = Model.AllDifferentiableVariables.zero
6161

6262
public func update(_ model: inout Model.AllDifferentiableVariables,
63-
along vector: Model.AllDifferentiableVariables) {
63+
along direction: Model.AllDifferentiableVariables) {
6464
step += 1
6565
let learningRate = self.learningRate * 1 / (1 + decay * step)
6666
let stepSize = learningRate * (sqrt(1 - pow(beta2, step)) / (1 - pow(beta1, step)))
6767
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Scalar>.self) {
6868
firstMoments[keyPath: kp] =
69-
firstMoments[keyPath: kp] * beta1 + (1 - beta1) * vector[keyPath: kp]
69+
firstMoments[keyPath: kp] * beta1 + (1 - beta1) * direction[keyPath: kp]
7070
secondMoments[keyPath: kp] =
7171
secondMoments[keyPath: kp] * beta2 + (1 - beta2) *
72-
vector[keyPath: kp] * vector[keyPath: kp]
72+
direction[keyPath: kp] * direction[keyPath: kp]
7373
model[keyPath: kp] -=
7474
stepSize * firstMoments[keyPath: kp] / (sqrt(secondMoments[keyPath: kp]) + epsilon)
7575
}
@@ -105,14 +105,14 @@ public class RMSProp<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
105105
private var alpha = Model.AllDifferentiableVariables.zero
106106

107107
public func update(_ model: inout Model.AllDifferentiableVariables,
108-
along vector: Model.CotangentVector) {
108+
along direction: Model.CotangentVector) {
109109
step += 1
110110
let learningRate = self.learningRate * 1 / (1 + decay * step)
111111
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Scalar>.self) {
112112
alpha[keyPath: kp] =
113-
rho * alpha[keyPath: kp] + (1 - rho) * pow(vector[keyPath: kp], 2)
113+
rho * alpha[keyPath: kp] + (1 - rho) * pow(direction[keyPath: kp], 2)
114114
model[keyPath: kp] -=
115-
learningRate * vector[keyPath: kp] / (sqrt(alpha[keyPath: kp]) + epsilon)
115+
learningRate * direction[keyPath: kp] / (sqrt(alpha[keyPath: kp]) + epsilon)
116116
}
117117
}
118118
}
@@ -146,15 +146,15 @@ public class SGD<Model: Layer, Scalar: TensorFlowFloatingPoint>: Optimizer
146146
private var velocity = Model.AllDifferentiableVariables.zero
147147

148148
public func update(_ model: inout Model.AllDifferentiableVariables,
149-
along vector: Model.CotangentVector) {
149+
along direction: Model.CotangentVector) {
150150
step += 1
151151
let learningRate = self.learningRate * 1 / (1 + decay * step)
152152
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Scalar>.self) {
153153
velocity[keyPath: kp] =
154-
momentum * velocity[keyPath: kp] - learningRate * vector[keyPath: kp]
154+
momentum * velocity[keyPath: kp] - learningRate * direction[keyPath: kp]
155155
if nesterov {
156156
model[keyPath: kp] +=
157-
momentum * velocity[keyPath: kp] - learningRate * vector[keyPath: kp]
157+
momentum * velocity[keyPath: kp] - learningRate * direction[keyPath: kp]
158158
} else {
159159
model[keyPath: kp] += velocity[keyPath: kp]
160160
}
@@ -177,7 +177,7 @@ public class RiemannSGD<Model: Layer, Scalar: FloatingPoint>: Optimizer
177177
}
178178

179179
public func update(_ model: inout Model.AllDifferentiableVariables,
180-
along vector: Model.CotangentVector) {
181-
model = model.moved(along: learningRate * (.zero - model.tangentVector(from: vector)))
180+
along direction: Model.CotangentVector) {
181+
model = model.moved(along: learningRate * (.zero - model.tangentVector(from: direction)))
182182
}
183183
}

0 commit comments

Comments
 (0)