我使用的是TensorFlow 1.8.0版。尽管使用了make_initializable_iterator()
并运行了iterator.initializer
,但当我试图在映射中运行lookup
时,还是遇到了一个“Table not initialized”错误。下面是一个简化的崩溃示例:
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))
错误就在这里:
^{pr2}$
您必须添加
sess.run(tf.tables_initializer())
添加with tf.Session()
-块的开头相关问题 更多 >
编程相关推荐