Your reg_l and R_x should be computed every iteration rather than passed as input. Each bwd call traverses through them. Hence your error. PyTorch uses dynamic graph, which isn’t like the static graph of tf. So reg_l and R_x are not symbolic expressions but actual tensors. So you need to explicitly compute them every time.