Expected min_ndim=2, found ndim=1. Full shape received: (None,)

Question:

In my model, I have a normalizing layer for a 1 column feature array. I assume this gives a 1 ndim output:

single_feature_model = keras.models.Sequential([
    single_feature_normalizer,
    layers.Dense(1)
])

Normailaztion step:

single_feature_normalizer = preprocessing.Normalization(axis=None)
single_feature_normalizer.adapt(single_feature)

The error I’m getting is:

ValueError                                Traceback (most recent call last)
<ipython-input-98-22191285d676> in <module>()
      2 single_feature_model = keras.models.Sequential([
      3     single_feature_normalizer,
----> 4     layers.Dense(1) # Linear Model
      5 ])

/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    225       ndim = x.shape.rank
    226       if ndim is not None and ndim < spec.min_ndim:
--> 227         raise ValueError(f'Input {input_index} of layer "{layer_name}" '
    228                          'is incompatible with the layer: '
    229                          f'expected min_ndim={spec.min_ndim}, '

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

I seems that the dense layer is looking for a 2 ndim array while the normalization layer outputs a 1 ndim array.
Is there anyway to solve this and getting the model working?

Asked By: sean harricharan

||

Answers:

I think you need to explicitly define an input layer with your input shape, since your output layer cannot infer the shape of the tensor coming from the normalization layer:

import tensorflow as tf

single_feature_normalizer = tf.keras.layers.Normalization(axis=None)
feature = tf.random.normal((314, 1))
single_feature_normalizer.adapt(feature)

single_feature_model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(1,)),
    single_feature_normalizer,
    tf.keras.layers.Dense(1)
])

Or define the input shape directly in the normalization layer without using an input layer:

single_feature_normalizer = tf.keras.layers.Normalization(input_shape=[1,], axis=None)
Answered By: AloneTogether

Got exactly the same error. I found that if you don’t explicitly define the input shape while constructing the model then by default the shape is expected to be of 2 dimension. This can be solved in two ways,

  1. Add an extra dimension to feature(input) tensor. Like this,
    input_tensor = tf.reshape(input_tensor,shape=(1,n))    # n is the number of samples, feature tensor have 
  1. Explicitly define the input shape while constructing the model.
Answered By: Zain Ul haq