Learning to Learn Optimally: A Practical Framework for Machine Learning Applications with Finite Time Horizon