0

Going by the tutorial, this is an example of a simple 3 layer sequential neural network:

model = keras.Sequential(
    [
        layers.Dense(2, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(4, name="layer3"),
    ]
) 

Does this mean that the input layer is 2 neurons? Because I find that it changes if I give the model a different size vector. I would expect an error if I were fitting to any size vector other than 2, since the input layer only has 2 neurons.

John S
  • 71
  • 3

2 Answers2

0

You need to define the input shape in the first layer of your sequential model. Try this:

    from keras.models import Sequential
    from keras.layers import Dense, Activation
    model = Sequential([
                        Dense(32, input_shape=(784,)),
                        Activation('relu'),
                        Dense(10),
                        Activation('softmax'),
                        ])
Xpie
  • 43
  • 6
  • But the first layer being passed to Sequential has 32 neurons, not 784, so I'm quite confused. The first layer, which is the input layer, should have as many neurons as there are features (in this case, there are 784 features, probably representing the 784 pixels of an image)? – John S May 30 '21 at 04:16
  • 1
    @JohnS : You are right, but this is handled by Keras API. Actually it creates an input layer with your input, then the next layer (first layer you define in your model) has 32 neurons. – Kaveh May 30 '21 at 06:38
  • 1
    @JohnS For the sequential model, the first layer here is not the 'input layer', it is in fact the first layer with trainable parameters. The argument 'input_shape' creates a tensor-like object (i.e., a placeholder) for your input. Then it is fed to the first Dense layer of 32 units (i.e., the output size of this Dense layer would be 32) – Xpie May 31 '21 at 05:35
0

The first layer, which is the input layer, should have as many neurons as there are features (in this case, there are 784 features,

this is not correct , you can put just one neuron in the layer and it will work, but the number of its weights must be equal to the number of the features.

Tou You
  • 1,149
  • 8
  • 7