forked from DeepRec-AI/DeepRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwardprop_test.py
247 lines (202 loc) · 8.47 KB
/
forwardprop_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import functools
import weakref
import numpy as np
from tensorflow.python.eager import backprop
from tensorflow.python.eager import def_function
from tensorflow.python.eager import forwardprop
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import custom_gradient
from tensorflow.python.ops import gradient_checker_v2
from tensorflow.python.ops import math_ops
from tensorflow.python.platform import test
from tensorflow.python.util import nest
# TODO(allenl): Move this somewhere useful once forward gradients are stable.
def _jvp(f, primals, tangents):
"""Compute the jacobian of `f` at `primals` multiplied by `tangents`."""
with forwardprop.ForwardGradientAccumulator() as acc:
acc.watch(primals, tangents)
primals_out = f(*primals)
return primals_out, acc.jvp(primals_out)
def _jacfwd(f, primals):
"""Compute the jacobian of `f` at `primals` using forward-mode autodiff."""
jac_flat = []
flat_primals = nest.flatten(primals)
tangent_mask = [array_ops.zeros_like(primal) for primal in flat_primals]
for primal_index, primal in enumerate(flat_primals):
primal_vector = array_ops.reshape(primal, [-1])
primal_vector_length = array_ops.size(primal_vector)
jac_columns = []
for element_index in math_ops.range(primal_vector_length):
mask = array_ops.one_hot(element_index, primal_vector_length)
tangent_mask[primal_index] = array_ops.reshape(mask,
array_ops.shape(primal))
jac_columns.append(
nest.map_structure(
functools.partial(array_ops.reshape, shape=[-1]),
_jvp(f, primals, tangent_mask)[1]))
jac_flat.append(array_ops.stack(jac_columns, axis=1))
tangent_mask[primal_index] = array_ops.zeros_like(primal)
return nest.pack_sequence_as(primals, jac_flat)
def _grad(f, argnums=0):
"""Return a function which computes the gradient of `f`."""
def _f(*params):
with backprop.GradientTape() as tape:
tape.watch(params)
primals_out = f(*params)
return tape.gradient(primals_out, params[argnums])
return _f
def _hvp(f, primals, tangents):
"""Compute a forward-over-back Hessian-vector product."""
return _jvp(_grad(f), primals, tangents)[1]
def _test_gradients(testcase,
f,
primals,
order,
delta=1e-3,
rtol=1e-2,
atol=1e-6):
"""Tests forward/backward jacobians of `f`'s [0, `order`)-order gradients."""
if order < 1:
raise ValueError("`order` should be a positive integer, got '{}'."
.format(order))
if order > 1:
_test_gradients(
testcase=testcase,
f=_grad(f),
primals=primals,
order=order - 1,
delta=delta,
rtol=rtol,
atol=atol)
sym_jac_back, num_jac = gradient_checker_v2.compute_gradient(
f, primals, delta=delta)
testcase.assertAllClose(num_jac, sym_jac_back, rtol=rtol, atol=atol)
# TODO(b/134972215): compute_gradient should use the definition of a Jacobian
# matrix on Wikipedia, then this transpose can go away.
sym_jac_fwd = nest.map_structure(array_ops.transpose, _jacfwd(f, primals))
testcase.assertAllClose(num_jac, sym_jac_fwd, rtol=rtol, atol=atol)
# And the symbolic computations should be much closer.
testcase.assertAllClose(sym_jac_back, sym_jac_fwd)
class ForwardpropTest(test.TestCase):
@test_util.assert_no_new_pyobjects_executing_eagerly
def testMultipleWatchesAdd(self):
x = constant_op.constant(-2.)
with forwardprop.ForwardGradientAccumulator() as acc:
acc.watch(x, constant_op.constant(10.))
self.assertAllClose(10., acc.jvp(x))
acc.watch(x, constant_op.constant(11.))
self.assertAllClose(21., acc.jvp(x))
y = constant_op.constant(3.) * x
self.assertAllClose(21., acc.jvp(x))
self.assertAllClose(21. * 3., acc.jvp(y))
@test_util.assert_no_new_pyobjects_executing_eagerly
def testDeadTensorsJVPCleared(self):
x = array_ops.ones([100])
x_weak = weakref.ref(x)
grad_tensor = constant_op.constant(array_ops.zeros([100]))
grad_tensor_weak = weakref.ref(grad_tensor)
with forwardprop.ForwardGradientAccumulator() as acc:
acc.watch(x, grad_tensor)
derived_tensor = constant_op.constant(2.) * x
del grad_tensor
self.assertAllClose(array_ops.zeros([100]), acc.jvp(x))
del x
self.assertIsNone(x_weak())
self.assertIsNone(grad_tensor_weak())
derived_tensor_weak = weakref.ref(derived_tensor)
derived_tensor_grad = acc.jvp(derived_tensor)
derived_tensor_grad_weak = weakref.ref(derived_tensor_grad)
del derived_tensor
del derived_tensor_grad
self.assertIsNone(derived_tensor_weak())
self.assertIsNone(derived_tensor_grad_weak())
@test_util.assert_no_new_tensors
def testJVPManual(self):
primal, tangent = _jvp(math_ops.sin, (constant_op.constant(0.1),),
(constant_op.constant(0.2),))
self.assertAllClose(math_ops.sin(0.1), primal)
self.assertAllClose(math_ops.cos(0.1) * 0.2, tangent)
@test_util.assert_no_new_tensors
def testNumericHigherOrder(self):
def f(x):
pointwise = math_ops.sin(x) * math_ops.tan(x)
return math_ops.reduce_prod(
pointwise + math_ops.reduce_sum(pointwise), axis=1)
_test_gradients(
self, f, [constant_op.constant([[2.0, 3.0], [1.0, 4.0]])], order=3)
@test_util.assert_no_new_tensors
def testCustomGradient(self):
@custom_gradient.custom_gradient
def f(x):
def grad(dy):
return dy * math_ops.cos(x)
return np.sin(x.numpy()), grad
_test_gradients(self, f, [constant_op.constant([1., 2.])], order=3)
@test_util.assert_no_new_tensors
def testCustomGradientRecomputeGrad(self):
@custom_gradient.recompute_grad
def f(x):
return math_ops.reduce_prod(math_ops.tanh(x)**2)
_test_gradients(self, f, [constant_op.constant([1.])], order=3)
def testFunctionGrad(self):
@def_function.function
def f(x):
return math_ops.reduce_prod(math_ops.tanh(x)**2)
_test_gradients(
self,
f,
[constant_op.constant([1., 2.])],
# TODO(allenl): figure out why functions aren't N times differentiable
order=1)
@test_util.assert_no_new_pyobjects_executing_eagerly
def testHVPMemory(self):
def fun(x):
return math_ops.reduce_prod(math_ops.tanh(x)**2)
primals = constant_op.constant([1., 2., 3.])
tangents = constant_op.constant([3., 4., 5.])
_hvp(fun, (primals,), (tangents,))
@test_util.assert_no_new_tensors
def testHVPCorrectness(self):
def fun(x):
return math_ops.reduce_prod(math_ops.tanh(x)**2)
primals = constant_op.constant([1., 2., 3.])
tangents = constant_op.constant([3., 4., 5.])
forwardback_hvp_eager = _hvp(fun, (primals,), (tangents,))
forwardback_hvp_function = def_function.function(_hvp)(fun, (primals,),
(tangents,))
with backprop.GradientTape(persistent=True) as g:
g.watch(primals)
with backprop.GradientTape() as gg:
gg.watch(primals)
out = fun(primals)
grad = array_ops.unstack(gg.gradient(out, primals))
hessian = []
for i in range(3):
hessian.append(g.gradient(grad[i], primals))
hessian = array_ops.stack(hessian, axis=0)
backback_hvp = math_ops.tensordot(hessian, tangents, axes=1)
self.assertAllClose(backback_hvp, forwardback_hvp_eager)
self.assertAllClose(backback_hvp, forwardback_hvp_function)
if __name__ == '__main__':
ops.enable_eager_execution()
test.main()