Aren’t your loops in the wrong order? I think you want them like:
for t in range(self.trajectory_param):
loss=closure()
for p in parameters:
…
I checked pyro implementation 2 (used from infer.mcmc.hmc), it seems to agree, doing one closure evalutation per step.
Otherwise, this closure approach should work, in principle. But you should zero gradients between calls.