【问题标题】:How do I get TensorFlow's 'import_graph_def' to return Tensors如何让 TensorFlow 的“import_graph_def”返回张量
【发布时间】:2016-05-10 18:29:03
【问题描述】:

如果我尝试使用

导入保存的TensorFlow 图形定义
import tensorflow as tf
from tensorflow.python.platform import gfile

with gfile.FastGFile(FLAGS.model_save_dir.format(log_id) + '/graph.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
x, y, y_ = tf.import_graph_def(graph_def, 
                               return_elements=['data/inputs',
                                                'output/network_activation',
                                                'data/correct_outputs'],
                               name='')

返回的值不是预期的Tensors,而是其他东西:例如,将x 获取为

Tensor("data/inputs:0", shape=(?, 784), dtype=float32)

我明白了

name: "data/inputs_1"
op: "Placeholder"
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "shape"
  value {
    shape {
    }
  }
}

也就是说,我得到的不是预期的张量x,而是x.op。这让我很困惑,因为documentation 似乎说我应该得到一个Tensor(尽管那里有一堆 or 让人难以理解)。

我如何让tf.import_graph_def 返回特定的Tensors,然后我可以使用(例如,在输入加载的模型或运行分析时)?

【问题讨论】:

  • 第二行代码应该是from tensorflow.python.platform import gfile

标签: python machine-learning tensorflow restore


【解决方案1】:

名称'data/inputs''output/network_activation''data/correct_outputs' 实际上是操作名称。要让tf.import_graph_def() 返回tf.Tensor 对象,您应该将输出索引附加到操作名称,对于单输出操作,通常为':0'

x, y, y_ = tf.import_graph_def(graph_def, 
                               return_elements=['data/inputs:0',
                                                'output/network_activation:0',
                                                'data/correct_outputs:0'],
                               name='')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多