Keras: How to get output of intermediate layers when the input is multiple ?

Created on 5 Dec 2016  路  5Comments  路  Source: keras-team/keras

My model has 14 inputs, finally concatenated into one, I want to get the output of 'merged' and 'dense1', but when I run my code ,it reports error like below:
2016-12-05 11 36 32
This is my model:

    sub_model = Sequential()
    sub_model.add(Convolution3D(number_filter,size_filter,size_filter,size_filter, input_shape=(50,4, 3,3),activation='tanh'))
    sub_model.add(Dropout(0.1))
    sub_model.add(Flatten())
    sub_model.add(Dense(sub_fc,W_regularizer = l1l2(l1 = 0.01,l2 = 0.2),activation='tanh'))
    numIn = en_number
    Inputi = ['']*numIn
    sub_outi = ['']*numIn
    for i in range(numIn):
        Inputi[i] = Input(shape =(50, 4, 3, 3))
        sub_outi[i] = sub_model(Inputi[i])
    merged = merge(sub_outi,mode = 'concat')
    dense1 = Dense(number_fc,W_regularizer = l1l2(l1 = 0.01,l2 = 0.2),activation='tanh')(merged)
    dense1 = Dropout(0.5)(dense1)
    output = Dense(2, activation='softmax')(dense1)
    model = Model(input=Inputi, output=output)
    model.summary()  

2016-12-05 11 37 17
2016-12-05 11 38 47

and this is my code to extract the feature:

###        define theano funtion to get output of FC layer
        get_feature = K.function([model.layers[0].input,K.learning_phase()],[model.layers[-2].output]) 
        FC_train_feature = get_feature([X_train,1])[0]
        FC_test_feature = get_feature([X_test,0])[0]
        FC_val_feature = get_feature([X_val,0])[0]

The model works well, but the feature cannot be extracted. Can you help me? I am sorry to disturb you.

@fchollet

@bmabey

@jfsantos

stale

Most helpful comment

This is probably better asked in the google group in the future, because it's not a bug.

  • You might want to look into the TimeDistributed wrapper instead of doing a giant loop. Stack all your inputs into a single tensor, and then use TimeDistributed.

  • To answer your specific question, K.function takes two arguments: a list of inputs and a list of outputs. Your output (the Dropout layer) depends on all of your inputs. So, you need to feed the function all of your inputs.

All 5 comments

This is probably better asked in the google group in the future, because it's not a bug.

  • You might want to look into the TimeDistributed wrapper instead of doing a giant loop. Stack all your inputs into a single tensor, and then use TimeDistributed.

  • To answer your specific question, K.function takes two arguments: a list of inputs and a list of outputs. Your output (the Dropout layer) depends on all of your inputs. So, you need to feed the function all of your inputs.

Thank you for your reply. I'll have a try using Time Distributed wrapper.
But my input length is 14 the same as my model defined, I think I had feed all of inputs, so I don't know why the error occurs.

@jmhessel
Thank you for your help.

image
This model can be realized by TimeDistributed wrapper in this form.
Thanks for your advices. @jmhessel

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.

@Tristacheng @jmhessel
Thanks. how to change the code for image, not text.
My model has two inputs and one outputs.
The backend is tensorflow
the code is

def getFeatureMap(model,layer,X_batch):
featureMap=K.function([model.layers[0].input,model.layers[0].input],[model.layers[layer].output])
gmp = featureMap([X_batch,0])
return gmp

but it show me an error.
I also use the following code.

model_extractfeatures = Model(input=[model.layers[0].input,model.layers[0].input], output=model.layers[-2].output)
everyFm = model_extractfeatures.predict(li[i])

It doesn't work.
how can i rewrite the code? Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braingineer picture braingineer  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

yil8 picture yil8  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments