Rodrigo Torbes
Rodrigo Torbes

Reputation: 23

ValueError: Input 0 of layer sequential_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]

I've made all the attempts I knew. Also all combinations of input_dim = 15 already. If anyone can help me?

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

(233941, 15)

(233941, 1)

(100261, 15)

(100261,)

I've already done the test using input_dim = (233941, 15) and input_dim = (233941, 1). But I still can't find the problem. Can my problem be in the dataset division?

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dense, Dropout, LSTM
model = Sequential()
model.add(LSTM(100, input_dim=15, return_sequences=True))
model.add(Dropout(0.3))

model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))
#3 camada
model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))

model.add(LSTM(units = 50))
model.add(Dropout(0.3))

model.add(Dense(1, activation='sigmoid'))

# Compile model
model.compile(optimizer = 'adam', loss = 'mean_squared_error',
                  metrics = ['mean_absolute_error'])
# Fit the model
model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))
Epoch 1/100
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-2e6c4d489e38> in <module>()
     21                   metrics = ['mean_absolute_error'])
     22 # Fit the model
---> 23 model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:180 assert_input_compatibility
        str(x.shape.as_list()))

    ValueError: Input 0 of layer sequential_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]

Upvotes: 2

Views: 271

Answers (1)

Yacine Mahdid
Yacine Mahdid

Reputation: 731

As Marco Certliani mentioned in the comments you need to format your input properly for a RNN, which as the error you got mentioned is 3 dimensional.

Here is a representation of what the input tensor should look like: enter image description here

Meaning that your 3D tensor will have a shape of (batch_size, time_step, input_dimension).

Upvotes: 1

Related Questions