Thanks. But how can I calculate the gradient inside the loop, mathematically I need a mean loss on all test data points (not a batch of them) to calculate the gradient on test data, so if I put grad(loss, params) inside the loop, it computes the gradients for current batch, right?