keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 913 forks source link

Input 0 is incompatible with layer k_max_pooling_12: expected ndim=3, found ndim=2 #111

Closed aviknayakusa closed 4 years ago

aviknayakusa commented 5 years ago

I am having an error i cant seem to comprehend how to solve. Basically i am trying to implement a 1Dconv model with KMAX pooling but when i am adding the KMAXpooling layer the model is throwing error

model codel:

model1 = Sequential()
model1.add(Conv1D(100, 1, activation='relu', input_shape=(1, 44)))
model1.add(Conv1D(100, 1, activation='relu'))
model1.add(MaxPooling1D(1))
model1.add(Conv1D(160, 1, activation='relu'))
model1.add(Dropout(0.5))
model1.add(KMaxPooling(1))
model1.add(GlobalAveragePooling1D())
model1.add(Dense(2, activation='softmax'))

Input data shape is x_train shape: (52400, 1, 44) x_test shape: (13101, 1, 44) Training samples: 52400 Test samples: 13101 (52400,) 54758 1 14943 1 33251 -1 43037 1 10054 1 Name: label, dtype: int64

class KMaxPooling(Layer):
    """
    K-max pooling layer that extracts the k-highest activations from a sequence (2nd dimension).
    TensorFlow backend.
    """
    def __init__(self, k=1, **kwargs):
        super().__init__(**kwargs)
        self.input_spec = InputSpec(ndim=3)
        self.k = k

    def compute_output_shape(self, input_shape):
        return (input_shape[0], (input_shape[2] * self.k))

    def call(self, inputs):

        # swap last two dimensions since top_k will be applied along the last dimension
        shifted_input = tf.transpose(inputs, [0, 2, 1])

        # extract top_k, returns two tensors [values, indices]
        top_k = tf.nn.top_k(shifted_input, k=self.k, sorted=True, name=None)[0]

        # return flattened output
        return Flatten()(top_k)

The above is the kMax Pooling code ..

taehoonlee commented 5 years ago

@aviknayakusa, In order to make the outputs 3-dim, you can revise the following two points:

  1. return (input_shape[0], input_shape[1], (input_shape[2] * self.k)) in compute_output_shape,
  2. return tf.transpose(top_k, [0, 2, 1]) in call.

And is the 1-length time-series (input_shape=(1, 44)) intended? The input_shape must be (timesteps, features) for time-series.

taehoonlee commented 4 years ago

@aviknayakusa, I'll close the issue for now. Please feel free to open it again at any time if you have additional comments.