Keras is a high level neural network library used for fast experimentation, user friendliness and easy extensibility. It is highly recommended library for a beginner in neural networks. In this blog we will learn how to use an intermediate layer of a neural network as input to another network.
Sometimes you might get stuck while using an output of an intermediate layer with the errors like ‘graph disconnected‘. Lets see how we can solve this through the code.
First, Lets create an autoencoder model. If you are not aware of what is an autoencoder, you can follow this blog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# creating autoencoder model encoder_inputs = Input(shape = (28,28,1)) conv1 = Conv2D(16, (3,3), activation = 'relu', padding = "SAME")(encoder_inputs) pool1 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv1) conv2 = Conv2D(32, (3,3), activation = 'relu', padding = "SAME")(pool1) pool2 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv2) flat = Flatten()(pool2) encoder_outputs = Dense(32, activation = 'relu')(flat) dense_layer_d = Dense(7*7*32, activation = 'relu')(encoder_outputs) output_from_d = Reshape((7,7,32))(dense_layer_d) conv1_1 = Conv2D(32, (3,3), activation = 'relu', padding = "SAME")(output_from_d) upsampling_1 = Conv2DTranspose(32, 3, padding='same', activation='relu', strides=(2, 2))(conv1_1) upsampling_2 = Conv2DTranspose(16, 3, padding='same', activation='relu', strides=(2, 2))(upsampling_1) decoded_outputs = Conv2DTranspose(1, 3, padding='same', activation='relu')(upsampling_2) autoencoder = Model(encoder_inputs, decoded_outputs) |
In the above code we have created an autoencoder model. At line 9, we have generated encoder outputs. Now if you want to create decoder network from this model with encoder_outputs layer as it input, what should you do? A beginner will do something like this:
1 |
decoder_network = Model(dense_layer_d, decoded_outputs) |
But this will throw an error ‘graph disconnected’. This is because
1 2 3 4 5 6 |
decoder_input = Input(shape = (32,)) next_layer = decoder_input for layer in autoencoder.layers[-6:]: next_layer = layer(next_layer) decoder = Model(decoder_input, next_layer) |
Earlier we have created a model autoencoder. Now if you want to get its intermediate layer, use following steps:
- Find index of the input layer to decoder( in the given autoencoder model it is the 6th layer from last so -6)
- Use autoencoder.layers to get that layer.
- Iterate through the following layers in the autoencoder model, till the decoder_output layer.
- Then create model using decoder_input and last iterated layer.
This will successfully create a decoder model which will take the output of an intermediate layer ‘encoder_outputs’ as its input. And that’s it!!
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.