On the Regularization of Convolutional Neural Networks and Transformers under Distribution Shifts