[英]How to query the tensorflow 'wide and deep' model using the saved_model_cli?
[英]Issue with saved_model_cli while using saved estimator in tensorflow
我一直在使用 BERT large uncased 進行二進制文本分類。 我正在使用 google colab 訓練模型。 為了保存估算器,我使用了以下serving_input_funtion
:
def serving_input_receiver_fn():
with tf.variable_scope("foo"):
feature_spec = {
"input_ids": tf.FixedLenFeature([128], tf.int64),
"input_mask": tf.FixedLenFeature([128], tf.int64),
"segment_ids": tf.FixedLenFeature([128], tf.int64),
"label_ids": tf.FixedLenFeature([], tf.int64),
}
serialized_tf_example = tf.placeholder(dtype=tf.string, shape=None,
name='input_example_tensor')
receiver_tensors = {'examples': serialized_tf_example}
features = tf.parse_example(serialized_tf_example, feature_spec)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
並保存了以下估算器:
estimator._export_to_tpu = False
estimator.export_saved_model(export_dir_base = "/bert_0.3/",serving_input_receiver_fn = serving_input_receiver_fn)
這保存了估算器,但是現在當我嘗試使用saved_model_cli
來測試估算器時,它不起作用。 它拋出我的錯誤,如:
ValueError: Type <class 'bytes'> for value b'\n\x12\n\x10\n\x08sentence\x12\x04\n\x02\n\x00' is not supported for tf.train.Feature.
命令是:
saved_model_cli run --dir '/bert_0.3/1564572852' --tag_set serve --signature_def serving_default --input_examples '"examples"=[{"input_ids":[b"\n\x12\n\x10\n\x08sentence\x12\x04\n\x02\n\x00"],"input_mask":[b"\n-\n+\n\x08sentence\x12\x1f\n\x1d\n\x1bThis API is a little tricky"],"segment_ids":[None],"label_ids":["label_1"]}]'
它不直接接受字符串,所以這就是為什么我在dict
手動划分它。 將其轉換為dict
,我意識到這僅接受bytes_list
字節。 這就是我將字符串轉換為字節格式的原因。
堆棧跟蹤
File "/usr/local/bin/saved_model_cli", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/tools/saved_model_cli.py", line 990, in main
args.func(args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/tools/saved_model_cli.py", line 724, in run
init_tpu=args.init_tpu, tf_debug=args.tf_debug)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/tools/saved_model_cli.py", line 420, in run_saved_model_with_feed_dict
loader.load(sess, tag_set.split(','), saved_model_dir)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/saved_model/loader_impl.py", line 269, in load
return loader.load(sess, tags, import_scope, **saver_kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/saved_model/loader_impl.py", line 423, in load
self.restore_variables(sess, saver, import_scope)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/saved_model/loader_impl.py", line 377, in restore_variables
saver.restore(sess, self._variables_path)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/training/saver.py", line 1290, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/client/session.py", line 956, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/client/session.py", line 1180, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/client/session.py", line 1359, in _do_run
run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/client/session.py", line 1384, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InternalError: Dst tensor is not initialized.
[[node save_2/RestoreV2 (defined at /lib/python2.7/dist-packages/tensorflow_core/python/framework/ops.py:1748) ]]
如果有人能告訴我這哪里出了問題,任何幫助將不勝感激。 我猜問題也可能出在serving_input_receiver_fn
中。
最大序列長度為 128
謝謝。
編輯1 :
如果使用 tf.placeholder() 而不是 tf.FixedLenFeature
您可以使用以下命令查看保存的模型:
saved_model_cli show --all --dir <path to/bert_0.3/1564572852>
這將顯示輸入和輸出的數據類型、形狀和名稱。
請嘗試在serving_input_receiver_fn()中使用tf.placeholder()而不是tf.FixedLenFeature,如下所示:
input_ids = tf.placeholder(tf.int32, [None, 128], name='input_ids')
input_mask = tf.placeholder(tf.int32, [None, 128], name='input_mask')
segment_ids = tf.placeholder(tf.int32, [None, 128], name='segment_ids')
如果在使用保存的模型時仍然出現錯誤,請分享錯誤截圖。
您可以參考以下 github 倉庫了解更多詳情: https : //github.com/bigboNed3/bert_serving/tree/44d33920da6888cf91cb72e6c7b27c7b0c7d8815
希望這有幫助。謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.