I wanted to get the gradients from the gradient tape so in tf version 1 I used something like -
grads = model.optimizer.get_gradients(model.total_loss, model.trainable_weights)
symb_inputs = (model._feed_inputs + model._feed_targets + model._feed_sample_weights)
Now for tf v2 I want to get gradients so I used -
grads = tf.GradientTape(model.total_loss, model.trainable_weights)
symb_inputs = (model.feed_inputs + model.feed_targets + model.feed_sample_weights)
It is not working, I got errors like feed_inputs or feed_targets are not there.
Hi @Shashank_Priyadarshi, TensorFlow records relevant operations executed inside the context of a tf.GradientTape. TensorFlow then uses that tape to compute the gradients of a recorded computation using reverse mode differentiation. To get gradients from a model,
layer = tf.keras.layers.Dense(2, activation='relu')
x = tf.constant([[1., 2., 3.]])
with tf.GradientTape() as tape:
y = layer(x)
loss = tf.reduce_mean(y**2)
grad = tape.gradient(loss, layer.trainable_variables)
for var, g in zip(layer.trainable_variables, grad):
print(f'{var.name}, gradients: {g}')
Thank You!
Closing this as I fixed the issue