how to load and use a saved model on tensorflow?

后端 未结 4 1510
无人及你
无人及你 2021-01-31 19:22

I have found 2 ways to save a model in Tensorflow: tf.train.Saver() and SavedModelBuilder. However, I can\'t find documentation on using the mo

4条回答
  •  醉梦人生
    2021-01-31 20:06

    What was missing was the signature

    # Saving
    builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
    builder.add_meta_graph_and_variables(sess, ["tag"], signature_def_map= {
            "model": tf.saved_model.signature_def_utils.predict_signature_def(
                inputs= {"x": x},
                outputs= {"finalnode": model})
            })
    builder.save()
    
    # loading
    with tf.Session(graph=tf.Graph()) as sess:
        tf.saved_model.loader.load(sess, ["tag"], export_dir)
        graph = tf.get_default_graph()
        x = graph.get_tensor_by_name("x:0")
        model = graph.get_tensor_by_name("finalnode:0")
        print(sess.run(model, {x: [5, 6, 7, 8]}))
    

提交回复
热议问题