def nadaraya_watson(x_train, y_train, x_val, sigma):
dists = x_val[:, None] - x_train[None, :] # (queries, keys)
k = d2l.exp(-dists**2 / (2 * sigma**2))
attention_w = k / k.sum(1, keepdims=True) # Normalize over the keys
return attention_w @ y_train, attention_w