*edit (I mean this sincerely, it made me laugh and I did not see it at first)
Yes, the syntax is ambiguous, but ambiguously-parseable sentences happen all the time in all languages and we resolve the ambiguity using context clues, which in this case is easy to do.
It's not wrong, but possibly ambiguous, and I'd bet an English teacher would prefer it was phrased differently. In speech, I wouldn't bat an eye at that arrangement. But, if I were to write the headline, it would have been...
"I used machine learning to count all the yurts in Mongolia."
or even just add a comma at the right place:
"I counted all of the yurts in Mongolia, using machine learning"
"One morning I shot an elephant in my pajamas. How he got into my pajamas I'll never know."
Yes, when the phrase is ambiguous, it’s usually more coherent to simply change the sentence order as you’ve done here.