I have a function f = x+y-(x*y), and I want to calculate the derivative of f with respect to the product xy: df/d(xy). How can I do that using Pytorch?
I tried defining xy as a separate variable as a product of x and y, and then using the retain_grad() feature calculated the gradient, as shown below. The problem is that the new variable is taken to be an independent variable, but it’s not since it depends on the variables x and y.
x = Variable(torch.FloatTensor([0.0]),requires_grad=True) y = Variable(torch.FloatTensor([0.0]),requires_grad=True) xy = x*y xy.retain_grad() f = x+y-xy f.backward() print(xy.grad.data) # outputs a value of -1
The output is -1, but I know from how the function behaves that the gradient at x=0,y=0 should be positive. For the curious, f is the algebraic form of the logical OR function.