pythonpytorchbotorch

Plotting BoTorch Synthetic functions


I'm trying to plot the Ackley synthetic function from botorch (mainly to understand how it works and why if I feed it 2 tensors it only returns 1 value), and I'm facing a bunch of different problems. I've also tried other functions but with the same results... Any person able to shed some light over this topic will be welcome.

Here's the code I'm using:

import numpy as np
import torch
import matplotlib.pyplot as plt
import botorch.test_functions.synthetic as funcs

#We only use Ackley for now
branin = funcs.Branin()
hartman = funcs.Hartmann()
ackley = funcs.Ackley()

x1 = np.linspace(0, 5, 2)
x2 = np.linspace(0, 5, 2)
X, Y = np.meshgrid(x1,x2)
Z = ackley(torch.tensor([X,Y]))

fig, ax = plt.subplots(subplot_kw={'projection':'3d'})
surf = ax.plot_surface(X,Y,Z)

The fun part is that with these settings, I'm able to plot it, but if I change the linspace so it has more points, the plotting part gives an error for shape mismatch...


Solution

  • It's all about argument shaping, combining your code with this, got:

    import numpy as np
    import torch
    import matplotlib.pyplot as plt
    import botorch.test_functions.synthetic as funcs
    
    #We only use Ackley for now
    branin = funcs.Branin()
    hartman = funcs.Hartmann()
    ackley = funcs.Ackley()
    
    x1 = np.linspace(-5, 5, 30)
    x2 = np.linspace(-5, 5, 30)
    X, Y = np.meshgrid(x1,x2)
    Z = ackley(torch.tensor([np.ravel(X),np.ravel(Y)]).T)
    
    fig, ax = plt.subplots(subplot_kw={'projection':'3d'})
    surf = ax.plot_surface(X,Y,Z.reshape(X.shape))