keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.93k stars 19.46k forks source link

How can I get the output of an intermediate layer? #2495

Closed stevenhanjun closed 7 years ago

stevenhanjun commented 8 years ago

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

lemuriandezapada commented 8 years ago

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

joelthchao commented 8 years ago

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

chanchalIITP commented 7 years ago

@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

stevenhanjun commented 7 years ago

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 stevenhanjun@gmail.com

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_ .

chanchalIITP commented 7 years ago

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 stevenhanjun@gmail.com

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_ .

— 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_ .

Nimi42 commented 6 years ago

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

nitinsethi22 commented 6 years ago

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

MJamali89 commented 5 years ago

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 stevenhanjun@gmail.com 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.

Lak6 commented 4 years ago

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 stevenhanjun@gmail.com 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.

Did you get the answer??

inspirepassion commented 4 years ago

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