比较两个 Tensorflow 图 [英] Compare Two Tensorflow Graphs

查看:35
本文介绍了比较两个 Tensorflow 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较两个 GCMLE 部署的预测模型并确定其图表中的任何差异的最简单方法是什么?我已经目视检查了两个张量板,它们看起来完全相同(应该如此).但是,我有代码来可视化它们的激活(基本上只是从图中加载权重并手动执行所有前向步骤),并且在其中一张图中的某个地方,我手写的前向传递计算与 tensorflow 的前向传递计算不同.用于训练模型的代码应该是相同的,但相同的前向传递似乎只对其中一个模型是准确的.有没有办法比较结构?

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]

最终,基于比较 set() 差异的张量名称等也没有明显差异:[n.name for n in tf.get_default_graph().as_graph_def().node] 对于每个saved_models.

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.

推荐答案

我发现的最佳方法是获取 GraphDef 对象并使用 tensorflow 测试对它们进行比较..

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.

这篇关于比较两个 Tensorflow 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆