**问题:
在调用训练好的模型时,有时使用某一层的输出,例如dense层:
outputs = tf.layers.dense(inputs=logits1, units=n_outputs, name="outputs_1")
在调用该层的输出时,如果你使用:
outputs = graph.get_tensor_by_name('outputs_1:0')
就会报错:
KeyError: "The name 'outputs_1:0' refers to a Tensor which does not exist. The operation, 'outputs_1', does not exist in the graph."
这是因为:
dense层具有偏差bias和权重weight参数, 因此在命名时,您将命名整个layer为"outputs_1",而不是输出张量tensor.
展开后为
修改:
所以要使用该层的输出tensor, 可以使用 tf.identity:返回一个与输入具有相同形状和内容的张量。
outputs = tf.layers.dense(inputs=logits1, units=n_outputs)
outputs = tf.identity(outputs, name="outputs_1")
修改后重新训练,完成后即可实现获取输出tensor
使用:
outputs = graph.get_tensor_by_name('outputs_1:0')