0

I am trying to create a cnn model.My code is as follows:

from keras.layers import Convolution1D, Dense, Dropout, Flatten, MaxPooling1D
from keras.layers import Input, Dense, concatenate
from keras.layers import InputLayer
import keras
inputs = Input(shape=(41,1))
cnn = Sequential()
X=cnn.add(Convolution1D(64, 3, border_mode="same",activation="relu")(inputs))
X=cnn.add(Convolution1D(128, 3, border_mode="same", activation="relu"))
X=cnn.add(MaxPooling1D(pool_length=(2)))
X=cnn.add(Convolution1D(256, 3, border_mode="same", activation="relu"))
X=cnn.add(MaxPooling1D(pool_length=(2)))
X=cnn.add(Flatten())
X=cnn.add(Dense(128, activation="relu"))
X=cnn.add(Dropout(0.5))
X=cnn.add(Dense(2, activation="sigmoid"))
cnn.compile(loss="binary_crossentropy", optimizer="adam",metrics=['accuracy'])

It works fine with following line of code X=cnn.add(Convolution1D(64,3,border_mode="same",activation="relu", input_shape=(41, 1))) But i need to extract layer outputs and i am doing it by using following lines of code:

from keras.models import Model
intermediate_layer_model = Model(inputs= inputs, outputs=X)
intermediate_output = intermediate_layer_model.predict(Xtrain)

So i need to pass inputs to my cnn layer that i cannot do so if i hard code my inputs_shape. But my above code is not working and giving following error:

Kindly tell me how i can solve this problem.

Ayesha Javed
  • 3
  • 1
  • 1
  • 6
  • TO be clear, do you want to predict using this above mentioned CNN model or do you just want to feed the data and then want to see how the output (image) of each layer looks like? – Rishabh Sahrawat Oct 11 '19 at 13:22
  • Exactly i want to see the output of each layer. Actually i want to extract features learned at respective layers. – Ayesha Javed Oct 11 '19 at 15:09

1 Answers1

0

You can get the output of each layer using the function ‘layer.output’ and here is the link for documentation in Tensorflow. This link will provide you how exactly you get the output from the layer on feeding data. I can not tell it exactly with your provided model structure because I am on the go right now and don’t have my pc with me too. But let us know if this helps.

Rishabh Sahrawat
  • 2,437
  • 1
  • 15
  • 32