Plotting contour of pytorch functions
I have a function like the one below:
x0 = torch.tensor([1.5, .1])
Q = torch.tensor([[10.0, 6.0],
                  [9.0, 8.0]])

def f1(x):
    z = x - x0
    Qz = z @ Q
    return 0.5 * Qz@z

How do I get a contour plot for this?
x is a 2D tensor.
I am messing up somewhere while using meshgrid.

First, let's make your code work with a batch of 2d points, that is, x of shape nx2:
def f1(x):
  z = x - x0  # z of shape n-2
  Qz = z @ Q  # Qz of shape n-2
  return 0.5 * (Qz * z).sum(dim=-1)  # we want output of size n and not n-n

Now we can create a grid over which we want to plot f1(x):
grid = torch.stack(torch.meshgrid(torch.linspace(-20., 20., 100), torch.linspace(-20., 20., 100), indexing='xy'))
# convert the grid to a batch of 2d points:
grid = grid.reshape(2, -1).T
# get the output on all points in the grid
out = f1(grid)
# plot
plt.matshow(out.detach().numpy().reshape(100,100))

You'll get:

