Can I obtain the expression of derivation by using Pytorch or Tensorflow?
My question is whether I could obtain the expression after I compute the derivation by using PyTorch or TensorFlow. For better understanding my question I provide an example below:

I have a function (x1, x2, and x3 are all the decision variables)

y = x1 + x1*x2 + x1*x3


The derivation of y in terms of x1 should be:

1 + x2 + x3


My question is, if I use PyTorch or TensorFlow do the computation, could I print out the expression of "1 + x2 + x3"? Very much appreciate if the codes are provided no matter by using PyTorch or TensorFlow.

I don't know if it's possible in Pytorch or Tensorflow, but you can definitely do it using SymPy python library:

import sympy
x1 = sympy.symbols('x1')
x2 = sympy.symbols('x2')
x3 = sympy.symbols('x3')

y = x1 + x1*x2 + x1*x3
res = sympy.diff(y, x1)

print(res)


output:

x2 + x3 + 1

