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

Commit ffba693

Browse files
bartchr808rxwei
authored andcommitted
Fix the warnings and errors from Cotangent deprecation. (#122)
This PR is parallel to the following [change](swiftlang/swift#24825) which deprecated `Cotangent` and removed the `tangentVector(from:`) method.
1 parent 09ce86a commit ffba693

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Sources/DeepLearning/Layer.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ public extension Layer {
5151
@differentiating(inferring(from:))
5252
@usableFromInline
5353
internal func _vjpInferring(from input: Input)
54-
-> (value: Output, pullback: (Output.CotangentVector)
55-
-> (CotangentVector, Input.CotangentVector)) {
54+
-> (value: Output, pullback: (Output.TangentVector)
55+
-> (TangentVector, Input.TangentVector)) {
5656
return withLearningPhase(LearningPhase.inference) {
5757
let (output, pullback) = appliedForBackpropagation(to: input)
5858
return (output, { v in pullback(v) })
5959
}
6060
}
6161

62-
typealias Backpropagator = (_ direction: Output.CotangentVector)
63-
-> (layerGradient: CotangentVector, inputGradient: Input.CotangentVector)
62+
typealias Backpropagator = (_ direction: Output.TangentVector)
63+
-> (layerGradient: TangentVector, inputGradient: Input.TangentVector)
6464

6565
/// Returns the inference output and the backpropagation function obtained from applying the
6666
/// layer to the given input.
@@ -728,7 +728,7 @@ public struct BatchNorm<Scalar: TensorFlowFloatingPoint>: Layer {
728728
@usableFromInline
729729
func _vjpApplied(to input: Tensor<Scalar>) ->
730730
(Tensor<Scalar>, (Tensor<Scalar>) ->
731-
(BatchNorm<Scalar>.CotangentVector, Tensor<Scalar>)) {
731+
(BatchNorm<Scalar>.TangentVector, Tensor<Scalar>)) {
732732
switch Context.local.learningPhase {
733733
case .training:
734734
return valueWithPullback(at: input) {
@@ -1086,7 +1086,7 @@ public struct Dropout<Scalar: TensorFlowFloatingPoint>: Layer {
10861086
@usableFromInline
10871087
func _vjpApplied(to input: Tensor<Scalar>) ->
10881088
(Tensor<Scalar>, (Tensor<Scalar>) ->
1089-
(Dropout<Scalar>.CotangentVector, Tensor<Scalar>)) {
1089+
(Dropout<Scalar>.TangentVector, Tensor<Scalar>)) {
10901090
switch Context.local.learningPhase {
10911091
case .training:
10921092
return valueWithPullback(at: input) {
@@ -1435,8 +1435,8 @@ public struct RNN<Cell: RNNCell>: Layer {
14351435
internal func _vjpCall(
14361436
_ inputs: [Cell.TimeStepInput], initialState: Cell.State
14371437
) -> ([Cell.TimeStepOutput],
1438-
(Array<Cell.TimeStepOutput>.CotangentVector)
1439-
-> (CotangentVector, Array<Cell.TimeStepInput>.CotangentVector)) {
1438+
(Array<Cell.TimeStepOutput>.TangentVector)
1439+
-> (TangentVector, Array<Cell.TimeStepInput>.TangentVector)) {
14401440
let timeStepCount = inputs.count
14411441
var currentHiddenState = cell.zeroState
14421442
var timeStepOutputs: [Cell.TimeStepOutput] = []
@@ -1454,9 +1454,9 @@ public struct RNN<Cell: RNNCell>: Layer {
14541454
return (timeStepOutputs, { 𝛁outputs in
14551455
precondition(𝛁outputs.base.count == timeStepCount,
14561456
"The number of output gradients must equal the number of time steps")
1457-
var 𝛁cell = Cell.CotangentVector.zero
1458-
var 𝛁state = Cell.State.CotangentVector.zero
1459-
var reversed𝛁inputs: [Cell.TimeStepInput.CotangentVector] = []
1457+
var 𝛁cell = Cell.TangentVector.zero
1458+
var 𝛁state = Cell.State.TangentVector.zero
1459+
var reversed𝛁inputs: [Cell.TimeStepInput.TangentVector] = []
14601460
reversed𝛁inputs.reserveCapacity(timeStepCount)
14611461
for (𝛁output, backpropagator) in zip(𝛁outputs.base, backpropagators).reversed() {
14621462
let (new𝛁cell, 𝛁input) = backpropagator(.init(output: 𝛁output, state: 𝛁state))

Sources/DeepLearning/Optimizer.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public protocol Optimizer {
3030
/// Updates the specified differentiable variables along the specified
3131
/// direction.
3232
mutating func update(_ variables: inout Model.AllDifferentiableVariables,
33-
along direction: Model.CotangentVector)
33+
along direction: Model.TangentVector)
3434
}
3535

3636
fileprivate extension Tensor where Scalar: Numeric {
@@ -46,7 +46,7 @@ fileprivate extension Tensor where Scalar: Numeric {
4646
/// Reference: ["Adam - A Method for Stochastic Optimization"](
4747
/// https://arxiv.org/abs/1412.6980v8)
4848
public class Adam<Model: Layer>: Optimizer
49-
where Model.AllDifferentiableVariables == Model.CotangentVector {
49+
where Model.AllDifferentiableVariables == Model.TangentVector {
5050
/// The learning rate.
5151
public var learningRate: Float
5252
/// A coefficient used to calculate the first and second moments of
@@ -142,7 +142,7 @@ public class Adam<Model: Layer>: Optimizer
142142
/// Reference: ["rmsprop: Divide the gradient by a running average of its recent magnitude"](
143143
/// http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf)
144144
public class RMSProp<Model: Layer>: Optimizer
145-
where Model.AllDifferentiableVariables == Model.CotangentVector {
145+
where Model.AllDifferentiableVariables == Model.TangentVector {
146146
/// The learning rate.
147147
public var learningRate: Float
148148
// TODO: Document `rho`. Keras doesn't document `rho`.
@@ -182,7 +182,7 @@ public class RMSProp<Model: Layer>: Optimizer
182182

183183

184184
public func update(_ model: inout Model.AllDifferentiableVariables,
185-
along direction: Model.CotangentVector) {
185+
along direction: Model.TangentVector) {
186186
step += 1
187187
let learningRate = self.learningRate * 1 / (1 + decay * Float(step))
188188
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
@@ -206,7 +206,7 @@ public class RMSProp<Model: Layer>: Optimizer
206206
/// An optimizer that implements stochastic gradient descent, with support for momentum, learning
207207
/// rate decay, and Nesterov momentum.
208208
public class SGD<Model: Layer>: Optimizer
209-
where Model.AllDifferentiableVariables == Model.CotangentVector {
209+
where Model.AllDifferentiableVariables == Model.TangentVector {
210210
/// The learning rate.
211211
public var learningRate: Float
212212
/// The momentum factor. It accelerates stochastic gradient descent in the relevant direction
@@ -246,7 +246,7 @@ public class SGD<Model: Layer>: Optimizer
246246
}
247247

248248
public func update(_ model: inout Model.AllDifferentiableVariables,
249-
along direction: Model.CotangentVector) {
249+
along direction: Model.TangentVector) {
250250
step += 1
251251
let learningRate = self.learningRate * 1 / (1 + decay * Float(step))
252252
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
@@ -294,7 +294,7 @@ public class RiemannSGD<Model: Layer, Scalar: FloatingPoint>: Optimizer
294294
}
295295

296296
public func update(_ model: inout Model.AllDifferentiableVariables,
297-
along direction: Model.CotangentVector) {
298-
model = model.moved(along: learningRate * (.zero - model.tangentVector(from: direction)))
297+
along direction: Model.TangentVector) {
298+
model = model.moved(along: learningRate * (.zero - direction))
299299
}
300300
}

0 commit comments

Comments
 (0)