简体   繁体   English

在 Keras 模型中使用特征列的问题

[英]Problem using feature column with Keras model

I'm using Tensorflow 2.0 and I would like to create a model with tf.keras that is taking Feature Column inputs and then convert it to an estimator, train it and finally serve it with Tensorflow serving.我正在使用 Tensorflow 2.0,我想用tf.keras创建一个模型,该模型采用 Feature Column 输入,然后将其转换为估算器,对其进行训练,最后使用 Tensorflow 服务为其提供服务。 So I have the following code :所以我有以下代码:

column = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_file('feature0', vocab_file_name))

def input_fn(filenames, batch_size, num_epochs, shuffle=True, drop_final_batch=False):

    feature_description = {
        'feature0': tf.io.FixedLenSequenceFeature([], tf.string, default_value="", allow_missing=True),
        'labels': tf.io.FixedLenSequenceFeature([], tf.int64, default_value=0, allow_missing=True)
    }

    raw_dataset = tf.data.experimental.make_batched_features_dataset(
            label_key="labels",
            file_pattern=filenames,
            batch_size=batch_size,
            drop_final_batch=drop_final_batch,
            sloppy_ordering=True,
            shuffle_buffer_size=batch_size,
            num_epochs=num_epochs,
            features=feature_description,
            reader=tf.data.TFRecordDataset,
            shuffle=shuffle)

    def _encode(x,y):
        return {"feature0":tf.map_fn(__preprocess,x["feature0"])}, y

    dataset = raw_dataset.map(_encode)

    return dataset

def make_model(params):

    model = tf.keras.Sequential([
        tf.keras.layers.DenseFeatures(params["feature_columns"]),
        tf.keras.layers.Dense(units=params["hidden_units"][0], activation='relu'),
        tf.keras.layers.Dense(params['n_classes'], activation='relu')])

    return model

params = {
    'feature_columns': [column],
    'hidden_units': [1024],
    'n_classes': 1841,
    'threshold': 0.5}

model = make_model(params=params)
model.compile(loss="mean_squared_error", optimizer='adam', metrics=['accuracy'])

classifier = tf.keras.estimator.model_to_estimator(keras_model=model)
input_train_fn = functools.partial(input_fn, train_data_path, 1024, 1)

classifier.train(input_train_fn)

The issue is here :问题在这里:

classifier.train(input_train_fn)

I have the following stack trace :我有以下堆栈跟踪:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<timed eval> in <module>

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    365 
    366       saving_listeners = _check_listeners_type(saving_listeners)
--> 367       loss = self._train_model(input_fn, hooks, saving_listeners)
    368       logging.info('Loss for final step: %s.', loss)
    369       return self

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
   1156       return self._train_model_distributed(input_fn, hooks, saving_listeners)
   1157     else:
-> 1158       return self._train_model_default(input_fn, hooks, saving_listeners)
   1159 
   1160   def _train_model_default(self, input_fn, hooks, saving_listeners):

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model_default(self, input_fn, hooks, saving_listeners)
   1186       worker_hooks.extend(input_hooks)
   1187       estimator_spec = self._call_model_fn(
-> 1188           features, labels, ModeKeys.TRAIN, self.config)
   1189       global_step_tensor = training_util.get_global_step(g)
   1190       return self._train_with_estimator_spec(estimator_spec, worker_hooks,

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
   1144 
   1145     logging.info('Calling model_fn.')
-> 1146     model_fn_results = self._model_fn(features=features, **kwargs)
   1147     logging.info('Done calling model_fn.')
   1148 

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/keras.py in model_fn(features, labels, mode)
    283         features=features,
    284         labels=labels,
--> 285         optimizer_config=optimizer_config)
    286     model_output_names = []
    287     # We need to make sure that the output names of the last layer in the model

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/keras.py in _clone_and_build_model(mode, keras_model, custom_objects, features, labels, optimizer_config)
    221       in_place_reset=(not keras_model._is_graph_network),
    222       optimizer_iterations=global_step,
--> 223       optimizer_config=optimizer_config)
    224 
    225   if sample_weight_tensors is not None:

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/models.py in clone_and_build_model(model, input_tensors, target_tensors, custom_objects, compile_clone, in_place_reset, optimizer_iterations, optimizer_config)
    536         clone = clone_model(model, input_tensors=input_tensors)
    537     else:
--> 538       clone = clone_model(model, input_tensors=input_tensors)
    539 
    540     if all([isinstance(clone, Sequential),

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/models.py in clone_model(model, input_tensors, clone_function)
    321   if isinstance(model, Sequential):
    322     return _clone_sequential_model(
--> 323         model, input_tensors=input_tensors, layer_fn=clone_function)
    324   else:
    325     return _clone_functional_model(

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/models.py in _clone_sequential_model(model, input_tensors, layer_fn)
    256     layers = [
    257         layer_fn(layer)
--> 258         for layer in model._layers
    259         if not isinstance(layer, InputLayer)
    260     ]

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/models.py in <listcomp>(.0)
    257         layer_fn(layer)
    258         for layer in model._layers
--> 259         if not isinstance(layer, InputLayer)
    260     ]
    261     if len(generic_utils.to_list(input_tensors)) != 1:

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/models.py in _clone_layer(layer)
     52 
     53 def _clone_layer(layer):
---> 54   return layer.__class__.from_config(layer.get_config())
     55 
     56 

~/.virtualenvs/tensorflow2/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config)
    449         A layer instance.
    450     """
--> 451     return cls(**config)
    452 
    453   def compute_output_shape(self, input_shape):

TypeError: __init__() missing 1 required positional argument: 'feature_columns'

I also tried to change make_model function to this :我还尝试将make_model函数更改为:

def make_model(params):

    inputs = {'feature0' : tf.keras.layers.Input(name='inputs', shape=((None,)), dtype='string')}

    feature_layer = tf.keras.layers.DenseFeatures(params["feature_columns"])(inputs)

    layer1 = tf.keras.layers.Dense(units=params["hidden_units"][0], activation='relu')(feature_layer)

    output = tf.keras.layers.Dense(params['n_classes'], activation='relu')(layer1)

    model = tf.keras.Model(inputs=inputs, outputs=output)

    return model

But this time I had the same issue but on the following line :但这次我遇到了同样的问题,但在以下行:

classifier = tf.keras.estimator.model_to_estimator(keras_model=model)

So I don't really know where the problem is.. Is it a problem from my code ?所以我真的不知道问题出在哪里..是我的代码有问题吗? Or is it an issue from tensorflow ?还是 tensorflow 的问题? or maybe there is another way to do that ?或者也许有另一种方法可以做到这一点?

I tried with tensorflow 2.0.0-beta0 and 2.0.0-beta1 and I have the same errors.我尝试了 tensorflow 2.0.0-beta02.0.0-beta1并且我有同样的错误。

Thanks,谢谢,

Maxime马克西姆

我对 tensorflow-2.0.0b1 有同样的问题,并通过将 tensorflow 升级到 2.0.0 来修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 keras模型上的特征选择 - Feature selection on a keras model 结合 LSTM 中的文本和分类特征 model (Keras) - Combine Text and categorical feature in LSTM model (Keras) 使用hyperas查找Keras模型参数时的解码问题,可能是由于`hyperopt`中的`Trial`函数 - Decoding problem when using hyperas to find parameters of Keras model, maybe due to the `Trial` function in `hyperopt` 在 Tensorflow 2.0 中使用具有 keras api 的 LSTM 中的特征列 - Using feature columns in an LSTM with the keras api in Tensorflow 2.0 在我自己训练的 Keras 模型中预测我的图像的问题 - Problem in prediction of my image in my own trained Keras model Keras图层功能标签 - Keras layer feature labels 使用keras实现高斯混合模型 - Implement Gaussian Mixture Model using keras n_gram模型HashingVectorizer并将其与keras一起使用 - n_gram model HashingVectorizer and using it with keras 使用 keras 在分类问题中结合结构化和文本数据 - combining structured and text data in classification problem using keras 在使用 TFLiteConverter.from_keras_model(“model.h5”) 转动 Keras“model.h5”时,在 Google Colab python 中遇到问题 - Having trouble turning in Google Colab python in turning a Keras “model.h5” using TFLiteConverter.from_keras_model(“model.h5”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM