Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

layers.Dense

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
unitsintPositive integer specifying the output dimensionality.
activationcallable or stringActivation function, or null for linear activation.
use_biasboolWhether to include a bias vector.
kernel_initializerInitializerInitializer for the kernel matrix.
bias_initializerInitializerInitializer for the bias vector.
kernel_regularizerRegularizerRegularizer for the kernel matrix.
bias_regularizerRegularizerRegularizer for the bias vector.
activity_regularizerRegularizerRegularizer for the layer output.
kernel_constraintConstraintConstraint for the kernel matrix.
bias_constraintConstraintConstraint for the bias vector.
lora_rankint or nullOptional rank for Low-Rank Adaptation.
**kwargskeyword argumentsAdditional layer keyword arguments.

Returns

A Keras layer whose call produces tensors with the configured output dimensionality; for an input shape (..., input_dim), the output shape is (..., units).

Key points

  • units sets the number of values produced along the output feature axis.
  • The core operation is an affine projection followed by the configured activation.
  • Activation is applied separately to each resulting output value.
  • kernel_initializer controls how the learned weight matrix is initialized.
  • activity_regularizer applies regularization to the layer output.

Common mistakes

  • Leaving activation unset produces a linear output rather than a nonlinear transformation.
  • A bias is not part of the computation when use_bias is false.
  • Setting lora_rank changes the forward pass to LoRA and makes the original kernel non-trainable.

Example: Apply a dense layer

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.

When to use it

Hidden layers

Use Dense with a nonlinear activation between feature-processing stages in a feed-forward or functional model.

Prediction heads

Use a final Dense layer whose units match the prediction targets, selecting an activation appropriate to the task.

Controlled parameterization

Set regularizers or constraints on kernel, bias, or activity to incorporate model-specific training controls.

Interview questions

QWhat computation does Dense perform?

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.

QWhat does units control, and how is the input width used?

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.

QWhat does lora_rank do?

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.Layer
  • tf.keras.layers.EinsumDense
  • tf.keras.layers.Conv1D
  • tf.keras.layers.Dropout

Source: tf.keras.layers.Dense | TensorFlow v2.16.1. Checked 2026-10-15.