Hidden layers
Use Dense with a nonlinear activation between feature-processing stages in a feed-forward or functional model.
TensorFlow · Class
A standard fully connected neural-network layer. It applies a learned affine projection to the final input dimension and then an optional activation, producing the requested number of output units.
Dense maps the final input dimension to the requested number of output units using a learned kernel and, by default, a learned bias. It computes the affine transformation, then applies the selected element-wise activation; if the input has rank greater than two, the transformation is applied independently across the earlier dimensions.
tf.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, **kwargs)| Name | Type | Description |
|---|---|---|
units | int | Positive integer specifying the output dimensionality. |
activation | callable or string | Activation function, or null for linear activation. |
use_bias | bool | Whether to include a bias vector. |
kernel_initializer | Initializer | Initializer for the kernel matrix. |
bias_initializer | Initializer | Initializer for the bias vector. |
kernel_regularizer | Regularizer | Regularizer for the kernel matrix. |
bias_regularizer | Regularizer | Regularizer for the bias vector. |
activity_regularizer | Regularizer | Regularizer for the layer output. |
kernel_constraint | Constraint | Constraint for the kernel matrix. |
bias_constraint | Constraint | Constraint for the bias vector. |
lora_rank | int or null | Optional rank for Low-Rank Adaptation. |
**kwargs | keyword arguments | Additional layer keyword arguments. |
A Keras layer whose call produces tensors with the configured output dimensionality; for an input shape (..., input_dim), the output shape is (..., units).
import tensorflow as tf
layer = tf.keras.layers.Dense(3, activation="relu")
x = tf.constant([[1.0, 2.0, 3.0, 4.0]])
y = layer(x)
print(y.shape) # (1, 3)Creates a three-unit dense layer, applies ReLU, and infers the input width when first called.
Use Dense with a nonlinear activation between feature-processing stages in a feed-forward or functional model.
Use a final Dense layer whose units match the prediction targets, selecting an activation appropriate to the task.
Set regularizers or constraints on kernel, bias, or activity to incorporate model-specific training controls.
The layer computes an activation applied to the input multiplied by a learned kernel, with an optional bias added first. The output width is units, and the activation is applied element-wise.
Follow-up: How does it handle higher-rank input? It contracts the last input axis with the kernel, preserving the preceding axes while replacing that final width with units.
units is the positive integer defining the output-space dimensionality. The kernel maps the final input dimension to units, while use_bias controls whether a bias vector is included.
Follow-up: What determines the kernel shape? After the layer is built, its first dimension is the input width and its second dimension is units.
LoRA can be enabled with lora_rank. It freezes the original kernel and represents the trainable update using two lower-rank matrices whose product supplies a kernel delta.
Follow-up: Why can this reduce adaptation cost? The trainable update uses lower-rank factors instead of updating the full kernel matrix.
tf.keras.layers.Layertf.keras.layers.EinsumDensetf.keras.layers.Conv1Dtf.keras.layers.Dropout