[英]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-beta0
和2.0.0-beta1
并且我有同样的错误。
Thanks,谢谢,
Maxime马克西姆
我对 tensorflow-2.0.0b1 有同样的问题,并通过将 tensorflow 升级到 2.0.0 来修复它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.