How to update model parameters with accumulated gradients?

前端 未结 6 2074
孤独总比滥情好
孤独总比滥情好 2020-12-05 05:14

I\'m using TensorFlow to build a deep learning model. And new to TensorFlow.

Due to some reason, my model has limited batch size, then this limited batch-size will m

6条回答
  •  抹茶落季
    2020-12-05 06:09

    Tensorflow 2.0 Compatible Answer: In line with the weixsong's Answer mentioned above and the explanation provided in Tensorflow Website, mentioned below is the code for Accumulating Gradients in Tensorflow Version 2.0:

    def train(epochs):
      for epoch in range(epochs):
        for (batch, (images, labels)) in enumerate(dataset):
           with tf.GradientTape() as tape:
            logits = mnist_model(images, training=True)
            tvs = mnist_model.trainable_variables
            accum_vars = [tf.Variable(tf.zeros_like(tv.initialized_value()), trainable=False) for tv in tvs]
            zero_ops = [tv.assign(tf.zeros_like(tv)) for tv in accum_vars]
            loss_value = loss_object(labels, logits)
    
           loss_history.append(loss_value.numpy().mean())
           grads = tape.gradient(loss_value, tvs)
           #print(grads[0].shape)
           #print(accum_vars[0].shape)
           accum_ops = [accum_vars[i].assign_add(grad) for i, grad in enumerate(grads)]
    
    
    
        optimizer.apply_gradients(zip(grads, mnist_model.trainable_variables))
        print ('Epoch {} finished'.format(epoch))
    
    # call the above function    
    train(epochs = 3)
    

    Complete code can be found in this Github Gist.

提交回复
热议问题