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.

IndexError: list index out of range when using BayesianOptimization

See original GitHub issue

I’m currently having issues trying to BayesianOptimization for the tuning process for a binary classifier. This issue doesn’t appear with RandomSearch and Hyperband. Here’s how the hyper model is constructed:

class MyHyperModel(HyperModel):

    def __init__(self, num_classes):
        self.num_classes = num_classes

    def build(self, hp):

        model = keras.Sequential()

        for i in range(hp.Int('num_layers', 2, 20)):

            model.add(keras.layers.Dense(units=hp.Int('units_' + str(i),

        model.add(keras.layers.Dense(self.num_classes, activation='sigmoid'))

                          values=[1e-2, 1e-3, 1e-4])),

        return model

hypermodel = MyHyperModel(num_classes=1)

tuner = BayesianOptimization(
    project_name='test'), y_train.values.flatten(),
             validation_data=(X_test.values, y_test.values.flatten()))

The search works well for a while and crashes sometimes with this traceback error. It is worthwhile mentioning that the search even completes without this error but most of the time it crashes to this.

IndexError                                Traceback (most recent call last)
<ipython-input-21-d78d7772b203> in <module>()
      1, y_train.values.flatten(),
      2              epochs=10,
----> 3              validation_data=(X_test.values, y_test.values.flatten()))

4 frames
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/ in cumulative_prob_to_value(prob, hp)
    842         ele_prob = 1 / len(hp.values)
    843         index = math.floor(prob / ele_prob)
--> 844         return hp.values[index]
    845     elif isinstance(hp, (Int, Float)):
    846         sampling = hp.sampling or 'linear'

IndexError: list index out of range

Am I doing something wrong? If yes, how can I correct this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

omalleyt12commented, Nov 4, 2019

@PyDataBlog the fix is now merged into master, could you please confirm this fixes the issue you saw, and if not please reopen?

rafaelstevensoncommented, Mar 25, 2021

Hello, it seems i encountered the same issue but overcame it after updating to 1.0.2 but I have a confusion in using keras tuner for loop for determining hidden layers as search space, if there are anyone willing to help. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

List Index Out of Range – Python Error Message Solved
You'll get the Indexerror: list index out of range error when iterating through a list and trying to access an item that doesn't...
Read more >
IndexError: list index out of range and python - LearnDataSci
This index error is triggered when indexing a list using a value outside of its range of indexes. The best way to avoid...
Read more >
Why is my list index out of range when using scipy optimization?
I am trying to solve a quadratic program in python using scipy.optimize and keep getting a list out of range error when solving...
Read more >
Indexerror: list Index Out of Range in Python - STechies
If you are working with lists in Python, you have to know the index of the list elements. This will help you access...
Read more >
How to Fix IndexError in Python - Rollbar
The IndexError: list index out of range error occurs in Python when ... This can be done by using the range() function along...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found