繁体   English   中英

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

[英]Tensorflow: FailedPreconditionError: Table not initialized (using tf.data.Dataset API)

我正在使用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))

然后我在我的dataset映射预处理函数:

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.

我google了,人们建议我需要包括:

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)

希望对你有帮助 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM