How can I get data for a quiver plot in torch?
I have some function z(x, y) and I would like to generate a quiver plot (a 2D plot of the gradients). Something like this:



In order to do it, I have to run gradient over a linear mesh and adjust data to the format that matplotlib.quiver does.

A naive way is to iterate forward and backward in a loop:

for i in range(10):
    for j in range(10):
        x = torch.tensor(1. * i, requires_grad=True)
        y = torch.tensor(1. * j, requires_grad=True)
        z = x ** 2 + y ** 2
        z.backward()
        print(x.grad, y.grad)


This is obviously very inefficient. There are some examples on how to generate a linear mesh from x, y but I would need later change the mesh back to the format of the forward formula, get vectors of gradient and put them back, etc..

A simple example in numpy would be:

import matplotlib.pyplot as plt
n = 25
x_range = np.linspace(-25, 25, n)
y_range = np.linspace(-25, 25, n)
X, Y = np.meshgrid(x_range, y_range)
Z = X**2 + Y**2
U, V = 2*X, 2*Y
plt.quiver(X, Y, U, V, Z, alpha=.9)


What would be the standard way of doing this with pytorch? Are there some simple examples available?

You can compute gradients of non-scalars by passing torch.Tensors of ones.

import matplotlib.pyplot as plt
import torch

# create meshgrid
n = 25
a = torch.linspace(-25, 25,  n)
b = torch.linspace(-25, 25, n)
x = a.repeat(n)
y = b.repeat(n, 1).t().contiguous().view(-1)

x.requires_grad = True
y.requires_grad=True
z = x**2 + y**2
# this line will compute the gradients
torch.autograd.backward([z], [torch.ones(x.size()), torch.ones(y.size())])

# detach to plot
plt.quiver(x.detach(), y.detach(), x.grad, y.grad, z.detach(), alpha=.9)
plt.show()




If you need to do this repeatedly you need to zero the gradients (set x.grad = y.grad = None).
