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

Keras · Class

layers.Dense

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
unitsintPositive integer specifying the output-space dimensionality.
activationstr or callable or NoneActivation function to apply; omitted means linear activation.
use_biasboolWhether to include a bias vector.
kernel_initializerInitializer or strInitializer for the kernel weights.
bias_initializerInitializer or strInitializer for the bias vector.
kernel_regularizerRegularizer or NoneRegularizer applied to the kernel weights.
bias_regularizerRegularizer or NoneRegularizer applied to the bias.
activity_regularizerRegularizer or NoneRegularizer applied to the layer output.
kernel_constraintConstraint or NoneConstraint applied to the kernel weights.
bias_constraintConstraint or NoneConstraint applied to the bias.
lora_rankint or NoneOptional LoRA rank; enables low-rank adaptation when set.
lora_alphaint or NoneOptional LoRA scaling parameter.

Returns

An N-dimensional tensor with shape (batch_size, ..., units).

Key points

  • The layer computes a dot product with its kernel, adds the optional bias, and applies the activation.
  • units must be a positive integer and determines the size of the output feature axis.
  • kernel_initializer controls how the kernel weights are initialized.
  • activity_regularizer applies regularization to the layer output rather than directly to its weights.
  • Dense preserves leading dimensions and replaces the final feature dimension with units.
  • lora_rank optionally enables low-rank adaptation in the layer's forward pass.

Common mistakes

  • Leaving activation unset produces a linear output rather than a nonlinear transformation.
  • A bias is created only when use_bias is enabled; disabling it removes that additive parameter.
  • Setting lora_rank changes the forward pass to use LoRA, so it should be chosen intentionally when adapting a pretrained layer.

Example: Dense layers in a functional model

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.

When to use it

Feature projection

Transforming feature vectors between representation sizes.

Classification heads

Producing class scores or probabilities from a learned representation.

Regression outputs

Mapping hidden features to a continuous target.

LoRA adaptation

Adding a trainable low-rank adaptation path to an existing dense transformation.

Interview questions

QWhat computation does Dense perform, and what shape does it return?

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.

QWhat does the 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.

QWhat happens when activation is not specified?

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.Layer
  • keras.layers.Dropout
  • keras.layers.Conv2D
  • keras.Model

Source: Dense layer - Keras. Checked 2026-10-15.