簡體   English   中英

在 Keras 模型中使用特征列的問題

[英]Problem using feature column with Keras model

我正在使用 Tensorflow 2.0,我想用tf.keras創建一個模型,該模型采用 Feature Column 輸入,然后將其轉換為估算器,對其進行訓練,最后使用 Tensorflow 服務為其提供服務。 所以我有以下代碼:

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)

問題在這里:

classifier.train(input_train_fn)

我有以下堆棧跟蹤:

---------------------------------------------------------------------------
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'

我還嘗試將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

但這次我遇到了同樣的問題,但在以下行:

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

所以我真的不知道問題出在哪里..是我的代碼有問題嗎? 還是 tensorflow 的問題? 或者也許有另一種方法可以做到這一點?

我嘗試了 tensorflow 2.0.0-beta02.0.0-beta1並且我有同樣的錯誤。

謝謝,

馬克西姆

我對 tensorflow-2.0.0b1 有同樣的問題,並通過將 tensorflow 升級到 2.0.0 來修復它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM