失败的先决条件:Table 未初始化。在 aws sagemaker 部署的通用句子编码器上

Failed precondition: Table not initialized. on deployed universal sentence encoder from aws sagemaker

我已经将 universal_sentence_encoder_large_3 部署到 aws sagemaker。当我尝试使用已部署的模型进行预测时,出现 Failed precondition: Table not initialized. 错误。我在下面包含了我保存模型的部分:

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
def tfhub_to_savedmodel(model_name, export_path):

    model_path = '{}/{}/00000001'.format(export_path, model_name)
    tfhub_uri = 'http://tfhub.dev/google/universal-sentence-encoder-large/3'

    with tf.Session() as sess:
        module = hub.Module(tfhub_uri)
        sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
        input_params = module.get_input_info_dict()
        dtype = input_params['text'].dtype
        shape = input_params['text'].get_shape()

        # define the model inputs
        inputs = {'text': tf.placeholder(dtype, shape, 'text')}
        output = module(inputs['text'])
        outputs = {
            'vector': output,
        }

        # export the model
        tf.saved_model.simple_save(
            sess,
            model_path,
            inputs=inputs,
            outputs=outputs)  

    return model_path

我看到其他人问过这个问题,但没有发布解决方案。这似乎是 tensorflow_hub 句子编码器

的常见问题

本周早些时候,我在尝试修改此示例 Sagemaker notebook 时 运行 正在研究这个确切的问题。特别是为模型服务的部分。也就是说,在 Sagemaker Tensorflow Estimator 上 运行ning predictor.predict()

问题中概述的解决方案对我来说非常有效- https://github.com/awslabs/amazon-sagemaker-examples/issues/773#issuecomment-509433290

我认为这只是因为 tf.tables_initializer() 只有 运行 用于训练,但如果你想在预测期间 运行 它需要通过 legacy_init_op 指定。