如何在TensorFlow中访问TensorProto中的tensor_content值?

时间:2017-05-23 03:39:16

标签: tensorflow

How to access values in protos in TensorFlow?类似,但并不适合这种情况。

我在TensorProto中看到bytes tensor_content属性。我试图通过以下方式获取有关节点的信息:

for node in tf.get_default_graph().as_graph_def().node: node.attr['value'].tensor.tensor_content # decode these bytes

有关信息,节点的打印如下所示:

name: "conv2d/convolution/Shape"
op: "Const"
device: "/device:GPU:0"
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_INT32
      tensor_shape {
        dim {
          size: 4
        }
      }
      tensor_content: "\003\000\000\000\003\000\000\000\001\000\000\000 \000\000\000"
    }
  }
}

2 个答案:

答案 0 :(得分:9)

from tensorflow.python.framework import tensor_util

for n in tf.get_default_graph().as_graph_def().node:
    print tensor_util.MakeNdarray(n.attr['value'].tensor)

答案 1 :(得分:1)

解码tensor_array字节,然后按照给定形状进行整形:

for node in tf.get_default_graph.as_graph_def().node:
    tensor_bytes = node.attr["value"].tensor.tensor_content
    tensor_dtype = node.attr["value"].tensor.dtype
    tensor_shape = [x.size for x in node.attr["value"].tensor.tensor_shape.dim]
    tensor_array = tf.decode_raw(tensor_bytes, tensor_dtype)
    tensor_array = tf.reshape(tensor_array, tensor_shape)