python - tf.data.Dataset + tf.lookup.index_table_from_file 导致“表未初始化”错误
问题描述
我正在使用 TensorFlow 1.8.0 版。尽管使用make_initializable_iterator()
并运行iterator.initializer
,但当我尝试lookup
在tf.data.Dataset
. 这是一个崩溃的简化示例:
import tensorflow as tf
words = tf.contrib.lookup.index_table_from_file("words.txt", num_oov_buckets=1, key_column_index=0)
sentences = tf.data.TextLineDataset("sentences.txt")
sentences = sentences.map(lambda string: tf.string_split([string]).values)
dataset = sentences.map(lambda tokens: words.lookup(tokens))
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer)
with tf.Session() as sess:
for i in range(2):
sess.run(iterator.initializer)
print(sess.run(next_element))
这是错误:
2018-07-06 10:33:23.371736: W tensorflow/core/framework/op_kernel.cc:1318] OP_REQUIRES failed at lookup_table_op.cc:675 : Failed precondition: Table not initialized.
Traceback (most recent call last):
File "/anaconda2/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1322, in _do_call
return fn(*args)
File "/anaconda2/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/anaconda2/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1409, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.
[[Node: string_to_index_Lookup/hash_table_Lookup = LookupTableFindV2[Tin=DT_STRING, Tout=DT_INT64](string_to_index_lookup_hash_table_lookup_placeholder, arg0, string_to_index_lookup_hash_table_lookup_placeholder_1)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?]], output_types=[DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]
解决方案
您必须添加添加-blocksess.run(tf.tables_initializer())
的开头with tf.Session()
推荐阅读
- python - sklearn TfidfTransformer fit() 和 transform() 可以使用同一个源吗
- c - 使用 printf() 函数而不是 cout 的问题
- javascript - 检测某个文本是否是 JavaScript
- typescript - 打字稿枚举的vue v-for
- rust - 错误[E0277]:特征绑定`std::result::Result<_, Box
>: std::error::Error` 不满足 - python - 应该应用哪种技术来拆分大型文本数据集以进行数据匹配?
- android - Android/Jitpack:无法导入子模块
- c++ - std::trivially_copyable_v 和 std::is_pod_v (std::is_standard_layout && std::is_trivial_v) 有什么区别
- apache-flink - Flink:如何实时监控作业
- rust - How to use delayed initialization in rust code and pass compiler "possibly-uninitialized variable" rule?