 # Gradient with respect to product of variables

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)
xy = x*y

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.

Hi,

The result looks correct to me no? if you write b = x*y.
Then f = x+y - b and df/db = -1. So df/dxy = -1.
I don’t know how you print the function? But f(x, y) and f(x, y, b) are two completely different functions so you can extrapolate from knowing the first one how the second one will behave.

That is true if b is an independent variable, but we have b=g(x,y). So, effectively, we have f(x,y,g(x,y)). The question is how to compute df/dg. I guess it’s more of a math question than a Pytorch question. Yet, I wondered if it can answered by writing a piece of Pytorch code. Any ideas?