Input 0 of layer sequential_42 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, None, None, None)

train=pd.read_csv('/kaggle/input/stock-time-series-20050101-to-20171231/IBM_2006-01-01_to_2018-01-01.csv')

close_prices = train['Close']
values = close_prices.values

training_data_len = math.ceil(len(values)* 0.8)

scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(values.reshape(-1,1))
train_data = scaled_data[0: training_data_len, :]

x_train = []
y_train = []

for i in range(60, len(train_data)):
    x_train.append(train_data[i-60:i, 0])
    y_train.append(train_data[i, 0])
    
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))

generator=TimeseriesGenerator(x_train,x_train,
                              length=x_train.shape[1])

model = Sequential()
model.add(LSTM(200,activation='relu',recurrent_dropout=0.1,
               return_sequences=True, 
               input_shape=(x_train.shape[1], 1)))
model.add(LSTM(60,activation='relu',recurrent_dropout=0.1))
model.add(Flatten())
model.add(Dense(1))

model.compile(optimizer='adam',loss='mean_squared_error')

model.fit_generator(generator, epochs=5,steps_per_epoch=len(generator))

trying to make this model when I do the .fit_generator I get this error and I couldn’t figure out why.

@Luis_Valera,

Welcome to the Tensorflow Forum!

LSTM layer expects 3D input data with the shape (batch_size, timesteps, features) but it is receiving 4D input data ( i.e. x_train)

You can reshape the x_train data to match 3D input data before passing it to the model and then you can use this as input for your model.

generator=TimeseriesGenerator(x_train,x_train,
length=x_train.shape[1])

May i know why are you feeding x_train as both input and target for the generator?

Thank you!

1 Like