我正在使用Tensorflow 2.0,我想使用tf.keras
创建一个模型,该模型接受要素列输入,然后将其转换为估计量,对其进行训练,最后将其与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的问题?也许还有另一种方法可以做到这一点?
我尝试了张量流2.0.0-beta0
和2.0.0-beta1
,但遇到了相同的错误。
谢谢
马克西姆
答案 0 :(得分:0)
我在tensorflow-2.0.0b1中也遇到了同样的问题,并通过将tensorflow升级到2.0.0来解决它