tensorflow2如何获取中间层输出

时间:2019-12-27 18:48:47

标签: tensorflow2.0

我想使用tensorflow2实现图像特征的cnn提取,然后输出到SVM进行分类。有什么好的方法? 我已经检查了tensorflow2的文档,并且没有很好的解释。谁可以指导我?

1 个答案:

答案 0 :(得分:0)

谢谢您上面的澄清答案。我曾经写过类似问题的答案。但是,您可以使用所谓的功能模型API 构建辅助模型,从而从tf.keras模型中提取中间层输出。 “功能模型API”使用tf.keras.Model()。调用函数时,请指定参数inputsoutputs。您可以在outputs参数中包含中间层的输出。请参见下面的简单代码示例:

import tensorflow as tf

# This is the original model.
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=[28, 28, 1]),
    tf.keras.layers.Dense(100, activation="relu"),
    tf.keras.layers.Dense(10, activation="softmax")])

# Make an auxiliary model that exposes the output from the intermediate layer
# of interest, which is the first Dense layer in this case.
aux_model = tf.keras.Model(inputs=model.inputs,
                           outputs=model.outputs + [model.layers[1].output])

# Access both the final and intermediate output of the original model
# by calling `aux_model.predict()`.
final_output, intermediate_layer_output = aux_model.predict(some_input)
相关问题