图中的张量名列表(以十为单位)

2024-04-26 12:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

Tensorflow中的graph对象有一个名为“get_tensor_by_name(name)”的方法。有没有得到一个有效的张量名列表?

如果不知道,是否有人知道预训练模型inception-v3from here的有效名称?从他们的例子来看,poolç3是一个有效的张量,但是一个所有张量的列表会很好。我查看了the paper referred to,其中一些层似乎与表1中的大小相对应,但并不是全部。


Tags: 对象方法name模型名称列表getby
3条回答

查看图中的操作(您将看到许多操作,因此简而言之,我在这里只给出了第一个字符串)。

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>,)

这篇论文没有准确地反映这个模型。如果您从arxiv下载源代码,它有一个精确的模型描述model.txt,其中的名称与发布的模型中的名称有着强烈的关联。

为了回答您的第一个问题,sess.graph.get_operations()提供了一个操作列表。对于op,op.name为您提供名称,op.values()为您提供它生成的张量列表(在inception-v3模型中,所有张量名称都是附加“:0”的op名称,因此pool_3:0是最终池op生成的张量。)

以上答案是正确的。我在上面的任务中遇到了一个易于理解/简单的代码。所以在这里分享它:

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")

相关问题 更多 >