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

Keras · Class

Dense

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.

How it works

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.

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 dimensionality.
activationcallable or stringActivation function; omitted means linear activation.
use_biasboolWhether to use a bias vector.
kernel_initializerInitializer or stringInitializer for the kernel weights.
bias_initializerInitializer or stringInitializer for the bias vector.
kernel_regularizerRegularizerRegularizer for the kernel weights.
bias_regularizerRegularizerRegularizer for the bias.
activity_regularizerRegularizerRegularizer for the layer output.
kernel_constraintConstraintConstraint for the kernel weights.
bias_constraintConstraintConstraint for the bias.
lora_rankint or NoneRank for optional LoRA adaptation.
lora_alphaint or NoneScaling parameter for LoRA.

Returns

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

Key points

  • Dense is Keras's standard fully connected neural-network layer.
  • Its core operation is a matrix multiplication, optional bias addition, and element-wise activation.
  • units must be a positive integer and determines the output width.
  • Inputs may have any rank, with the feature dimension in the final axis.
  • The output retains all leading dimensions and replaces the final dimension with units.

Common mistakes

  • Leaving activation unset produces a linear transformation rather than a nonlinear one.
  • Setting lora_rank changes the forward pass to use LoRA, so it should be chosen intentionally when adapting an existing layer.

Example: Apply a Dense layer

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.

When to use it

Hidden layers

Build multilayer perceptrons and fully connected blocks.

Prediction heads

Convert learned features into class scores or probabilities.

Feature projection

Map a sequence or other structured tensor's final feature axis to a new width.

Interview questions

QWhat computation does Dense perform?

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.

QWhat do units and the input shape control?

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.

QWhich options customize Dense parameters and adaptation?

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.Dense
  • keras.layers.Conv1D
  • keras.layers.Conv2D
  • keras.layers.Conv3D

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