import torch


def _gram_matrix(outputs):
    """Computes the gram matrix, the inner product of a matrix with itself
    """
    # Apply operation batch wise
    G = []
    for output in outputs:
        # View output in two dimensions
        c, *_ = output.size()
        X = output.view(c, -1)

        # Compute inner product and normalise by size of output
        G_ = torch.mm(X, X.t())

        # Normalise w.r.t number of elements
        G_ /= X.numel()
        G.append(G_)

    return torch.stack(G)