Visualizing the prediction from each layer

I have some questions regarding the visualization of the predictions of the layers.

  1. layer_outputs = [layer.output for layer in model.layers]
  2. activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)
  3. f1 = activation_model.predict(test_images[FIRST_IMAGE].reshape(1, 28, 28, 1))

Using the first line of code above we are creating a list of all the layers from the model. In the third line, when using model.predict the model will have the layers from the list as an output layer to predict according to the activation_model created in the second line. right? But when building a model for multiclass classification we use a dense layer with the number of classes. How does this model.predict method create a prediction without having a Dense layer as the output layer?

The tf.keras.models.Model(inputs = model.input, outputs = layer_outputs creates a model with the input as mentioned and several ouputs as in the list of line 1, i.e. you have a multiclass classification here.


Would it be correct to think that prediction just means layer output in this context? The predict() function returns the model’s last layer output, regardless of its dimension or how we choose to interpret its values. From the last layer, maybe the outputs are from a softmax with num_classes values. From the previous layers, it is matrices of values representing what that layer’s activation output (relu sigmoid etc). So this little code fragment basically lets you capture the intermediate results as an input is downsampled and transformed into the final vector of floating points that we use as the multi-class predictions. Thoughts?


I checked that Lab and you are right @ai_curious, the model has several outputs corresponding to each layer in the model under consideration. The intermediate layers have also activations in the original model (some of them) but their values in any case fluctuate between between the allowed pixel ranges, these “predictions” are only used for visualization of internal workings of the model only.

Also a correction from previous reply, its not a multiclass classification per se, its just a multi output model :slight_smile:


I understood these “predictions” are mere representations of the output vectors of these layers. But I thought the .predict method needed a Dense layer with the above-mentioned structure to execute itself. I was wrong. Tested it out just now. .predict can be used to get the output vector from any layer.
Thank you very much.

1 Like

Thank you very much for the help.