Skip to content

Updated train metrics generation for AutoMLX model #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ads/opctl/operator/lowcode/forecast/model/automlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def _build_model(self) -> pd.DataFrame:
self.models[s_id] = {}
self.models[s_id]["model"] = model
self.models[s_id]["le"] = self.le[s_id]
self.models[s_id]["score"] = self.get_validation_score_and_metric(model)

# In case of Naive model, model.forecast function call does not return confidence intervals.
if f"{target}_ci_upper" not in summary_frame:
Expand Down Expand Up @@ -511,3 +512,31 @@ def explain_model(self):
f"Failed to generate explanations for series {s_id} with error: {e}."
)
logger.debug(f"Full Traceback: {traceback.format_exc()}")

def get_validation_score_and_metric(self, model):
trials = model.completed_trials_summary_
model_params = model.selected_model_params_
if len(trials) > 0:
score_col = [col for col in trials.columns if "Score" in col][0]
validation_score = trials[trials.Hyperparameters == model_params][score_col].iloc[0]
else:
validation_score = 0
return -1 * validation_score

def generate_train_metrics(self) -> pd.DataFrame:
"""
Generate Training Metrics when fitted data is not available.
"""
total_metrics = pd.DataFrame()
for s_id in self.forecast_output.list_series_ids():
try:
metrics = {self.spec.metric.upper(): self.models[s_id]["score"]}
metrics_df = pd.DataFrame.from_dict(metrics, orient="index", columns=[s_id])
logger.warning("AutoMLX failed to generate training metrics. Recovering validation loss instead")
total_metrics = pd.concat([total_metrics, metrics_df], axis=1)
except Exception as e:
logger.debug(
f"Failed to generate training metrics for target_series: {s_id}"
)
logger.debug(f"Error: {e}")
return total_metrics