使用Keras Tuner优化CNN时获取pool超参数出现KeyError问题排查
Keras Tuner调优CNN时
pool参数KeyError问题解决 问题现象
使用Keras Tuner优化CNN模型时,能正常获取滤波器、核大小的最优值,但获取池化大小(pool size)时触发如下错误:
File "C:\Users\...", line 180, in pp -optimal learning rate for the optimizer is {best_hps.get('learning_rate')}.""") File "C:\Users\anaconda3\envs\venv\lib\site-packages\keras_tuner\engine\hyperparameters\hyperparameters.py", line 241, in get raise KeyError(f"{name} does not exist.") KeyError: 'pool does not exist.'
用户的实现代码:
def build_model(hp): model = Sequential() model.add(Conv2D(filters=hp.Int('conv_1_filter', min_value=32, max_value=128, step=16), kernel_size=hp.Choice('conv_1_kernel', values = [2, 3,5]), strides=2, input_shape=(num_rows, num_columns, num_channels), activation='relu')) model.add(MaxPooling2D(pool_size=hp.Choice('pool', values = [2, 3]))) model.add(Flatten()) model.add(Dense(num_labels, activation='softmax')) # Display model architecture summary # model.summary() # Compile the model model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3]))) return model from kerastuner import RandomSearch tuner = RandomSearch(build_model, objective='val_accuracy', max_trials = 5) tuner.search(X_train, Y_train,epochs=3,validation_data=(X_train, Y_train),verbose = 1) # Get the optimal hyperparameters best_hps=tuner.get_best_hyperparameters(num_trials=1)[0] print(f"""The hyperparameter search is complete. The parameters are as follow: -optimal filter size for the first layer is {best_hps.get('conv_1_filter')} -optimal pool size is {best_hps.get('pool')} -optimal kernel size for the first layer is {best_hps.get('conv_1_kernel')} -optimal learning rate for the optimizer is {best_hps.get('learning_rate')}.""")
解决方案
在RandomSearch初始化时添加overwrite=True参数,问题根源是程序复用了之前的旧调优结果,导致新添加的pool参数未被纳入搜索结果中。修改后的初始化代码如下:
tuner = RandomSearch(build_model, objective='val_accuracy', max_trials = 5, overwrite=True)
内容的提问来源于stack exchange,提问作者A. Gehani
相关产品推荐
相关产品推荐

