1

I want to see the output of each layer of a keras model. So far I have something like this

for layer in model.layers:
  print(layer.output)

And I am getting the output like this:

Tensor("word_input_6:0", shape=(None, 198), dtype=int32)
Tensor("embedding_6/Identity:0", shape=(None, 198, 100), dtype=float32)
Tensor("lstm/Identity:0", shape=(None, 100), dtype=float32)
Tensor("main_output_8/Identity:0", shape=(None, 6), dtype=float32)

But I want to see what is inside the tensor. Is there any way to see it?

odbhut.shei.chhele
  • 5,834
  • 16
  • 69
  • 109

1 Answers1

2

There are couple of options to get outputs of each layer. You could.
(1) define a keras function and evaluate it for each layer, or.
(2) define a functional model and run predict method to get the output.

More details on these approaches are in this stackoverflow_question (Keras, How to get the output of each layer?)

Depending on your requirements, you can select either one. I selected first one and demonstrated with simple mnist example. Full example code here.

#Getting output of each layer
from tensorflow.keras import backend as K

inp = model.input                                           # input 
outputs = [layer.output for layer in model.layers]          # all layer outputs
functors = [K.function([inp], [out]) for out in outputs]    # evaluation functions

# Testing
test = test = x_test[1]
count = 0
for func in functors:
  print('\n')
  print("Layer Name: ",layer_names[count])
  print('\n')
  print(func([test]))
  count+=1
Vishnuvardhan Janapati
  • 3,088
  • 1
  • 16
  • 25