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

Commit 6ff581e

Browse files
authored
Fix optimizer numerical tests (#804)
* Change gradient to match reference implementation * Disable rounding in print * Update reference implementation results
1 parent 3830c7a commit 6ff581e

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

Tests/TensorFlowTests/OptimizerTests.swift

+13-17
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class OptimizerTests: XCTestCase {
149149
) where Opt.Model == NumericalValues {
150150
var optimizer = optimizer
151151
var values = startingValues
152-
let gradient = NumericalValues.TangentVector(value: [-0.5, 0.1, 3])
152+
let gradient = NumericalValues.TangentVector(value: [-5, 0.1, 0.2])
153153
for _ in 0..<stepCount {
154154
optimizer.update(&values, along: gradient)
155155
}
@@ -159,21 +159,17 @@ class OptimizerTests: XCTestCase {
159159
func testSGDNumerical() {
160160
let values = NumericalValues()
161161
let optimizer = SGD(for: values, learningRate: 1e-3)
162-
// FIXME(TF-759): Investigate large differences with Python reference implementation results:
163-
// `[ 0.49999967, -0.00999999, -0.01999998]`.
164162
testNumericalCorrectness(
165163
optimizer: optimizer, startingValues: values,
166-
expectedValues: [0.49999535, -0.10000112, -3.000017])
164+
expectedValues: [5.0000668 , -0.10000112, -0.20000224])
167165
}
168166

169167
func testRMSPropNumerical() {
170168
let values = NumericalValues()
171169
let optimizer = RMSProp(for: values, learningRate: 1e-3, epsilon: 1e-7)
172-
// FIXME(TF-759): Investigate small differences with Python reference implementation results:
173-
// `[ 1.0091327, -1.0091326, -1.0091326]`.
174170
testNumericalCorrectness(
175171
optimizer: optimizer, startingValues: values,
176-
expectedValues: [1.0091327, -1.0091326, -1.0091327])
172+
expectedValues: [1.0091327, -1.0091326, -1.0091326])
177173
}
178174

179175
func testAdamNumerical() {
@@ -183,17 +179,17 @@ class OptimizerTests: XCTestCase {
183179
// `[ 0.9999907, -0.9999898, -0.9999904]`.
184180
testNumericalCorrectness(
185181
optimizer: optimizer, startingValues: values,
186-
expectedValues: [0.9999906, -0.9999898, -0.99999064])
182+
expectedValues: [0.99999064, -0.9999898, -0.9999905])
187183
}
188184

189185
func testAdaDeltaNumerical() {
190186
let values = NumericalValues()
191187
let optimizer = AdaDelta(for: values, learningRate: 1e-3, epsilon: 1e-7)
192188
// FIXME(TF-759): Investigate small differences with Python reference implementation results:
193-
// `[ 0.00215183, -0.00215151, -0.00215175]`.
189+
// `[ 0.0021518278, -0.0021515056, -0.0021517489]`.
194190
testNumericalCorrectness(
195191
optimizer: optimizer, startingValues: values,
196-
expectedValues: [0.0021518078, -0.002151505, -0.0021518408])
192+
expectedValues: [0.0021518273, -0.002151505, -0.0021517489])
197193
}
198194

199195
func testAMSGradNumerical() {
@@ -203,7 +199,7 @@ class OptimizerTests: XCTestCase {
203199
// `[ 0.9999907, -0.9999898, -0.9999904]`.
204200
testNumericalCorrectness(
205201
optimizer: optimizer, startingValues: values,
206-
expectedValues: [0.9999906, -0.9999898, -0.99999064])
202+
expectedValues: [0.99999064, -0.9999898, -0.9999905])
207203
}
208204

209205
func testAdaMaxNumerical() {
@@ -213,27 +209,27 @@ class OptimizerTests: XCTestCase {
213209
// `[ 0.99999076, -0.99999064, -0.99999064]`.
214210
testNumericalCorrectness(
215211
optimizer: optimizer, startingValues: values,
216-
expectedValues: [0.9999907, -0.99999064, -0.9999907])
212+
expectedValues: [0.9999907, -0.99999064, -0.99999064])
217213
}
218214

219215
func testAdaGradNumerical() {
220216
let values = NumericalValues()
221217
let optimizer = AdaGrad(for: values, learningRate: 1e-3, epsilon: 1e-7)
222-
// FIXME(TF-759): Investigate large differences with Python reference implementation results:
223-
// `[ 0.06179592, -0.05709525, -0.05987222]`.
218+
// FIXME(TF-759): Investigate small differences with Python reference implementation results:
219+
// `[ 0.061795924, -0.057095252, -0.059872225]`.
224220
testNumericalCorrectness(
225221
optimizer: optimizer, startingValues: values,
226-
expectedValues: [0.061354622, -0.057095252, -0.061786927])
222+
expectedValues: [0.06179592, -0.057095252, -0.059872225])
227223
}
228224

229225
func testRAdamNumerical() {
230226
let values = NumericalValues()
231227
let optimizer = RAdam(for: values, learningRate: 1e-3, epsilon: 1e-7)
232-
// FIXME(TF-759): Investigate large differences with Python reference implementation results:
228+
// FIXME(TF-759): Investigate small differences with Python reference implementation results:
233229
// `[ 0.46914074, -0.44463935, -0.44513944]`.
234230
testNumericalCorrectness(
235231
optimizer: optimizer, startingValues: values,
236-
expectedValues: [ 0.44664007, -0.44463903, -0.45914108])
232+
expectedValues: [0.46914074, -0.44463903, -0.44513932])
237233
}
238234

239235
static var allTests = [

Utilities/ReferenceImplementations/optimizers.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tensorflow.keras.optimizers import Adam, Adadelta, Adagrad, Adamax, RMSprop, SGD
1212
from tensorflow_addons.optimizers import RectifiedAdam
1313

14+
np.set_printoptions(precision=None, floatmode="unique")
1415

1516
def test_optimizer(optimizer, step_count=1000):
1617
var = tf.Variable([0, 0, 0], dtype=tf.float32)

0 commit comments

Comments
 (0)