Sunday 18 October 2020

Shall we set experimental_aggregate_gradients to False or True when manually accumulating gradients in Tensorflow?

I am trying to perform a manual gradient accumulation in Tensorflow 2.2.0 using Python 3.8.5.

I have a piece of code where I gather a series of gradients( of total number of grads_total_number), in a list: grads_list and I do the accumulation using following code:

avg_accum_grads=[]    
for grad_ind in range(grads_total_number):
        avg_accum_grads.append(tf.reduce_mean(grads_list[grad_ind], axis=0))

I then intend to apply these grads via my optimizer to my model:

myopt.apply_gradients(zip(avg_accum_grads, model.trainable_variables),experimental_aggregate_gradients=True)

, where my optimizer is Adam defined as tf.keras.optimizers.Adam

However, due to the documentation here, I am confused if I have to set experimental_aggregate_gradients to False. I could not clearly understand, here that I have done the accumulation manually, if I let it be true, it continues to accumulate?

Any help is very appreciated.



from Shall we set experimental_aggregate_gradients to False or True when manually accumulating gradients in Tensorflow?

No comments:

Post a Comment