Tf.estimator.BaselineClassifier踩坑之旅

编译器:JupyterLab

代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
#matplotlib inline 
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__,module.__version__)
matplotlib 3.1.1
numpy 1.17.2
pandas 0.25.1
sklearn 0.21.3
tensorflow 2.1.0
tensorflow_core.python.keras.api._v2.keras 2.2.4-tf
#  泰坦尼克号生存预测
#https://storage.googleapis.com/tf-datasets/titanic/eval.csv
#https://storage.googleapis.com/tf-datasets/titanic/train.csv
train_file="./data/titanic/train.csv"
eval_file="./data/titanic/eval.csv"


train_df=pd.read_csv(train_file)
eval_df=pd.read_csv(eval_file)

print(train_df.head())
print(eval_df.head())
survived     sex   age  n_siblings_spouses  parch     fare  class     deck  \
0         0    male  22.0                   1      0   7.2500  Third  unknown   
1         1  female  38.0                   1      0  71.2833  First        C   
2         1  female  26.0                   0      0   7.9250  Third  unknown   
3         1  female  35.0                   1      0  53.1000  First        C   
4         0    male  28.0                   0      0   8.4583  Third  unknown   

   embark_town alone  
0  Southampton     n  
1    Cherbourg     n  
2  Southampton     y  
3  Southampton     n  
4   Queenstown     y  
   survived     sex   age  n_siblings_spouses  parch     fare   class  \
0         0    male  35.0                   0      0   8.0500   Third   
1         0    male  54.0                   0      0  51.8625   First   
2         1  female  58.0                   0      0  26.5500   First   
3         1  female  55.0                   0      0  16.0000  Second   
4         1    male  34.0                   0      0  13.0000  Second   

      deck  embark_town alone  
0  unknown  Southampton     y  
1        E  Southampton     y  
2        C  Southampton     y  
3  unknown  Southampton     y  
4        D  Southampton     y 
#将 生存值 剔除出来
y_train=train_df.pop('survived')
y_eval=eval_df.pop('survived')
print(train_df.head())
print(eval_df.head())
print(y_eval)
print(y_train)
#                   Tf.feature_column使用
categorical_columns=['sex','n_siblings_spouses','parch','class','deck','embark_town','alone']#离散特征
numeric_columns=['age','fare']  #连续特征
feature_columns=[]
for categorical_column in categorical_columns:  #离散特征处理
    vocab=train_df[categorical_column].unique()  #  读取离散特征所有的值
    print(categorical_column,vocab)
    feature_columns.append(
        tf.feature_column.indicator_column(
            tf.feature_column.categorical_column_with_vocabulary_list(
    categorical_column,vocab)))#离散特征编码化

for categorical_column in numeric_columns:
    feature_columns.append(
        tf.feature_column.numeric_column(
            categorical_column,dtype=tf.float64))
feature_columns.append(tf.feature_column.indicator_column(tf.feature_column.crossed_column(['age','sex'],hash_bucket_size=100)))#表示压缩矩阵
#                                构建dataset
def make_dataset(data_df,label_df,epoch=10,shuffle=True,
                batch_size=32):
    dataset=tf.data.Dataset.from_tensor_slices((dict(data_df),label_df))
    if shuffle:
        dataset=dataset.shuffle(10000)
    dataset=dataset.repeat(epoch).batch(batch_size)
    return dataset
baseline_estimator = tf.estimator.BaselineClassifier(model_dir=output_dir,n_classes=2)
baseline_estimator.train(input_fn= lambda : make_dataset(train_df,y_train,epoch=100))

出现错误!!!!!

INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': 'baseline_model', '_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': ClusterSpec({}), '_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}
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from baseline_model/model.ckpt-1960

---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
   1366     try:
-> 1367       return fn(*args)
   1368     except errors.OpError as e:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1351       return self._call_tf_sessionrun(options, feed_dict, fetch_list,
-> 1352                                       target_list, run_metadata)
   1353 

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1444                                             fetch_list, target_list,
-> 1445                                             run_metadata)
   1446 

NotFoundError: Key training/Ftrl/baseline/bias/accumulator not found in checkpoint
	 [[{{node save/RestoreV2}}]]

During handling of the above exception, another exception occurred:

NotFoundError                             Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py in restore(self, sess, save_path)
   1289         sess.run(self.saver_def.restore_op_name,
-> 1290                  {self.saver_def.filename_tensor_name: save_path})
   1291     except errors.NotFoundError as err:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    959       result = self._run(None, fetches, feed_dict, options_ptr,
--> 960                          run_metadata_ptr)
    961       if run_metadata:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1182       results = self._do_run(handle, final_targets, final_fetches,
-> 1183                              feed_dict_tensor, options, run_metadata)
   1184     else:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1360       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1361                            run_metadata)
   1362     else:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
   1385                     'disable_meta_optimizer = True')
-> 1386       raise type(e)(node_def, op, message)
   1387 

NotFoundError: Key training/Ftrl/baseline/bias/accumulator not found in checkpoint
	 [[node save/RestoreV2 (defined at /home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py:1493) ]]

Original stack trace for 'save/RestoreV2':
  File "/home/rui/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/rui/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py", line 16, in 
    app.launch_new_instance()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/traitlets/config/application.py", line 664, in launch_instance
    app.start()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 563, in start
    self.io_loop.start()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 148, in start
    self.asyncio_loop.run_forever()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once
    handle._run()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/ioloop.py", line 690, in 
    lambda f: self._run_callback(functools.partial(callback, future))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/ioloop.py", line 743, in _run_callback
    ret = callback()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 787, in inner
    self.run()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 365, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 272, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 542, in execute_request
    user_expressions, allow_stdin,
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 294, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
    return runner(coro)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "", line 2, in 
    baseline_estimator.train(input_fn= lambda : make_dataset(train_df,y_train,epoch=100))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 374, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1164, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1198, in _train_model_default
    saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1493, in _train_with_estimator_spec
    log_step_count_steps=log_step_count_steps) as mon_sess:
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 604, in MonitoredTrainingSession
    stop_grace_period_secs=stop_grace_period_secs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1038, in __init__
    stop_grace_period_secs=stop_grace_period_secs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 749, in __init__
    self._sess = _RecoverableSession(self._coordinated_creator)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1231, in __init__
    _WrappedSession.__init__(self, self._create_session())
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1236, in _create_session
    return self._sess_creator.create_session()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 902, in create_session
    self.tf_sess = self._session_creator.create_session()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 660, in create_session
    self._scaffold.finalize()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 243, in finalize
    self._saver.build()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 840, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 878, in _build
    build_restore=build_restore)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 502, in _build_internal
    restore_sequentially, reshape)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 381, in _AddShardedRestoreOps
    name="restore_shard"))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 328, in _AddRestoreOps
    restore_sequentially)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 575, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_io_ops.py", line 1506, in restore_v2
    name=name)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py", line 742, in _apply_op_helper
    attrs=attr_protos, op_def=op_def)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3322, in _create_op_internal
    op_def=op_def)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1756, in __init__
    self._traceback = tf_stack.extract_stack()


During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/py_checkpoint_reader.py in get_tensor(self, tensor_str)
     69     return CheckpointReader.CheckpointReader_GetTensor(
---> 70         self, compat.as_bytes(tensor_str))
     71   # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the

RuntimeError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

NotFoundError                             Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py in restore(self, sess, save_path)
   1299       try:
-> 1300         names_to_keys = object_graph_key_mapping(save_path)
   1301       except errors.NotFoundError:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py in object_graph_key_mapping(checkpoint_path)
   1617   reader = py_checkpoint_reader.NewCheckpointReader(checkpoint_path)
-> 1618   object_graph_string = reader.get_tensor(trackable.OBJECT_GRAPH_PROTO_KEY)
   1619   object_graph_proto = (trackable_object_graph_pb2.TrackableObjectGraph())

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/py_checkpoint_reader.py in get_tensor(self, tensor_str)
     73   except RuntimeError as e:
---> 74     error_translator(e)
     75 

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/py_checkpoint_reader.py in error_translator(e)
     34       'matching files for') in error_message:
---> 35     raise errors_impl.NotFoundError(None, None, error_message)
     36   elif 'Sliced checkpoints are not supported' in error_message or (

NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

NotFoundError                             Traceback (most recent call last)
 in 
      1 baseline_estimator = tf.estimator.BaselineClassifier(model_dir=output_dir,n_classes=2)
----> 2 baseline_estimator.train(input_fn= lambda : make_dataset(train_df,y_train,epoch=100))

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

~/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
   1162       return self._train_model_distributed(input_fn, hooks, saving_listeners)
   1163     else:
-> 1164       return self._train_model_default(input_fn, hooks, saving_listeners)
   1165 
   1166   def _train_model_default(self, input_fn, hooks, saving_listeners):

~/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_model_default(self, input_fn, hooks, saving_listeners)
   1196       return self._train_with_estimator_spec(estimator_spec, worker_hooks,
   1197                                              hooks, global_step_tensor,
-> 1198                                              saving_listeners)
   1199 
   1200   def _train_model_distributed(self, input_fn, hooks, saving_listeners):

~/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py in _train_with_estimator_spec(self, estimator_spec, worker_hooks, hooks, global_step_tensor, saving_listeners)
   1491         config=self._session_config,
   1492         max_wait_secs=self._config.session_creation_timeout_secs,
-> 1493         log_step_count_steps=log_step_count_steps) as mon_sess:
   1494       loss = None
   1495       any_step_done = False

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in MonitoredTrainingSession(master, is_chief, checkpoint_dir, scaffold, hooks, chief_only_hooks, save_checkpoint_secs, save_summaries_steps, save_summaries_secs, config, stop_grace_period_secs, log_step_count_steps, max_wait_secs, save_checkpoint_steps, summary_dir, save_graph_def)
    602       session_creator=session_creator,
    603       hooks=all_hooks,
--> 604       stop_grace_period_secs=stop_grace_period_secs)
    605 
    606 

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in __init__(self, session_creator, hooks, stop_grace_period_secs)
   1036         hooks,
   1037         should_recover=True,
-> 1038         stop_grace_period_secs=stop_grace_period_secs)
   1039 
   1040 

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in __init__(self, session_creator, hooks, should_recover, stop_grace_period_secs)
    747         stop_grace_period_secs=stop_grace_period_secs)
    748     if should_recover:
--> 749       self._sess = _RecoverableSession(self._coordinated_creator)
    750     else:
    751       self._sess = self._coordinated_creator.create_session()

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in __init__(self, sess_creator)
   1229     """
   1230     self._sess_creator = sess_creator
-> 1231     _WrappedSession.__init__(self, self._create_session())
   1232 
   1233   def _create_session(self):

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in _create_session(self)
   1234     while True:
   1235       try:
-> 1236         return self._sess_creator.create_session()
   1237       except _PREEMPTION_ERRORS as e:
   1238         logging.info(

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in create_session(self)
    900       """Creates a coordinated session."""
    901       # Keep the tf_sess for unit testing.
--> 902       self.tf_sess = self._session_creator.create_session()
    903       # We don't want coordinator to suppress any exception.
    904       self.coord = coordinator.Coordinator(clean_stop_exception_types=[])

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py in create_session(self)
    667         init_op=self._scaffold.init_op,
    668         init_feed_dict=self._scaffold.init_feed_dict,
--> 669         init_fn=self._scaffold.init_fn)
    670 
    671 

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/session_manager.py in prepare_session(self, master, init_op, saver, checkpoint_dir, checkpoint_filename_with_path, wait_for_checkpoint, max_wait_secs, config, init_feed_dict, init_fn)
    292         wait_for_checkpoint=wait_for_checkpoint,
    293         max_wait_secs=max_wait_secs,
--> 294         config=config)
    295     if not is_loaded_from_checkpoint:
    296       if init_op is None and not init_fn and self._local_init_op is None:

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/session_manager.py in _restore_checkpoint(self, master, saver, checkpoint_dir, checkpoint_filename_with_path, wait_for_checkpoint, max_wait_secs, config)
    222 
    223     # Loads the checkpoint.
--> 224     saver.restore(sess, ckpt.model_checkpoint_path)
    225     saver.recover_last_checkpoints(ckpt.all_model_checkpoint_paths)
    226     return sess, True

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py in restore(self, sess, save_path)
   1304         # a helpful message (b/110263146)
   1305         raise _wrap_restore_error_with_msg(
-> 1306             err, "a Variable name or other graph key that is missing")
   1307 
   1308       # This is an object-based checkpoint. We'll print a warning and then do

NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key training/Ftrl/baseline/bias/accumulator not found in checkpoint
	 [[node save/RestoreV2 (defined at /home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py:1493) ]]

Original stack trace for 'save/RestoreV2':
  File "/home/rui/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/rui/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py", line 16, in 
    app.launch_new_instance()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/traitlets/config/application.py", line 664, in launch_instance
    app.start()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 563, in start
    self.io_loop.start()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 148, in start
    self.asyncio_loop.run_forever()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once
    handle._run()
  File "/home/rui/anaconda3/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/ioloop.py", line 690, in 
    lambda f: self._run_callback(functools.partial(callback, future))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/ioloop.py", line 743, in _run_callback
    ret = callback()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 787, in inner
    self.run()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 365, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 272, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 542, in execute_request
    user_expressions, allow_stdin,
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 294, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
    return runner(coro)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/home/rui/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "", line 2, in 
    baseline_estimator.train(input_fn= lambda : make_dataset(train_df,y_train,epoch=100))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 374, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1164, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1198, in _train_model_default
    saving_listeners)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1493, in _train_with_estimator_spec
    log_step_count_steps=log_step_count_steps) as mon_sess:
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 604, in MonitoredTrainingSession
    stop_grace_period_secs=stop_grace_period_secs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1038, in __init__
    stop_grace_period_secs=stop_grace_period_secs)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 749, in __init__
    self._sess = _RecoverableSession(self._coordinated_creator)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1231, in __init__
    _WrappedSession.__init__(self, self._create_session())
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 1236, in _create_session
    return self._sess_creator.create_session()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 902, in create_session
    self.tf_sess = self._session_creator.create_session()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 660, in create_session
    self._scaffold.finalize()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/monitored_session.py", line 243, in finalize
    self._saver.build()
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 840, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 878, in _build
    build_restore=build_restore)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 502, in _build_internal
    restore_sequentially, reshape)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 381, in _AddShardedRestoreOps
    name="restore_shard"))
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 328, in _AddRestoreOps
    restore_sequentially)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/training/saver.py", line 575, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_io_ops.py", line 1506, in restore_v2
    name=name)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py", line 742, in _apply_op_helper
    attrs=attr_protos, op_def=op_def)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3322, in _create_op_internal
    op_def=op_def)
  File "/home/rui/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1756, in __init__
    self._traceback = tf_stack.extract_stack()

其中会提示NotFoundError  和 GetNext()初始化错误问题

直接将预测器改为baseline_estimator = tf.compat.v1.estimator.BaselineClassifier(model_dir=output_dir,n_classes=2)

即可解决问题  !!!  我看的教学视频tensorflow是2.0运行tf.estimator.BaselineClassifier没有问题 自己用的是tf2.1(stable),竟然会出现问题。浪费我一下午时间搜解决方案,可是没有发现有类似问题出现,可能是自己环境原因,希望这篇文章能够帮到像我一样的倒霉蛋子。

你可能感兴趣的:(UNBUNTU)