本文介绍了Tensorflow's Graph 和 GraphDef 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对张量流很陌生.我想了解 Graph 和 GraphDef 之间的概念差异.

I'm quite new to the tensorflow. I would like to understand to conceptual difference between Graph and GraphDef.

此外,我应该运行从 protobuf 文件 (.pb) 加载的图形?

furthermore, which one should I have to run a graph loaded from protobuf file (.pb)?

谢谢!

推荐答案

GraphComputional Graph 是 tensorflow 呈现计算的核心概念.使用 tensorflow 时,首先创建自己的 Computation Graph 并将 Graph 传递给 tensorflow.怎么做?您可能知道,tensorflow 支持很多前端编程语言,如 Python、C++、Java 和 Go,核心语言是 C++;其他语言如何将 Graph 转换为 C++?他们使用名为 protobuf 的工具可以生成特定的语言存根,这就是 GraphDef 的来源.它是 Graph 的序列化版本.

Graph or Computional Graph is the core concept of tensorflow to present computation. When you use tensorflow, you firstly create you own Computation Graph and pass the Graph to tensorflow. How to do that? As you may know, tensorflow support many front-end programming languages, like Python, C++, Java and Go and the core language is C++; how do the other languages transform the Graph to C++? They use a tool called protobuf which can generate specific language stubs, that's where the GraphDef come from. It's a serialized version of Graph.

我应该运行从 protobuf 文件 (.pb) 加载的图表

您应该使用GraphDefbindGraphDef 读取到您的*pb 文件(默认)Graph,然后使用会话运行 Graph 进行计算,例如 以下代码:

You should read your *pb file using GraphDef and bind the GraphDef to a (default) Graph, then use a session to run the Graph for computation, like the following code:

import tensorflow as tf
from tensorflow.python.platform import gfile
with tf.Session() as sess:
    model_filename ='PATH_TO_PB.pb'
    with gfile.FastGFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        g_in = tf.import_graph_def(graph_def)
LOGDIR='/logs/tests/1/'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)

这篇关于Tensorflow's Graph 和 GraphDef 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:13