keras转成estimator后训练报错

来源:5-4 keras_to_estimator

捡海星的小男孩

2019-10-16

estimator = keras.estimator.model_to_estimator(model)
estimator.train(input_fn = lambda : make_dataset(train_df, y_train, epochs=100))

报错信息

INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmp32rd0oqm
INFO:tensorflow:Using the Keras model provided.
WARNING:tensorflow:You are creating an Estimator from a Keras model manually subclassed from `Model`, that was already called on some inputs (and thus already had weights). We are currently unable to preserve the model's state (its weights) as part of the estimator in this case. Be warned that the estimator has been created using a freshly initialized version of your model.
Note that this doesn't affect the state of the model instance you passed as `keras_model` argument.
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmp32rd0oqm', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7ff5c4515690>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
WARNING:tensorflow:From /home/yang_zhou/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /home/yang_zhou/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
INFO:tensorflow:Calling model_fn.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-7904a567a5dd> in <module>
      2 # 1. function
      3 # 2. return a. (features, labels) b. dataset -> (feature, label)
----> 4 estimator.train(input_fn = lambda : make_dataset(
      5     train_df, y_train, epochs=100))

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    368 
    369       saving_listeners = _check_listeners_type(saving_listeners)
--> 370       loss = self._train_model(input_fn, hooks, saving_listeners)
    371       logging.info('Loss for final step: %s.', loss)
    372       return self

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
   1158       return self._train_model_distributed(input_fn, hooks, saving_listeners)
   1159     else:
-> 1160       return self._train_model_default(input_fn, hooks, saving_listeners)
   1161 
   1162   def _train_model_default(self, input_fn, hooks, saving_listeners):

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model_default(self, input_fn, hooks, saving_listeners)
   1188       worker_hooks.extend(input_hooks)
   1189       estimator_spec = self._call_model_fn(
-> 1190           features, labels, ModeKeys.TRAIN, self.config)
   1191       global_step_tensor = training_util.get_global_step(g)
   1192       return self._train_with_estimator_spec(estimator_spec, worker_hooks,

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
   1146 
   1147     logging.info('Calling model_fn.')
-> 1148     model_fn_results = self._model_fn(features=features, **kwargs)
   1149     logging.info('Done calling model_fn.')
   1150 

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py in model_fn(features, labels, mode)
    286         features=features,
    287         labels=labels,
--> 288         optimizer_config=optimizer_config)
    289     model_output_names = []
    290     # We need to make sure that the output names of the last layer in the model

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py in _clone_and_build_model(mode, keras_model, custom_objects, features, labels, optimizer_config)
    225       in_place_reset=(not keras_model._is_graph_network),
    226       optimizer_iterations=global_step,
--> 227       optimizer_config=optimizer_config)
    228 
    229   if sample_weight_tensors is not None:

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/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)
    632         clone = clone_model(model, input_tensors=input_tensors)
    633     else:
--> 634       clone = clone_model(model, input_tensors=input_tensors)
    635 
    636     if all([isinstance(clone, Sequential),

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/models.py in clone_model(model, input_tensors, clone_function)
    417   if isinstance(model, Sequential):
    418     return _clone_sequential_model(
--> 419         model, input_tensors=input_tensors, layer_fn=clone_function)
    420   else:
    421     return _clone_functional_model(

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/models.py in _clone_sequential_model(model, input_tensors, layer_fn)
    336       input_tensors = list(input_tensors)
    337     x = generic_utils.to_list(input_tensors)[0]
--> 338     if K.is_keras_tensor(x):
    339       origin_layer = x._keras_history.layer
    340       if isinstance(origin_layer, InputLayer):

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in is_keras_tensor(x)
    985                         sparse_tensor.SparseTensor)):
    986     raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) +
--> 987                      '`. Expected a symbolic tensor instance.')
    988   return hasattr(x, '_keras_history')
    989 

ValueError: Unexpectedly found an instance of type `<class 'dict'>`. Expected a symbolic tensor instance.

请问老师,这是什么原因,tensorflow版本是2.0正式版

2.0.0
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
matplotlib 3.1.1
numpy 1.17.2
pandas 0.25.1
sklearn 0.21.3
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
写回答

1回答

正十七

2019-10-24

同学你好,关于在tf2.0正式版上的问题,我需要统一处理,已经放到git的readme的pending issue中,稍后会更新到代码库,届时我再来问题下提醒。 https://git.imooc.com/coding-344/tensorflow2.0_course

1
2
正十七
回复
qq_雨后天晴_0
还没有,初步感觉是框架的问题,如果有更新我会再来回复。
2020-01-02
共2条回复

Google老师亲授 TensorFlow2.0 入门到进阶

Tensorflow2.0实战—以实战促理论的方式学习深度学习

1849 学习 · 896 问题

查看课程