Python – tensorflow.GradientTape.gradient()
Last Updated :
10 Jul, 2020
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
gradient() is used to computes the gradient using operations recorded in context of this tape.
Syntax: gradient(target, sources, output_gradients, unconnected_gradients)
Parameters:
- target: It is Tensor or list of Tensor to be differentiated.
- sources: It is Tensor or list of Tensor. Target values are differentiated against the source.
- output_gradients: It is a list of gradients with default value None.
- unconnected_gradients: It’s value can be either none or zero with default value none.
Returns: It returns a list or nested structure of Tensor.
Example 1:
Python3
import tensorflow as tf
x = tf.constant( 4.0 )
with tf.GradientTape() as gfg:
gfg.watch(x)
y = x * x * x
res = gfg.gradient(y, x)
print ( "res: " ,res)
|
Output:
res: tf.Tensor(48.0, shape=(), dtype=float32)
Example 2:
Python3
import tensorflow as tf
x = tf.constant( 4.0 )
with tf.GradientTape() as gfg:
gfg.watch(x)
with tf.GradientTape() as gg:
gg.watch(x)
y = x * x * x
first_order = gg.gradient(y, x)
second_order = gfg.gradient(first_order, x)
print ( "first_order: " ,first_order)
print ( "second_order: " ,second_order)
|
Output:
first_order: tf.Tensor(48.0, shape=(), dtype=float32)
second_order: tf.Tensor(24.0, shape=(), dtype=float32)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...