We propose a machine learning method to investigate the propagation of cosmic rays, based on the precisely measured spectra of primary and secondary nuclei Li, Be, B, C, and O by AMS-02, ACE, and Voyager-1. We train two Convolutional Neural Network machines: one learns how to infer the propagation and source parameters from the energy spectra of cosmic rays, and the other one is similar to the former but with flexibility of learning from the data with added artificial fluctuations. Together with the mock data generated by GALPROP, we find that both machines can properly invert the propagation process and infer the propagation and source parameters reasonably well. This approach can be much more efficient than the traditional Markov Chain Monte Carlo fitting method in deriving the propagation parameters if the users would like to update the confidence intervals with new experimental data. The trained models are also publicly available.