[英]Tensorflow 2.0 save trained model for serving
請幫我。 我正在使用 Tensorflow 2.0 GPU。 我訓練 model 並保存為 .h5 格式
model = keras.Sequential()
model.add(layers.Bidirectional(layers.CuDNNLSTM(self._window_size, return_sequences=True),
input_shape=(self._window_size, x_train.shape[-1])))
model.add(layers.Dropout(rate=self._dropout, seed=self._seed))
model.add(layers.Bidirectional(layers.CuDNNLSTM((self._window_size * 2), return_sequences=True)))
model.add(layers.Dropout(rate=self._dropout, seed=self._seed))
model.add(layers.Bidirectional(layers.CuDNNLSTM(self._window_size, return_sequences=False)))
model.add(layers.Dense(units=1))
model.add(layers.Activation('linear'))
model.summary()
model.compile(
loss='mean_squared_error',
optimizer='adam'
)
# обучаем модель
history = model.fit(
x_train,
y_train,
epochs=self._epochs,
batch_size=self._batch_size,
shuffle=False,
validation_split=0.1
)
model.save('rts.h5')
然后我加載這個 model 並將其用於預測,一切正常。
model = keras.models.load_model('rts.h5')
y_hat = model.predict(x_test)
但是出現了在 Tensorflow 服務中使用訓練有素的 model 的問題。 並且不接受.h5格式的model。 我跑:
sudo docker run --gpus 1 -p 8501:8501 --mount type=bind,source=/home/alex/PycharmProjects/TensorflowServingTestData/RtsModel,target=/models/rts_model -e MODEL_NAME=rts_model -t tensorflow/serving:latest-gpu
但是出現了在 Tensorflow 服務中使用訓練有素的 model 的問題。 並且不接受.h5格式的model。 我跑:我得到錯誤:
tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:267] No versions of servable rts_model found under base path /models/rts_model
我嘗試保存訓練有素的 model,如此處所述, https://www.tensorflow.org/guide/saved_model#using_savedmodel_with_estimators
我得到了錯誤:
ValueError: Layer has 2 states but was passed 0 initial states.
我嘗試將 model 保存如下, https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model :
並有同樣的錯誤:
ValueError: Layer has 2 states but was passed 0 initial states.
以 Tensorflow Serving 格式保存 model 的唯一方法是:
keras.experimental.export_saved_model(model, 'saved_model/1/')
保存 model 在服務中的工作。 但我收到警告說此方法已被棄用,並將在未來版本中刪除。
Instructions for updating:
Please use `model.save(..., save_format="tf")` or `tf.keras.models.save_model(..., save_format="tf")`.
它關閉了我。 當我嘗試使用這些方法時,它給出了一個錯誤。 當我使用有效的方法時,寫道它已被棄用。
請幫忙。 如何在 Tensorflow 2.0 中保存經過訓練的 model。 以便它可以用於 Tensorflow 服務。
我也試圖解決這個問題!
根據這里的答案,普通 LSTM(即tf.keras.layers.LSTM
)將使用 GPU,並且通常應該在 cuDNNLSTM ZA2F2ED4F8EBC2CBB4C21A29DC40AB61 上使用(除非你特別需要)
根據文檔,如果滿足某些要求,正常的 LSTM 將使用 cuDNN 實現(見下文)。
使用這個 LSTM 層時,我可以成功保存到 tf output 類型,只需使用model.save_model('output_path', save_format='tf')
使用 cuDNN 的 LSTM 的要求如下(注意所有要求都滿足默認值):
如果 GPU 可用並且該層的所有 arguments 都滿足 CuDNN kernel 的要求(詳見下文),則該層將使用快速 cuDNN 實現。
使用 cuDNN 實現的要求是:
activation == tanh
recurrent_activation == sigmoid
recurrent_dropout == 0
- 展開是 False
- use_bias 為 True 輸入未被屏蔽或嚴格右填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.