张量流集线器问题:表未初始化

时间:2018-11-09 21:59:09

标签: tensorflow tensorflow-hub

当我遇到以下问题时,我正在尝试将tf_hub用于通用句子编码器大:

FailedPreconditionError (see above for traceback): Table not initialized.

TensorFlow似乎认为我没有运行init op,但实际上,我已经运行了init op:

embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
embeddings = embed([
"The quick brown fox jumps over the lazy dog."])    
init = tf.global_variables_initializer()


with tf.Session() as sess:
    sess.run(init)
    embeddings = sess.run(embeddings)

    print(embeddings)

对于其他tf_hub模型,例如elmo,相同的代码结构也很好。

2 个答案:

答案 0 :(得分:2)

想使用这个tensorflow集线器,我需要运行一个附加的初始化程序:

init = tf.global_variables_initializer()
table_init = tf.tables_initializer()

with tf.Session() as sess:
    sess.run([init, table_init])
    embeddings_ = sess.run(embeddings)

    print(embeddings)

答案 1 :(得分:0)

您可以尝试

with tf.train.SingularMonitoredSession() as sess:
  ...

自行执行所有标准初始化(包括“共享资源”,上次我没有检查其公开API)。