def _scale(tensor):
  """Scale a tensor based on min and max of each example and channel

  Resulting tensor has range (-1, 1).

  Parameters
  ----------
  tensor : torch.Tensor or torch.autograd.Variable
    Tensor to scale of shape BxCxHxW
  Returns
  -------
    Tuple (scaled_tensor, min, max), where min and max are tensors
    containing the values used for normalizing the tensor
  """
  b, c, h, w = tensor.shape
  out = tensor.view(b, c, h * w)
  minimum, _ = out.min(dim=2, keepdim=True)
  out = out - minimum
  maximum, _ = out.max(dim=2, keepdim=True)
  out = out / maximum  # out has range (0, 1)
  out = out * 2 - 1  # out has range (-1, 1)

  return out.view(b, c, h, w), minimum, maximum