'ran into TensorFlow ValueError during my TensorFlow Course with Udemy

I have being trying to fit the error during my Tensorflow course (Section 3: Neural network Regression with Tensorflow) with Udemy.

import tensorflow as tf
import numpy as np

X = np.array([-8.0, -5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0])
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])

#convert numpy array into tensors
X = tf.constant(X)
y = tf.constant(y)

X.ndim, y.ndim

#create a model

model = tf.keras.Sequential([
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(1, activation = None)])

#compile a model

model.compile(loss = tf.keras.losses.mae, optimizer = tf.keras.optimizers.SGD(lr=0.001), metrics = ["mae"])

#Fitting a model

model.fit(X, y, epochs=5)

and I finally got this error

 Epoch 1/5
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call last)
 <ipython-input-29-2898fc83e7db> in <module>()
 #Fitting a model
 ----> 2 model.fit(X, y, epochs=5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
  File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" '

    ValueError: Exception encountered when calling layer "sequential_3" (type Sequential).
    
    Input 0 of layer "dense_20" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(None,), dtype=float64)
      • training=True
      • mask=None

Meanwhile, I am using tensorflow version 2.8. Please, I would really appreciate your help. Thanks



Solution 1:[1]

As suggested by @AloneTogether and @Mohan Radhakrishnan, both methods will resolve your issue.

expand_dims will not add or reduce elements in a tensor, it just changes the shape by adding 1 to dimensions. For more details please refer here

For the benefit of the community, provide complete code to use expand_dims without modify the model structure as shown below

import tensorflow as tf
print(tf.__version__)
import numpy as np

X = np.array([-8.0, -5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0])
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])

#convert numpy array into tensors
X = tf.constant(X)
y = tf.constant(y)

X.ndim, y.ndim

# add new dimension 
X = tf.expand_dims(X, axis=-1)

model = tf.keras.Sequential([
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(100, activation = "relu"),
tf.keras.layers.Dense(1, activation = None)])

model.compile(loss = tf.keras.losses.mae, optimizer = tf.keras.optimizers.SGD(learning_rate=0.001), metrics = ["mae"])

model.fit(X, y, epochs=5)

Output:

2.8.0
(1, 1)

Epoch 1/5
1/1 [==============================] - 0s 416ms/step - loss: 13.7571 - mae: 13.7571
Epoch 2/5
1/1 [==============================] - 0s 10ms/step - loss: 13.7240 - mae: 13.7240
Epoch 3/5
1/1 [==============================] - 0s 10ms/step - loss: 13.6911 - mae: 13.6911
Epoch 4/5
1/1 [==============================] - 0s 8ms/step - loss: 13.6582 - mae: 13.6582
Epoch 5/5
1/1 [==============================] - 0s 9ms/step - loss: 13.6257 - mae: 13.6257
<keras.callbacks.History at 0x7fdc6a379750>

Please find the complete code for another method in the gist for reference.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer2