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

Function compute_gradient

tensorflow/python/ops/gradient_checker.py:277–335  ·  view source on GitHub ↗

Computes and returns the theoretical and numerical Jacobian. If `x` or `y` is complex, the Jacobian will still be real but the corresponding Jacobian dimension(s) will be twice as large. This is required even if both input and output is complex since TensorFlow graphs are not necessarily h

(x,
                     x_shape,
                     y,
                     y_shape,
                     x_init_value=None,
                     delta=1e-3,
                     init_targets=None,
                     extra_feed_dict=None)

Source from the content-addressed store, hash-verified

275 "support for functions. Note that the two versions have different usage, "
276 "so code change is needed.")
277def compute_gradient(x,
278 x_shape,
279 y,
280 y_shape,
281 x_init_value=None,
282 delta=1e-3,
283 init_targets=None,
284 extra_feed_dict=None):
285 """Computes and returns the theoretical and numerical Jacobian.
286
287 If `x` or `y` is complex, the Jacobian will still be real but the
288 corresponding Jacobian dimension(s) will be twice as large. This is required
289 even if both input and output is complex since TensorFlow graphs are not
290 necessarily holomorphic, and may have gradients not expressible as complex
291 numbers. For example, if `x` is complex with shape `[m]` and `y` is complex
292 with shape `[n]`, each Jacobian `J` will have shape `[m * 2, n * 2]` with
293
294 J[:m, :n] = d(Re y)/d(Re x)
295 J[:m, n:] = d(Im y)/d(Re x)
296 J[m:, :n] = d(Re y)/d(Im x)
297 J[m:, n:] = d(Im y)/d(Im x)
298
299 Args:
300 x: a tensor or list of tensors
301 x_shape: the dimensions of x as a tuple or an array of ints. If x is a list,
302 then this is the list of shapes.
303 y: a tensor
304 y_shape: the dimensions of y as a tuple or an array of ints.
305 x_init_value: (optional) a numpy array of the same shape as "x"
306 representing the initial value of x. If x is a list, this should be a list
307 of numpy arrays. If this is none, the function will pick a random tensor
308 as the initial value.
309 delta: (optional) the amount of perturbation.
310 init_targets: list of targets to run to initialize model params.
311 extra_feed_dict: dict that allows fixing specified tensor values
312 during the Jacobian calculation.
313
314 Returns:
315 Two 2-d numpy arrays representing the theoretical and numerical
316 Jacobian for dy/dx. Each has "x_size" rows and "y_size" columns
317 where "x_size" is the number of elements in x and "y_size" is the
318 number of elements in y. If x is a list, returns a list of two numpy arrays.
319 """
320 # TODO(mrry): remove argument `init_targets`
321 if extra_feed_dict is None:
322 extra_feed_dict = {}
323
324 if isinstance(x, list):
325 return _compute_gradient_list(x, x_shape, y, y_shape, x_init_value, delta,
326 init_targets, extra_feed_dict=extra_feed_dict)
327 else:
328 if init_targets is not None:
329 assert isinstance(init_targets, (list, tuple))
330 for init in init_targets:
331 init.run()
332 dx, dy = _compute_dx_and_dy(x, y, y_shape)
333 ret = _compute_gradient(x, x_shape, dx, y, y_shape, dy, x_init_value, delta,
334 extra_feed_dict=extra_feed_dict)

Callers 1

compute_gradient_errorFunction · 0.70

Calls 4

_compute_dx_and_dyFunction · 0.85
_compute_gradient_listFunction · 0.70
_compute_gradientFunction · 0.70
runMethod · 0.45

Tested by

no test coverage detected