Find the gradient of an intermediate variable

Hi All,

Is it possible to calculate the gradients of an intermediate variable with respect to an input?

The layout is as follows:
intermediate = g(inp1)
output = f(inp1, intermediate,…)

To do so, we minimize the loss: Solve MSE optimization problem loss ||output-target||

Then after the model is trained I am interested in finding the sensitivities of an intermediate variable: