温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何给keras层命名并提取中间层输出值,保存到文档

发布时间:2020-07-22 14:59:25 来源:亿速云 阅读:449 作者:小猪 栏目:开发技术

这篇文章主要讲解了如何给keras层命名并提取中间层输出值,保存到文档,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

更新:

感谢评论区提供的方案。

采用model.summary(),model.get_config()和for循环均可获得Keras的层名。

示例如下图

如何给keras层命名并提取中间层输出值,保存到文档

对于keras特定层的命名,只需在层内添加 name 即可

model.add(Activation('softmax',name='dense_1') )  # 注意 name 要放于函数内

#提取中间层
from keras.models import Model
import keras
layer_name = 'dense_1' #获取层的名称
intermediate_layer_model = Model(inputs=model.input, 
         outputs=model.get_layer(layer_name).output)#创建的新模型
intermediate_output = intermediate_layer_model.predict(X_test)

doc = open(r'C://Users//CCUT04//Desktop//1.txt','w')
for i in intermediate_output:
 print(i)
 print(i , file = doc)
doc.close()

补充知识:关于用keras提取NN中间layer输出

Build model...
__________________________________________________________________________________________________
Layer (type)     Output Shape   Param #  Connected to      
==================================================================================================
main_input (InputLayer)   (None, 89, 39)  0           
__________________________________________________________________________________________________
cropping1d_1 (Cropping1D)  (None, 85, 39)  0   main_input[0][0]     
__________________________________________________________________________________________________
cropping1d_2 (Cropping1D)  (None, 85, 39)  0   main_input[0][0]     
__________________________________________________________________________________________________
cropping1d_3 (Cropping1D)  (None, 85, 39)  0   main_input[0][0]     
__________________________________________________________________________________________________
cropping1d_4 (Cropping1D)  (None, 85, 39)  0   main_input[0][0]     
__________________________________________________________________________________________________
cropping1d_5 (Cropping1D)  (None, 85, 39)  0   main_input[0][0]     
__________________________________________________________________________________________________
concatenate_1 (Concatenate)  (None, 85, 195)  0   cropping1d_1[0][0]    
                 cropping1d_2[0][0]    
                 cropping1d_3[0][0]    
                 cropping1d_4[0][0]    
                 cropping1d_5[0][0]    
__________________________________________________________________________________________________
fc1 (BatchNormalization)  (None, 85, 195)  780   concatenate_1[0][0]    
__________________________________________________________________________________________________
fc2 (Bidirectional)    (None, 85, 2048)  9994240  fc1[0][0]      
__________________________________________________________________________________________________
fc3 (BatchNormalization)  (None, 85, 2048)  8192  fc2[0][0]      
__________________________________________________________________________________________________
global_average_pooling1d_1 (Glo (None, 2048)   0   fc3[0][0]      
__________________________________________________________________________________________________
main_output (Dense)    (None, 2)   4098  global_average_pooling1d_1[0][0] 
==================================================================================================
Total params: 10,007,310
Trainable params: 10,002,824
Non-trainable params: 4,486
__________________________________________________________________________________________________

假设我网络层数是上面这个结构.

如果我想得到pooling的输出, keras上有两张方法。

intermediate_layer_model = Model(inputs=model.input,outputs=model.get_layer(str('global_average_pooling1d_1')).output)
#model.summary()
#model.get_layer(str('cropping1d_1'))
intermediate_output = intermediate_layer_model.predict(data)

data是你的输入所用的数据....

from keras import backend as K
get_11rd_layer_output = K.function([model.layers[0].input],
         [model.layers[10].output])
layer_output = get_11rd_layer_output([data])[0]

我这里第10层是Pooling层.

看完上述内容,是不是对如何给keras层命名并提取中间层输出值,保存到文档有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI