Reputation: 45
I am trying to train a CNN and LSTM network with S&P 500 dataset. This is the shape of my train dataset:
xtrain shape: (6445, 16) ytrain shape: (6445,)
this is the input shape I have gave to CNN:
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None,16)))
withe input shape parameter shown in the code I get this error:
ValueError: Input 0 of layer conv1d_8 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 16]
Upvotes: 2
Views: 675
Reputation: 303
expected min_ndim=3, found ndim=2.
Keras expects three-dimensional arrays when working with Conv1D: the expected shape is [batch_size, sequence_length, feature_dimension]
. In your case, you only have one feature dimension, I suspect the price, but imagine you also wanted to pass the trading volumes data, you would have xtrain.shape == (6445,16,2)
. The last dimension would contain information about the price and the volume.
To solve your issue, you need to reshape your xtrain
to
(batch_size, sequence_length, feature_dimension=(6445,16,1)
To do so, you can use tensorflow:
xtrain = tf.expand_dims(xtrain, axis=-1) # -1 means expand the LAST axis
or with numpy:
xtrain = np.expand_dims(xtrain, axis=-1) # -1 means expand the LAST axis
This function just does what the name implies: it adds a new axis in the position specified by axis
. This results in xtrain
having the shape we wanted, now you can just proceed with your model, for example:
model = keras.models.Sequential()
model.add(keras.layers.TimeDistributed(keras.layers.Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None,16,1)))
Upvotes: 2