tf.data.Dataset+ tf.lookup.index_表来自_文件导致“表未初始化”

2024-09-28 23:52:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是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}$

Tags: txtstringindexmaketfas错误sentences