Compute the directional derivative of a function with a tensor

I konw that the directional derivative of a function with a vector = gradient.vector (dot product). I want to know how to compute the directional derivative of a function with a tensor given the gradient. I am using tensorflow and Keras.

Hi @dali_dali, You can calculate the derivative of a function with a given tensor using tf.GradientTape( ). For example, consider the function y = x * *2 . The gradient at x = 3.0 can be computed as:

x = tf.Variable((3.0))

with tf.GradientTape() as tape:
  y = x**2
dy_dx=tape.gradient(y, x)

print(dy_dx)

tf.Tensor(6.0, shape=(), dtype=float32)

Thank You.

Hi @Kiran_Sai_Ramineni,
Thank you very much, but I want to know how to compute the directional derivative, not the gradient.

Hi @dali_dali, once you find the gradients you can use tf.tensordot(gradients, input_vector) to find the directional derivative. For example,

@tf.function
def example():
  a = tf.ones([1, 2])
  b = tf.ones([3, 1])
  return tf.gradients([b], [a],unconnected_gradients='zero')[0]

tf.tensordot(example().numpy(), (3.,4.),axes=0)

Thank You.