Artificial Neural Networks (ANNS) in TensorFlow
Last Updated :
01 Mar, 2025
Artificial Neural Networks (ANNs) compose layers of nodes (neurons), where each node processes information and passes it to the next layer.
TensorFlow, an open-source machine learning framework developed by Google, provides a powerful environment for implementing and training ANNs.
Layers in Artificial Neural Networks
- Input Layer: Accepts the input features.
- Hidden Layers: Perform the computation and capture relationships between inputs.
- Output Layer: Provides the final prediction.
Backpropagation
During training, the model uses backpropagation to adjust the weights of the neurons based on the error between the predicted and actual outputs.

Advantages of Using TensorFlow for ANNs
- Scalability: TensorFlow can handle large datasets and run on multiple devices, including GPUs and TPUs.
- Flexibility: With TensorFlow, you can easily modify and experiment with different neural network architectures.
- Keras Integration: Keras provides an easy-to-use interface to build and train neural networks, making it accessible for both beginners and advanced users.
Building an Artificial Neural Network in TensorFlow
In TensorFlow, you can build an artificial neural network using its high-level API, Keras. Keras simplifies the process of designing and training ANNs by providing pre-built components like layers, loss functions, and optimizers.
Step 1: Importing Required Libraries
Before starting, you’ll need to install TensorFlow and import the necessary libraries:
Python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
Step 2: Preparing the Dataset
For our example, we’ll use the popular MNIST dataset, which contains images of handwritten digits. TensorFlow provides built-in support for this dataset.
Python
# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
# Normalize the images to values between 0 and 1
train_images = train_images / 255.0
test_images = test_images / 255.0
# Flatten the 28x28 images into 1D arrays of 784 pixels
train_images = train_images.reshape((train_images.shape[0], 28 * 28))
test_images = test_images.reshape((test_images.shape[0], 28 * 28))
Step 3: Building the Neural Network Model
We will create a simple ANN with three layers:
- An input layer of 784 neurons (one for each pixel in the 28×28 images).
- A hidden layer with 128 neurons.
- An output layer with 10 neurons (one for each digit from 0 to 9).
Python
model = Sequential([
Dense(128, activation='relu', input_shape=(28 * 28,)), # Hidden layer
Dense(10, activation='softmax') # Output layer
])
Step 4: Compiling the Model
Next, we need to compile the model by specifying the optimizer, loss function, and metrics.
- Adam: An optimization algorithm that adjusts the learning rate during training.
- Sparse Categorical Crossentropy: A loss function used for multi-class classification problems.
- Accuracy: The metric used to evaluate the performance of the model.
Python
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Step 5: Training the Model
Now, we can train the model using the training data:
Python
model.fit(train_images, train_labels, epochs=5)
This will train the model for 5 epochs, meaning the model will process the entire dataset 5 times.
Step 6: Evaluating the Model
Once the model is trained, we can evaluate its performance on the test data:
Python
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")
Complete Code
Python
# Import required libraries
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
# Normalize the images to values between 0 and 1
train_images = train_images / 255.0
test_images = test_images / 255.0
# Flatten the 28x28 images into 1D arrays of 784 pixels
train_images = train_images.reshape((train_images.shape[0], 28 * 28))
test_images = test_images.reshape((test_images.shape[0], 28 * 28))
# Build the neural network model
model = Sequential([
Dense(128, activation='relu', input_shape=(28 * 28,)), # Hidden layer
Dense(10, activation='softmax') # Output layer
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=5)
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
# Print test accuracy
print(f"Test accuracy: {test_acc}")
Output:

This will output the model’s accuracy on the test dataset.
Additional Techniques to Enhance Model Performance
1. Data Augmentation
Data augmentation is a technique used to artificially increase the size of a dataset by applying random transformations to the training data. This helps the model generalize better and prevents overfitting.
Python
# Example using ImageDataGenerator for image augmentation
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
'path_to_train_directory',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
2. Regularization Techniques
- Dropout: A technique where random neurons are turned off during training to prevent overfitting.
Python
from tensorflow.keras.layers import Dropout
model = Sequential([
Dense(128, activation='relu', input_shape=(28 * 28,)),
Dropout(0.5), # Apply dropout with 50% probability
Dense(10, activation='softmax')
])
- L2 Regularization: Adds a penalty to the loss function to reduce the magnitude of weights, helping prevent overfitting.
Python
from tensorflow.keras.regularizers import l2
model = Sequential([
Dense(128, activation='relu', input_shape=(28 * 28,), kernel_regularizer=l2(0.01)),
Dense(10, activation='softmax')
])
3. Saving and Loading the Model
Once the model is trained, you can save it for later use and load it when needed.
Python
# Save the model
model.save('my_model.h5')
loaded_model = tf.keras.models.load_model('my_model.h5')
Artificial Neural Networks are powerful tools for solving complex problems, and TensorFlow provides a flexible and efficient framework for implementing them. By using TensorFlow and Keras, you can quickly build and train ANNs for various machine learning tasks, from image recognition to natural language processing.
Similar Reads
Artificial Neural Network in TensorFlow
Artificial Neural Networks (ANNs) compose layers of nodes (neurons), where each node processes information and passes it to the next layer. TensorFlow, an open-source machine learning framework developed by Google, provides a powerful environment for implementing and training ANNs. Layers in Artific
5 min read
Convolutional Neural Network (CNN) in Tensorflow
Convolutional Neural Networks (CNNs) have revolutionized the field of computer vision by automatically learning spatial hierarchies of features from images. In this article we will explore the basic building blocks of CNNs and show you how to implement a CNN model using TensorFlow. Building Blocks o
5 min read
Classification of Neural Network in TensorFlow
Classification is used for feature categorization, and only allows one output response for every input pattern as opposed to permitting various faults to occur with a specific set of operating parameters. The category that has the greatest output value is chosen by the classification network. When i
10 min read
Layers in Artificial Neural Networks (ANN)
In Artificial Neural Networks (ANNs), data flows from the input layer to the output layer through one or more hidden layers. Each layer consists of neurons that receive input, process it, and pass the output to the next layer. The layers work together to extract features, transform data, and make pr
4 min read
Neural Network Layers in TensorFlow
TensorFlow provides powerful tools for building and training neural networks. Neural network layers process data and learn features to make accurate predictions. A neural network consists of multiple layers, each serving a specific purpose. These layers include: Input Layer: The entry point for data
2 min read
Building Artificial Neural Networks (ANN) from Scratch
Artificial Neural Networks (ANNs)are a collection of interconnected layers of neurons. Key components of an ANN include: Input Layer: Receives input features.Hidden Layers: Process information through weighted connections and activation functions.Output Layer: Produces the final prediction.Weights a
6 min read
Artificial Neural Networks and its Applications
As you read this article, which organ in your body is thinking about it? It's the brain, of course! But do you know how the brain works? Well, it has neurons or nerve cells that are the primary units of both the brain and the nervous system. These neurons receive sensory input from the outside world
9 min read
Introduction to Artificial Neural Networks | Set 1
Artificial Neural Networks (ANNs) are computational models inspired by the human brain. They are widely used for solving complex tasks such as pattern recognition, speech processing, and decision-making. By mimicking the interconnected structure of biological neurons, ANNs can learn patterns and mak
5 min read
Working of Convolutional Neural Network (CNN) in Tensorflow
In this article, we are going to see the working of convolution neural networks with TensorFlow a powerful machine learning library to create neural networks. Now to know, how a convolution neural network lets break it into parts. the 3 most important parts of this convolution neural networks are, C
5 min read
Introduction to Artificial Neural Network | Set 2
Artificial Neural Networks contain artificial neurons which are called units. These units are arranged in a series of layers that together constitute the whole Artificial Neural Network in a system. This article provides the outline for understanding the Artificial Neural Network. Characteristics of
3 min read