import torch


def hamming_dist(y_true, y_pred):
    """
    Calculate the Hamming distance between a given predicted label and the
    true label. Assumes inputs are torch Variables!

    Args:
        y_true (autograd.Variable): The true label
        y_pred (autograd.Variable): The predicted label

    Returns:
        (float): The Hamming distance between the two vectors
    """

    # Make sure y_pred is rounded to 0/1
    y_pred = torch.round(y_pred)

    result = torch.mean(torch.abs(y_true - y_pred), dim=1)
    result = torch.mean(result, dim=0)

    return float(result.data.cpu().numpy())