Theoretical insights on generalization in supervised and self-supervised deep learning