Keras调谐器抛出无效参数

2024-10-04 03:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Keras调谐器来测试我的神经网络的不同配置和超参数
我有在调谐器外部运行的代码(但我必须在禁用“急切执行”的情况下运行),
但在调谐器内部,会引发异常。我对使用更复杂的神经网络还不熟悉,所以我试着浏览文档,但有时会迷路。
她的“自定义丢失和模型加调谐器代码”

def custom_loss(yardline):
yardline=K.cumsum(yardline)
def loss(y_true, y_pred):
    y_true=K.cumsum(y_true)
    y_pred= y_pred*yardline
    new_total_prob= K.sum(y_pred)
    inverse_prob=K.pow(new_total_prob+K.epsilon(), -1)
    y_pred= y_pred*inverse_prob
    cdf_pred= K.cumsum(y_pred)
    return K.mean(K.square(y_true-cdf_pred))
return loss

这是模型

def build_model(hp):
input_layer= Input(179,)
yardline=Input(199,)
x=Dense(32)(input_layer)
for i in range(hp.Int('num_layers', 2,4, default=4)):
    x= Dense(units=hp.Int('units_' + str(i),
                                        min_value=32,
                                        max_value=512,
                                        step=32),
                           activation='relu')(x)
    if (i==3 or i==5):
           x=Dropout(hp.Choice('dropout_'+ str(i),[0.15,0.25]))(x)
output_layer = Dense(199, activation='softmax')(x)
model=Model(inputs=[input_layer, yardline], outputs=output_layer)
model.compile(optimizer='adam',loss=custom_loss(yardline), metrics=['accuracy'])
return model

这是调谐器代码

tuner = kt.Hyperband(
    hypermodel=build_model,
    objective='val_accuracy',
    max_epochs=5,
    factor=2,
    hyperband_iterations=3,
    distribution_strategy=tf.distribute.MirroredStrategy(),
    directory='.',
    project_name='project')
tuner.search([X_train,yardline_train],y_train,
         epochs=5, batch_size=43,
         validation_data=([X_val, yardline_val],y_val))

谢谢你的帮助 该错误特别指出无效参数错误:并期望数据进入input_2。。。
我加载模型时没有使用调谐器,所以它运行起来似乎很奇怪


Tags: 代码模型layertrueinputmodelreturndef