Created
February 23, 2018 01:05
-
-
Save yupbank/217cf1db2e4ab85d32282e86d30f697e to your computer and use it in GitHub Desktop.
savedModel in tensorflow doing factorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import os | |
tf.app.flags.DEFINE_string('output_dir', '/tmp/factorial_output', | |
"""Directory where to export inference model.""") | |
tf.app.flags.DEFINE_integer('model_version', 1, | |
"""Version number of the model.""") | |
FLAGS = tf.app.flags.FLAGS | |
def factorial_model(x): | |
return tf.exp(tf.lgamma(x + 1)) | |
def main(_): | |
with tf.Graph().as_default(): | |
x = tf.placeholder(tf.float32, shape=[None]) | |
result = factorial_model(x) | |
with tf.Session() as sess: | |
output_path = os.path.join( | |
tf.compat.as_bytes(FLAGS.output_dir), | |
tf.compat.as_bytes(str(FLAGS.model_version))) | |
print('Exporting trained model to %s' % output_path) | |
builder = tf.saved_model.builder.SavedModelBuilder(output_path) | |
x_info = tf.saved_model.utils.build_tensor_info(x) | |
result_info = tf.saved_model.utils.build_tensor_info(result) | |
classification_signature = ( | |
tf.saved_model.signature_def_utils.build_signature_def( | |
inputs={ | |
'x': | |
x_info, | |
}, | |
outputs={ | |
'result': | |
result_info, | |
}, | |
method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME) | |
) | |
legacy_init_op = tf.group( | |
tf.tables_initializer(), name='legacy_init_op') | |
builder.add_meta_graph_and_variables( | |
sess, [tf.saved_model.tag_constants.SERVING], | |
signature_def_map={ | |
tf.saved_model.signature_constants. | |
DEFAULT_SERVING_SIGNATURE_DEF_KEY: | |
classification_signature, | |
}, | |
legacy_init_op=legacy_init_op) | |
builder.save() | |
print('Successfully exported model to %s' % FLAGS.output_dir) | |
if __name__ == '__main__': | |
tf.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment