簡體   English   中英

Tensorflow 圖中的張量名稱列表

[英]List of tensor names in graph in Tensorflow

Tensorflow 中的圖形對象有一個名為“get_tensor_by_name(name)”的方法。 無論如何可以獲得有效張量名稱的列表?

如果沒有,有沒有人知道這里的預訓練模型 inception-v3 的有效名稱? 在他們的示例中,pool_3 是一個有效的張量,但列出所有張量會很好。 我查看了所引用的論文,有些層似乎與表 1 中的大小相對應,但並非全部。

論文沒有准確反映模型。 如果你從 arxiv 下載源代碼,它有一個准確的模型描述作為 model.txt,其中的名稱與發布的模型中的名稱密切相關。

要回答您的第一個問題, sess.graph.get_operations()為您提供了一個操作列表。 對於操作, op.name為您提供名稱, op.values()為您提供它生成的張量列表(在 inception-v3 模型中,所有張量名稱都是附加了“:0”的操作名稱,所以pool_3:0是最終池化操作產生的張量。)

以上答案都是正確的。 對於上述任務,我遇到了一個易於理解/簡單的代碼。 所以在這里分享:-

import tensorflow as tf

def printTensors(pb_file):

    # read pb into graph_def
    with tf.gfile.GFile(pb_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # import graph_def
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def)

    # print operations
    for op in graph.get_operations():
        print(op.name)


printTensors("path-to-my-pbfile.pb")

查看圖中的操作(你會看到很多,所以為了簡短起見,我在這里只給出了第一個字符串)。

sess = tf.Session()
op = sess.graph.get_operations()
[m.values() for m in op][1]

out:
(<tf.Tensor 'conv1/weights:0' shape=(4, 4, 3, 32) dtype=float32_ref>,)

您甚至不必創建會話即可查看圖中所有操作名稱的名稱。 為此,您只需要獲取默認圖形tf.get_default_graph()並提取所有操作: .get_operations 每個操作都有許多字段,您需要的是名稱。

這是代碼:

import tensorflow as tf
a = tf.Variable(5)
b = tf.Variable(6)
c = tf.Variable(7)
d = (a + b) * c

for i in tf.get_default_graph().get_operations():
    print i.name

作為嵌套列表理解:

tensor_names = [t.name for op in tf.get_default_graph().get_operations() for t in op.values()]

獲取圖形中張量名稱的函數(默認為默認圖形):

def get_names(graph=tf.get_default_graph()):
    return [t.name for op in graph.get_operations() for t in op.values()]

在圖中獲取張量的函數(默認為默認圖):

def get_tensors(graph=tf.get_default_graph()):
    return [t for op in graph.get_operations() for t in op.values()]

saved_model_cli是 TF 附帶的替代命令行工具,如果您處理“SavedModel”格式,它可能會很有用。 文檔

!saved_model_cli show --dir /tmp/mobilenet/1 --tag_set serve --all

此輸出可能很有用,例如:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['dense_input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1280)
        name: serving_default_dense_input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['dense_1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall:0
  Method name is: tensorflow/serving/predict

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM