Folded Optimization for End-to-End Model-Based Learning