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

Added 2 missing testcases for Keras switch backend #25250

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions tensorflow/python/keras/backend_test.py
Expand Up @@ -1880,5 +1880,37 @@ def test_get_session_different_graphs(self):
self.assertIsNot(session, keras.backend.get_session())


@test_util.run_all_in_graph_and_eager_modes
class ControlOpsTests(test.TestCase):

def test_function_switch_basics(self):
amitsrivastava78 marked this conversation as resolved.
Show resolved Hide resolved
x = array_ops.constant(2.0)
y = array_ops.constant(3.0)

def xpowy():
return keras.backend.pow(x, y)
def ypowx():
return keras.backend.pow(y, x)

tensor = keras.backend.switch(keras.backend.less(x, y), xpowy, ypowx)
self.assertEqual(keras.backend.eval(tensor), [8.0])

tensor = keras.backend.switch(keras.backend.greater(x, y), xpowy, ypowx)
self.assertEqual(keras.backend.eval(tensor), [9.0])

def test_unequal_rank(self):
x = ops.convert_to_tensor(np.array([[1, 2, 3], [4, 5, 6]]),
dtype='float32')
y = ops.convert_to_tensor(np.array([1, 2, 3]), dtype='float32')
def true_func():
return x

def false_func():
return y

with self.assertRaisesRegexp(ValueError, "Rank of `condition` should be less than"):
keras.backend.switch(keras.backend.equal(x, x), false_func, true_func)


if __name__ == '__main__':
test.main()