import torch


def preds_per_cat(targs,bin_preds,cat):
    """ Compares model predictions with ground-truth labels for multi-label classification.
    Returns a column-wise sum of predictions by category, for each true category.
    In the output table, the true category is the row and the predictions made are the columns.

    targs: targets (tensor, n_images x n_categories), i.e., ground-truth labels
    bin_preds: binary predictions (tensor, same shape as targets)
    cat: the category index (for example, a number between 0-18 if there are 19 categories)
    """
    cat_idx = (targs[:,cat] > 0) #only rows where the category was seen
    dist = torch.sum(bin_preds[cat_idx],dim=0)
    return dist