gpt4 book ai didi

tensorflow - 如何在 TensorFlow 中访问 protos 中的值?

转载 作者:行者123 更新时间:2023-12-01 13:35:06 25 4
gpt4 key购买 nike

我从 tutorial 看到我们可以这样做:

for node in tf.get_default_graph().as_graph_def().node:
print node

当在任意网络上完成时,我们会得到许多键值对。例如:

name: "conv2d_2/convolution"
op: "Conv2D"
input: "max_pooling2d/MaxPool"
input: "conv2d_1/kernel/read"
device: "/device:GPU:0"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "data_format"
value {
s: "NHWC"
}
}
attr {
key: "padding"
value {
s: "SAME"
}
}
attr {
key: "strides"
value {
list {
i: 1
i: 1
i: 1
i: 1
}
}
}
attr {
key: "use_cudnn_on_gpu"
value {
b: true
}
}

如何访问所有这些值并将它们放入 Python 列表中?具体来说,我们如何获取“strides”属性并将那里的所有 1 转换为 [1, 1, 1, 1]?

最佳答案

TLDR:下面的代码是您可能想要使用的代码:

for n in tf.get_default_graph().as_graph_def().node:
if 'strides' in n.attr.keys():
print n.name, [int(a) for a in n.attr['strides'].list.i]
if 'shape' in n.attr.keys():
print n.name, [int(a.size) for a in n.attr['shape'].shape.dim]
这样做的诀窍是了解 protobufs是。让我们通过 tutorial上文提到的。
首先,有一个声明:
for node in graph_def.node

Each node is a NodeDef object, defined intensorflow/core/framework/node_def.proto. These are the fundamentalbuilding blocks of TensorFlow graphs, with each one defining a singleoperation along with its input connections. Here are the members of aNodeDef, and what they mean.


请注意 node_def.proto 中的以下内容:
  • 它导入 attr_value.proto。
  • 有name、op、input、device、attr等属性。具体来说,有一个 repeated输入前面的术语。我们现在可以忽略这一点。

  • 这与 Python 类完全一样,因此我们可以调用 node.name、node.op、node.input、node.device、node.attr 等。
    我们现在要访问的是 node.attr 中的内容。如果我们再次引用该教程,它会指定:

    This is a key/value store holding all the attributes of a node. Theseare the permanent properties of nodes, things that don't change atruntime such as the size of filters for convolutions, or the values ofconstant ops. Because there can be so many different types ofattribute values, from strings, to ints, to arrays of tensor values,there's a separate protobuf file defining the data structure thatholds them, in tensorflow/core/framework/attr_value.proto.

    Each attribute has a unique name string, and the expected attributesare listed when the operation is defined. If an attribute isn'tpresent in a node, but it has a default listed in the operationdefinition, that default is used when the graph is created.

    You can access all of these members by calling node.name, node.op,etc. in Python. The list of nodes stored in the GraphDef is a fulldefinition of the model architecture.


    由于这是一个键值存储,我们可以调用 n.attr.keys()查看此属性具有的键列表。我们可以进一步调用 n.attr['strides']访问步幅,如果这样的 key 可用。当我们尝试打印它时,我们得到以下信息:
    list {
    i: 1
    i: 2
    i: 2
    i: 1
    }
    这就是它开始变得困惑的地方,因为我们可能会尝试做 list(n.attr['strides'])或类似的东西。如果我们查看 attr_value.proto,我们可以理解发生了什么。我们看到它是 oneof value在这种情况下,它是 ListValue list ,所以我们可以调用 n.attr['strides'].list .如果我们打印它,我们会得到以下信息:
    i: 1
    i: 1
    i: 1
    i: 1
    我们接下来可能会尝试这样做: [a for a in n.attr['strides'].list][a.i for a in n.attr['strides'].list] .但是,没有任何效果。这是 repeated是一个需要理解的重要术语。这基本上意味着有一个 int64 列表,您必须使用 i 访问它。属性。做 [int(a) for a in n.attr['strides'].list.i]然后给我们我们想要的,一个我们可以使用的 Python 列表:
    [1, 1, 1, 1]

    关于tensorflow - 如何在 TensorFlow 中访问 protos 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44124942/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com