Machine learning method for computation of optimal transitions in magnetic nanosystems