Using MaxPool1D on inputs with 4 dimensions (4D tensor)?

Question:

I am trying to create a NN architecture for multiple instance learning, so the instances are actually bags of of time series segments. I would like to do COnv1D and MaxPool1D over the features (last dimension), I specify input as having 4 dimensions and that works fine for Conv1D but throws an error with MaxPool1D:

n = 6
sample_size = 300
code_size = 50
learning_rate = 0.001
bag_size = None

# autoencoder: n_bags X bag_size X n_samples (timesteps) X n_measurements
input_window = Input(shape=(bag_size,sample_size, n)) 
x = Conv1D(filters=40, kernel_size=21, activation='relu', padding='valid')(input_window)
x = MaxPooling1D(pool_size=2)(x)

The error is:

ValueError: Input 0 of layer max_pooling1d_4 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, None, 280, 40]

According to TF documentation
MaxPool1D only works on 3D tensors.
Is there a work-around?

Asked By: DAF

||

Answers:

Though it’s not clear on which axis you are trying to pool over, you could use the MaxPooling2D with the right pooling size, which IIUC in this case would be (1,2)

from tensorflow.keras import layers, Model

n = 6
sample_size = 300
code_size = 50
learning_rate = 0.001
n_bags= None

# autoencoder: n_bags X n_instances_in_bag X n_samples (timesteps) X n_measurements
input_window = layers.Input(shape=(n_bags,sample_size, n)) 
x = layers.Conv1D(filters=40, kernel_size=21, activation='relu', padding='valid')(input_window)

x = layers.MaxPooling2D(pool_size=(1,2))(x)

model = Model(input_window, x)

model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         [(None, None, 300, 6)]    0         
_________________________________________________________________
conv1d_3 (Conv1D)            (None, None, 280, 40)     5080      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, None, 140, 40)     0         
=================================================================
Total params: 5,080
Trainable params: 5,080
Non-trainable params: 0
_________________________________________________________________
Answered By: Akshay Sehgal