Gradient in Tensorflow Returns Equal Derivatives for Unequal Inputs

54 views Asked by At

I got the following problem. I want to compute the gradient of a model with respect to its inputs. The model expects scalars as inputs and puts out scalars. But if I calculate the model for multiple different inputs at once, the gradient gives the same answer, regardless of any numerical values.

Here is a small working example:

import tensorflow as tf
import keras
from keras import layers
from tensorflow.keras import initializers

# Creating a model with scalar input and output
inputs = keras.Input(shape=(1), name="digits")
dense = layers.Dense(
    32,
    activation="relu",
    name=f"dense_0",
    kernel_initializer = keras.initializers.GlorotNormal(seed=None),
    bias_initializer=initializers.Zeros(),
    )(inputs)
outputs = layers.Dense(1, name="predictions")(dense)
model = keras.Model(inputs=inputs, outputs=dense)

# calculating the gradient for 2 different inputs
x = tf.Variable([[1.0],[5.6]])
with tf.GradientTape() as tape:
    y = model(x)

grad = tape.gradient(y, x)
print(grad)

For me this give, e.g.:

tf.Tensor(
   [[0.28975776]
   [0.28975776]], shape=(2, 1), dtype=float32)

But I would expect that the output should not be identical for both variables. In fact, if I calculate the gradient of the model wrt the input numerically, I get different derivatives for different inputs. So I am doing something very wrong, I guess. Any help will be appreciated.

Thanks!

0

There are 0 answers