我遵循了本教程,涉及建立一个编码器 - 编码器翻译模型并为我的母语构建一种。
现在,我想保存它,在云ML引擎上部署并使用HTTP请求进行预测。
我找不到有关如何保存此模型的明确示例,
我是ML的新手,发现TF Save Guide V混淆了..
是否有一种方法可以使用类似的方法来保存此模型tf.keras.models.save_model
在打开会话后和完成培训后,请创建火车节省,然后再保存模型:
with tf.Session() as sess:
saver = tf.train.Saver()
# Training of the model
save_path = saver.save(sess, "logs/encoder_decoder")
print(f"Model saved in path {save_path}")
您可以将KERAS模型保存在Keras的HDF5格式中,请参见:
https://keras.io/getting-started/faq/#how-can-i-save-ave-a-keras-model
您将要做类似:
的事情import tf.keras
model = tf.keras.Model(blah blah)
model.save('my_model.h5')
如果您迁移到TF 2.0,则在tf.keras中构建模型并使用TF SavedModel格式部署更为简单。该2.0教程使用验证的TF.KERAS模型显示,以SavedModel格式保存该模型,部署到云,然后进行HTTP请求以进行预测:
https://www.tensorflow.org/beta/guide/saved_model
我知道我有点晚了,但是遇到了相同的问题(请参阅如何保存带有TensorFlow的编码器模型?有关更多详细信息)并找到了解决方案。有点骇客,但有效!
步骤1-保存模型
保存令牌(如果适用)。然后单独节省您用于训练数据的模型的权重(在此处命名层有帮助)。
# Save the tokenizer
with open('tokenizer.pickle', 'wb') as handle:
pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)
# save the weights individually
for layer in model.layers:
weights = layer.get_weights()
if weights != []:
np.savez(f'{layer.name}.npz', weights)
步骤2-重新加载权重
您将需要重新加载令牌(如适用),然后加载刚保存的权重。加载权重为npz
格式,因此无法直接使用,但是很短的文档将告诉您有关此文件的所有信息https://numpy.org/doc/doc/stable/reference/numpy/numpy.save.html
# load the tokenizer
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
# load the weights
w_encoder_embeddings = np.load('encoder_embeddings.npz', allow_pickle=True)
w_decoder_embeddings = np.load('decoder_embeddings.npz', allow_pickle=True)
w_encoder_lstm = np.load('encoder_lstm.npz', allow_pickle=True)
w_decoder_lstm = np.load('decoder_lstm.npz', allow_pickle=True)
w_dense = np.load('dense.npz', allow_pickle=True)
步骤3-重新创建您的训练模型并应用权重
您需要重新运行用于创建模型的代码。就我而言,这是:
encoder_inputs = Input(shape=(None,), name="encoder_inputs")
encoder_embeddings = Embedding(vocab_size, embedding_size, mask_zero=True, name="encoder_embeddings")(encoder_inputs)
# Encoder lstm
encoder_lstm = LSTM(512, return_state=True, name="encoder_lstm")
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embeddings)
# discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = Input(shape=(None,), name="decoder_inputs")
# target word embeddings
decoder_embeddings = Embedding(vocab_size, embedding_size, mask_zero=True, name="decoder_embeddings")
training_decoder_embeddings = decoder_embeddings(decoder_inputs)
# decoder lstm
decoder_lstm = LSTM(512, return_sequences=True, return_state=True, name="decoder_lstm")
decoder_outputs, _, _ = decoder_lstm(training_decoder_embeddings,
initial_state=encoder_states)
decoder_dense = TimeDistributed(Dense(vocab_size, activation='softmax'), name="dense")
decoder_outputs = decoder_dense(decoder_outputs)
# While training, model takes input and traget words and outputs target strings
loaded_model = Model([encoder_inputs, decoder_inputs], decoder_outputs, name="training_model")
现在您可以将保存的权重应用于这些层!需要一点调查,重量是哪一层,但是通过命名图层并使用model.layers
检查模型层。
# set the weights of the model
loaded_model.layers[2].set_weights(w_encoder_embeddings['arr_0'])
loaded_model.layers[3].set_weights(w_decoder_embeddings['arr_0'])
loaded_model.layers[4].set_weights(w_encoder_lstm['arr_0'])
loaded_model.layers[5].set_weights(w_decoder_lstm['arr_0'])
loaded_model.layers[6].set_weights(w_dense['arr_0'])
步骤4-创建推理模型
最后,您现在可以基于此培训模型创建推理模型!同样,这是:
encoder_model = Model(encoder_inputs, encoder_states)
# Redefine the decoder model with decoder will be getting below inputs from encoder while in prediction
decoder_state_input_h = Input(shape=(512,))
decoder_state_input_c = Input(shape=(512,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
inference_decoder_embeddings = decoder_embeddings(decoder_inputs)
decoder_outputs2, state_h2, state_c2 = decoder_lstm(inference_decoder_embeddings, initial_state=decoder_states_inputs)
decoder_states2 = [state_h2, state_c2]
decoder_outputs2 = decoder_dense(decoder_outputs2)
# sampling model will take encoder states and decoder_input(seed initially) and output the predictions(french word index) We dont care about decoder_states2
decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs2] + decoder_states2)
和voilà!您现在可以使用先前训练的模型来推断!