The rate for Higgs () bosons production in association with either one () or two () top quarks is measured in final states with multiple electrons, muons, and hadronically decaying leptons, using proton-proton collision data recorded at a center-of-mass energy of 13~\mathrm{TeV} by the CMS experiment. The analyzed data set corresponds to an integrated luminosity of 137~\mathrm{fb}^{-1}. The analysis targets events where the boson decays via , , or and the top quark(s) decay either leptonically or hadronically. The signal yield is maximized by including ten different signatures in the analysis depending on the lepton multiplicity. The separation of the boson signal from backgrounds is enhanced by machine learning techniques and matrix element methods. The measured production rates for the and signals amount to 0.92 \pm 0.19\,\text{(stat)} ^{+0.17}_{-0.13}\,\text{(syst)} and 5.7 \pm 2.7\,\text{(stat)} \pm 3.0\,\text{(syst)} times their respective standard model (SM) expectations. Assuming that the boson coupling to the lepton is equal in strength to the values expected in the SM, the coupling of the boson to the top quark is constrained, at confidence level, to be within or times the SM expectation for this coupling.