1

I am trying to create a neural network model with one hidden layer and then trying to evaluate it, but I am getting an error that I am not able to understand clearly:

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

It looks like I have an error with the dimensions of my input layer, but I can't quite spot what. I've googled and looked on stackoverflow, but haven't found anything that worked so far. Any help please?

Here's a minimal working example:

import tensorflow as tf

# Define Sequential model with 3 layers
input_dim = 30
num_neurons = 10
output_dim = 12
model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(input_dim, activation="relu", name="layer1"),
        tf.keras.layers.Dense(num_neurons, activation="relu", name="layer2"),
        tf.keras.layers.Dense(output_dim, name="layer3"),
    ]
)
model(tf.ones(input_dim))

1 Answer 1

2

Layers have an input and output dimension. For layers in the "middle" of the NN, they figure out their input domain from the output domain of the previous layer. The only exception is the first layer that has nothing to go by, and requires input_dim to be set. Here is how to fix your code. Note how we pass the dimensions. First (hidden) layer is input_dim x num_neurons, second (output layer) num_neurons x output_dim

You can stick more layers in between the two; they only require the first argument, their output dimension

also note I had to fix your last line as well, tf.ones needs to be a 2D shape num_observation x input_dim

import tensorflow as tf

# Define Sequential model with 1 hidden layer
input_dim = 30
num_neurons = 10
output_dim = 12
model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(num_neurons, input_dim = input_dim, activation="relu", name="layer1"),
        tf.keras.layers.Dense(output_dim, name="layer3"),
    ]
)
model(tf.ones((1,input_dim)))

produces (for me; I think the numbers are essentially random initialization)

<tf.Tensor: shape=(1, 12), dtype=float32, numpy=
array([[ 0.06973769, -0.1798143 , -0.2920275 ,  0.84811246,  0.44899416,
        -0.10300556,  0.00831143, -0.16158538,  0.13395026,  0.4352504 ,
         0.19114715,  0.44100884]], dtype=float32)>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I see that there were more than one problems with my code, thank you for helping to fix them and for the clear explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.