if you want to stop after a certain iterations, you could simply use epoch \times batch_idx as an iteration count right ? and then terminate the code once  epoch \times batch_idx reaches a certain threshold.