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

Function val_and_grad_function

tensorflow/python/eager/backprop.py:406–471  ·  view source on GitHub ↗

Returns a function that computes f and its derivative w.r.t. params. Example: ```python # f(x, y) = (x ^ 3) * y - x * (y ^ 2) # Therefore, the 1st order derivatives are: # df / dx = 3 * (x ^ 2) * y - y ^ 2 # df / dy = x ^ 3 - 2 * x * y def f(x, y): return x * x * x * y - x * y

(f, params=None)

Source from the content-addressed store, hash-verified

404
405
406def val_and_grad_function(f, params=None):
407 """Returns a function that computes f and its derivative w.r.t. params.
408
409 Example:
410 ```python
411 # f(x, y) = (x ^ 3) * y - x * (y ^ 2)
412 # Therefore, the 1st order derivatives are:
413 # df / dx = 3 * (x ^ 2) * y - y ^ 2
414 # df / dy = x ^ 3 - 2 * x * y
415 def f(x, y):
416 return x * x * x * y - x * y * y
417
418 # Obtain a function that returns the function value and the 1st order
419 # gradients.
420 val_grads_fn = tfe.value_and_gradients_function(f)
421
422 x = 2.0
423 y = 3.0
424
425 # Invoke the value-and-gradients function.
426 f_val, (x_grad, y_grad) = val_grads_fn(x, y)
427 assert f_val.numpy() == (2 ** 3) * 3 - 2 * (3 ** 2)
428 assert x_grad.numpy() == 3 * (2 ** 2) * 3 - 3 ** 2
429 assert y_grad.numpy() == (2 ** 3) - 2 * 2 * 3
430
431 # To obtain a callable that returns the value of `f` and the gradient(s) of
432 # `f` with respect to a subset of its inputs, use the `params` keyword
433 # argument with `value_and_gradients_function()`.
434 val_ygrad_fn = tfe.value_and_gradients_function(f, params=[1])
435
436 f_val, (y_grad,) = val_ygrad_fn(x, y)
437 assert f_val.numpy() == (2 ** 3) * 3 - 2 * (3 ** 2)
438 assert y_grad.numpy() == (2 ** 3) - 2 * 2 * 3
439 ```
440
441 Args:
442 f: function to be differentiated. If `f` returns a scalar, this scalar will
443 be differentiated. If `f` returns a tensor or list of tensors, by default
444 a scalar will be computed by adding all their values to produce a single
445 scalar. If desired, the tensors can be elementwise multiplied by the
446 tensors passed as the `dy` keyword argument to the returned gradient
447 function.
448 params: list of parameter names of f or list of integers indexing the
449 parameters with respect to which we'll differentiate. Passing `None`
450 differentiates with respect to all parameters.
451
452 Returns:
453 function which, when called, returns the value of f and the gradient
454 of f with respect to all of `params`. The function takes an extra optional
455 keyword argument "dy". Setting it allows computation of vector jacobian
456 products for vectors other than the vector of ones.
457
458 Raises:
459 ValueError: if the params are not all strings or all integers.
460 """
461
462 def decorated(*args, **kwds):
463 """Computes the value and gradient of the decorated function."""

Callers 1

decoratedFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected