In this code sample, you have two calls to backward. So for the first one, it is expected that you need to call retain_graph=True.
You problem is that dpmm_loss will still fail (or have an increasingly larger computational graph).
This is because initialization() is a differentiable function: it’s outputs do require gradients. And these outputs are inputs to every iteration of your loop. So every iteration of the loop will backprop this part of the graph (that corresponds to the initialization function).
If you don’t want that, you can detach() the results of the initialization.