| 75 | return {} |
| 76 | |
| 77 | def format_data_str(self, data: dict, step: int) -> str: |
| 78 | cleaned_data = { |
| 79 | k: ( |
| 80 | v.item() |
| 81 | if hasattr(v, "item") |
| 82 | else float(v) # tensor or numpy scalar |
| 83 | if isinstance(v, (np.integer, np.floating)) |
| 84 | else v # numpy types |
| 85 | ) |
| 86 | for k, v in data.items() |
| 87 | } |
| 88 | # Format floats to reasonable precision using default str (avoids scientific notation and long decimals) |
| 89 | formatted_data = ( |
| 90 | "{" |
| 91 | + ", ".join( |
| 92 | repr(k) + ": " + (f"{v:.6g}" if isinstance(v, float) else repr(v)) |
| 93 | for k, v in cleaned_data.items() |
| 94 | ) |
| 95 | + "}" |
| 96 | ) |
| 97 | return f"Step {step}: {formatted_data}" |
| 98 | |
| 99 | |
| 100 | class TensorboardMonitor(Monitor): |