# Using hyperparameter-search in Trainer

**URL:** <https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785>\
**Category:** 🤗Transformers\
**Created:** [August 20, 2020, 12:58pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785 "2020-08-20T12:58:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [September 14, 2020, 1:39pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/22 "2020-09-14T13:39:37Z")

</div>

Note that you can use pretty much anything in `optuna` and `Ray Tune` by just subclassing the `Trainer` and overriding the proper methods.

---

<div class="post-metadata">

**Author:** ![quentin](https://avatars.discourse-cdn.com/v4/letter/q/e47c2d/32.png) [@quentin](https://discuss.huggingface.co/u/quentin)\
**Post date:** [October 16, 2020, 10:21pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/23 "2020-10-16T22:21:54Z")

</div>

I’m having some issues with this, under the `optuna` backend. Here is my hyperparameter space :

```auto
def hyperparameter_space(trial):

    return {
        "learning_rate": trial.suggest_float("learning_rate", 1e-6, 1e-4, log=True),
        "per_device_train_batch_size": trial.suggest_categorical("per_device_train_batch_size", [8, 16, 32]),
        "weight_decay": trial.suggest_float("weight_decay", 1e-12, 1e-1, log=True),
        "adam_epsilon": trial.suggest_float("adam_epsilon", 1e-10, 1e-6, log=True)
    }

```

When I call `trainer.hyperparameter_search` on this, I find that it varies the number of epochs, too, despite these being fixed in `TrainingArguments` to 5. The run that’s going now has run 5-epoch trials a few times but now it’s running a 20-epoch trial… Has anyone observed anything like this ?

Thank you very much.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [October 19, 2020, 3:02pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/24 "2020-10-19T15:02:33Z")

</div>

That may be linked to some bug I fixed a few weeks ago with the `Trainer` modifying its `TrainingArguments`: it used to change the value of `max_steps` which would then change the number of epochs for you since you are changing the batch size.

Can you check if you get this behavior on current master?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [October 22, 2020, 3:18pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/25 "2020-10-22T15:18:22Z")

</div>

Hi @sgugger, in case you’re not aware of it, it seems the latest commit on [master](https://github.com/huggingface/transformers/commit/e174bfeb340d3d3468d9c8eebce95c42aa2dcf84) broke the [Colab notebook](https://twitter.com/GuggerSylvain/status/1318215507242291202) you shared on Twitter

Trying to run that notebook, I hit the following error when trying to run

```auto
best_run = trainer.hyperparameter_search(n_trials=10, direction="maximize")

```

with the optuna backend.

Stack trace:

```auto
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:900: RuntimeWarning:

invalid value encountered in double_scalars

[W 2020-10-22 14:58:41,815] Trial 0 failed because of the following error: RuntimeError("Metrics contains more entries than just 'eval_loss', 'epoch' and 'total_flos', please provide your own compute_objective function.",)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/optuna/study.py", line 799, in _run_trial
    result = func(trial)
  File "/usr/local/lib/python3.6/dist-packages/transformers/integrations.py", line 114, in _objective
    trainer.train(model_path=model_path, trial=trial)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 803, in train
    self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 855, in _maybe_log_save_evaluate
    self._report_to_hp_search(trial, epoch, metrics)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 537, in _report_to_hp_search
    self.objective = self.compute_objective(metrics.copy())
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer_utils.py", line 120, in default_compute_objective
    "Metrics contains more entries than just 'eval_loss', 'epoch' and 'total_flos', please provide your own compute_objective function."
RuntimeError: Metrics contains more entries than just 'eval_loss', 'epoch' and 'total_flos', please provide your own compute_objective function.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-26-12c3f54763db> in <module>()
----> 1 best_run = trainer.hyperparameter_search(n_trials=10, direction="maximize")

10 frames
/usr/local/lib/python3.6/dist-packages/transformers/trainer_utils.py in default_compute_objective(metrics)
    118 if len(metrics) != 0:
    119 raise RuntimeError(
--> 120 "Metrics contains more entries than just 'eval_loss', 'epoch' and 'total_flos', please provide your own compute_objective function."
    121 )
    122 return loss

RuntimeError: Metrics contains more entries than just 'eval_loss', 'epoch' and 'total_flos', please provide your own compute_objective function.

```

I tried passing the `dict` produced by `trainer.evaluate()` in the `compute_objective` arg, but this complains that `TypeError: 'dict' object is not callable`.

I’d be happy to fix the docs / code if you can give me some pointers on where to start!

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [October 22, 2020, 6:39pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/26 "2020-10-22T18:39:10Z")

</div>

I did realize and this has been fixed in [this commit](https://github.com/huggingface/transformers/commit/06fc3954a1ed4bf8351143cf561761d9e8fda65b). Thanks for warning me 🙂

---

<div class="post-metadata">

**Author:** ![quentin](https://avatars.discourse-cdn.com/v4/letter/q/e47c2d/32.png) [@quentin](https://discuss.huggingface.co/u/quentin)\
**Post date:** [October 23, 2020, 7:04pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/27 "2020-10-23T19:04:50Z")

</div>

@sgugger Early tests from the master branch seem to indicate the change in epochs is gone ( unless I’m getting very unlucky with random numbers )… Actually, I’m no longer seeing this on the latest pip release either, I think. Thanks ! Doing great things.

---

<div class="post-metadata">

**Author:** ![abarbosa](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/abarbosa/32/2870_2.png) [@abarbosa](https://discuss.huggingface.co/u/abarbosa)\
**Post date:** [November 29, 2020, 4:51pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/28 "2020-11-29T16:51:08Z")

</div>

Hi @sgugger! Do you have any suggestion about how I should be able to use the hyperparameter-search from Trainer with `optuna` as backend and integrate it with `wandb`?

When I try to do it it returns that I can only use only one `wandb` per model ):

It throws the following error:  
` You can only call wandb.watch once per model. Pass a new instance of the model if you need to call wandb.watch again in your code.`

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [November 30, 2020, 2:12pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/29 "2020-11-30T14:12:47Z")

</div>

I have no idea of where the problem lies. I’ll look at it when I have some time, but we usually let the maintainers of the third-party libraries like optuna and wandb fix the integrations themselves as they know their tools better 🙂

---

<div class="post-metadata">

**Author:** ![tr3cks](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tr3cks](https://discuss.huggingface.co/u/tr3cks)\
**Post date:** [December 8, 2020, 12:28pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/30 "2020-12-08T12:28:33Z")

</div>

Hi @sgugger! I’m trying to train my model using `PopulationBasedTraining` from `ray`. This is how I’m doing the search:

```auto
from ray.tune.schedulers import PopulationBasedTraining
from ray.tune import uniform
from random import randint

scheduler = PopulationBasedTraining(
    mode = "max",
    metric='mean_accuracy',
    perturbation_interval=2,
    hyperparam_mutations={
        "weight_decay": lambda: uniform(0.0, 0.3),
        "learning_rate": lambda: uniform(1e-5, 5e-5),
        "per_gpu_train_batch_size": [16, 32, 64],
        "num_train_epochs": [2,3,4],
        "warmup_steps":lambda: randint(0, 500)
    }
)

best_trial = trainer.hyperparameter_search(
    direction="maximize",
    backend="ray",
    n_trials=4,
    keep_checkpoints_num=1,
    scheduler=scheduler)

```

I’m getting this error:

```auto
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/trial_runner.py", line 726, in _process_trial
    result = self.trial_executor.fetch_result(trial)
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/ray_trial_executor.py", line 489, in fetch_result
    result = ray.get(trial_future[0], timeout=DEFAULT_GET_TIMEOUT)
  File "/usr/local/lib/python3.6/dist-packages/ray/worker.py", line 1452, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(TuneError): ray::ImplicitFunc.train() (pid=1971, ip=172.28.0.2)
  File "python/ray/_raylet.pyx", line 482, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 436, in ray._raylet.execute_task.function_executor
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/trainable.py", line 336, in train
    result = self.step()
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/function_runner.py", line 366, in step
    self._report_thread_runner_error(block=True)
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/function_runner.py", line 513, in _report_thread_runner_error
    .format(err_tb_str)))
ray.tune.error.TuneError: Trial raised an exception. Traceback:
ray::ImplicitFunc.train() (pid=1971, ip=172.28.0.2)
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/function_runner.py", line 248, in run
    self._entrypoint()
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/function_runner.py", line 316, in entrypoint
    self._status_reporter.get_checkpoint())
  File "/usr/local/lib/python3.6/dist-packages/ray/tune/function_runner.py", line 575, in _trainable_func
    output = fn()
  File "/usr/local/lib/python3.6/dist-packages/transformers/integrations.py", line 180, in _objective
    trainer.train(model_path=model_path, trial=trial)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 577, in train
    self._hp_search_setup(trial)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 519, in _hp_search_setup
    value = type(old_attr)(value)
TypeError: float() argument must be a string or a number, not 'Float'

```

I have tried to use `hp_space` instead of defining the parameters inside of the scheduler, but the parameters don’t appear on the training and I get a similar error with `ìnt` instead of `float`.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [December 8, 2020, 1:29pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/31 "2020-12-08T13:29:11Z")

</div>

I have no idea of why you get this type `Float` that is not castable to `float`. If this is the return type of `ray.tune.uniform`, I think you might have to add something to convert it to a regular Python `float` in your `lambda` functions.

---

<div class="post-metadata">

**Author:** ![tr3cks](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tr3cks](https://discuss.huggingface.co/u/tr3cks)\
**Post date:** [December 8, 2020, 3:00pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/32 "2020-12-08T15:00:30Z")

</div>

I don’t know what really happened but I changed parameters to:

```auto
hyperparam_mutations={
        "weight_decay": tune.uniform(0.0, 0.3),
        "learning_rate": tune.uniform(1e-5, 5e-5),
        "per_device_train_batch_size": tune.choice([16, 32, 64]),
        "num_train_epochs": tune.choice([2,3,4]),
        "warmup_steps":tune.choice(range(0, 500))
    }

```

and it seems to work.

But now I have another problem. I created a custom function to return accuracy, which is passed to the trainer. I want to use that accuracy as metric in `ray`. I saw the example `compute_objective` function that you posted, but I don’t know what is `metrics` and how to use accuracy.

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [December 11, 2020, 10:24am UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/33 "2020-12-11T10:24:42Z")

</div>

Hi @tr3cks

here `metrics` is dict which contains the metrics you defined, loss, accuracy etc.

So to use accuracy as metric/objective for hparam search, you should return accuracy value from the `compute_objective` function.

If your key is `accuracy` then you could return `metrics["accuracy"]` from the `compute_objective` function.

---

<div class="post-metadata">

**Author:** ![sonam](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sonam/32/1211_2.png) [@sonam](https://discuss.huggingface.co/u/sonam)\
**Post date:** [March 15, 2021, 1:24pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/34 "2021-03-15T13:24:46Z")

</div>

Hi @sgugger, I am using raytune with huggingface for hyperparameter tunning, here is my code snippet:

```
from ray.tune.schedulers import PopulationBasedTraining
from ray.tune import uniform
from random import randint
scheduler = PopulationBasedTraining(
    mode = "max",
    metric='mean_accuracy',
    perturbation_interval=2,
    hyperparam_mutations={
        "weight_decay": lambda: uniform(0.0, 0.3),
        "learning_rate": lambda: uniform(1e-5, 5e-5),
        "per_gpu_train_batch_size": [16, 32, 64],
        "num_train_epochs": [2,3,4],
        "warmup_steps":lambda: randint(0, 500)
    }
)

best_trial = trainer.hyperparameter_search(
    direction="maximize",
    backend="ray",
    n_trials=4,
    keep_checkpoints_num=1,
    scheduler=scheduler)

```

However, this code results in the following error:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/trial_runner.py", line 586, in _process_trial
    results = self.trial_executor.fetch_result(trial)
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/ray_trial_executor.py", line 609, in fetch_result
    result = ray.get(trial_future[0], timeout=DEFAULT_GET_TIMEOUT)
  File "/usr/local/lib/python3.7/dist-packages/ray/_private/client_mode_hook.py", line 47, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/ray/worker.py", line 1456, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(TuneError): e[36mray::ImplicitFunc.train_buffered()e[39m (pid=800, ip=172.28.0.2)
  File "python/ray/_raylet.pyx", line 480, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 432, in ray._raylet.execute_task.function_executor
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/trainable.py", line 167, in train_buffered
    result = self.train()
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/trainable.py", line 226, in train
    result = self.step()
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 366, in step
    self._report_thread_runner_error(block=True)
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 513, in _report_thread_runner_error
    ("Trial raised an exception. Traceback:\n{}".format(err_tb_str)
ray.tune.error.TuneError: Trial raised an exception. Traceback:
e[36mray::ImplicitFunc.train_buffered()e[39m (pid=800, ip=172.28.0.2)
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 248, in run
    self._entrypoint()
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 316, in entrypoint
    self._status_reporter.get_checkpoint())
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 576, in _trainable_func
    output = fn()
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 651, in _inner
    inner(config, checkpoint_dir=None)
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/function_runner.py", line 644, in inner
    fn_kwargs[k] = parameter_registry.get(prefix + k)
  File "/usr/local/lib/python3.7/dist-packages/ray/tune/registry.py", line 167, in get
    return ray.get(self.references[k])
  File "/usr/local/lib/python3.7/dist-packages/ray/_private/client_mode_hook.py", line 47, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/ray/serialization.py", line 245, in deserialize_objects
    self._deserialize_object(data, metadata, object_ref))
  File "/usr/local/lib/python3.7/dist-packages/ray/serialization.py", line 192, in _deserialize_object
    return self._deserialize_msgpack_data(data, metadata_fields)
  File "/usr/local/lib/python3.7/dist-packages/ray/serialization.py", line 170, in _deserialize_msgpack_data
    python_objects = self._deserialize_pickle5_data(pickle5_data)
  File "/usr/local/lib/python3.7/dist-packages/ray/serialization.py", line 160, in _deserialize_pickle5_data
    obj = pickle.loads(in_band)
ModuleNotFoundError: No module named 'datasets_modules'

```

I would really appreciate if I could be help to identify the cause of this problem, thanks! 🙂

Note: I have dataset correctly imported, everything works as expected except this snippet results in the error mentioned.

---

<div class="post-metadata">

**Author:** ![pierreguillou](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pierreguillou/32/19626_2.png) [@pierreguillou](https://discuss.huggingface.co/u/pierreguillou)\
**Post date:** [March 16, 2021, 12:14pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/35 "2021-03-16T12:14:28Z")

</div>

Good morning @richardliaw and @sgugger.

First of all, many thanks for your work on `trainer.hyperparameter_search()` that will help a lot of people!

However, I think that your notebooks ([Hyperparameter Search with Transformers and Ray Tune](https://github.com/huggingface/blog/blob/master/ray-tune.md) & [text\_classification.ipynb](https://github.com/huggingface/notebooks/blob/master/examples/text_classification.ipynb)) need updates. Indeed, I tried to run them with Transformers + Ray [tune] but they failed with the same problem mentioned by @sonam ( `ModuleNotFoundError: No module named 'datasets_modules'`).

To show the problem for example with the code from [Hyperparameter Search with Transformers and Ray Tune](https://github.com/huggingface/blog/blob/master/ray-tune.md), I published a [test notebook in Colab](https://colab.research.google.com/drive/1I3VNCUVat3qEXxXxoY0Z_xp_viWaOuYZ?usp=sharing).

Could you tell us how to make them run?  
Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![theudster](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@theudster](https://discuss.huggingface.co/u/theudster)\
**Post date:** [April 18, 2021, 2:05pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/36 "2021-04-18T14:05:17Z")

</div>

Hi! Thank you for the detailed explanation. One thing I am not sure is how to get my\_objective to work? Like how would I define it if I want my objective to be f1 or maybe accuracy?  
I have tried

```
def my_objective(metrics):
    return metrics['eval_macro_f1']

```

But it doesn’t work. What is metrics?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [April 19, 2021, 7:51am UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/37 "2021-04-19T07:51:03Z")

</div>

hey @theudster, i suggest looking at the optuna docs to get an idea for how the objective functions are defined. here’s a pytorch example that optimises for accuracy: [optuna/pytorch\_simple.py at master · optuna/optuna · GitHub](https://github.com/optuna/optuna/blob/master/examples/pytorch/pytorch_simple.py)

you should be able to adapt this to compute F1 score (or whatever metric you want to optimise for) 🙂

---

<div class="post-metadata">

**Author:** ![tdelorenzo](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@tdelorenzo](https://discuss.huggingface.co/u/tdelorenzo)\
**Post date:** [April 21, 2021, 2:20pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/38 "2021-04-21T14:20:37Z")

</div>

Hello, I was not able to use optuna’s “gc\_after\_trial” option in hyperparameter\_search. Without it I always get CUDA out of memory. Is there a way?

---

<div class="post-metadata">

**Author:** ![theudster](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@theudster](https://discuss.huggingface.co/u/theudster)\
**Post date:** [April 30, 2021, 12:28pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/39 "2021-04-30T12:28:00Z")

</div>

I found out that when defining my metric, I need to add eval\_ before it. So the metric I called ‘f1’ had to be called ‘eval\_f1’

---

<div class="post-metadata">

**Author:** ![brgsk](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@brgsk](https://discuss.huggingface.co/u/brgsk)\
**Post date:** [July 16, 2021, 4:04pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/40 "2021-07-16T16:04:46Z")

</div>

Hi @sonam, have you managed to get it to work? I keep getting the same error, so any help would be much appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Gianluca](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@Gianluca](https://discuss.huggingface.co/u/Gianluca)\
**Post date:** [July 16, 2021, 11:46pm UTC](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785/41 "2021-07-16T23:46:54Z")

</div>

How do you pass parameters to the model\_init function? I have two parameters that need to be set in this function so I have defined it as so:

```auto
    def get_model(params):
        db_config = db_config_base
        db_config.update({'alpha': params['alpha_val'], 'dropout': params['dropout_val']})
        return DistilBERTForMultipleSequenceClassification.from_pretrained(db_config, num_labels1 = 2, num_labels2 = 8)

```

I thought maybe ray-tune would pass the parameters of the specific instance to this function but instead I get this error:

> TypeError: ‘NoneType’ object is not subscriptable

AKA ‘params’ is None

[Previous page](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785.md?page=1)

[Next page](https://discuss.huggingface.co/t/using-hyperparameter-search-in-trainer/785.md?page=3)
