【问题标题】:Keras, how to get predict with model with removed last layerKeras,如何使用移除最后一层的模型进行预测
【发布时间】:2017-03-16 20:24:05
【问题描述】:

假设我有 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 来做到这一点?非常感谢

【问题讨论】:

  • 你的模型训练了吗?
  • 是的,我接受过培训
  • 你的backend是什么,你能打印model.summary()吗?
  • 查看文档,这可能会对您有所帮助:keras.io/getting-started/faq/…
  • 感谢链接

标签: python matrix tensorflow keras layer


【解决方案1】:

感谢我找到的文档的链接

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)

【讨论】:

  • 在 TF2 中,Model() 的参数是 inputsoutputs,如果它对任何人都有帮助的话。
猜你喜欢
  • 2019-05-22
  • 2018-07-24
  • 2020-09-07
  • 1970-01-01
  • 2020-11-07
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多