class_docstr: Saves model topology, losses & metrics
class_docstr_tok: ["Saves","model","topology",",","losses","&","metrics"]
docstr: Call watch method to log model topology, gradients & weights
docstr_tok: ["Call","watch","method","to","log","model","topology",",","gradients","&","weights"]
code: def before_fit(self):
      "Call watch method to log model topology, gradients & weights"
      # Check if wandb.init has been called
      if wandb.run is None:
          raise ValueError(
              "You must call wandb.init() before WandbCallback()"
          )
      # W&B log step
      self._wandb_step = (
          wandb.run.step - 1
      )  # -1 except if the run has previously logged data (incremented at each batch)
      self._wandb_epoch = (
          0
          if not (wandb.run.step)
          else math.ceil(wandb.run.summary["epoch"])
      )  # continue to next epoch
  
      self.run = (
          not hasattr(self.learn, "lr_finder")
          and not hasattr(self, "gather_preds")
          and rank_distrib() == 0
      )
      if not self.run:
          return
  
      # Log config parameters
      log_config = self.learn.gather_args()
      _format_config(log_config)
      try:
          wandb.config.update(log_config, allow_val_change=True)
      except Exception as e:
          print(f"WandbCallback could not log config parameters -> {e}")
  
      if not WandbCallback._wandb_watch_called:
          WandbCallback._wandb_watch_called = True
          # Logs model topology and optionally gradients and weights
          if self.log is not None:
              wandb.watch(self.learn.model, log=self.log)
  
      # log dataset
      assert isinstance(
          self.log_dataset, (str, Path, bool)
      ), "log_dataset must be a path or a boolean"
      if self.log_dataset is True:
          if Path(self.dls.path) == Path("."):
              print(
                  'WandbCallback could not retrieve the dataset path, please provide it explicitly to "log_dataset"'
              )
              self.log_dataset = False
          else:
              self.log_dataset = self.dls.path
      if self.log_dataset:
          self.log_dataset = Path(self.log_dataset)
          assert (
              self.log_dataset.is_dir()
          ), f"log_dataset must be a valid directory: {self.log_dataset}"
          metadata = {
              "path relative to learner": os.path.relpath(
                  self.log_dataset, self.learn.path
              )
          }
          log_dataset(
              path=self.log_dataset,
              name=self.dataset_name,
              metadata=metadata,
          )
  
      if self.log_preds:
          try:
              if not self.valid_dl:
                  # Initializes the batch watched
                  wandbRandom = random.Random(
                      self.seed
                  )  # For repeatability
                  self.n_preds = min(
                      self.n_preds, len(self.dls.valid_ds)
                  )
                  idxs = wandbRandom.sample(
                      range(len(self.dls.valid_ds)), self.n_preds
                  )
                  if isinstance(self.dls, TabularDataLoaders):
                      test_items = getattr(
                          self.dls.valid_ds.items,
                          "iloc",
                          self.dls.valid_ds.items,
                      )[idxs]
                      self.valid_dl = self.dls.test_dl(
                          test_items, with_labels=True, process=False
                      )
                  else:
                      test_items = [
                          getattr(
                              self.dls.valid_ds.items,
                              "iloc",
                              self.dls.valid_ds.items,
                          )[i]
                          for i in idxs
                      ]
                      self.valid_dl = self.dls.test_dl(
                          test_items, with_labels=True
                      )
              self.learn.add_cb(
                  FetchPredsCallback(
                      dl=self.valid_dl,
                      with_input=True,
                      with_decoded=True,
                      reorder=self.reorder,
                  )
              )
          except Exception as e:
              self.log_preds = False
              print(
                  f"WandbCallback was not able to prepare a DataLoader for logging prediction samples -> {e}"
              )
  
imports: from __future__ import annotations
  from ..basics import *
  from .progress import *
  from ..text.data import TensorText
  from ..tabular.all import TabularDataLoaders, Tabular
  from .hook import total_params
  from .tracker import SaveModelCallback
  import wandb
label_desc: Use image node_id 3 for calling the WandbCallback obj's underlying member method code with example usage: obj.before_fit() without return types