You want to backpropagate through your graph for only a certain number of steps. Correct me if I am wrong.
One way would be to backprop through the complete graph and only update the gradients for your required layers.