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

时间:2019-07-24 18:37:12

标签: amazon-web-services tensorflow word-embedding amazon-sagemaker tensorflow-hub

我已经将aversal_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句子编码器似乎是一个常见问题

1 个答案:

答案 0 :(得分:0)

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

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

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