Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30)
Asked Answered
S

3

15

I have been working on a project for estimating the traffic flow using time series data combine with weather data. I am using a window of 30 values for my time series and I am using 20 weather related features. I have used the functional API to implement this, but I keep getting the same error and I do not know how it can be solved. I have looked at other similar threads such as this one Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 200], but it has not helped.

This is my model,

series_input = Input(shape = (series_input_train.shape[1], ), name = 'series_input')
x = Conv1D(filters=32, kernel_size=5, strides=1, padding="causal", activation="relu")(series_input)
x = LSTM(32, return_sequences = True)(x)
x = LSTM(32, return_sequences = True)(x)
x = Dense(1, activation = 'relu')(x)
series_output = Lambda(lambda w: w * 200)(x)

weather_input = Input(shape = (weather_input_train.shape[1], ), name = 'weather_input')
x = Dense(32, activation = 'relu')(weather_input)
x = Dense(32, activation = 'relu')(x)
weather_output = Dense(1, activation = 'relu')(x)

concatenate = concatenate([series_output, weather_output], axis=1, name = 'concatenate')

output = Dense(1, name = 'output')(concatenate)

model = Model([series_input, weather_input], output)

The shapes of series_input_train and weather_input_train are (34970, 30) and (34970, 20) respetively.

The error I keep getting is this one,

ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30)

What am I doing wrong?

Honestly, I have always had trouble figuring out how the shape of the inputs work in TensorFlow. If you could point me in the right direction, it would be appreciated but what I need right now is a fix for my model.

Sippet answered 20/3, 2021 at 4:48 Comment(1)
Did you solve it ?Argyle
E
4

As Tao-Lung says, the first connection for a convolutional layer expects a 3-position form. 1D convolution on sequences expects a 3D input. In other words, for each element of the batch, for each time step, a single vector. If you want the step to be unitary you solve your problem as follows:

series_input = Input(shape = (series_input_train.shape[1],1,)

and

x = Conv1D(filters=32, kernel_size=5, strides=1, padding="causal", \
    activation="relu",input_shape=[None,series_input])
Evocator answered 24/4, 2021 at 4:58 Comment(0)
G
0

Problem

3+D tensor with shape: batch_shape + (steps, input_dim) https://keras.io/api/layers/convolution_layers/convolution1d/

Solution

You don't specify "steps" parameters.
If "steps" is 1, Use the following code:

import numpy as np
series_input_train = np.expand_dims(series_input_train, axis=1)
weather_input_train = np.expand_dims(weather_input_train, axis=1)    
Gurkha answered 20/3, 2021 at 5:6 Comment(3)
What will I pass to the Shape parameters of my Input layers then? I get this error now, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30) with this layer, series_input = Input(shape = (series_input_train.shape[1], ), name = 'series_input')Sippet
In the above-mentioned website, the inputs are 128-length vectors with 10 timesteps, and the batch size is 4. input_shape = (4, 10, 128)Gurkha
My inputs are not vectors, they are simply a sequence of 30 values.Sippet
I
0

I've always had this problem. Check this link that is mentioned: https://keras.io/api/layers/convolution_layers/convolution1d/

the input shape of the Conv1D layer should be A 3D tensor with shape: (batch_shape, steps, channels) if it's channels_last and (channels, steps, batch_shape) if it's channels_first.

Interdictory answered 27/1 at 11:37 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewIritis

© 2022 - 2024 — McMap. All rights reserved.