Why do you have to reshape 1D array to 2D in Denselayer

I am following a lecture where we get introduced how to instaintiate weights.

X_train = np.array([[1.0], [2.0]], dtype=np.float32) 
Y_train = np.array([[300.0], [500.0]], dtype=np.float32) 
linear_layer = tf.keras.layers.Dense(units=1, activation = 'linear', )
a1 = linear_layer(X_train[0].reshape(1,1))

I don’t understand why I have to reshape the input features for the Dense function with X_train[0].reshape(1,1) .

@Jakob,

The reshaping of the input features is necessary because the Dense layer expects a 2D tensor as input with shape (batch_size, input_dim).

By reshaping X_train[0] from (1,) to (1, 1), we convert it from a 1D array to a 2D array , allowing you to pass it as input without any shape mismatch errors.

Thank you!