python - tf.data.Dataset + tf.lookup.index_table_from_file 导致 "Table not initialized"错误

标签 python tensorflow tensorflow-datasets

我使用的是 TensorFlow 版本 1.8.0。尽管使用了 make_initialized_iterator() 并运行了 iterator.initializer,但当我尝试在映射到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)]]

最佳答案

您必须添加sess.run(tf.tables_initializer())添加with tf.Session()-block的开头

关于python - tf.data.Dataset + tf.lookup.index_table_from_file 导致 "Table not initialized"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51212806/

相关文章:

python - tf.dataset 实例的热切执行

python - 属性错误 : 'NoneType' object has no attribute 'name' ( related to different tensorflow versions )

python - 使用 pyinstaller 运行包含 xgboost 包的 exe 文件时出错

python - 如何在 numpy 中进行分散和聚集操作?

python - 如何访问 tf.data.Dataset.list_files() 收集的文件名?

python - tensorflow - tf.data.Dataset 在批处理之前随机跳过样本以获得不同的批处理

python - 在 python 中硬编码精确的浮点值

python - opencv python使用houghcircle函数检测不规则形状

python - 如何修复数据类型不匹配以使用我的训练模型预测图像?

python - 如何让多个用户同时使用同一个系统运行tensorflow程序?