A mathematical theory for understanding when abstract representations emerge in neural networks