简体   繁体   中英

ValueError - Working with odd timesteps in input of convolutional autoencoder using keras

I am using 1d convolutional autoencoder in Keras for text processing. My input is in shape(?, 13, 22). I am adding a zeropadding1d layer before the maxpooling1d layer. In the maxpooling1d layer, i am using pool_size = 2 so that's why i add a zeropadding1d to make the input (?, 14, 22). I am getting "ValueError: Error when checking target: expected conv1d_4 to have shape (14, 22) but got array with shape (13, 22)". I am using padding='same' in the 4th conv1d layer, so it should take into account the zeropadding which i added to the input before the maxpooling1d layer (accoridng to my understanding). I am not sure what i am doing wrong.

    input_size = features.shape[1:]   //input_size (13, 22)

    # input layer
    input_layer = ks.layers.Input(shape=(*input_size,), name='input')  //shape=(?, 13, 22)

    # noise
    x = ks.layers.GaussianNoise(stddev=0.1)(input_layer)   //shape=(?, 13, 22)

    # conv layer
    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)     //shape=(?, 13, 8)
    x = ks.layers.ZeroPadding1D(padding=(1, 0))(x)     //shape=(?, 14, 8)
    x = ks.layers.MaxPool1D(pool_size=2, strides=None)(x)     //shape=(?, 7, 8)
    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)     //shape=(?, 7, 8)
    x = ks.layers.UpSampling1D(size=2)(x)    //shape=(?, 14, 8)
    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)    //shape=(?, 14, 8)
    x = ks.layers.Conv1D(filters=input_size[-1], kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)    //ValueError: Error when checking target: expected conv1d_4 to have shape (14, 22) but got array with shape (13, 22)

    # output
    output_layer = x

Here is the summary of my model.

    Layer (type)                 Output Shape              Param #   
    =================================================================
    input (InputLayer)           (None, 13, 22)            0         
    _________________________________________________________________
    gaussian_noise_1 (GaussianNo (None, 13, 22)            0         
    _________________________________________________________________
    conv1d_1 (Conv1D)            (None, 13, 8)             536       
    _________________________________________________________________
    zero_padding1d_1 (ZeroPaddin (None, 14, 8)             0         
    _________________________________________________________________
    max_pooling1d_1 (MaxPooling1 (None, 7, 8)              0         
    _________________________________________________________________
    conv1d_2 (Conv1D)            (None, 7, 8)              200       
    ValueError: Error when checking target: expected conv1d_4 to have shape 
    (14, 22) but got array with shape (13, 22)
    _________________________________________________________________
    up_sampling1d_1 (UpSampling1 (None, 14, 8)             0         
    _________________________________________________________________
    conv1d_3 (Conv1D)            (None, 14, 8)             200       
    _________________________________________________________________
    conv1d_4 (Conv1D)            (None, 14, 22)            550       
    =================================================================
    Total params: 1,486
    Trainable params: 1,486
    Non-trainable params: 0
    _________________________________________________________________
    None

I found a solution to my problem. Instead of adding a zeropadding layer before the maxpooling layer, I changed the dimension of my input by adding zero padding to the input. My code looks like this now,

def fit(self, dataset):
    features = self.load_data(dataset)  //features.shape (12500, 13, 22)

    npad = ((0, 0), (1, 0), (0, 0))     //
    features = np.pad(features, pad_width=npad, mode='constant', constant_values=0)  //features. shape (12500, 14, 22)

    import keras as ks

    input_size = features.shape[1:]

    # input layer
    input_layer = ks.layers.Input(shape=(*input_size,), name='input')
    # noise
    x = ks.layers.GaussianNoise(stddev=0.1)(input_layer)

    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)
    # downscale the spatial dimensions - downscaling factor: halve the input in width
    x = ks.layers.MaxPool1D(pool_size=2, strides=None)(x)

    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)

    # upsample to higher dimensional space
    x = ks.layers.UpSampling1D(size=2)(x)
    x = ks.layers.Conv1D(filters=8, kernel_size=3, strides=1, activation=ks.activations.relu, padding='same')(x)

    # decode the encoded data
    x = ks.layers.Conv1D(filters=input_size[-1], kernel_size=3, strides=1, activation=ks.activations.softmax, padding='same')(x)

    # output
    output_layer = x

    # build model
    self.model = ks.models.Model(inputs=input_layer, outputs=output_layer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM