A Local Algorithm to Learn Trajectories with Stochastic Neural Networks