Could you check, if wrapping the training step in a function helps, as Python uses function scoping as described here.