This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathLearningRateSchedule.swift
171 lines (152 loc) · 5.87 KB
/
LearningRateSchedule.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
/// The abstract shape of a function mapping `Float` into `Float`
public struct Shape {
/// Creates an instance representing the shape of the values of `curve` over the domain 0...1.
public init(_ curve: @escaping (Float) -> Float) {
self.curve = curve
curveSampledAt.0 = curve(0)
curveSampledAt.1 = curve(1)
precondition(
curveSampledAt.0 != curveSampledAt.1,
"Curve must have distinct values at 0 and 1.")
}
/// Returns a function `f` such that `f(domain.lowerBound) == startResult` and
/// `f(domain.upperBound) == endResult`, with the shape of intermediate values defined by `self`.
///
/// The `curve` with which `self` was created is sampled between 0 and 1 and scaled linearly to
/// fit the constraints.
public func projected(
intoDomain domain: ClosedRange<Float>, startResult: Float, endResult: Float
)
-> (Float) -> Float
{
if domain.lowerBound == domain.upperBound {
// This kind of projection can be useful for modeling discontinuities.
return {
x in
x == domain.lowerBound ? startResult : endResult
}
}
let domainSize = domain.upperBound - domain.lowerBound
let rangeScale = (endResult - startResult) / (curveSampledAt.1 - curveSampledAt.0)
return { x in
let domainFraction = (x - domain.lowerBound) / domainSize
return (curve(domainFraction) - curveSampledAt.0) * rangeScale + startResult
}
}
/// A function describing the shape, when sampled between 0.0 and 1.0
private let curve: (Float) -> Float
/// Samples of `curve` taken at 0 and 1.
private let curveSampledAt: (Float, Float)
}
/// A mapping from step number to learning rate, expressed as a collection of learning rates, and as
/// a callable “function instance”.
public struct LearningRateSchedule {
/// A fragment of the schedule, when paired with known start step
fileprivate typealias Segment = (endStep: Int, rateAtStep: (Int) -> Float)
/// The entire representation of self.
///
/// Always contains at least one segment with an endStep of 0.
private var segments: [Segment]
/// Creates a schedule that begins at `startRate`.
public init(startRate: Float) {
segments = [
(
endStep: 0,
rateAtStep: { _ in
startRate
}
),
]
}
/// Returns the learning rate at step `n`.
///
/// For the step that connects the neighbor segments, always return the
/// value at the previous segment.
///
/// - Precondition: `n >= 0 && n < count`
/// - Complexity: O(log(count))
public func callAsFunction(_ n: Int) -> Float {
precondition(n >= 0)
precondition(n < count)
let p = segments.partitionPoint { $0.endStep >= n }
return segments[p].rateAtStep(n)
}
/// Appends an `n`-step segment with shape `s` start rate `startRate` and end rate `endRate`
///
/// The start rate of the appended segment is `startRate` if provided, or the end rate of the last
/// segment appended, or if `self.isEmpty`, the `startRate` with which `self` was initialized.
///
/// If there is a discontinous rate jump at connecting step of neighbor segments, use rate of
/// the first segment as the rate at that step, and that of the second segment is only used to
/// compute rates at later steps in second segment.
///
/// - Precondition: n > 0
public mutating func appendSegment(
stepCount n: Int, shape s: Shape, startRate: Float? = nil, endRate: Float
) {
precondition(n >= 0)
let newEnd = count - 1 + n - 1
let lastRate: Float = segments.last!.rateAtStep(segments.last!.endStep)
let curve = s.projected(
intoDomain: Float(count - 1)...Float(newEnd),
startResult: startRate ?? lastRate, endResult: endRate)
segments.append((endStep: newEnd, rateAtStep: { curve(Float($0)) }))
}
}
extension LearningRateSchedule: BidirectionalCollection {
/// An element position.
public struct Index {
/// The absolute step number of the element at `self`.
fileprivate let step: Int
/// The position of the segment that generates the element at `self`.
fileprivate let segment: Array<Segment>.Index
}
/// The position of the first element.
public var startIndex: Index { Index(step: 0, segment: 0) }
/// The position one past the last element.
public var endIndex: Index { Index(step: count, segment: segments.count) }
/// The number of elements in `self`.
public var count: Int { segments.last!.endStep + 1 }
/// Returns the element at `i`.
public subscript(i: Index) -> Float {
return segments[i.segment].rateAtStep(i.step)
}
/// Returns the position in `self` following `i`.
public func index(after i: Index) -> Index {
let newStep = i.step + 1
let newSegment = i.segment + (newStep > segments[i.segment].endStep ? 1 : 0)
return Index(step: newStep, segment: newSegment)
}
/// Returns the position in `self` preceding `i`.
public func index(before i: Index) -> Index {
let newSegment = i.segment - (i.step == segments[i.segment - 1].endStep ? 1 : 0)
return Index(step: i.step - 1, segment: newSegment)
}
}
extension LearningRateSchedule.Index: Comparable {
public static func == (l: Self, r: Self) -> Bool { return l.step == r.step }
public static func < (l: Self, r: Self) -> Bool { return l.step < r.step }
}
extension Collection {
/// Returns the index of the first element that matches the predicate.
///
/// The collection must already be partitioned according to the predicate, as if
/// `self.partition(by: predicate)` had already been called.
func partitionPoint(
where predicate: (Element) throws -> Bool
) rethrows -> Index {
var n = distance(from: startIndex, to: endIndex)
var l = startIndex
while n > 0 {
let half = n / 2
let mid = index(l, offsetBy: half)
if try predicate(self[mid]) {
n = half
} else {
l = index(after: mid)
n -= half + 1
}
}
return l
}
}