By Asim


2018-10-14 06:19:35 8 Comments

My model is a simple fully connected network like this:

inp=Input(shape=(10,))
d=Dense(64, activation='relu')(inp)
d=Dense(128,activation='relu')(d)
d=Dense(256,activation='relu')(d)     #want to give input here, layer3
d=Dense(512,activation='relu')(d)
d=Dense(1024,activation='relu')(d)
d=Dense(128,activation='linear')(d)

So, after saving the model I want to give input to layer 3. What I am doing right now is this:

model=load_model('blah.h5')    #above described network
print(temp_input.shape)        #(16,256), which is equal to what I want to give

index=3
intermediate_layer_model = Model(inputs=temp_input,
                                 outputs=model.output)

End_output = intermediate_layer_model.predict(temp_input)

But it isn't working, i.e. I am getting errors like incompatible input, inputs should be tuple etc. The error message is:

raise TypeError('`inputs` should be a list or tuple.') 
TypeError: `inputs` should be a list or tuple.

Is there any way I can pass my own inputs in middle of network and get the output instead of giving an input at the start and getting output from the end? Any help will be highly appreciated.

1 comments

@today 2018-10-15 10:12:28

First you must learn that in Keras when you apply a layer on an input, a new node is created inside this layer which connects the input and output tensors. Each layer may have multiple nodes connecting different input tensors to their corresponding output tensors. To build a model, these nodes are traversed and a new graph of the model is created which consists all the nodes needed to reach output tensors from input tensors (i.e. which you specify when creating a model: model = Model(inputs=[...], outputs=[...]).

Now you would like to feed an intermediate layer of a model and get the output of the model. Since this is a new data-flow path, we need to create new nodes for each layer corresponding to this new computational graph. We can do it like this:

idx = 3  # index of desired layer
input_shape = model.layers[idx].get_input_shape_at(0) # get the input shape of desired layer
layer_input = Input(shape=input_shape) # a new input tensor to be able to feed the desired layer

# create the new nodes for each layer in the path
x = layer_input
for layer in model.layers[idx:]:
    x = layer(x)

# create the model
new_model = Model(layer_input, x)

Fortunately, your model consists of one-branch and we could simply use a for loop to construct the new model. However, for more complex models it may not be easy to do so and you may need to write more codes to construct the new model.

@Asim 2018-10-15 14:34:01

Much thanks, worked like a charm!!

Related Questions

Sponsored Content

0 Answered Questions

"Decoupled" feedforward network (all dense layers) possible in Keras?

1 Answered Questions

Where do the parameters in keras layers apply?

2 Answered Questions

[SOLVED] Get decoder from trained autoencoder model in Keras

2 Answered Questions

[SOLVED] Keras: wrong shape in Dense layer

0 Answered Questions

Keras - LSTM on embedding - dense layers

0 Answered Questions

Replace BackPropagation functionality of Keras Layers

1 Answered Questions

[SOLVED] Keras Model - Functional API - adding layers to existing model

  • 2018-02-02 14:37:47
  • vladimir.gorea
  • 2117 View
  • 2 Score
  • 1 Answer
  • Tags:   keras

1 Answered Questions

2 Answered Questions

1 Answered Questions

Sponsored Content