How pytorch implements back propagation from the output layer to the input layer
I am having difficulty implementing the following functions.
Assuming that we have trained a network model, I want to backpropagate from the output layer to the input layer (not the first layer) to obtain a new input data. I want to know if there is a function in pytorch or other existing functions that can achieve this function, I did not find the relevant function in the pytorch tutorials.

If you want the gradient w.r.t to the input, you can simply get it from the .grad:
x.requires_grad_(True)  # explicitly ask pytorch to estimate the gradient w.r.t x
# forward pass:
pred = model(x)  # make a prediction
loss = criterion(pred, y)  # compute the loss
# backward pass - compute gradients:
loss.bacward()

# now you have access to the gradient of loss w.r.t the input:
x_grad = x.grad

if you are interested in inspecting gradients of specific layers, you'll need to use hooks.
