code: def train_with_config(
      dataset: Union[str, pd.DataFrame, dd.core.DataFrame],
      config: dict,
      output_directory: str = OUTPUT_DIR,
      random_seed: int = default_random_seed,
      **kwargs,
  ) -> AutoTrainResults:
      """Performs hyperparameter optimization with respect to the given config and selects the best model.
  
      # Inputs
      :param dataset: (str) filepath to dataset.
      :param config: (dict) optional Ludwig configuration to use for training, defaults
                     to `create_auto_config`.
      :param output_directory: (str) directory into which to write results, defaults to
          current working directory.
      :param random_seed: (int, default: `42`) a random seed that will be used anywhere
                          there is a call to a random number generator, including
                          hyperparameter search sampling, as well as data splitting,
                          parameter initialization and training set shuffling
      :param kwargs: additional keyword args passed down to `ludwig.hyperopt.run.hyperopt`.
  
      # Returns
      :return: (AutoTrainResults) results containing hyperopt experiments and best model
      """
      _ray_init()
  
      model_type = get_model_type(config)
      hyperopt_results = _train(
          config,
          dataset,
          output_directory=output_directory,
          model_name=model_type,
          random_seed=random_seed,
          **kwargs,
      )
      # catch edge case where metric_score is nan
      # TODO (ASN): Decide how we want to proceed if at least one trial has
      # completed
      for trial in hyperopt_results.ordered_trials:
          if isinstance(trial.metric_score, str) or np.isnan(
              trial.metric_score
          ):
              warnings.warn(
                  "There was an error running the experiment. "
                  "A trial failed to start. "
                  "Consider increasing the time budget for experiment. "
              )
  
      # Extract credentials needed to pull artifacts, if provided
      creds = None
      backend: Backend = initialize_backend(kwargs.get("backend"))
      if backend is not None:
          creds = backend.storage.artifacts.credentials
  
      experiment_analysis = hyperopt_results.experiment_analysis
      return AutoTrainResults(experiment_analysis, creds)
  
imports: import argparse
  import copy
  import logging
  import os
  import warnings
  from typing import Any, Dict, List, Optional, Union
  import numpy
  import pandas
  import yaml
  from ludwig.api import LudwigModel
  from ludwig.api_annotations import PublicAPI
  from ludwig.automl.base_config import create_default_config, DatasetInfo, get_dataset_info, get_features_config, get_reference_configs
  from ludwig.backend import Backend, initialize_backend
  from ludwig.constants import AUTO, AUTOML_DEFAULT_IMAGE_ENCODER, AUTOML_DEFAULT_TABULAR_MODEL, AUTOML_DEFAULT_TEXT_ENCODER, BINARY, CATEGORY, ENCODER, HYPEROPT, IMAGE, INPUT_FEATURES, NAME, NUMBER, OUTPUT_FEATURES, TABULAR, TEXT, TRAINER, TYPE
  from ludwig.contrib import add_contrib_callback_args
  from ludwig.data.cache.types import CacheableDataset
  from ludwig.datasets import load_dataset_uris
  from ludwig.globals import LUDWIG_VERSION
  from ludwig.hyperopt.run import hyperopt
  from ludwig.schema.model_config import ModelConfig
  from ludwig.types import ModelConfigDict
  from ludwig.utils.automl.ray_utils import _ray_init
  from ludwig.utils.automl.utils import _add_transfer_config, get_model_type, set_output_feature_metric
  from ludwig.utils.data_utils import load_dataset, use_credentials
  from ludwig.utils.defaults import default_random_seed
  from ludwig.utils.fs_utils import open_file
  from ludwig.utils.heuristics import get_auto_learning_rate
  from ludwig.utils.misc_utils import merge_dict
  from ludwig.utils.print_utils import print_ludwig
label_desc: Use image node_id 5 for calling a global function with example usage: train_with_config(dataset, config, output_directory, random_seed) and returns: AutoTrainResults