我正在将Keras与Theano后端配合使用,并希望以适用于Cloud ML Engine的格式导出模型。我的模型称为model
,而model.input
和model.output
的类型均为theano.tensor.var.TensorVariable
。这是我在做什么:
from tensorflow import saved_model
import tf.keras.backend as K
model_builder = saved_model.builder.SavedModelBuilder("exported_model")
inputs = {
'input': saved_model.utils.build_tensor_info(model.input)
}
outputs = {
'output': saved_model.utils.build_tensor_info(model.output)
}
signature_def = saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=saved_model.signature_constants.PREDICT_METHOD_NAME
)
model_builder.add_meta_graph_and_variables(
K.get_session(),
tags=[saved_model.tag_constants.SERVING],
signature_def_map={saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
})
model_builder.save()
不幸的是,以上操作失败并显示:
File "/Users/...", line 280, in export
'input': saved_model.utils.build_tensor_info(model.input)
File "/Users/.../python2.7/site-packages/tensorflow/python/saved_model/utils_impl.py", line 45, in build_tensor_info
tensor_shape=tensor.get_shape().as_proto())
AttributeError: 'TensorVariable' object has no attribute 'get_shape'
知道我做错了什么吗?必须有某种方法可以导出带有Theano后端的Keras模型以用于ML Engine。