Bayes optimal learning of attention-indexed models