如何在 TensorFlow 中访问 TensorProto 中的 tensor_content 值? [英] How to access tensor_content values in TensorProto in TensorFlow?

查看:48
本文介绍了如何在 TensorFlow 中访问 TensorProto 中的 tensor_content 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于 如何在 TensorFlow 中访问 protos 中的值? 但不适合这种情况.

Similar to How to access values in protos in TensorFlow? but doesn't cater for this case.

我在 bytes tensor_content 属性"noreferrer">TensorProto.我正在尝试通过以下方式获取有关节点的信息:

I see a bytes tensor_content attribute in TensorProto. I'm trying to get information about the nodes through:

<代码>对于 tf.get_default_graph().as_graph_def().node 中的节点:node.attr['value'].tensor.tensor_content # 解码这些字节

有关信息,节点的打印看起来像这样:

For information, the print of a node looks something like this:

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

推荐答案

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)

这篇关于如何在 TensorFlow 中访问 TensorProto 中的 tensor_content 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆