【问题标题】:How to access tensor_content values in TensorProto in TensorFlow?如何在 TensorFlow 中访问 TensorProto 中的 tensor_content 值?
【发布时间】:2017-05-23 03:39:15
【问题描述】:

类似于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"
    }
  }
}

【问题讨论】:

    标签: tensorflow


    【解决方案1】:
    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)
    

    【讨论】:

    • 还有tf.make_ndarray
    【解决方案2】:

    解码 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2015-01-10
      • 2019-03-29
      相关资源
      最近更新 更多