Extract subnetwork from Keras Sequential model

Viewed 307

I trained a very simple autoencoder network similar to this example:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
        layers.Dense(128, activation="relu"),
        layers.Dense(64, activation="relu"),
        layers.Dense(32, activation="relu"),
        layers.Dense(16, activation="relu"),
        layers.Dense(8, activation="relu", name="latent_space"),
        layers.Dense(16, activation="relu"),
        layers.Dense(32, activation="relu", name="decode_32"),
        layers.Dense(64, activation="relu"),
        layers.Dense(128, activation="sigmoid"),
        ])

model.compile(...)
model.fit(...)

# Extract subnetwork here after training

I would like to know if it is possible to feed data to the latent_space layer such that I can afterwards extract the activations from layer decode_32? Ideally I would like to crop a subnetwork after training with the latent_space layer as the input and the decode_32 layer as the output layer. Is that possible?

1 Answers

Does this answer fits your question?

def extract_layers(main_model, starting_layer_ix, ending_layer_ix) :
  # create an empty model
  new_model = Sequential()
  for ix in range(starting_layer_ix, ending_layer_ix + 1):
    curr_layer = main_model.get_layer(index=ix)
    # copy this layer over to the new model
    new_model.add(curr_layer)
  return new_model 

If you prefer selecting your subnetwork with the names of the first and last layers, the get_layer method also has an argument for the layer's name, but an easier solution would be to retrieve the indexes of the layers to select thanks to the layer.name argument.

That way, you just have to modify the previous function by adding

layer_names = [layer.name for layer in main_model.layers]
starting_layer_ix = layer_names.index(starting_layer_name)
ending_layer_ix = layer_names.index(ending_layer_name)
Related