Home > Net >  BayesianOptimization search errors out "TypeError: 'float' object is not subscriptabl
BayesianOptimization search errors out "TypeError: 'float' object is not subscriptabl

Time:11-29

I am getting the error

TypeError: 'float' object is not subscriptable

from the following line:

tuner_nn.search(x_train, y_train, epochs=50, validation_data=(x_val,y_val ), verbose=0, callbacks=[Earlystopping])

I know there are a lot of questions with the the same error but still could not find a solution for this issue.

While removing the y_val from the code and having the following incomplete line:

tuner_nn.search(x_train, y_train, epochs=50, validation_data=(x_val,), verbose=0, callbacks=[Earlystopping])

The code somewhy pass without errors with green V.

Yet with the warnings:

INFO:tensorflow:Oracle triggered exit INFO:tensorflow:Reloading Oracle from existing project /Users/Farid Srouji/Documents/kerastuner\untitled_project\oracle.json INFO:tensorflow:Reloading Tuner from /Users/Farid Srouji/Documents/kerastuner\untitled_project\tuner0.json INFO:tensorflow:Oracle triggered exit INFO:tensorflow:Reloading Oracle from existing project /Users/Farid Srouji/Documents/kerastuner\untitled_project\oracle.json INFO:tensorflow:Reloading Tuner from /Users/Farid Srouji/Documents/kerastuner\untitled_project\tuner0.json INFO:tensorflow:Oracle triggered exit

The full code in this block is:


# Search hyperparameters

SEED = 121

# NN

tuner_nn = BayesianOptimization(nn_builder,
objective = 'val_loss',
max_trials = 20,
seed = SEED,
directory = '/Users/myuser/Documents/kerastuner',
overwrite = True
)

tuner_nn.search(x_train, y_train, epochs=50, validation_data=(x_val, ), verbose=0, callbacks=\[Earlystopping\])

## Build model based on the optimized hyperparameters

besthp_nn = tuner_nn.get_best_hyperparameters()\[0\]
model_nn = tuner_nn.hypermodel.build(besthp_nn)

# lstm

tuner_lstm = BayesianOptimization(lstm_builder,
objective = 'val_loss',
max_trials = 20,
seed = SEED,
directory = '/Users/myuser/Documents/kerastuner')

tuner_lstm.search(x_train, y_train, epochs=50, validation_data=(x_val, y_val), verbose=0, callbacks=\[Earlystopping\])

## Build model based on the optimized hyperparameters

besthp_lstm = tuner_lstm.get_best_hyperparameters()\[0\]
model_lstm = tuner_lstm.hypermodel.build(besthp_lstm)

# gru

tuner_gru = BayesianOptimization(gru_builder,
objective = 'val_loss',
max_trials = 20,
seed = SEED,
directory = '/Users/myuser/Documents/kerastuner')

tuner_gru.search(x_train, y_train, epochs=50, validation_data=(x_val, y_val), verbose=0, callbacks=\[Earlystopping\])

## Build model based on the optimized hyperparameters

besthp_gru = tuner_gru.get_best_hyperparameters()\[0\]
model_gru = tuner_gru.hypermodel.build(besthp_gru)

CodePudding user response:

I think the proble comes from the following lines

besthp_nn = tuner_nn.get_best_hyperparameters()\[0\]

and

besthp_gru = tuner_gru.get_best_hyperparameters()\[0\]

You might want to try something like

besthp_nn = tuner_nn.get_best_hyperparameters(1)[0]
besthp_gru = tuner_gru.get_best_hyperparameters(1)[0]

CodePudding user response:

Python raises the TypeError: 'float' object is not subscriptable if you use indexing or slicing with the square bracket notation on a float variable that is not indexable.

  • Related