tf.test.compute_gradient | TensorFlow v2.16.1 (original) (raw)
tf.test.compute_gradient
Stay organized with collections Save and categorize content based on your preferences.
Computes the theoretical and numeric Jacobian of f
.
tf.test.compute_gradient(
f, x, delta=None
)
With y = f(x), computes the theoretical and numeric Jacobian dy/dx.
Args | |
---|---|
f | the function. |
x | the arguments for the function as a list or tuple of values convertible to a Tensor. |
delta | (optional) perturbation used to compute numeric Jacobian. |
Returns |
---|
A pair of lists, where the first is a list of 2-d numpy arrays representing the theoretical Jacobians for each argument, and the second list is the numerical ones. Each 2-d array has "y_size" rows and "x_size" columns where "x_size" is the number of elements in the corresponding argument and "y_size" is the number of elements in f(x). |
Raises | |
---|---|
ValueError | If result is empty but the gradient is nonzero. |
ValueError | If x is not list, but any other type. |
Example:
@tf.function
def test_func(x):
return x*x
class MyTest(tf.test.TestCase):
``
def test_gradient_of_test_func(self):
theoretical, numerical = tf.test.compute_gradient(test_func, [1.0])
# ((array([[2.]], dtype=float32),),
# (array([[2.000004]], dtype=float32),))
self.assertAllClose(theoretical, numerical)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.