0
votes

I have a large working model where I'd like to change the last dense layer to a convolutional layer (with pooling and output). But I get the following error when using a cnn:

ValueError: Input 0 of layer conv1d_21 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 768]

This is how the last layers looked before trying to use a cnn:

dense = model.get_layer('NSP-Dense').output
outputs = keras.layers.Dense(len(100), activation='sigmoid')(dense)

This is how it looks when using the CNN and where the error occures (in the second line):

dense = model.get_layer('NSP-Dense').output
conv = keras.layers.Conv1D(filters=32, kernel_size=8, activation='relu')(dense)
pooling = keras.layers.MaxPooling1D(pool_size=2)(conv)
flatten = keras.layers.Flatten()(pooling)
mid_dense = keras.layers.Dense(400, activation='relu' )(flatten)
outputs = keras.layers.Dense(len(test_y[0]), activation='sigmoid')(mid_dense)

I read questions like this and this, but mine seems to be another issue, because the CNN is not the first layer but in the middle of the network.

The NSP-Dense Layer has an output shape of (None, 768). I tried to set the shape in the conv-layer to input_shape =(None, 768)or input_shape =(None, 768, 1), but that did not resolve the issue. Does anyone have an idea?

1

1 Answers

1
votes

I put together this example based on your code which seems to avoid the error. This may get you pointed in the right direction. Please note the Reshape layer which essentially adds a dummy third dimension of size 1 to address the concern from the Conv1D layer. I hope this helps.

import tensorflow as tf

myInput = tf.keras.layers.Input(shape=(2,))
myOutput = tf.keras.layers.Dense(768,name='NSP-Dense')(myInput)
model = tf.keras.models.Model(myInput,myOutput)
model.summary()

newDense = model.get_layer('NSP-Dense').output
newReshape = tf.keras.layers.Reshape((768,1), name='newReshape')(newDense)
conv =  tf.keras.layers.Conv1D(filters=32, kernel_size=8, activation='relu', name='newModelConv1D' )(newReshape)
newModel = tf.keras.models.Model(myInput, conv)
newModel.summary()

import numpy as np
x_train = np.random.random((5,2))  # 5 samples
print('prediction:')
newModel.predict(x_train)