  We develop a machine learning algorithm to infer the 3D cumulative radial
profiles of total and gas mass in galaxy clusters from thermal
Sunyaev-Zel'dovich effect maps. We generate around 73,000 mock images along
various lines of sight using 2,522 simulated clusters from the
\thethreehundred{} project at redshift $z< 0.12$ and train a model that
combines an autoencoder and a random forest. Without making any prior
assumptions about the hydrostatic equilibrium of the clusters, the model is
capable of reconstructing the total mass profile as well as the gas mass
profile, which is responsible for the SZ effect. We show that the recovered
profiles are unbiased with a scatter of about $10\%$, slightly increasing
towards the core and the outskirts of the cluster. We selected clusters in the
mass range of $10^{13.5} \leq M_{200} /(\hMsun) \leq 10^{15.5}$, spanning
different dynamical states, from relaxed to disturbed halos. We verify that
both the accuracy and precision of this method show a slight dependence on the
dynamical state, but not on the cluster mass. To further verify the consistency
of our model, we fit the inferred total mass profiles with an NFW model and
contrast the concentration values with those of the true profiles. We note that
the inferred profiles are unbiased for higher concentration values, reproducing
a trustworthy mass-concentration relation. The comparison with a widely used
mass estimation technique, such as hydrostatic equilibrium, demonstrates that
our method recovers the total mass that is not biased by non-thermal motions of
the gas.
