I am using 2D data in a classification problem using keras.
So I am defining a keras model as following:
in_ = Input((5, 10))
out = Dense(100, activation='relu', name = 'dense_1')(in_)
model = Model(in_, out)
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
which returns a compiled model with the following parameters:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_9 (InputLayer) (None, 5, 10) 0
_________________________________________________________________
dense_1 (Dense) (None, 5, 100) 1100
=================================================================
Total params: 1,100
Trainable params: 1,100
Non-trainable params: 0
_________________________________________________________________
What I don't understand is why the dense_1 layer has only 1100 parameters and not 5100 parameters. What I was expecting is that the Dense Layer is going to connect to all the inputs 50 (5*10=50 inputs) giving a number of parameters of 5100 (100*50+100=5100, weights + biases). So apparently the Dense Layer only connects to the last dimension of the input? What happens in the other dimension?
If I flatten the input layer I obtain my expected number of parameters:
in_ = Input((5,10))
x = Flatten()(in_)
out = Dense(100, activation='relu', name = 'dense_1')(x)
model = Model(in_, out)
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_13 (InputLayer) (None, 5, 10) 0
_________________________________________________________________
flatten_6 (Flatten) (None, 50) 0
_________________________________________________________________
dense_1 (Dense) (None, 100) 5100
=================================================================
Total params: 5,100
Trainable params: 5,100
Non-trainable params: 0
_________________________________________________________________
So what is going on with a Dense Layer when the previous layer has more than one dimension? What happens with the dimensions and the dot products and biases? Why does the number of parameters changes?