[英]Deployment of a model containing The Universal Sentence Encoder Multilingual on google ai-platform
我一直在尝试使用The Universal Sentence Encoder Multilingual
与 tensorflow(1.15) keras 进行迁移学习。 我在 Sequential model 中使用了句子编码器作为 KerasLayer。 训练结束后,我使用tf.saved_model.save
saved_model.pb
这是我的 model:
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
USE_V3 = "https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3"
my_model = tf.keras.models.Sequential(
[
tf.keras.layers.InputLayer(input_shape=(), dtype=tf.string),
hub.KerasLayer(USE_V3, trainable=False),
tf.keras.layers.Dense(3, activation="softmax"),
]
)
my_model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
我使用谷歌人工智能平台进行训练和预测。 训练没有问题,它训练 model 并将其保存在谷歌云存储上。 当我部署我保存的 model 进行预测时,会在 ai 平台的模型部分创建一个发布版本。 我使用 TensorFlow 作为框架,python 3.7,运行时版本 1.15。 当我使用TEST & USE
部分获取样本预测时,它给出了这个错误:(我也尝试了 api,返回相同的错误)
{
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.NOT_FOUND, details=\"{{function_node __inference_signature_wrapper_139623}} {{function_node __inference_signature_wrapper_139623}} {{function_node __inference___call___137889}} {{function_node __inference___call___137889}} {{function_node __inference_restored_function_body_100331}} {{function_node __inference_restored_function_body_100331}} [_Derived_]{{function_node __inference___call___55591}} {{function_node __inference___call___55591}} Op type not registered 'SentencepieceOp' in binary running on localhost. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.\n\t [[{{node StatefulPartitionedCall}}]]\n\t [[StatefulPartitionedCall]]\n\t [[muse_sentiment_classification/embedding/StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]\")"
}
总之,在预测机中找不到SentencepieceOp
(来自 tensorflow_text)。 然后,我尝试使用我在此处找到的解决方法。 这篇文章使用了缺少SentencepieceEncodeSparse
的解决方法,但我认为原因是相似的。 我按照建议创建了自己的Custom Prediction Routine
,并将tensorflow_text
的依赖项放入其中。 自定义预测例程的 Model 大小限制为 500MB,我的 model 为 ~350MB。 当我尝试为我的 model 创建新版本时,这一次它不会创建发布版本并给出此 memory 错误:
Create Version failed. Bad model detected with error: Model requires more memory than allowed. Please try to decrease the model size and re-deploy. If you continue to experience errors, please contact support.
第一个问题:在没有自定义预测例程解决方法的情况下,是否有适当的方法来部署在 AI 平台中使用Universal Sentence Encoder Multilingual
的 model?
第二个问题:如果我必须使用自定义预测例程,我该如何解决这个 memory 问题? 我的意思是我只有 1 个额外的密集层。 如何减少 memory 的使用量?
编辑:我使用 gcloud (v296.0.1) 和云控制台进行相同配置的部署。 这些是框架TensorFlow
和Custom Prediction Routine
的部署脚本:
TensorFlow 部署:
MODEL_DIR="gs://my-bucket--us-central1/training/sentiment_training/_model/"
VERSION_NAME="test_v1"
MODEL_NAME="Sentiment"
FRAMEWORK="TensorFlow"
gcloud ai-platform versions create $VERSION_NAME \
--model $MODEL_NAME \
--origin $MODEL_DIR \
--runtime-version=1.15 \
--framework $FRAMEWORK \
--python-version=3.7
自定义预测例程部署:
MODEL_DIR="gs://my-bucket--us-central1/training/sentiment_training/_model/"
VERSION_NAME="test_v1"
MODEL_NAME="Sentiment"
CUSTOM_CODE_PATH="gs://my-bucket--us-central1/packages/custom-op-tf-predictor-0.1.tar.gz"
PREDICTOR_CLASS="predictor.CustomOpTfPredictor"
gcloud beta ai-platform versions create $VERSION_NAME \
--model $MODEL_NAME \
--origin $MODEL_DIR \
--runtime-version=1.15 \
--python-version=3.7 \
--package-uris=$CUSTOM_CODE_PATH \
--prediction-class=$PREDICTOR_CLASS
根据 Google 支持,使用未记录的机器类型( mls1-c4-m4
)作为预测机器解决了我的问题。 我只是在自定义预测例程部署结束时添加了--machine-type=mls1-c4-m4
。 这台机器有 4gb memory 限制,但仍处于 alpha 状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.