Skip to main content
 首页 » 编程设计

python之Keras,如何使用移除最后一层的模型进行预测

2025年02月15日11sharpest

假设我有 100k x 400 的数据集。我创建了这个模型:

model = Sequential() 
model.add(Dense(200, input_dim = 400, init = init_weights)) 
model.add(BatchNormalization()) 
model.add(SReLU()) 
model.add(Dropout(0.5)) 
model.add(Dense(200, init = init_weights)) 
model.add(BatchNormalization()) 
model.add(SReLU()) 
model.add(Dropout(0.5)) 
model.add(Dense(1, activation = 'linear', init = init_weights)) 

比我打电话

model.compile(loss = .. 

和

model.fit(input_matrix,.. 

训练后我可以调用 model.predict(.. 进行预测。

我想得到的是没有最后一个线性层的模型的预测矩阵..

所以是这样的:

model.remove_last_layer 
pred_matrix = model.predict(input_matrix) 

其中输出为 100k x 200 数组,我如何使用 keras 执行此操作?非常感谢

请您参考如下方法:

感谢我找到的文档链接

layer_name = 'dropout_2' 
intermediate_layer_model = Model(input = model.input, output = model.get_layer(layer_name).output) 
intermediate_output = intermediate_layer_model.predict(matrix_test)