Keras, How to get the output of each layer?

后端 未结 10 1998
借酒劲吻你
借酒劲吻你 2020-11-22 07:34

I have trained a binary classification model with CNN, and here is my code

model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_si         


        
10条回答
  •  情书的邮戳
    2020-11-22 08:04

    Wanted to add this as a comment (but don't have high enough rep.) to @indraforyou's answer to correct for the issue mentioned in @mathtick's comment. To avoid the InvalidArgumentError: input_X:Y is both fed and fetched. exception, simply replace the line outputs = [layer.output for layer in model.layers] with outputs = [layer.output for layer in model.layers][1:], i.e.

    adapting indraforyou's minimal working example:

    from keras import backend as K 
    inp = model.input                                           # input placeholder
    outputs = [layer.output for layer in model.layers][1:]        # all layer outputs except first (input) layer
    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
    

    p.s. my attempts trying things such as outputs = [layer.output for layer in model.layers[1:]] did not work.

提交回复
热议问题