Python - tensorflow.GradientTape.jacobian() - GeeksforGeeks

10 Jul, 2020
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 
jacobian() is used to compute the jacobian using operations recorded in context of this tape.
Syntax: jacobian( target, source, unconnected_gradients, parallel_iterations, experimental_use_pfor )
Parameters:
target: It is a Tensor having minimum rank 2.
source: It is a Tensor having minimum rank 2.
unconnected_gradients(optional): It’s value can either be zero or None. Default value is None.
parallel_iterations(optional): It is used to control parallel iterations and memory usage.
experimental_use_pfor(optional): It is a boolean with default value True. It uses pfor to calculate jacobian when set to true otherwise tf.while_loop is used.
Returns: It returns a Tensor. 
Example 1:
Python3
# Importing the libraryimport tensorflow as tf  x = tf.constant([[4, 2],[1, 3]], dtype=tf.dtypes.float32)  # Using GradientTapewith tf.GradientTape() as gfg:  gfg.watch(x)  y = x * x * x  # Computing jacobianres  = gfg.jacobian(y, x)   # Printing resultprint("res: ",res)
Output:


res:  tf.Tensor(
[[[[48.  0.]
   [ 0.  0.]]

  [[ 0. 12.]
   [ 0.  0.]]]


 [[[ 0.  0.]
   [ 3.  0.]]

  [[ 0.  0.]
   [ 0. 27.]]]], shape=(2, 2, 2, 2), dtype=float32)



Example 2:
Python3
# Importing the libraryimport tensorflow as tf  x = tf.constant([[4, 2],[1, 3]], dtype=tf.dtypes.float32)  # Using GradientTapewith tf.GradientTape() as gfg:  gfg.watch(x)    # Using nested GradientTape for calculating higher order jacobian  with tf.GradientTape() as gg:    gg.watch(x)    y = x * x * x  # Computing first order jacobian  first_order = gg.jacobian(y, x)  # Computing Second order jacobiansecond_order  = gfg.batch_jacobian(first_order, x)   # Printing resultprint("first_order: ",first_order)print("second_order: ",second_order)
Output:


first_order:  tf.Tensor(
[[[[48.  0.]
   [ 0.  0.]]

  [[ 0. 12.]
   [ 0.  0.]]]


 [[[ 0.  0.]
   [ 3.  0.]]

  [[ 0.  0.]
   [ 0. 27.]]]], shape=(2, 2, 2, 2), dtype=float32)
second_order:  tf.Tensor(
[[[[[[24.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]


   [[[ 0.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]]



  [[[[ 0.  0.]
     [ 0.  0.]]

    [[ 0. 12.]
     [ 0.  0.]]]


   [[[ 0.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]]]




 [[[[[ 0.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]


   [[[ 0.  0.]
     [ 6.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]]



  [[[[ 0.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0.  0.]]]


   [[[ 0.  0.]
     [ 0.  0.]]

    [[ 0.  0.]
     [ 0. 18.]]]]]], shape=(2, 2, 2, 2, 2, 2), dtype=float32)





Python Tensorflow-math-functions
Python-Tensorflow
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | os.path.join() method
Python | Get unique values from a list
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()