Compare Two Tensorflow Graphs

假如想象 提交于 2021-02-07 20:26:05

问题


What is the easiest way to compare two GCMLE deployed prediction models and identify any differences in their graphs? I have visually inspected both tensorboards and they look identical (as they should be). However, I have code to visualize their activations (basically just loads the weights from the graphs and manually performs all forward steps) and somewhere along the way on one of the graphs my hand-written forward pass calculations diverge from tensorflow's forward pass calculations. The code used to train the models should have been identical, yet the same forward pass only appears to be accurate for one of the models. Is there any way to compare the structure?

Beyond inspecting tensorboard, I have separately tried the following:

model1_tensors = [n.name for n in tf.get_default_graph().as_graph_def().node]
model2_tensors = [n.name for n in tf.get_default_graph().as_graph_def().node]

Ultimately, there was also no apparent difference in the tensor names, etc. (suggesting they are the same graph) based on comparing the set() difference of: [n.name for n in tf.get_default_graph().as_graph_def().node] for each of the saved_models.

Given that the same forward pass code yields accurate results for one model and does not yield accurate results for another model I am pretty convinced that the graphs are different, but I am scratching my head at pointing to WHAT that difference is.

To go one step further, I am actually confident that I know the exact layer where the calculations diverge (it is with the batch norm layer), so if there was a way to output all tensors that go into a specific layer then I might be able to identify that there is something else being done.


回答1:


The best approach that I have found is based off is to obtain GraphDef objects and compare them using tensorflow tests..

with tf.Session(graph=tf.Graph()) as sess:
    tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], _VERSION_2)  
    graph_1 = tf.get_default_graph().as_graph_def()

with tf.Session(graph=tf.Graph()) as sess:
    tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], _VERSION_2)  
    graph_2 = tf.get_default_graph().as_graph_def()

Then I can use these to compare the two graphs:

from tensorflow.python import pywrap_tensorflow
diff = pywrap_tensorflow.EqualGraphDefWrapper(graph_65.SerializeToString(), # actual
                                          graph_60.SerializeToString()) # expected
print(diff)

This does appear to show a single difference, but it doesn't display all of the differences.



来源:https://stackoverflow.com/questions/50710474/compare-two-tensorflow-graphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!