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

gug*_*n1c 4 python matrix layer keras tensorflow

假设我有 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))
Run Code Online (Sandbox Code Playgroud)

比我打电话

model.compile(loss = ..
Run Code Online (Sandbox Code Playgroud)

model.fit(input_matrix,..
Run Code Online (Sandbox Code Playgroud)

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

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

所以像这样:

model.remove_last_layer
pred_matrix = model.predict(input_matrix)
Run Code Online (Sandbox Code Playgroud)

输出是 100k x 200 数组,我该如何使用 keras 做到这一点?多谢

gug*_*n1c 5

感谢我找到的文档链接

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)
Run Code Online (Sandbox Code Playgroud)