ryh12
ryh12

Reputation: 367

Getting the output of layer as a feature vector (KERAS)

I have a CNN model in keras (used for signal classification):

    cnn = Sequential()

    cnn.add(Conv1D(10,kernel_size=8,strides=4, padding="same",activation="relu",input_shape=(Dimension_of_input,1)))
    cnn.add(MaxPooling1D(pool_size=3))

    cnn.add(Conv1D(10,kernel_size=8,strides=4, padding="same",activation="relu"))
    cnn.add(MaxPooling1D(2))
    cnn.add(Flatten())

    cnn.add(Dense(2, activation="softmax"))

Using the method 'model.summary()', I can get the shape of the output of each layer. In my model, the output of the last max pooling layer is (None, 1, 30) and of flatten layer is (None, 30).

For each train and test sample: Is it possible in keras to get the output of the flatten layer as a feature vector with the 30 features (numbers), before it is given as input to the dense layer??

Upvotes: 4

Views: 2987

Answers (1)

Mo-
Mo-

Reputation: 850

Select the last layer by:

last = cnn.layers[-1]

then create a new model using:

inp = Input(shape=(Dimension_of_input,))
features = Model(inp, last)

So,

feature_vec = features.predict(x_train)

give you the output of the flatten layer as a feature vector for each train sample

Upvotes: 3

Related Questions