def compute_top_k_accuracy(labels, predictions):
    """Compute the accuracy of the `predictions` using `labels` as ground
        truth.
    Args:
        labels: An iterable of ground truth labels.
        predictions: A matrix where each row contains k label predictions. The
            true label is in the corresponding entry of `labels`.
    Returns:
        float: The accuracy.
    Raises:
        ValueError: If number of `labels` does not equal the number of rows in
            `predictions`.
    """

    num_samples = len(labels)

    if num_samples == 0:
        return 0.

    if num_samples != len(predictions):
        raise ValueError("`labels` and `predictions` must have the same "\
                         "number of entries")

    num_correct = 0
    for i in range(num_samples):
        if labels[i] in predictions[i]:
            num_correct += 1
    return num_correct / (1. * num_samples)