LoK
LoK

Reputation: 99

ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None]

I am new to Tensorflow and trying to work on it using the sample code below:

def build_model():
    model = keras.Sequential([
            layers.Dense(10, activation=tf.nn.relu),
            layers.Dense(10, activation=tf.nn.relu),
            layers.Dense(1)
        ])
    optimizer = tf.keras.optimizers.RMSprop(0.001)

    model.compile(loss='mean_squared_error',
            optimizer=optimizer,
            metrics=['mean_absolute_error', 'mean_squared_error'])
    return model

model = build_model()
model.fit(training_dataset, epochs=5, steps_per_epoch=179)

The training_dataset is as below with 179 rows:

features:[29225 29259 29210 29220] Label:2
features:[29220 29236 29201 29234] Label:1
features:[29234 29241 29211 29221] Label:2
features:[29221 29224 29185 29185] Label:2
features:[29185 29199 29181 29191] Label:2
features:[29191 29195 29171 29195] Label:1
features:[29195 29228 29189 29225] Label:1
features:[29225 29236 29196 29199] Label:2
features:[29199 29222 29197 29218] Label:1
features:[29218 29235 29207 29224] Label:1
features:[29224 29244 29223 29234] Label:1
features:[29234 29247 29222 29240] Label:1
features:[29240 29264 29240 29263] Label:1
features:[29263 29267 29234 29237] Label:1
features:[29237 29270 29232 29267] Label:0
features:[29267 29270 29252 29253] Label:2

And I got the following error when running it:

"ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None]".

Can anyone advise on how to fix it?

Traceback (most recent call last):
  File "ML.py", line 145, in <module>
model.fit(training_dataset, epochs=5, steps_per_epoch=179)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 851, in fit
initial_epoch=initial_epoch)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_generator.py", line 191, in model_iteration
batch_outs = batch_function(*batch_data)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1175, in train_on_batch
x, y, sample_weight=sample_weight, class_weight=class_weight)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2289, in _standardize_user_data
self._set_inputs(cast_inputs)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/checkpointable/base.py", line 442, in _method_wrapper
method(self, *args, **kwargs)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2529, in _set_inputs
outputs = self.call(inputs, training=training)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py", line 233, in call
inputs, training=training, mask=mask)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py", line 254, in _call_and_compute_mask
layer._maybe_build(x)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1591, in _maybe_build
self.input_spec, inputs, self.name)
 File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 139, in assert_input_compatibility
str(x.shape.as_list()))
 ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None]

Upvotes: 5

Views: 8677

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36584

You most likely used tf.data.Dataset.from_tensor_slices() when you should have used tf.data.Dataset.from_tensors().

Or, you must put .batch(16) at the end of your tf.data.Dataset.

Upvotes: 8

Related Questions