Dense Layer (tf.keras.layers.Dense) in TensorFlow (original) (raw)

Last Updated : 23 Jul, 2025

In TensorFlow, the **tf.keras.layers.Dense layer represents a fully connected (or dense) layer, where every neuron in the layer is connected to every neuron in the previous layer. This layer is essential for building deep learning models, as it is used to learn complex patterns and relationships in data.

The Dense layer applies the following mathematical operation:

\text{output} = \text{activation} (\text{dot}(\text{input}, \text{kernel}) + \text{bias})

where:

This means every neuron receives a weighted sum of all the inputs, applies an activation function, and then passes the result to the next layer.

**Syntax of tf.keras.layers.Dense:

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
)

**Parameters Explained:

How to Use tf.keras.layers.Dense?

The tf.keras.layers.Dense layer is typically used in sequential or functional API models in TensorFlow. Below are examples of how to use it:

Example 1: Using Dense Layer in a Sequential Model

Python `

from tensorflow import keras

Define a simple neural network model

model = keras.Sequential([ keras.layers.Dense(64, activation='relu', input_shape=(20,)), # First hidden layer keras.layers.Dense(32, activation='relu'), # Second hidden layer keras.layers.Dense(10, activation='softmax') # Output layer with 10 classes ])

Compile the model

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Model Summary

model.summary()

`

**Output:

Model-summary

Example 2: Using Dense Layer in the Functional API

Python `

import tensorflow as tf from tensorflow.keras import Input, Model from tensorflow.keras.layers import Dense

Define input

inputs = Input(shape=(100,))

Define layers

x = Dense(64, activation='relu')(inputs) x = Dense(32, activation='relu')(x) outputs = Dense(10, activation='softmax')(x)

Create model

model = Model(inputs, outputs)

Summary of the model

model.summary()

`

**Output:

Capture