MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _test_gradients

Function _test_gradients

tensorflow/python/eager/forwardprop_test.py:100–128  ·  view source on GitHub ↗

Tests forward/backward jacobians of `f`'s [0, `order`)-order gradients.

(testcase,
                    f,
                    primals,
                    order,
                    delta=1e-3,
                    rtol=1e-2,
                    atol=1e-6)

Source from the content-addressed store, hash-verified

98
99
100def _test_gradients(testcase,
101 f,
102 primals,
103 order,
104 delta=1e-3,
105 rtol=1e-2,
106 atol=1e-6):
107 """Tests forward/backward jacobians of `f`'s [0, `order`)-order gradients."""
108 if order < 1:
109 raise ValueError(
110 "`order` should be a positive integer, got '{}'.".format(order))
111 if order > 1:
112 _test_gradients(
113 testcase=testcase,
114 f=_grad(f),
115 primals=primals,
116 order=order - 1,
117 delta=delta,
118 rtol=rtol,
119 atol=atol)
120 sym_jac_back, num_jac = gradient_checker_v2.compute_gradient(
121 f, primals, delta=delta)
122 testcase.assertAllClose(num_jac, sym_jac_back, rtol=rtol, atol=atol)
123 # TODO(b/134972215): compute_gradient should use the definition of a Jacobian
124 # matrix on Wikipedia, then this transpose can go away.
125 sym_jac_fwd = nest.map_structure(array_ops.transpose, _jacfwd(f, primals))
126 testcase.assertAllClose(num_jac, sym_jac_fwd, rtol=rtol, atol=atol)
127 # And the symbolic computations should be much closer.
128 testcase.assertAllClose(sym_jac_back, sym_jac_fwd)
129
130
131class ForwardpropTest(test.TestCase, parameterized.TestCase):

Callers 4

testCustomGradientMethod · 0.85
testFunctionGradMethod · 0.85

Calls 4

_jacfwdFunction · 0.85
_gradFunction · 0.70
formatMethod · 0.45
assertAllCloseMethod · 0.45

Tested by

no test coverage detected