AHMET HACI
AHMET HACI

Reputation: 45

ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

I want to apply LSTM. I have 12 features and 74 rows

my data shape after dropping the targeted variable and reshape it for 3d arrays:(1, 74, 12) and my targeted shape: (74,) when I split the data using this code:

x_train, x_test, y_train, y_test = train_test_split(data_1, target, test_size = 0.2,random_state =25)

I got this error:

ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

I defined the model well but when I fit the model also I have another error

defining the model:

model = Sequential()
model.add(LSTM(1, batch_input_shape=(1, 74, 12), return_sequences = True))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accurecy'])
model.summary()

fitting the model:

history = model.fit(x_train, y_train, epochs = 100, validation_data= (x_test, y_test))

here I have also this error:

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

How can I resolve this error?

Upvotes: 1

Views: 948

Answers (1)

user11530462
user11530462

Reputation:

tf.keras.layers.LSTM expects inputs: A 3D tensor with shape [batch, timesteps, feature].

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)

Output

(1, 74, 4)

If your input shape is of 2D, use tf.expand_dims(input, axis=0) to add extra dimension.

Upvotes: 1

Related Questions