A General Framework for Structured Learning of Mechanical Systems