How to compute the horizontal/vertical gradient of a matrix

I want to compute the vertical/horizontal gradient of a matrix/ tensor in PyTorch, just like a numpy function - np.gradient (

Say, the matrix is $G$, $\Delta_w G$ and $\Delta_h G$ are needed. Does any built-in function in PyTorch work like these? Or how can I implement it?
Thanks a lot.