class_docstr: A dataclass containing all of the information to make an objective function.
class_docstr_tok: ["A","dataclass","containing","all","of","the","information","to","make","an","objective","function","."]
docstr: Suggest parameters then train the model.
code: def __call__(self, trial: Trial) -> Optional[float]:
      """Suggest parameters then train the model."""
      if self.model_kwargs is not None:
          problems = [
              x
              for x in (
                  "loss",
                  "regularizer",
                  "optimizer",
                  "lr_scheduler",
                  "training",
                  "negative_sampler",
                  "stopper",
              )
              if x in self.model_kwargs
          ]
          if problems:
              raise ValueError(
                  f"model_kwargs should not have: {problems}. {self}"
              )
  
      # 2. Model
      _model_kwargs = _get_kwargs(
          trial=trial,
          prefix="model",
          default_kwargs_ranges=self.model.hpo_default,
          kwargs=self.model_kwargs,
          kwargs_ranges=self.model_kwargs_ranges,
      )
  
      try:
          loss_default_kwargs_ranges = self.loss.hpo_default
      except AttributeError:
          logger.warning(
              "using a loss function with no hpo_default field: %s",
              self.loss,
          )
          loss_default_kwargs_ranges = {}
  
      # 3. Loss
      _loss_kwargs = _get_kwargs(
          trial=trial,
          prefix="loss",
          default_kwargs_ranges=loss_default_kwargs_ranges,
          kwargs=self.loss_kwargs,
          kwargs_ranges=self.loss_kwargs_ranges,
      )
      # 4. Regularizer
      _regularizer_kwargs: Optional[Mapping[str, Any]]
      if self.regularizer is None:
          _regularizer_kwargs = {}
      else:
          _regularizer_kwargs = _get_kwargs(
              trial=trial,
              prefix="regularizer",
              default_kwargs_ranges=self.regularizer.hpo_default,
              kwargs=self.regularizer_kwargs,
              kwargs_ranges=self.regularizer_kwargs_ranges,
          )
      # 5. Optimizer
      _optimizer_kwargs = _get_kwargs(
          trial=trial,
          prefix="optimizer",
          default_kwargs_ranges=optimizers_hpo_defaults[self.optimizer],
          kwargs=self.optimizer_kwargs,
          kwargs_ranges=self.optimizer_kwargs_ranges,
      )
      # 5.1 Learning Rate Scheduler
      _lr_scheduler_kwargs: Optional[Mapping[str, Any]] = None
      if self.lr_scheduler is not None:
          _lr_scheduler_kwargs = _get_kwargs(
              trial=trial,
              prefix="lr_scheduler",
              default_kwargs_ranges=lr_schedulers_hpo_defaults[
                  self.lr_scheduler
              ],
              kwargs=self.lr_scheduler_kwargs,
              kwargs_ranges=self.lr_scheduler_kwargs_ranges,
          )
  
      _negative_sampler_kwargs: Mapping[str, Any]
      if self.training_loop is not SLCWATrainingLoop:
          _negative_sampler_kwargs = {}
      elif self.negative_sampler is None:
          raise ValueError(
              "Negative sampler class must be made explicit when training under sLCWA"
          )
      else:
          # TODO this fixes the issue for negative samplers, but does not generally address it.
          #  For example, some of them obscure their arguments with **kwargs, so should we look
          #  at the parent class? Sounds like something to put in class resolver by using the
          #  inspect module. For now, this solution will rely on the fact that the sampler is a
          #  direct descendent of a parent NegativeSampler
          direct_params = inspect.signature(
              self.negative_sampler
          ).parameters
          parent_params = inspect.signature(
              self.negative_sampler.__bases__[0]
          ).parameters
          valid_keys = set(direct_params).union(parent_params) - {
              "kwargs"
          }
          invalid_keys = (
              set(self.negative_sampler_kwargs_ranges or [])
              - valid_keys
          )
          if invalid_keys:
              raise ExtraKeysError(invalid_keys)
          _negative_sampler_kwargs = _get_kwargs(
              trial=trial,
              prefix="negative_sampler",
              default_kwargs_ranges=self.negative_sampler.hpo_default,
              kwargs=self.negative_sampler_kwargs,
              kwargs_ranges=self.negative_sampler_kwargs_ranges,
          )
  
      _training_kwargs = _get_kwargs(
          trial=trial,
          prefix="training",
          default_kwargs_ranges=self.training_loop.hpo_default,
          kwargs=self.training_kwargs,
          kwargs_ranges=self.training_kwargs_ranges,
      )
  
      # a fixed checkpoint_name leads avoid collision across trials
      checkpoint_name = _training_kwargs.get("checkpoint_name", None)
      if checkpoint_name:
          raise ValueError(
              f"Cannot set a fixed {checkpoint_name=} across all trials; if you want to save the final model per "
              f"trial, use `save_model_directory` instead!",
          )
  
      # create result tracker to allow to gracefully close failed trials
      result_tracker = tracker_resolver.make(
          query=self.result_tracker,
          pos_kwargs=self.result_tracker_kwargs,
      )
  
      _stopper_kwargs = dict(self.stopper_kwargs or {})
      if self.stopper is not None and issubclass(
          self.stopper, EarlyStopper
      ):
          self._update_stopper_callbacks(
              _stopper_kwargs,
              trial,
              metric=self.metric,
              result_tracker=result_tracker,
          )
  
      try:
          result = pipeline(
              # 1. Dataset
              dataset=self.dataset,
              dataset_kwargs=self.dataset_kwargs,
              training=self.training,
              testing=self.testing,
              validation=self.validation,
              evaluation_entity_whitelist=self.evaluation_entity_whitelist,
              evaluation_relation_whitelist=self.evaluation_relation_whitelist,
              # 2. Model
              model=self.model,
              model_kwargs=_model_kwargs,
              # 3. Loss
              loss=self.loss,
              loss_kwargs=_loss_kwargs,
              # 4. Regularizer
              regularizer=self.regularizer,
              regularizer_kwargs=_regularizer_kwargs,
              # 5. Optimizer
              optimizer=self.optimizer,
              optimizer_kwargs=_optimizer_kwargs,
              clear_optimizer=True,
              # 5.1 Learning Rate Scheduler
              lr_scheduler=self.lr_scheduler,
              lr_scheduler_kwargs=_lr_scheduler_kwargs,
              # 6. Training Loop
              training_loop=self.training_loop,
              negative_sampler=self.negative_sampler,
              negative_sampler_kwargs=_negative_sampler_kwargs,
              # 7. Training
              training_loop_kwargs=self.training_loop_kwargs,
              training_kwargs=_training_kwargs,
              stopper=self.stopper,
              stopper_kwargs=_stopper_kwargs,
              # 8. Evaluation
              evaluator=self.evaluator,
              evaluator_kwargs=self.evaluator_kwargs,
              evaluation_kwargs=self.evaluation_kwargs,
              filter_validation_when_testing=self.filter_validation_when_testing,
              # 9. Tracker
              result_tracker=result_tracker,
              result_tracker_kwargs=None,
              # Misc.
              use_testing_data=False,  # use validation set during HPO!
              device=self.device,
          )
      except (MemoryError, RuntimeError) as e:
          # close run in result tracker
          result_tracker.end_run(success=False)
          # raise the error again (which will be catched in study.optimize)
          raise e
      else:
          if self.save_model_directory:
              model_directory = os.path.join(
                  self.save_model_directory, str(trial.number)
              )
              os.makedirs(model_directory, exist_ok=True)
              result.save_to_directory(model_directory)
  
          trial.set_user_attr("random_seed", result.random_seed)
  
          for k, v in result.metric_results.to_flat_dict().items():
              trial.set_user_attr(k, v)
  
          return result.metric_results.get_metric(self.metric)
  
imports: import dataclasses
  import ftplib
  import inspect
  import json
  import logging
  import os
  import pathlib
  from dataclasses import dataclass
  from typing import Any, Callable, Collection, Dict, Iterable, Mapping, Optional, Type, Union, cast
  import torch
  from class_resolver.contrib.optuna import pruner_resolver, sampler_resolver
  from optuna import Study, Trial, TrialPruned, create_study
  from optuna.pruners import BasePruner
  from optuna.samplers import BaseSampler
  from optuna.storages import BaseStorage
  from ..constants import USER_DEFINED_CODE
  from ..datasets import dataset_resolver, has_dataset
  from ..datasets.base import Dataset
  from ..evaluation import Evaluator, evaluator_resolver
  from ..losses import Loss, loss_resolver
  from ..lr_schedulers import LRScheduler, lr_scheduler_resolver, lr_schedulers_hpo_defaults
  from ..models import Model, model_resolver
  from ..optimizers import Optimizer, optimizer_resolver, optimizers_hpo_defaults
  from ..pipeline import pipeline, replicate_pipeline_from_config
  from ..regularizers import Regularizer, regularizer_resolver
  from ..sampling import NegativeSampler, negative_sampler_resolver
  from ..stoppers import EarlyStopper, Stopper, stopper_resolver
  from ..trackers import ResultTracker, tracker_resolver
  from ..training import SLCWATrainingLoop, TrainingLoop, training_loop_resolver
  from ..triples import CoreTriplesFactory
  from ..typing import Hint, HintType
  from ..utils import Result, ensure_ftp_directory, fix_dataclass_init_docs, get_df_io, get_json_bytes_io, normalize_path
  from ..version import get_git_hash, get_version
label_desc: Use image node_id 2 for calling the Objective obj's underlying member method code with example usage: obj.__call__(trial) and returns: result