def NSS_loss_2(x, y):
    """
        Computes the Normalized Scanpath Saliency loss between x (output of a model)
        and y (label).
        x and y are assumed to be torch tensors, either individual images or batches.
        """
    # If dimensionality of x is 2, insert a singleton batch dimension
    if len(x.shape) == 2:
        x = x.unsqueeze(0)
        y = y.unsqueeze(0)
    # Loop over each image in the batch, apply NSS, return the average
    loss = 0
    for i in range(x.shape[0]):
        x_i, y_i = x[i, :, :], y[i, :, :]
        # normalize saliency map
        sal_map = (x_i - x_i.mean()) / x_i.std()
        # mean value at fixation locations
        sal_map = sal_map.masked_select(y_i > 0)
        loss += sal_map.mean()
    # Return the -ve avg NSS score
    return -1 * loss / x.shape[0]