Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to get output of intermediate layer in Keras model?

I have trained a model in Keras and want to extract the output from an intermediate layer. The model contains dropout layers and I want to be absolutely sure nothing is dropped when doing this.

According to the documentation, a layer's output can be extracted like this:

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)

However, docs also show how to do so with a Keras function:

get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],
                                  [model.layers[3].output])

# output in test mode = 0
layer_output = get_3rd_layer_output([x, 0])[0]

# output in train mode = 1
layer_output = get_3rd_layer_output([x, 1])[0]

Here, the learning_phase() flag tells keras whether to actually use dropout and similar things that are only used during training.

My question is, if I use the first approach, will dropout automatically be deactivated, or do I need to do something similar to setting the learning phase flag (as is done in the second approach).

like image 228
bjarkemoensted Avatar asked Jun 28 '18 20:06

bjarkemoensted


People also ask

How do you get the output of a model in Keras?

Just do a model. summary() . It will print all layers and their output shapes.

How do you get the output of each layer of a Keras model in python?

Just do a model. summary(). It will print all layers and their output shapes.


1 Answers

Yes, Model knows when it's training or testing, the flag is auto set when you call train() or predict().

like image 66
THN Avatar answered Oct 22 '22 16:10

THN