Hi,
You can have your function be able to take a batch of inputs (most provided functions handle that already). And then backward through that.
Not sure what your code sample is doing since i is not used inside the loop.