def unreduce_tensor(tensor, shape, axis, keepdims):
  """Reverse summing over a dimension.

  See utils.py.

  Args:
    tensor: The tensor that was reduced.
    shape: A list, the original shape of the tensor before reduction.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    A tensor with axes broadcast to match the shape of the original tensor.
  """
  if not keepdims:
    if axis is None:
      axis = range(len(shape))
    elif isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      tensor = tf.expand_dims(tensor, ax)
  tile_shape = np.array(shape) / np.array(shape_as_list(tensor))
  return tf.tile(tensor, tile_shape)