Issue
Please can you help me understand the error I am getting from the model I am trying to build?
I have a training, validation and test set. The training data has the following shape:
input_shape = train.shape[1:] #(1500,)
I have written the following model using Keras:
input = Input(shape=(input_shape))
# Conv1D + global max pooling
x = layers.Conv1D(filters=32, padding="valid", activation="relu", strides=1, kernel_size=4)(input)
x = layers.Conv1D(filters=32, padding="valid", activation="relu", strides=1, kernel_size=4)(x)
x = layers.GlobalMaxPooling1D()(x)
x = layers.Dense(128, activation="relu")(x)
x = layers.Dropout(0.5)(x)
predictions = layers.Dense(1,kernel_initializer='normal', name="predictions")(x)
model = tf.keras.Model(input, predictions)
model.compile(loss="mean squared error", optimizer="adam", metrics=[concordance_index])
I get the following error:
ValueError Traceback (most recent call last)
<ipython-input-60-59c3578104d3> in <module>()
6
7 # Conv1D + global max pooling
----> 8 x = layers.Conv1D(filters=32, padding="valid", activation="relu", strides=1, kernel_size=4)(protein_input)
9 x = layers.Conv1D(filters=32, padding="valid", activation="relu", strides=1, kernel_size=4)(x)
10 x = layers.GlobalMaxPooling1D()(x)
5 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
230 ', found ndim=' + str(ndim) +
231 '. Full shape received: ' +
--> 232 str(tuple(shape)))
233 # Check dtype.
234 if spec.dtype is not None:
ValueError: Input 0 of layer conv1d_49 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 1500)
Is my Input layer incorrect? Or is it due to the ordering of Conv1d, MaxPooling layers?
Solution
Since a Conv1D layer expects input as batch_shape + (steps, input_dim)
, you need to add a new dimension. So:
X = tf.expand_dims(X,axis=2)
print(X.shape) # X.shape=(Samples, 1500, 1)
Then, you have X shape as (Samples,150,1)
Now, let's specify the input shape as:
input = Input(shape=(X.shape[1:])
Answered By - Kaveh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.