K
K
k4nt2021-11-09 16:38:42
Python
k4nt, 2021-11-09 16:38:42

Keras. LSTM. Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. FAQ?

I have 2 csv files with a bunch of numbers, 3413 lines each.
The input data is 4606 in a row.
Weekend 20.

Trying to run them through the LSTM. And nothing comes out for me at the time of launch. Tried many things, but nothing worked. Maybe someone will tell from the height of experience where my mistake is and in what.

The code:

from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

path = r"bigData.csv"
tdt_x = pd.read_csv(path) # training data temp для x
tdt_x.head()

path = r"bigDataReturn.csv"
tdt_y = pd.read_csv(path) # training data temp для y
tdt_y.head()

sc = MinMaxScaler(feature_range = (0, 1))
tdt_xSC = sc.fit_transform(tdt_x)
tdt_ySC = sc.fit_transform(tdt_y)

tdt_xx = []
tdt_yy = []
tdt_xx.append(tdt_xSC)
tdt_yy.append(tdt_ySC)

training_data = np.array(tdt_xx)
correct_data = np.array(tdt_yy)

training_data = training_data.reshape(3413, 4606)
correct_data = correct_data.reshape(3413, 20)
print(training_data.shape[1])

test_fit_data = tf.data.Dataset.from_tensor_slices((training_data, correct_data))

regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (3413, 4606)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.25))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.25))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.25))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])
regressor.summary()
regressor.fit(test_fit_data, epochs = 100, batch_size = 32)

regressor.summary()


I am getting an error:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (4606, 1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antares4045, 2021-11-09
@antares4045

I note right away that I have never worked with tesorfolw, and my answer may turn out to be divorced from reality.
A free translation of the error that occurred: the input of the neural network was waiting for data in three dimensions, but it was submitted in two.
In my head I have five reasons why this could happen, but for any of them you just need to reshape the input data: from do [ ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question