How to output the second layer of a network?
Looks like you are mixing old keras (before tensorflow 2.0: import keras
) and new keras (from tensorflow import keras
).
Try not to use old keras alongside tensorflow>=2.0 (and not to refer to the old documentation as in your first link), as it is easily confused with the new one (although nothing strictly illogical):
from tensorflow import keras
from keras.models import Model
print(Model.__module__) #outputs 'keras.engine.training'
from tensorflow.keras.models import Model
print(Model.__module__) #outputs 'tensorflow.python.keras.engine.training'
Behaviour will be highly unstable mixing those two libraries.
Once this is done, using an answer from what you tried, m being your model, and my_input_shape
being the shape of your models input ie the shape of one picture (here (28, 28) or (1, 28, 28) if you have batches):
from tensorflow import keras as K
my_input_data = np.random.rand(*my_input_shape)
new_temp_model = K.Model(m.input, m.layers[3].output) #replace 3 with index of desired layer
output_of_3rd_layer = new_temp_model.predict(my_input_data) #this is what you want
If you have one image img
you can directly write new_temp_model.predict(img)
(Assuming TF2)
I think the most straightforward approach would be to name your layers, and then call them with standard input, so your model might look like
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28), name='flatten'),
keras.layers.Dense(128, activation='relu', name='hidden'),
keras.layers.Dense(10, activation='softmax')
])
Then just create an inputs and
my_input = tf.random.normal((1, 28, 28)) # Should be like the standard input to your network
output_of_flatten = model.get_layer('flatten')(my_input)
output_of_hidden = model.get_layer('hidden')(output_of_flatten)
output_of_hidden
is what you are looking for
Alternative approach
If you are looking for a more general solution, assuming your model is sequential, you can use the index
keyword of get_layer
like this
my_input = tf.random.normal((1, 28, 28)) # Should be like the standard input to your network
desired_index = 1 # 1 == second layer
for i in range(desired_index):
my_input = model.get_layer(index=i)(my_input)
At the end of this loop my_input
should be what you are looking for