问题描述
我使用CNN训练了二分类模型,这是我的Keras代码
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
# (16, 16, 32)
model.add(Convolution2D(nb_filters*2, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters*2, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
# (8, 8, 64) = (2048)
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2)) # define a binary classification problem
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=1,
validation_data=(x_test, y_test))
在这里,我想像TensorFlow一样获得每一层的输出,我该怎么做?
最佳回答
您可以使用以下方法轻松获取任何层的输出:model.layers[index].output
对于所有图层,请使用以下命令:
from keras import backend as K
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs] # evaluation functions
# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = [func([test, 1.]) for func in functors]
print layer_outs
注意:要模拟Dropout,请使用learning_phase 作为layer_outs 中的1. ,否则使用0.
改进1:(基于评论)
K.function 创建theano /tensorflow张量函数,该函数随后用于从给定输入的符号图中获取输出。
现在,需要K.learning_phase() 作为输入,因为许多Keras层(如Dropout /Batchnomalization)都依赖它来在训练和测试期间更改行为。
因此,如果您删除代码中的dropout,则可以简单地使用:
from keras import backend as K
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functors = [K.function([inp], [out]) for out in outputs] # evaluation functions
# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = [func([test]) for func in functors]
print layer_outs
改进2:更优化
先前的答案并不是针对每个函数评估进行了优化,因为数据将被传输到CPU-> GPU内存中,并且还需要对较低层进行张量计算。
相应的,这里有一种更好的方法,因为您不需要多个函数,而只需一个函数即可为您提供所有输出的列表:
from keras import backend as K
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functor = K.function([inp, K.learning_phase()], outputs ) # evaluation function
# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = functor([test, 1.])
print layer_outs
次佳回答
来自https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer的
一种简单的方法是创建一个新模型,该模型将输出您感兴趣的图层:
from keras.models import Model
model = ... # include here your original model
layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
另外,您可以构建Keras函数,该函数将在给定特定输入的情况下返回特定图层的输出,例如:
from keras import backend as K
# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
[model.layers[3].output])
layer_output = get_3rd_layer_output([x])[0]
补充信息
基于上述优秀答案,这里编写了一个库来获取每一层的输出。它抽象了所有复杂性,并被设计为尽可能地对用户友好:
https://github.com/philipperemy/keract
它处理了几乎所边际情况,望能帮助到你!
参考资料
- Keras, How to get the output of each layer?
|