0

The dimension seems to be the issue. Should the input be in numpy array?

    import tensorflow as tf
    import numpy as np

    X = np.array([-7.0, 3.0, 10.0, 20.0, 30.0])
    y = np.array([3.0, 13.0, 23.0, 33.0, 43.0])
    x_train = tf.constant(X)
    y_train = tf.constant(y)

    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 the model
    model.compile(loss=tf.keras.losses.mae, optimizer=tf.keras.optimizers.Adam(lr=0.001))

    # fit the model
    model.fit(x_train, y_train, epochs=5)

ValueError: Exception encountered when calling layer "sequential_2" (type Sequential). Input 0 of layer "dense_8" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)

    Epoch 1/5
    ------------------------------------------------------------------
    ValueError                                 Traceback (most recent call last)
    <ipython-input-19-fc6fe32aa384> in <module>()
    1 # fit the model
    ----> 2 model.fit(x_train, y_train, 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_2" (type Sequential).
    
    Input 0 of layer "dense_8" 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
Happy N. Monday
  • 371
  • 1
  • 3
  • 8

1 Answers1

0

I was able to reshape the dimension of the numpy array to the expected dimension of 2 using .reshape(-1,1)

X = np.array([-7.0, 3.0, 10.0, 20.0, 30.0]).reshape (-1,1)
y = np.array([3.0, 13.0, 23.0, 33.0, 43.0]).reshape(-1,1)

The function reshape(-1,1) reshapes the 1-dimensional array into a 2-dimensional array. I got the clue from this link What does -1 mean in numpy reshape? If you want to know more about how reshape works, please follow the link. This solved the problem. Thanks

Happy N. Monday
  • 371
  • 1
  • 3
  • 8