import torch


def get_num_samples(targets, num_classes, dtype=None):
    """Returns a vector with the number of samples in each class.
    """
    batch_size = targets.size(0)
    with torch.no_grad():
        ones = torch.ones_like(targets, dtype=dtype)
        num_samples = ones.new_zeros((batch_size, num_classes))
        num_samples.scatter_add_(1, targets, ones)
    return num_samples