我正在使用 keras-tuner 来获得适合我的模型的最佳超参数集。
我可以为随机数据集重现我的问题:
def generate_data(n_windows, n_timesteps):
feature_vector_list = []
label_list = []
for i in range(10):
x = tf.random.normal((n_windows, n_timesteps))
feature_vector = [x]
choices = [np.array([1, 0]), np.array([0, 1]),
np.array([0, 0]), np.array([1,1])]
labels = np.array([random.choice(choices) for i in range(n_windows)])
feature_vector_list.append(feature_vector)
label_list.append(labels)
return feature_vector_list, label_list
def custom_generator(feat_vector_list, label_list):
assert len(feat_vector_list) == len(label_list), \
"Number of feature vectors inconsistent with the number of labels"
counter = 0
while True:
feat_vec = feat_vector_list[counter]
list_labels = label_list[counter]
counter = (counter + 1) % len(feat_vector_list)
yield feat_vec, list_labels
这是模型:
def model_builder(hp):
n_timesteps, n_features, n_outputs = 60, 1, 2
hp_units = hp.Int("units", min_value=50, max_value=500, step=50)
hp_filters = hp.Int("filters", 4, 32, step=4, default=8)
hp_kernel_size = hp.Int("kernel_size", 3, 50, step=1)
hp_pool_size = hp.Int("pool_size", 2, 8, step=1)
hp_dropout = hp.Float("dropout", 0.1, 0.5, step=0.1)
input1 = Input(shape=(n_timesteps, n_features))
conv1 = Conv1D(filters=hp_filters,
kernel_size=hp_kernel_size,
activation='relu')(input1)
drop1 = Dropout(hp_dropout)(conv1)
if hp.Choice("pooling", ["max", "avg"]) == "max":
pool1 = MaxPooling1D(pool_size=hp_pool_size)(drop1)
else:
pool1 = AveragePooling1D(pool_size=hp_pool_size)(drop1)
flatten1 = Flatten()(pool1)
# hidden layers
dense1 = Dense(hp_units, activation='relu')(flatten1)
outputs = Dense(n_outputs, activation='softmax')(dense1)
model = Model(inputs=[input1, input2], outputs=outputs)
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(learning_rate=hp.Float("learning_rate",
0.01,
0.1,
step=0.2)),
metrics=['accuracy'])
return model
这是训练脚本:
if __name__ == '__main__':
x_train, y_train = generate_data(350, 60)
x_val, y_val = generate_data(80, 60)
training_generator = custom_generator(x_train, y_train)
validation_generator = custom_generator(x_val, y_val)
tuner = kt.Hyperband(
model_builder,
objective="val_accuracy",
max_epochs=70,
factor=3,
directory="Results",
project_name="cnn_tunning"
)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=5,
min_delta=0.002)
tuner.search(
training_generator,
steps_per_epoch=N_WINDOWS,
validation_data=validation_generator,
validation_steps=75,
callbacks=[stop_early],
)
现在我发现,在超带开始使用相当数量的迭代之后,我设置的回调应该发挥作用,我收到此错误:
W tensorflow/core/framework/op_kernel.cc:1733] INVALID_ARGUMENT: ValueError: Could not find callback with key=pyfunc_530 in the registry.
Traceback (most recent call last):
File "/home/diogomota/.cache/pypoetry/virtualenvs/WUAle-Z1-py3.7/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 259, in __call__
raise ValueError(f"Could not find callback with key={token} in the "
ValueError: Could not find callback with key=pyfunc_530 in the registry.
W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: INVALID_ARGUMENT: ValueError: Could not find callback with key=pyfunc_530 in the registry.
Traceback (most recent call last):
File "/home/diogomota/.cache/pypoetry/virtualenvs/WUAle-Z1-py3.7/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 259, in __call__
raise ValueError(f"Could not find callback with key={token} in the "
ValueError: Could not find callback with key=pyfunc_530 in the registry.
然而,它只是进行下一次试验,所以我不确定发生了什么,有人可以解释为什么它找不到回调吗?
我在用着tensorflow 2.8
and keras-tuner 1.1.2
我在网上只找到一个有类似问题的地方,但没有提供解决方案:https://issuemode.com/issues/tensorflow/tensorflow/72982126 https://issuemode.com/issues/tensorflow/tensorflow/72982126
EDIT:
- 提供完整的错误消息
- 经过进一步调试,问题完全来自于使用生成器作为输入
.search()
。我不知道这个问题的原因。定期训练使用.fit()
工作没有任何问题
- 添加了数据集生成代码以提高可重复性