A Two-Stage Training Method for Modeling Constrained Systems With Neural Networks