keras 获取中间层的输出结果

KERAS 获取中间层的输出结果

现在我们要获取第三个全连接层dense_3的输出,有以下两种常用的方式:

1 通过K.function()函数打印模型中间层的输出
思路是通过K.function()函数创建Keras函数,然后再将数据输入到函数中即可。
keras.backend.function(inputs, outputs, updates=None) 输出中间结果

实例化keras函数
representation_layer = K.function(inputs=[model.layers[0].input], outputs=[model.get_layer(‘dense_3’).output])
调用实例化后的keras函数获取学习到的特征
representation = representation_layer([X_train])

注意这里的inputs和outputs应该为list或者tuple对象!!!
representation_layer = K.function(inputs=[model.layers[0].input], outputs=[model.get_layer(‘dense_3’).output])

调用实例化后的keras函数获取学习到的特征
representation = representation_layer([X_train])
representation = np.array(representation)[0]
print(representation.shape)
print(type(representation))
print(representation)

  1. 通过函数模型API
    representation_model = Model(inputs=model.inputs, outputs=model.get_layer(‘dense_3’).output)

这个模型的预测值即为学习到的特征
dense_3_output = representation_model.predict(X_train)

print(dense_3_output.shape)
print(type(dense_3_output))
print(dense_3_output)
————————————————
版权声明:本文为CSDN博主「selectopti」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/selectopti/article/details/115933551

posted @ 2022-11-01 17:38  谁动了我的奶盖  阅读(429)  评论(0编辑  收藏  举报