class_docstr: A training job represents a Sagemaker training job within an experiment.
class_docstr_tok: ["A","training","job","represents","a","Sagemaker","training","job","within","an","experiment","."]
docstr: Fetches the metrics defined by the training script from the training
  job's logs.
  
  For each metric, it returns a 1D NumPy array (ordered chronologically).
code: def metrics(self) -> dict[str, np.ndarray]:
      """
      Fetches the metrics defined by the training script from the training
      job's logs.
  
      For each metric, it returns a 1D NumPy array (ordered chronologically).
      """
      # Check if the logs are already available locally
      metrics_file = self._cache_dir() / "metrics.json"
      if metrics_file.exists():
          with metrics_file.open("r") as f:
              return {
                  k: np.array(v, dtype=np.float32)
                  for k, v in json.load(f).items()
              }
  
      # If not, get them from the logs, write them to the file system and return
      metrics = {
          metric["Name"]: [
              float(x)
              for x in re.findall(metric["Regex"], "\n".join(self.logs))
          ]
          for metric in self.info["AlgorithmSpecification"][
              "MetricDefinitions"
          ]
      }
      with metrics_file.open("w+") as f:
          json.dump(metrics, f)
  
      # Return them as numpy arrays
      return {
          k: np.array(v, dtype=np.float32) for k, v in metrics.items()
      }
  
imports: from __future__ import annotations
  import datetime
  import json
  import logging
  import re
  import shutil
  import tempfile
  import time
  from collections import Counter
  from functools import cached_property, lru_cache
  from pathlib import Path
  from typing import Any, Callable, cast, Dict, Iterator, List, Tuple, Union
  import boto3
  import numpy
  from botocore.exceptions import ClientError
  from .session import default_session
label_desc: Use image node_id 8 for calling the TrainingJob obj's underlying member method code with example usage: obj.metrics() and returns: unknown, unknown