'Clips gradients if the hyperparameter `gradient_clip_norm` requires it.
Sparse tensors, in the form of IndexedSlices returned for the
gradients of embeddings, require special handling.
Args:
grad: Gradient Tensor, IndexedSlices, or None.
Returns:
Optionally clipped gradient.'

def _clip_gradients(self, grad):

    if ((grad is not None) and (self.hyperparams.gradient_clip_norm > 0)):
        logging.info('Clipping    gradient    %s', grad)
        if isinstance(grad, tf.IndexedSlices):
            tmp = tf.clip_by_norm(grad.values, self.hyperparams.gradient_clip_norm)
            return tf.IndexedSlices(tmp, grad.indices, grad.dense_shape)
        else:
            return tf.clip_by_norm(grad, self.hyperparams.gradient_clip_norm)
    else:
        return grad
