How to accumulate my loss over mini batches then calculate my gradient

1.2k Views Asked by At

My main question is; is averaging the loss the same thing as averaging the gradient and how do i accumulate my loss over mini batches then calculate my gradient?

I have been trying to implement policy gradient in Tensorflow and run into the issue where i can not feed all my game states into my network at once and then update. The problem is if i lower my network size then train on all frames at once and take the mean of the loss then it begins to converge nicely. But if I accumulate the gradients over mini batches then average them, my gradients explode and i overflow my weights.

Any help or insight will be very appreciated.

Keep in mind also, this is my first time asking a question here.

1

There are 1 best solutions below

0
On

What you can do is to accumulate gradients after each mini-batch and then update the weights based on gradient averages. Consider following simple case for fitting 50 Gaussian blobs with a single-layered perceptron:

from sklearn.datasets import make_blobs
import tensorflow as tf
import numpy as np

x_train, y_train = make_blobs(n_samples=50,
                              n_features=2,
                              centers=[[1, 1], [-1, -1]],
                              cluster_std=0.5)

with tf.name_scope('x'):
    x = tf.placeholder(tf.float32, [None, 2])
    y = tf.placeholder(tf.int32, [None])

with tf.name_scope('layer'):
    logits = tf.layers.dense(x,
                             units=2,
                             kernel_initializer=tf.contrib.layers.xavier_initializer())
with tf.name_scope('loss'):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
    loss_op = tf.reduce_mean(xentropy)

The minimize() method of the tensorflow optimizers calls compute_gradients() and then apply_gradients(). Instead of calling the minimize(), I'm going to call both methods directly. First, to get the gradients we call compute_gradients() (which returns a list of tuples grads_and_vars) and for apply_gradients() instead of gradients I'm going to feed placeholders for future gradient's averages:

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
grads_and_vars = optimizer.compute_gradients(loss_op)
grads = [g for g, v in grads_and_vars]

# placeholders for gradients averages
placeholder_grads = [tf.placeholder(tf.float32, [None] + g.get_shape().as_list())
                     for g in grads]

new_grads_and_vars = [(tf.reduce_mean(p, axis=0), gv[1])
                      for p, gv in zip(placeholder_grads, grads_and_vars)]

apply_grads_op = optimizer.apply_gradients(new_grads_and_vars)

During mini-batches we only compute losses (you can accumulate losses as well - append to some list and then compute average) and gradients, without applying gradients to weights. At the end of each epoch we execute apply_grads_op operation while feeding accumulated gradients to its placeholders:

data = tf.data.Dataset.from_tensor_slices({'x':x_train, 'y':y_train}).batch(10)
iterator = data.make_initializable_iterator()
n_epochs = 2
with tf.Session() as sess:
    _ = sess.run([tf.global_variables_initializer(), iterator.initializer])
    next_batch = iterator.get_next()
    for epoch in range(n_epochs):
        epoch_grads = []
        while True:
            try:
                batch = sess.run(next_batch)
                evaled = sess.run([loss_op] + grads,
                                  feed_dict={x:batch['x'], y:batch['y']})
                epoch_grads.append(evaled[1:])
                print('batch loss:', evaled[0])
            except tf.errors.OutOfRangeError:
                _ = sess.run(iterator.initializer)
                feed_dict = {p:[g[i] for g in epoch_grads]
                             for i, p in enumerate(placeholder_grads)}
                _ = sess.run(apply_grads_op, feed_dict=feed_dict)

                break