python - Tensorflow:FailedPreconditionError:表未初始化(使用 tf.data.Dataset API)

标签 python tensorflow tensorflow-datasets

我将 tf.data.Dataset API 与 tf.contrib.lookup.index_table_from_tensor 结合使用。

我的数据集是这样创建的:

dataset = tf.data.Dataset.from_tensor_slices(({'reviews': x}, y)))

这是我正在做的:

data_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(data_vocab))
labels_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(labels_vocab))

然后我在我的数据集上映射一个预处理函数:

def preprocess(x, y):
    # split on whitespace
    x['reviews'] = tf.string_split([x['reviews']])
    # turn into integers
    return data_table.lookup(x['reviews']), labels_table.lookup(y)

到目前为止一切都很好。但是,当我尝试将我的数据集传递给我的 Keras 模型进行训练时,我得到:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.

我四处搜索,人们建议我需要包括:

sess = tf.Session()
sess.run(tf.tables_initializer())

但现在我得到:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.
     [[Node: hash_table_Lookup = LookupTableFindV2[Tin=DT_STRING, Tout=DT_INT64](hash_table_lookup_placeholder, StringSplit:1, hash_table_lookup_placeholder_1)]]
     [[Node: IteratorGetNext_1 = IteratorGetNext[output_shapes=[[?,?], [?,20]], output_types=[DT_INT64, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator_1)]]

知道为什么我的查找表仍然没有初始化/如何解决这个问题?

谢谢!

最佳答案

嗨,这很奇怪,也许下面的工作示例会对您有所帮助:

x = ['this is aswesome', 'i dont like it', 'i love it', 'i hate it']
y = ['positive','negative','positive','negative']
data_vocab = list({word for sentence in x for word in sentence.split(' ')})
label_vocab = list(set(y))

dataset = tf.data.Dataset.from_tensor_slices(({'reviews': x}, y))

data_table=tf.contrib.lookup.index_table_from_tensor(tf.constant(data_vocab))
labels_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(label_vocab))

def preprocess(x, y):
    # split on whitespace
    x['reviews'] = tf.string_split([x['reviews']])
    # turn into integers
    return data_table.lookup(x['reviews']), labels_table.lookup(y)

preprocessed = dataset.map(preprocess)

it = preprocessed.make_initializable_iterator()
sess = tf.Session()
sess.run(it.initializer)
sess.run(tf.tables_initializer()) 

如果你调用 sess.run(it.get_next())你会得到 (SparseTensorValue(indices=array([[0, 0], [0, 1], [0, 2]]), values=array([2, 7, 4]), dense_shape=array([1, 3])), 1)

希望对您有所帮助!

关于python - Tensorflow:FailedPreconditionError:表未初始化(使用 tf.data.Dataset API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53013242/

相关文章:

python - 当收到 `sample_from_datasets` 警告时,TensorFlow 的 `DirectedInterleave selected an exhausted input` 是否仍然从数据集中采样?

python - Keras 应用程序 - imagenet 上的 VGG16 低精度

python - TensorFlow:将 tf.Dataset 转换为 tf.Tensor

python 将子列表中的每个项目乘以列表

python - KeyError: '\n' python 2.7.5

python - 如何使Keras网络不输出全1

python - 如何在 tensorflow 中打乱 3D 张量维度?

python - PyDictionary 单词 "has no Synonyms in the API"

python - 在循环中 append 数据帧

python - 在 keras-python 中使用 ImageDataGenerator 进行数据增强