1313
1414import matplotlib .pyplot as plt
1515import numpy as np
16+ import torch
1617
1718from tests .paths import TRAJECTORIES_RESULTS_DIR
1819from trajectories ._constants import (
2223 LATEX_NAMES ,
2324 OBJECTIVES ,
2425)
25- from trajectories ._objectives import ElementWiseQuadratic , WithSPSMappingMixin
26+ from trajectories ._objectives import ElementWiseQuadratic , ValleyBowl , WithSPSMappingMixin
2627from trajectories ._optimization import compute_gradient_cosine_similarities
2728from trajectories ._pareto_utils import sample_2d_spss
2829from trajectories ._paths import get_param_plots_dir , get_params_dir
@@ -84,6 +85,11 @@ def main() -> None:
8485
8586 common_plotter = SquareBoxAspectSetter ()
8687
88+ if isinstance (objective , ValleyBowl ):
89+ sps_points = objective .pareto_set ().numpy ()
90+ main_content = np .concatenate ([main_content , sps_points ])
91+ common_plotter += SPSPlotter (sps_points )
92+
8793 if objective .n_values == 2 and isinstance (objective , WithSPSMappingMixin ):
8894 sps_points = sample_2d_spss (objective ).numpy ()
8995 main_content = np .concatenate ([main_content , sps_points ])
@@ -97,7 +103,22 @@ def main() -> None:
97103 adjust_plotter = ContentLimAdjuster (main_content )
98104 common_plotter += adjust_plotter
99105
100- if objective .n_values == 2 :
106+ if isinstance (objective , ValleyBowl ):
107+ x0 = torch .linspace (adjust_plotter .xlim [0 ], adjust_plotter .xlim [1 ], 200 )
108+ x1 = torch .linspace (adjust_plotter .ylim [0 ], adjust_plotter .ylim [1 ], 200 )
109+ grid = torch .cartesian_prod (x0 , x1 )
110+ mean_loss = torch .vmap (objective )(grid ).mean (dim = 1 ).reshape (200 , 200 )
111+ common_plotter += HeatmapPlotter (
112+ values = mean_loss .numpy (),
113+ x_min = adjust_plotter .xlim [0 ],
114+ x_max = adjust_plotter .xlim [1 ],
115+ y_min = adjust_plotter .ylim [0 ],
116+ y_max = adjust_plotter .ylim [1 ],
117+ vmin = float (mean_loss .min ()),
118+ vmax = float (mean_loss .max ()),
119+ cmap = "viridis" ,
120+ )
121+ elif objective .n_values == 2 :
101122 similarities = compute_gradient_cosine_similarities (
102123 objective ,
103124 x0_min = adjust_plotter .xlim [0 ],
@@ -147,6 +168,10 @@ def main() -> None:
147168 plotter += YAxisLabeller ("$x_2$" ) if j == 0 else YTicksClearer ()
148169
149170 plotter (axes [i ][j ])
171+ if isinstance (objective , ValleyBowl ):
172+ axes [i ][j ].scatter (
173+ * objective .optimum .tolist (), marker = "*" , color = "black" , s = 45 , zorder = 10
174+ )
150175
151176 fig .tight_layout (h_pad = - 2.5 )
152177 print ("Saving figure" )
0 commit comments