A normative theory of adaptive dimensionality reduction in neural networks