我在本地训练了一个简单的Keras网络,该网络使用GRU生成文本序列。模型具有输入形状[1, window_size, number_charazters]
。
我已将模型导出为TF模型并使用tensorflow_model_server
对其进行服务。对于预测,我使用形状为[1, window_size, number_charazters]
的输入。
当我部署到Google Cloud ML时,模型的输入为[number_charazter, window_size]
。
为什么要更改Google Cloud ML中的输入形状?
Keras网络
def create_gru_model( num_chars):
"""
Define the network
:param
numbers_chars .- Number chars using in the training process
:return:
model .- Model network defined
"""
model = Sequential()
# 1 Layer .- GRU layer 1 should be an GRU module with 200 hidden units
model.add(GRU(200, input_shape=(window_size, num_chars),return_sequences=True))
# 2 Layer .- GRU layer 2 should be an GRU module with 200 hidden units
model.add(GRU(200))
# 2 Layer .- Dense, with number chars unit and softmax activation
model.add(Dense(num_chars, activation='softmax'))
return model
将模型导出到tensorflow_model_server
。
if os.path.isdir(export_path):
shutil.rmtree(export_path)
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'sequence': model.input},
outputs={'scores': model.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'predict': signature})
builder.save()
使用tensorflow_model_server
预测模型。
input_init="pla panfletaria contra as leoninas taxas impostas polo ministro de xustiza actual malia que vulneran"
# Load values
window_size = 100
chars_to_indices, indices_to_chars = load_coded_dictionaries()
number_chars=len(chars_to_indices)
# Clean the text
input_clean=clean_text(input_init.lower())
input_clean = input_clean[:window_size]
# Text to array [1,input_lenght,num_chars]
x_test = np.zeros((1,window_size, number_chars))
for t, char in enumerate(input_clean):
x_test[0, t, chars_to_indices[char]] = 1.
x_test
# Get the array with the probabilities for the next charazter
channel = grpc.insecure_channel("localhost:" + str(9000))
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
# Name of the model
request.model_spec.name = 'default'
request.model_spec.signature_name = 'predict'
request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto(
x_test,dtype='float32'))
result=stub.Predict(request)
# Get the charazter from array
test_predict=np.array(result.outputs['outputs'].float_val)
r = np.argmax(test_predict) # predict class of each test input
d = indices_to_chars[r]
将模型导出到Google Cloud ML
import keras.backend as K
K.set_learning_phase(0)
# Input size of the network, the entry text must have the same length
window_size = 100
# Get dictionaries
chars_to_indices, indices_to_chars = load_coded_dictionaries()
number_chars=len(chars_to_indices)
# regenerate the model
model=create_gru_model(number_chars)
model.load_weights(
'../model_weights/best_beiras_gru_textdata_weights.hdf5')
# Path to export, 1 is the version,
# we can serve differents version with the same server
export_path = "../export-google-ml/1"
if os.path.isdir(export_path):
shutil.rmtree(export_path)
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'sequence': model.input},
outputs={'scores': model.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'serving_default': signature})
builder.save()
使用Google Cloud ML预测模型
input_init="pla panfletaria contra as leoninas taxas impostas polo ministro de xustiza actual malia que vulneran"
# Load values
window_size = 100
chars_to_indices, indices_to_chars = load_coded_dictionaries()
number_chars=len(chars_to_indices)
# Clean the text
input_clean=clean_text(input_init.lower())
input_clean = input_clean[:window_size]
# Text to array [number_chars,window_size]
x_test = np.zeros((number_chars,window_size))
for t, char in enumerate(input_clean):
x_test[ chars_to_indices[char],t] = 1.
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
instances={'sequence':x_test.tolist()}
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
test_predict=np.array(response['predictions'][0]['scores'])
r = np.argmax(test_predict) # predict class of each test input
indices_to_chars[r]