Skip to content

Alternative to run_all_in_graph_and_eager_mode #1288

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

Merged
merged 5 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions tensorflow_addons/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ py_library(
name = "tensorflow_addons",
data = [
"__init__.py",
"conftest.py",
"options.py",
"register.py",
"version.py",
Expand Down
5 changes: 5 additions & 0 deletions tensorflow_addons/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from tensorflow_addons.utils.test_utils import maybe_run_functions_eagerly # noqa: F401

# fixtures present in this file will be available
# when running tests and can be referenced with strings
# https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions
71 changes: 37 additions & 34 deletions tensorflow_addons/losses/focal_loss_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ def to_logit(self, prob):
logit = np.log(prob / (1.0 - prob))
return logit

def log10(self, x):
numerator = tf.math.log(x)
denominator = tf.math.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator

# Test with logits
def test_with_logits(self):
# predictiions represented as logits
Expand Down Expand Up @@ -87,35 +82,7 @@ def test_with_logits(self):
)

# order_of_ratio = np.power(10, np.floor(np.log10(bce/FL)))
order_of_ratio = tf.pow(10.0, tf.math.floor(self.log10(bce / fl)))
pow_values = tf.constant([1000, 100, 10, 10, 100, 1000])
self.assertAllClose(order_of_ratio, pow_values)

# Test without logits
def test_without_logits(self):
# predictiions represented as logits
prediction_tensor = tf.constant(
[[0.97], [0.91], [0.73], [0.27], [0.09], [0.03]], tf.float32
)
# Ground truth
target_tensor = tf.constant([[1], [1], [1], [0], [0], [0]], tf.float32)

fl = sigmoid_focal_crossentropy(
y_true=target_tensor, y_pred=prediction_tensor, alpha=None, gamma=None
)
bce = tf.reduce_sum(
K.binary_crossentropy(target_tensor, prediction_tensor), axis=-1
)

# When alpha and gamma are None, it should be equal to BCE
self.assertAllClose(fl, bce)

# When gamma==2.0
fl = sigmoid_focal_crossentropy(
y_true=target_tensor, y_pred=prediction_tensor, alpha=None, gamma=2.0
)

order_of_ratio = tf.pow(10.0, tf.math.floor(self.log10(bce / fl)))
order_of_ratio = tf.pow(10.0, tf.math.floor(log10(bce / fl)))
pow_values = tf.constant([1000, 100, 10, 10, 100, 1000])
self.assertAllClose(order_of_ratio, pow_values)

Expand All @@ -129,5 +96,41 @@ def test_keras_model_compile(self):
model.compile(loss="Addons>sigmoid_focal_crossentropy")


def log10(x):
numerator = tf.math.log(x)
denominator = tf.math.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator


# Test without logits
@pytest.mark.usefixtures("maybe_run_functions_eagerly")
def test_without_logits():
# predictiions represented as logits
prediction_tensor = tf.constant(
[[0.97], [0.91], [0.73], [0.27], [0.09], [0.03]], tf.float32
)
# Ground truth
target_tensor = tf.constant([[1], [1], [1], [0], [0], [0]], tf.float32)

fl = sigmoid_focal_crossentropy(
y_true=target_tensor, y_pred=prediction_tensor, alpha=None, gamma=None
)
bce = tf.reduce_sum(
K.binary_crossentropy(target_tensor, prediction_tensor), axis=-1
)

# When alpha and gamma are None, it should be equal to BCE
assert np.allclose(fl, bce)

# When gamma==2.0
fl = sigmoid_focal_crossentropy(
y_true=target_tensor, y_pred=prediction_tensor, alpha=None, gamma=2.0
)

order_of_ratio = tf.pow(10.0, tf.math.floor(log10(bce / fl)))
pow_values = tf.constant([1000, 100, 10, 10, 100, 1000])
assert np.allclose(order_of_ratio, pow_values)


if __name__ == "__main__":
sys.exit(pytest.main([__file__]))
23 changes: 12 additions & 11 deletions tensorflow_addons/metrics/cohens_kappa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,6 @@ def test_with_sparse_labels(self):
self.evaluate(obj.update_state(y_true, y_pred))
self.assertAllClose(0.19999999, obj.result())

def test_with_ohe_labels(self):
y_true = np.array([4, 4, 3, 4], dtype=np.int32)
y_true = tf.keras.utils.to_categorical(y_true, num_classes=5)
y_pred = np.array([4, 4, 1, 2], dtype=np.int32)

obj = CohenKappa(num_classes=5, sparse_labels=False)
self.evaluate(tf.compat.v1.variables_initializer(obj.variables))

self.evaluate(obj.update_state(y_true, y_pred))
self.assertAllClose(0.19999999, obj.result())

def test_keras_binary_reg_model(self):
kp = CohenKappa(num_classes=2)
inputs = tf.keras.layers.Input(shape=(10,))
Expand Down Expand Up @@ -231,5 +220,17 @@ def test_keras_multiclass_classification_model(self):
model.fit(x, y, epochs=1, verbose=0, batch_size=32)


@pytest.mark.usefixtures("maybe_run_functions_eagerly")
def test_with_ohe_labels():
y_true = np.array([4, 4, 3, 4], dtype=np.int32)
y_true = tf.keras.utils.to_categorical(y_true, num_classes=5)
y_pred = np.array([4, 4, 1, 2], dtype=np.int32)

obj = CohenKappa(num_classes=5, sparse_labels=False)

obj.update_state(y_true, y_pred)
np.testing.assert_allclose(0.19999999, obj.result().numpy())


if __name__ == "__main__":
sys.exit(pytest.main([__file__]))
3 changes: 3 additions & 0 deletions tensorflow_addons/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ py_library(
"resource_loader.py",
"types.py",
]),
data = [
"//tensorflow_addons:conftest.py",
],
)

py_test(
Expand Down
15 changes: 15 additions & 0 deletions tensorflow_addons/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time
import unittest

import pytest
import tensorflow as tf

# TODO: find public API alternative to these
Expand Down Expand Up @@ -182,3 +183,17 @@ def time_all_functions(cls):
):
setattr(cls, name, time_function(method))
return cls


def finalizer():
tf.config.experimental_run_functions_eagerly(False)


@pytest.fixture(scope="function", params=["eager_mode", "tf_function"])
def maybe_run_functions_eagerly(request):
if request.param == "eager_mode":
tf.config.experimental_run_functions_eagerly(True)
elif request.param == "tf_function":
tf.config.experimental_run_functions_eagerly(False)

request.addfinalizer(finalizer)