Transformers as Algorithms: Generalization and Implicit Model Selection in In-context Learning