Keras Dense Layer Input Shape Conundrum
This question explores an apparent contradiction between Keras documentation and the behavior of its Dense layer. The documentation states that the Dense layer flattens its input before applying the dot product with its kernel. However, as the provided code snippet demonstrates, the output shape of the Dense layer does not seem to be flattened.
Understanding the Behavior
The key to resolving this discrepancy lies in understanding how the Dense layer is applied in Keras. Contrary to the documentation, the Dense layer actually operates on the last axis of the input tensor. Therefore, in the example code snippet, the Dense layer is applied to each column of the (2,3) input tensor, resulting in an output shape of (2, 4).
Implications and Side Notes
This behavior has significant implications:
Visual Illustration
The following visual illustration clarifies the behavior of the Dense layer:
[Image of a tensor with (2,3) shape and a Dense layer with 4 units applied to the last axis]
Each unit in the Dense layer is connected to every element in a column of the input tensor with the same set of weights. The result is an output tensor with a shape of (2, 4).
The above is the detailed content of Why Does the Keras Dense Layer Preserve Dimensionality?. For more information, please follow other related articles on the PHP Chinese website!