python Bayesian优化搜索错误“TypeError:'float'对象不可订阅”

lp0sw83n  于 2022-12-02  发布在  Python
关注(0)|答案(2)|浏览(146)

我收到错误
TypeError:'float'对象不是可订阅的
从以下行:

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

我知道有很多问题都有同样的错误,但仍然找不到这个问题的解决方案。
从代码中删除y_瓦尔时,出现以下不完整行:

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

代码在某种程度上通过,没有错误,绿色V。
一声声的警告:
信息:tensorflow:Oracle触发退出信息:tensorflow:从现有项目重新加载Oracle/用户/Farid Srouji/文档/kerastuner\untitled_project\oracle. json信息:tensorflow:从/用户/Farid Srouji/文档/kerastuner\untitled_project\tuner 0. json重新加载调谐器信息:tensorflow:Oracle触发退出信息:tensorflow:从现有项目重新加载Oracle/用户/Farid Srouji/文档/kerastuner\untitled_project\oracle. json信息:tensorflow:从/用户/Farid Srouji/文档/kerastuner\untitled_project\tuner 0. json重新加载调谐器信息:tensorflow:Oracle触发退出
此块中的完整代码为:

# 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)

为什么删除y_val的代码会起作用?而且没有丢失参数的错误

bweufnob

bweufnob1#

我认为问题来自以下几行

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

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

你可能想试试

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

juzqafwq2#

Python会引发TypeError:如果在不可索引的浮点变量上使用带方括号标记的索引或切片,则“float”对象不可下标。

相关问题