Machine Learning
&
Neural Networks Blog

Supervised Learning in Artificial Neural Networks

Calin Sandu | Published on January 10, 2024

Artificial Neural Networks (ANNs) are a fundamental component of machine learning, mimicking the human brain's structure and functioning. Within ANNs, one of the key paradigms is supervised learning, a powerful approach that enables the network to learn from labeled training data. In this article, we'll delve into the concept of supervised learning within artificial neural networks and explore its applications through illustrative code snippets.

Understanding Supervised Learning
Supervised learning is a type of machine learning where the model is trained on a labeled dataset, meaning that the input data is paired with corresponding output labels. The goal is for the model to learn the mapping from input to output, making predictions or classifications when presented with new, unseen data.
In the context of artificial neural networks, supervised learning involves adjusting the weights and biases of the network to minimize the difference between predicted and actual outputs. This process, known as training, allows the network to generalize patterns from the training data to make accurate predictions on new inputs.

Implementation in Python using TensorFlow
Let's dive into a practical example of implementing a simple supervised learning task using TensorFlow, a popular machine learning library. In this example, we'll create a basic neural network to classify handwritten digits from the MNIST dataset.


 # Importing necessary libraries
 import tensorflow as tf
 from tensorflow.keras import layers, models
 from tensorflow.keras.datasets import mnist
 from tensorflow.keras.utils import to_categorical
                                
 # Load and preprocess the MNIST dataset
 (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
 test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255
 train_labels = to_categorical(train_labels)
 test_labels = to_categorical(test_labels)
                                
 # Build the neural network model
 model = models.Sequential()
 model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(layers.MaxPooling2D((2, 2)))
 model.add(layers.Conv2D(64, (3, 3), activation='relu'))
 model.add(layers.MaxPooling2D((2, 2)))
 model.add(layers.Conv2D(64, (3, 3), activation='relu'))
 model.add(layers.Flatten())
 model.add(layers.Dense(64, activation='relu'))
 model.add(layers.Dense(10, activation='softmax'))
                                
 # Compile the model
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
                                
 # Train the model
 model.fit(train_images, train_labels, epochs=10, batch_size=64, validation_data=(test_images, test_labels))
                                


This code sets up a convolutional neural network (CNN) using TensorFlow's Keras API for image classification. The model is trained on the MNIST dataset, a collection of 28x28 grayscale images of handwritten digits.

Key Components of the Code
- Data Loading and Preprocessing: The MNIST dataset is loaded and preprocessed to ensure compatibility with the neural network.
- Neural Network Model: A CNN model is defined using sequential layers, including convolutional layers, max-pooling layers, and dense layers.
- Model Compilation: The model is compiled with an optimizer, loss function, and evaluation metric.
- Training: The model is trained on the training data for a specified number of epochs.
- "ReLU" stands for Rectified Linear Unit, and it is an activation function commonly used in artificial neural networks, including deep learning models. The ReLU activation function introduces non-linearity to the network, allowing it to learn complex patterns and relationships in the data.

The mathematical expression of the ReLU activation function is quite simple:
   f(x)=max(0,x)
In simpler terms, the output is the maximum of zero and the input x. If the input is positive, the function returns the input value; if the input is negative, it returns zero. Visually, the ReLU function looks like a ramp, allowing positive values to pass through unchanged and setting negative values to zero.

Conclusion
Supervised learning in artificial neural networks plays a pivotal role in solving various real-world problems, from image recognition to natural language processing. Through this article, we've explored the basics of supervised learning and implemented a simple example using TensorFlow.

You might also like:

If you found this article useful and informative, you can support my work by accessing the below link.

Boost Your Brand's Visibility

Partner with us to boost your brand's visibility and connect with our community of tech enthusiasts and professionals. Our platform offers great opportunities for engagement and brand recognition.

Interested in advertising on our website? Reach out to us at office@ml-nn.eu.