question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError when using conditional hyperparameters

See original GitHub issue

Describe the bug When I use parent_name and parent_valus I always get a KeyError when building a model.

Invalid model 5/5
Traceback (most recent call last):
  File "/Users/douira/Documents/dev/uni/bachelorarbeit/tf-time-series/venv/lib/python3.8/site-packages/keras_tuner/engine/hypermodel.py", line 127, in build
    model = self.hypermodel.build(hp)
  File "keras-tuner-test.py", line 22, in model_builder
    dense_units1 = hp.Int(
  File "/Users/douira/Documents/dev/uni/bachelorarbeit/tf-time-series/venv/lib/python3.8/site-packages/keras_tuner/engine/hyperparameters.py", line 850, in Int
    return self._retrieve(hp)
  File "/Users/douira/Documents/dev/uni/bachelorarbeit/tf-time-series/venv/lib/python3.8/site-packages/keras_tuner/engine/hyperparameters.py", line 707, in _retrieve
    return self.values[hp.name]
KeyError: 'dense_units1'

To Reproduce Use keras-tuner 1.0.4: https://colab.research.google.com/drive/1dlys0Dmpt9hjLkKOP62SfhYnIQmOgVxy?usp=sharing

import keras_tuner as kt
from tensorflow import keras

def model_builder(hp):
    dense_layers = hp.Int("dense_layers", min_value=0, max_value=2, step=1)

    dense_units1 = hp.Int(
        "dense_units1",
        min_value=16,
        max_value=512,
        step=16,
        parent_name="dense_layers",
        parent_values=[1, 2],
    )

    return keras.Sequential()

tuner = kt.RandomSearch(
    model_builder,
    objective="val_accuracy",
    directory="./model_tuning",
    max_trials=1,
)

Expected behavior The model should build normally. This worked on keras-tuner 1.0.3. It does not work (this bug happens) on keras-tuner 1.0.4.

Additional context If there is any way of using conditional hyperparameters on keras-tuner while avoiding this bug I’d be interested to hear about it. Otherwise I’ll downgrade my version of keras-tuner until this issue is resolved somehow. Thank you for the great work on this project!

Would you like to help us fix it? I don’t know why it’s happening. Maybe I’m doing something wrong. If this actually is a bug, then looking at what changed between keras-tuner 1.0.3 and 1.0.4 is probably a good idea. (the Google Collab confirms this difference)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gerwimcommented, Dec 13, 2022

@YASIRAQ I downgraded to 1.0.3, which did not have this regression.

1reaction
EzraWolfcommented, Dec 20, 2022

Had to search for 5-6 hours for this answer, except with Hyperband’s objective. The downgrade to 1.0.3 was the only thing that worked. Thank you @gerwim

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trouble with setting the objective function in ... - Stack Overflow
I'm trying to tune hyperparameters for an LSTM model in Keras using Keras tuner's BayesianOptimization tuner. I keep getting error messages ...
Read more >
Getting started with KerasTuner
Some hyperparameter is only used when a certain condition is satisfied. For example, units_3 is only used when num_layers is larger than 3....
Read more >
Step 4: Train a Model - Amazon SageMaker
The Amazon SageMaker Python SDK provides framework estimators and generic estimators to train your model while orchestrating the machine learning (ML) ...
Read more >
Ray RLlib API — Ray 2.2.0 - the Ray documentation
Get startedUse cases. Libraries ... Ray TuneScale hyperparameter tuning · Ray ServeScale model serving · Ray RLlibScale reinforcement learning.
Read more >
Amazon SageMaker - Developer Guide
discredits Amazon. All other trademarks not owned by Amazon are the property of their respective owners, who may or may not be affiliated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found