Thanks for the code snippet.
You are recreating a tensor indeed (F in your code), which will detach lambda from the computation graph. Instead you would need to use torch.cat with torch.ones, torch.zeros, etc. to create F.