Open In App

Artificial Neural Networks (ANNS) in TensorFlow

Last Updated : 01 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. Scalability: TensorFlow can handle large datasets and run on multiple devices, including GPUs and TPUs.
  2. Flexibility: With TensorFlow, you can easily modify and experiment with different neural network architectures.
  3. 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:

training-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