How to get the output of each layer of a Keras model in Python

A Keras model runs data sequentially through a series of layers with associated functions. The first layer takes the input to the overall model as its input, and outputs the layer function called on that input. Each subsequent layer takes the previous layer’s output as input and applies the layer funtion.

Solution for How to get the output of each layer of a Keras model in Python : You can use layer.output to get the output Tensors After constructing and training the model, use layer.output to get the output Tensors for each layer. Then call keras.backend.function(input_list, output_list) where input_list is the input to the model, obtained with Model.input, and output_list is the output Tensors of a layer of the model, obtained from the value of the previous layer.output attributes. The result of this is a new function that takes a 2 element list of where the first element is the training data and the second element is 1. Call this function to get the output values of each layer for the training data, represented by a two-dimensional numpy.ndarray where each row represents a sample from the training data and each column represents a single node of that layer.

Use a list comprehension for a more compact implementation.

print(training_data)


how-to-get-the-output-of-each-layer-of-a-keras-model-in-python