首页 > 解决方案 > 如何将特征列名称添加到 Keras Estimator

问题描述

我想将特征列名称添加到已转换为 Estimator 的 Keras 模型的输入中。这将允许我将 JSON 消息发送到服务模型,我可以在其中指定功能名称。我的模型如下,有3个输入节点:

model = keras.models.Sequential()
model.add(keras.layers.Dense(300, input_dim=3))
model.add(keras.layers.PReLU())
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(2, activation='softmax'))
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

然后我创建一个估算器:

estimator_model = keras.estimator.model_to_estimator(keras_model=model)

我的自定义输入功能是:

# Define input fn
def input_function(features, labels=None, shuffle=False):
  input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"dept_tenure": dept_tenure,
       "prior_tenure": prior_tenure,
       "employ_tenure": employ_tenure},
    y=labels,
    shuffle=shuffle
  )
  return input_fn

但是,当我尝试评估模型时,出现以下错误:

ValueError: Cannot find input with name "employ_tenure" in Keras Model. It needs to match one of the following: dense_4_input

model.input_n返回[dense_4_input],其中dense_4是第一层的名称。

那么如何配置我的模型,以使输入函数中的特征名称与模型的输入名称匹配?

我查看了文档,但没有帮助。

标签: tensorflowkeras

解决方案


推荐阅读