code: def create_optimizer(trial, model):
      # We optimize the choice of optimizers as well as their parameters.
      optimizer_name = trial.suggest_categorical(
          "optimizer", ["Adam", "MomentumSGD"]
      )
      if optimizer_name == "Adam":
          adam_alpha = trial.suggest_float(
              "adam_alpha", 1e-5, 1e-1, log=True
          )
          optimizer = chainer.optimizers.Adam(alpha=adam_alpha)
      else:
          momentum_sgd_lr = trial.suggest_float(
              "momentum_sgd_lr", 1e-5, 1e-1, log=True
          )
          optimizer = chainer.optimizers.MomentumSGD(lr=momentum_sgd_lr)
  
      weight_decay = trial.suggest_float(
          "weight_decay", 1e-10, 1e-3, log=True
      )
      optimizer.setup(model)
      optimizer.add_hook(chainer.optimizer.WeightDecay(weight_decay))
      return optimizer
  
file_path: chainer/examples/optuna/chainer_simple.py
imports: import chainer
  import chainer.functions
  import chainer.links
  import numpy
  from packaging import version
  import optuna
  from optuna.integration import ChainerPruningExtension
label_desc: Use image node_id 2 for calling a global function with example usage: create_optimizer(trial, model) and returns: optimizer