def plot_function_plotly( obj_func, mins: TensorType, maxs: TensorType, grid_density=20, title=None, xlabel=None, ylabel=None, alpha=1.0, ): """ Draws an objective function. :obj_func: the vectorized objective function :param mins: list of 2 lower bounds :param maxs: list of 2 upper bounds :param grid_density: integer (grid size) :return: a plotly figure """ # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function F = to_numpy(obj_func(Xplot)) if len(F.shape) == 1: F = F.reshape(-1, 1) n_output = F.shape[1] fig = make_subplots( rows=1, cols=n_output, specs=[np.repeat({ "type": "surface" }, n_output).tolist()], subplot_titles=title, ) for k in range(n_output): f = F[:, k] fig = add_surface_plotly(xx, yy, f, fig, alpha=alpha, figrow=1, figcol=k + 1) fig.update_xaxes(title_text=xlabel, row=1, col=k + 1) fig.update_yaxes(title_text=ylabel, row=1, col=k + 1) return fig
def plot_gp_plotly(model, mins: TensorType, maxs: TensorType, grid_density=20): """ :param model: a gpflow model :param mins: list of 2 lower bounds :param maxs: list of 2 upper bounds :param grid_density: integer (grid size) :return: a plotly figure """ mins = to_numpy(mins) maxs = to_numpy(maxs) # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function Fmean, Fvar = model.predict_f(Xplot) n_output = Fmean.shape[1] fig = make_subplots( rows=1, cols=n_output, specs=[np.repeat({"type": "surface"}, n_output).tolist()] ) for k in range(n_output): fmean = Fmean[:, k].numpy() fvar = Fvar[:, k].numpy() lcb = fmean - 2 * np.sqrt(fvar) ucb = fmean + 2 * np.sqrt(fvar) fig = add_surface_plotly(xx, yy, fmean, fig, alpha=1.0, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, lcb, fig, alpha=0.5, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, ucb, fig, alpha=0.5, figrow=1, figcol=k + 1) return fig
def plot_function_2d( obj_func, mins, maxs, grid_density=20, contour=False, log=False, title=None, xlabel=None, ylabel=None, figsize=None, ): """ 2D/3D plot of an obj_func for a grid of size grid_density**2 between mins and maxs :param obj_func: a function that returns a n-array given a [n, d] array :param mins: list of 2 lower bounds :param maxs: list of 2 upper bounds :param grid_density: positive integer for the grid size :param contour: Boolean. If False, a 3d plot is produced :param log: Boolean. If True, the log transformation (log(f - min(f) + 0.1)) is applied :param title: :param xlabel: :param ylabel: :param figsize: """ # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function F = to_numpy(obj_func(Xplot)) if len(F.shape) is 1: F = F.reshape(-1, 1) n_output = F.shape[1] if contour: fig, ax = plt.subplots(1, n_output, squeeze=False, sharex="all", sharey="all", figsize=figsize) else: fig = plt.figure(figsize=figsize) for k in range(F.shape[1]): # Apply log transformation f = F[:, k] if log: f = np.log(f - np.min(f) + 1e-1) # Either plot contour of surface if contour: axx = ax[0, k] else: ax = axx = fig.add_subplot(1, n_output, k + 1, projection="3d") plot_surface(xx, yy, f, axx, contour=contour, alpha=1.0) if title is not None: axx.set_title(title[k]) axx.set_xlabel(xlabel) axx.set_ylabel(ylabel) axx.set_xlim(mins[0], maxs[0]) axx.set_ylim(mins[1], maxs[1]) return fig, ax
def plot_gp_2d( model, mins: TensorType, maxs: TensorType, grid_density=20, contour=False, xlabel=None, ylabel=None, figsize=None, predict_y=False, ): """ 2D/3D plot of a gp model for a grid of size grid_density**2 between mins and maxs :param model: a gpflow model :param mins: 2 lower bounds :param maxs: 2 upper bounds :param grid_density: positive integer for the grid size :param contour: Boolean. If False, a 3d plot is produced :param xlabel: optional string :param ylabel: optional string :param figsize: """ mins = to_numpy(mins) maxs = to_numpy(maxs) # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function if predict_y: Fmean, Fvar = model.predict_y(Xplot) else: Fmean, Fvar = model.predict_f(Xplot) n_output = Fmean.shape[1] if contour: fig, ax = plt.subplots(n_output, 2, squeeze=False, sharex="all", sharey="all", figsize=figsize) ax[0, 0].set_xlim(mins[0], maxs[0]) ax[0, 0].set_ylim(mins[1], maxs[1]) else: fig = plt.figure(figsize=figsize) for k in range(n_output): # Apply log transformation fmean = Fmean[:, k].numpy() fvar = Fvar[:, k].numpy() # Either plot contour of surface if contour: axx = ax[k, 0] plot_surface(xx, yy, fmean, ax[k, 0], contour=contour, alpha=1.0) plot_surface(xx, yy, fvar, ax[k, 1], contour=contour, alpha=1.0) ax[k, 0].set_title("mean") ax[k, 1].set_title("variance") ax[k, 0].set_xlabel(xlabel) ax[k, 0].set_ylabel(ylabel) ax[k, 1].set_xlabel(xlabel) ax[k, 1].set_ylabel(ylabel) else: ax = axx = fig.add_subplot(1, n_output, k + 1, projection="3d") plot_surface(xx, yy, fmean, axx, contour=contour, alpha=0.5) ucb = fmean + 2.0 * np.sqrt(fvar) lcb = fmean - 2.0 * np.sqrt(fvar) plot_surface(xx, yy, ucb, axx, contour=contour, alpha=0.1) plot_surface(xx, yy, lcb, axx, contour=contour, alpha=0.1) axx.set_xlabel(xlabel) axx.set_ylabel(ylabel) axx.set_xlim(mins[0], maxs[0]) axx.set_ylim(mins[1], maxs[1]) return fig, ax
def plot_dgp_plotly( model: DeepGP, mins: TensorType, maxs: TensorType, grid_density: int = 20, num_samples: int = 100, ) -> go.Figure: """ Plots sample-based mean and 2 standard deviations for DGP models in 2 dimensions. :param model: a dgp model :param mins: list of 2 lower bounds :param maxs: list of 2 upper bounds :param grid_density: integer (grid size) :return: a plotly figure """ mins = to_numpy(mins) maxs = to_numpy(maxs) # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function means = [] vars = [] for _ in range(num_samples): Fmean_sample, Fvar_sample = model.predict_f(Xplot) means.append(Fmean_sample) vars.append(Fvar_sample) Fmean = tf.reduce_mean(tf.stack(means), axis=0) Fvar = tf.reduce_mean(tf.stack(vars) + tf.stack(means)**2, axis=0) - Fmean**2 n_output = Fmean.shape[1] fig = make_subplots( rows=1, cols=n_output, specs=[np.repeat({ "type": "surface" }, n_output).tolist()]) for k in range(n_output): fmean = Fmean[:, k].numpy() fvar = Fvar[:, k].numpy() lcb = fmean - 2 * np.sqrt(fvar) ucb = fmean + 2 * np.sqrt(fvar) fig = add_surface_plotly(xx, yy, fmean, fig, alpha=1.0, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, lcb, fig, alpha=0.5, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, ucb, fig, alpha=0.5, figrow=1, figcol=k + 1) return fig