时间:2021-05-22
更新:
感谢评论区提供的方案。
采用model.summary(),model.get_config()和for循环均可获得Keras的层名。
示例如下图
对于keras特定层的命名,只需在层内添加 name 即可
model.add(Activation('softmax',name='dense_1') ) # 注意 name 要放于函数内#提取中间层from keras.models import Modelimport keraslayer_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,310Trainable params: 10,002,824Non-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 Kget_11rd_layer_output = K.function([model.layers[0].input], [model.layers[10].output])layer_output = get_11rd_layer_output([data])[0]我这里第10层是Pooling层.
这两个代码的output是一样的..
一般我看人用的都是第二个...
以上这篇给keras层命名,并提取中间层输出值,保存到文档的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
pytorch输出中间层特征:tensorflow输出中间特征,2种方式:1.保存全部模型(包括结构)时,需要之前先add_to_collection或者用sl
三门冰箱中间层叫保鲜层。它的作用如下: 1、恒温保鲜作用。三门冰箱中间层能够表现很好的保鲜作用。将中间层的温度调到零度,食物就能够在不结冰的状态下完成保鲜。这
实际应用时可能比较想获取VGG中间层的输出,那么就可以如下操作:importnumpyasnpimporttorchfromtorchvisionimportm
冰箱中间层一般适合放蔬菜、水果、短时保鲜肉等。中间层的特殊保鲜作用使得新鲜的蔬菜水果能够更长时间保持新鲜和营养,因此,买回家的绿叶蔬菜,新鲜水果都可以放在这一层
WPSOffice独有的中间层转换框架保证WPS和微软Office的文档可以实现内核级双向精确兼容。WPS2012深度兼容MicrosoftOffice,你