Hidden layers
Build multilayer perceptrons and fully connected blocks.
Keras · Class
Dense is Keras's standard fully connected layer. It transforms the final input axis into a chosen number of outputs using learned weights, an optional bias, and an optional activation.
Dense maps the final dimension of each input to the requested number of output units by multiplying by a learned kernel, optionally adding a learned bias, and applying the selected activation. It also supports configurable initialization, regularization, constraints, and optional LoRA adaptation.
keras.layers.Dense(units, activation=None, use_bias=True, kernel_initializer="glorot_uniform", bias_initializer="zeros", kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, lora_rank=None, lora_alpha=None, quantization_config=None, **kwargs)| Name | Type | Description |
|---|---|---|
units | int | Positive integer specifying the output dimensionality. |
activation | callable or string | Activation function; omitted means linear activation. |
use_bias | bool | Whether to use a bias vector. |
kernel_initializer | Initializer or string | Initializer for the kernel weights. |
bias_initializer | Initializer or string | Initializer for the bias vector. |
kernel_regularizer | Regularizer | Regularizer for the kernel weights. |
bias_regularizer | Regularizer | Regularizer for the bias. |
activity_regularizer | Regularizer | Regularizer for the layer output. |
kernel_constraint | Constraint | Constraint for the kernel weights. |
bias_constraint | Constraint | Constraint for the bias. |
lora_rank | int or None | Rank for optional LoRA adaptation. |
lora_alpha | int or None | Scaling parameter for LoRA. |
An N-D tensor with shape (batch_size, ..., units).
import keras
from keras import layers
x = keras.random.uniform((8, 20))
layer = layers.Dense(10, activation="relu")
y = layer(x)
print(y.shape) # (8, 10)Creates a Dense layer that maps each 20-feature input vector to 10 outputs with ReLU activation.
Build multilayer perceptrons and fully connected blocks.
Convert learned features into class scores or probabilities.
Map a sequence or other structured tensor's final feature axis to a new width.
Dense computes an affine transformation followed by an optional element-wise activation: output = activation(dot(input, kernel) + bias). The kernel and, when enabled, bias are learned layer weights.
Follow-up: How does the output shape change for a rank-3 input? The last dimension becomes units while the preceding dimensions, including batch and other axes, are preserved.
units is the positive integer defining the output dimensionality. For an input shaped (batch_size, ..., input_dim), Dense returns (batch_size, ..., units).
Follow-up: What determines input_dim? It is the size of the input tensor's final dimension, which is used to shape the kernel.
use_bias controls whether a bias vector participates in the transformation. Initializers, regularizers, and constraints can independently configure the kernel and bias, while activity_regularizer applies to the layer output.
Follow-up: How can low-rank adaptation be enabled? Set lora_rank to an integer; lora_alpha is also available to configure the LoRA behavior.
keras.layers.Densekeras.layers.Conv1Dkeras.layers.Conv2Dkeras.layers.Conv3D