# Understand tf.gradients(): Compute Tensor Gradient for TensorFlow Beginners – TensorFlow Tutorial

By | January 15, 2020

TensorFlow tf.gradients() function can return the gradient of a tensor. How to understand the result of it? We will use some examples to help tensorflow beginners to understand and use it in this tutorial.

Syntax

```tf.gradients(
ys,
xs,
aggregation_method=None,
)```

Constructs symbolic derivatives of sum of ys w.r.t. x in xs

where ys and xs are each a tensor or a list of tensors

How to understand the result of tf.gradients()?

Suppose

y = [y1, y2], x = [x1, x2, x3]

y = f(x)

To compute the gradient of x based on y, we can do like this:

or

g = tf.gradients([y1, y2], [x1, x2, x3])

As to g = [g1, g2, g3]

where Here is an example:

```import tensorflow as tf
import numpy as np

x = tf.Variable(np.array([[1, 1.2, 1.3],[1, 1.5, 1.2]]), dtype = tf.float32)
z = tf.Variable(np.array([[1, 1],[1.2, 1.2], [2, 1.7]]), dtype = tf.float32)

y = tf.matmul(x, z)

```[[array([[2. , 2.4, 3.7],