Keras/tensorflow: Get Predictions Or Output Of All Layers Efficiently
I am able to get the output/predictions of all layers as suggested in Keras Docs: how-can-i-obtain-the-output-of-an-intermediate-layer def get_output_of_all_layers(model, test_inpu
Solution 1:
As suggested by Ben Usman, you can first wrap the model in a basic end-to-end Model
, and provide its layers as outputs to a second Model
:
import keras.backend as K
from keras.models import Model
from keras.layers import Input, Dense
input_layer = Input((10,))
layer_1 = Dense(10)(input_layer)
layer_2 = Dense(20)(layer_1)
layer_3 = Dense(5)(layer_2)
output_layer = Dense(1)(layer_3)
basic_model = Model(inputs=input_layer, outputs=output_layer)
# some random inputimport numpy as np
features = np.random.rand(100,10)
# With a second Model
intermediate_model = Model(inputs=basic_model.layers[0].input,
outputs=[l.output for l in basic_model.layers[1:]])
intermediate_model.predict(features) # outputs a list of 4 arrays
Or, you could use a Keras function in similar fashion:
# With a Keras function
get_all_layer_outputs = K.function([basic_model.layers[0].input],
[l.output for l in basic_model.layers[1:]])
layer_output = get_all_layer_outputs([features]) # return the same thing
Post a Comment for "Keras/tensorflow: Get Predictions Or Output Of All Layers Efficiently"