[Caffe2] How to find gradients of last layer with respect to any middle layer in the model

I want a similar functionality as tf.gradients. How to achieve that in Caffe2. Please guide. Thanks