Keras: How can I get the output of an intermediate layer?

Created on 25 Apr 2016  Â·  10Comments  Â·  Source: keras-team/keras

Here is my code
model = Sequential()
model.add(Convolution2D(32,5,5, border_mode='valid', input_shape=(1, 28, 28)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(32,5,5))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(11))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',optimizer=sgd)
model.fit(X_train, y_train,nb_epoch=2)

I want to get the output of the dense layer with 256 units, how I should modify the code.
I have tried these code
model_part=Model(input=[model.layers[0]], output=[model.layers[8]])
model_part.compile(loss='categorical_crossentropy',optimizer=sgd)
model_part.fit(X_train, y_train,nb_epoch=2)
but it returns an error.

Traceback (most recent call last):
File "/Users/hanjun/Desktop/Code/deep.py", line 60, in
model_part=Model(input=[model.layers[0]], output=[model.layers[8]])
TypeError: object() takes no parameters

stale

Most helpful comment

if you're using the functional API just make a new model = Model(input=[inputs], output=[intermediate_layer]), compile and predict

All 10 comments

if you're using the functional API just make a new model = Model(input=[inputs], output=[intermediate_layer]), compile and predict

If you don't use functional API, you can extract output of any layer by following instruction in FAQ

@stevenhanjun Did you solved your problem ?? I have the same querry, but i am not understanding by the FAQ of keras. Please help me regarding thsi

Here is the solution:
https://keras.io/getting-started/faq/#how-can-i-visualize-the-output-of-an-intermediate-layer
or you can use this code:model = Model(input=[inputs],
output=[intermediate_layer])

Steven Han
Master, Xi'an Shannxi
Department of Computer Science
Xidian University
[email protected]

2017-10-04 0:40 GMT-04:00 CHANCHAL SUMAN notifications@github.com:

@stevenhanjun https://github.com/stevenhanjun Did you solved your
problem ?? I have the same querry, but i am not understanding by the FAQ of
keras

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/2495#issuecomment-334047926,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEP3K-1SVVhW_BvuefOIorD5u6L3Hvtuks5sowxAgaJpZM4IOtN_
.

Thanks sir,
It will help me a lot for my M.Tech project

Thanks,
Chanchal Suman

On Thu, Oct 5, 2017 at 12:53 AM, stevenhanjun notifications@github.com
wrote:

Here is the solution:
https://keras.io/getting-started/faq/#how-can-i-
visualize-the-output-of-an-intermediate-layer
or you can use this code:model = Model(input=[inputs],
output=[intermediate_layer])

Steven Han
Master, Xi'an Shannxi
Department of Computer Science
Xidian University
[email protected]

2017-10-04 0:40 GMT-04:00 CHANCHAL SUMAN notifications@github.com:

@stevenhanjun https://github.com/stevenhanjun Did you solved your
problem ?? I have the same querry, but i am not understanding by the FAQ
of
keras

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/2495#issuecomment-334047926,
or mute the thread
BvuefOIorD5u6L3Hvtuks5sowxAgaJpZM4IOtN_>
.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/2495#issuecomment-334262638,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AaIhYRJkHaEhGjfRsTYz5odwNAM0nbTJks5so9sygaJpZM4IOtN_
.

What if I don't want to use the predict, but the fit method to see the consequences of a dropout layer for example?

why does model['input'].assign(content_image)) not work?

Here is the solution: https://keras.io/getting-started/faq/#how-can-i-visualize-the-output-of-an-intermediate-layer or you can use this code:model = Model(input=[inputs], output=[intermediate_layer]) Steven Han Master, Xi'an Shannxi Department of Computer Science Xidian University [email protected] 2017-10-04 0:40 GMT-04:00 CHANCHAL SUMAN notifications@github.com:
…
@stevenhanjun https://github.com/stevenhanjun Did you solved your problem ?? I have the same querry, but i am not understanding by the FAQ of keras — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#2495 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AEP3K-1SVVhW_BvuefOIorD5u6L3Hvtuks5sowxAgaJpZM4IOtN_ .

Hi, I saw this link but I could not understand what is X and what should I put instead of it!? I am a beginner could you please help me with this.

Here is the solution: https://keras.io/getting-started/faq/#how-can-i-visualize-the-output-of-an-intermediate-layer or you can use this code:model = Model(input=[inputs], output=[intermediate_layer]) Steven Han Master, Xi'an Shannxi Department of Computer Science Xidian University [email protected] 2017-10-04 0:40 GMT-04:00 CHANCHAL SUMAN [email protected]:
…
@stevenhanjun https://github.com/stevenhanjun Did you solved your problem ?? I have the same querry, but i am not understanding by the FAQ of keras — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#2495 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AEP3K-1SVVhW_BvuefOIorD5u6L3Hvtuks5sowxAgaJpZM4IOtN_ .

Hi, I saw this link but I could not understand what is X and what should I put instead of it!? I am a beginner could you please help me with this.

Did you get the answer??

if you're using the functional API just make a new model = Model(input=[inputs], output=[intermediate_layer]), compile and predict

To more specifically,
inter_output_model = keras.Model(model.input, model.get_layer(index = 3).output ) assuming the intermedia layer is indexed at 3.
To use it,
inter_output = inter_output_model.predict(x_test) x_test is the data you feed into your model

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fredtcaroli picture fredtcaroli  Â·  3Comments

NancyZxll picture NancyZxll  Â·  3Comments

MarkVdBergh picture MarkVdBergh  Â·  3Comments

amityaffliction picture amityaffliction  Â·  3Comments

snakeztc picture snakeztc  Â·  3Comments