Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardized Testing for All Optimizers #2208

Merged
merged 3 commits into from
Nov 1, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tensorflow_addons/optimizers/tests/standard_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
hyang0129 marked this conversation as resolved.
Show resolved Hide resolved
import numpy as np
import tensorflow as tf

from tensorflow_addons import optimizers
import inspect
import sys

classes_to_test = ["RectifiedAdam", "LazyAdam"]


def discover_classes(module, parent):

classes = [
m[1]
for m in inspect.getmembers(module, inspect.isclass)
if issubclass(m[1], parent) and m[0] in classes_to_test
]

return classes
hyang0129 marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
"optimizer", discover_classes(optimizers, tf.keras.optimizers.Optimizer)
)
def test_optimizer_minimize(optimizer):

model = tf.keras.Sequential([tf.keras.Input(shape=[1]), tf.keras.layers.Dense(1)])

x = np.array(np.ones([1]))
y = np.array(np.zeros([1]))

opt = optimizer()
loss = tf.keras.losses.MSE

model.compile(optimizer=opt, loss=loss)

history = model.fit(x, y, batch_size=1, epochs=10)

loss_values = history.history["loss"]

np.testing.assert_array_less(loss_values[-1], loss_values[0])


if __name__ == "__main__":
sys.exit(pytest.main(["standard_test.py"]))

# python -m flake8 tensorflow_addons/optimizers/tests/standard_test.py
# python -m black tensorflow_addons/optimizers/tests/standard_test.py
hyang0129 marked this conversation as resolved.
Show resolved Hide resolved