首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tensorflow2如何获得中间层输出

tensorflow2如何获得中间层输出
EN

Stack Overflow用户
提问于 2019-12-27 18:48:47
回答 1查看 2K关注 0票数 0

我想利用tensorflow2实现对图像特征的cnn提取,然后输出到支持向量机进行分类。好办法是什么?我已经检查了tensorflow2的文档,没有很好的解释。谁能指引我?

EN

回答 1

Stack Overflow用户

发布于 2019-12-29 18:22:23

感谢您对以上问题的澄清。我以前曾以书面答覆过类似的问题。但是,您可以通过使用所谓的tf.keras函数模型API构造辅助模型,从模型中提取中间层的输出。"Function“使用tf.keras.Model()。调用函数时,可以指定参数inputsoutputs。您可以在outputs参数中包含中间层的输出。请参阅下面的简单代码示例:

代码语言:javascript
复制
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)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59504884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档