Safely retrieves value of the metric logged in LightningModule. :param metric_dict: A dict containing metric values. :param metric_name: If provided, the name of the metric to retrieve. :return: If a metric name was provided, the value of the metric.
(metric_dict: Dict[str, Any], metric_name: Optional[str])
| 99 | |
| 100 | |
| 101 | def get_metric_value(metric_dict: Dict[str, Any], metric_name: Optional[str]) -> Optional[float]: |
| 102 | """Safely retrieves value of the metric logged in LightningModule. |
| 103 | |
| 104 | :param metric_dict: A dict containing metric values. |
| 105 | :param metric_name: If provided, the name of the metric to retrieve. |
| 106 | :return: If a metric name was provided, the value of the metric. |
| 107 | """ |
| 108 | if not metric_name: |
| 109 | log.info("Metric name is None! Skipping metric value retrieval...") |
| 110 | return None |
| 111 | |
| 112 | if metric_name not in metric_dict: |
| 113 | raise Exception( |
| 114 | f"Metric value not found! <metric_name={metric_name}>\n" |
| 115 | "Make sure metric name logged in LightningModule is correct!\n" |
| 116 | "Make sure `optimized_metric` name in `hparams_search` config is correct!" |
| 117 | ) |
| 118 | |
| 119 | metric_value = metric_dict[metric_name].item() |
| 120 | log.info(f"Retrieved metric value! <{metric_name}={metric_value}>") |
| 121 | |
| 122 | return metric_value |
| 123 | |
| 124 | |
| 125 | def read_strings_from_txt(path: str) -> List[str]: |