import torch


def cosine_distance(x, y):
    """Cosine distance between vectors x and y."""
    x_norm = torch.sqrt(torch.sum(torch.pow(x, 2), dim=-1))
    x_norm = x_norm.view(-1, 1)
    y_norm = torch.sqrt(torch.sum(torch.pow(y, 2), dim=-1))
    y_norm = y_norm.view((-1, 1))
    normalized_x = x / x_norm
    normalized_y = y / y_norm
    return torch.mean(torch.sum(normalized_x * normalized_y, dim=-1))