diff --git a/RELEASES.md b/RELEASES.md index 03093ac6f..5752cc56c 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,8 @@ #### Closed issues +- Fix TensorFlow custom gradients ignoring the upstream gradient, so scaling an OT loss now scales its supplied gradients consistently. + - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) - Fix an out-of-bounds read in `ot.bsp.merge_bijections`: the number of plans was taken from `perms.shape[1]` (the number of samples) instead of `perms.shape[0]`, so merging fewer plans than samples read past the array and merging more silently dropped plans. A `perms` outside the documented `(n_plans, n_samples)` layout, an empty plan stack and rows that are not permutations are now rejected (PR #911, Issue #910) diff --git a/ot/backend.py b/ot/backend.py index b74377041..6d17b3ef3 100644 --- a/ot/backend.py +++ b/ot/backend.py @@ -3239,7 +3239,7 @@ def set_gradients(self, val, inputs, grads): @tf.custom_gradient def tmp(input): def grad(upstream): - return grads + return tf.nest.map_structure(lambda g: upstream * g, grads) return val, grad diff --git a/test/test_backend.py b/test/test_backend.py index 0b784aa62..7f111bcec 100644 --- a/test/test_backend.py +++ b/test/test_backend.py @@ -1010,6 +1010,43 @@ def test_no_cuda_context_for_cpu_only_work(): ) +@pytest.mark.skipif(not tf, reason="TensorFlow not installed") +@pytest.mark.parametrize("coefficient", [0.0, 1.0, -2.0, 7.0]) +@pytest.mark.parametrize("compiled", [False, True]) +def test_tensorflow_set_gradients_chain_rule(coefficient, compiled): + nx = ot.backend.TensorflowBackend() + x = tf.constant([1.0, 2.0], dtype=tf.float64) + y = tf.constant(3.0, dtype=tf.float64) + + def differentiate(x, y): + with tf.GradientTape() as tape: + tape.watch((x, y)) + value = nx.set_gradients( + tf.constant(5.0, dtype=x.dtype), (x, y), (2 * x, 3 * y) + ) + loss = coefficient * value + return value, tape.gradient(loss, (x, y)) + + if compiled: + differentiate = tf.function(differentiate) + value, gradients = differentiate(x, y) + np.testing.assert_allclose(value, 5.0) + np.testing.assert_allclose(gradients[0], coefficient * 2 * x) + np.testing.assert_allclose(gradients[1], coefficient * 3 * y) + + +@pytest.mark.skipif(not tf, reason="TensorFlow not installed") +@pytest.mark.parametrize("coefficient", [0.0, -2.0, 7.0]) +def test_tensorflow_emd2_scaled_gradient(coefficient): + a = tf.constant([0.5, 0.5], dtype=tf.float64) + cost = tf.constant([[0.0, 2.0], [3.0, 0.0]], dtype=tf.float64) + with tf.GradientTape() as tape: + tape.watch(cost) + loss = coefficient * ot.emd2(a, a, cost) + gradient = tape.gradient(loss, cost) + np.testing.assert_allclose(gradient, coefficient * np.diag([0.5, 0.5])) + + @pytest.mark.parametrize("power,expected,second", [(0, 3., 0.), (1, 6., 3.), (2, 12., 12.)]) def test_jax_supplied_gradients(power, expected, second): jax = pytest.importorskip("jax")