Feature projection
Transforming feature vectors between representation sizes.
Keras · Class
Dense is a fully connected Keras layer that maps each input feature vector to a chosen number of output units. It combines a learned kernel and optional bias, then applies an optional activation.
Dense maps the last input dimension to the requested number of output units using a learned kernel and, by default, a learned bias. It computes the affine transformation and then applies the selected element-wise activation, while preserving any leading dimensions.
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-space dimensionality. |
activation | str or callable or None | Activation function to apply; omitted means linear activation. |
use_bias | bool | Whether to include a bias vector. |
kernel_initializer | Initializer or str | Initializer for the kernel weights. |
bias_initializer | Initializer or str | Initializer for the bias vector. |
kernel_regularizer | Regularizer or None | Regularizer applied to the kernel weights. |
bias_regularizer | Regularizer or None | Regularizer applied to the bias. |
activity_regularizer | Regularizer or None | Regularizer applied to the layer output. |
kernel_constraint | Constraint or None | Constraint applied to the kernel weights. |
bias_constraint | Constraint or None | Constraint applied to the bias. |
lora_rank | int or None | Optional LoRA rank; enables low-rank adaptation when set. |
lora_alpha | int or None | Optional LoRA scaling parameter. |
An N-dimensional tensor with shape (batch_size, ..., units).
import keras
from keras import layers
inputs = keras.Input(shape=(20,))
x = layers.Dense(64, activation="relu")(inputs)
outputs = layers.Dense(10, activation="softmax")(x)
model = keras.Model(inputs, outputs)
predictions = model(keras.random.uniform((2, 20)))
print(predictions.shape)Builds a classifier with a 64-unit hidden layer and a 10-unit probability output layer.
Transforming feature vectors between representation sizes.
Producing class scores or probabilities from a learned representation.
Mapping hidden features to a continuous target.
Adding a trainable low-rank adaptation path to an existing dense transformation.
Dense is an affine transformation followed by an optional element-wise activation: the kernel multiplies the input, the bias is added when enabled, and the activation is applied afterward. Its output shape is (batch_size, ..., units).
Follow-up: How does Dense handle higher-rank input? It applies the transformation along the final input axis while retaining the preceding dimensions.
units argument control?units is a positive integer that specifies the output-space dimensionality. The layer therefore creates an output whose final dimension equals units.
Follow-up: What determines the kernel shape? Once the layer sees the input, the kernel maps the input's final dimension to units.
The default activation is linear because no activation is applied when activation is omitted. You can pass an activation function or its supported identifier to add a nonlinear element-wise operation.
Follow-up: When might a linear activation be useful? It is commonly appropriate for an unconstrained regression output, whereas classification outputs often use a task-appropriate activation.
keras.layers.Layerkeras.layers.Dropoutkeras.layers.Conv2Dkeras.Model