Skip to main content

Artificial Neural Networks Representation

Neural Network Perception and Backpropagation

Concepts of Neural Networks

    • Introduction,
    • Neural network representation
    • Perceptron
    • Multi-Layer Perceptron (MLP)
    • Convolutional Neural Networks (CNN)
    • Recurrent Neural Networks (RNN)
    • back propagation algorithm and Remarks
    • An illustrative example of face recognition
    • Advanced topics in artificial neural networks

Neural Networks Concepts

Artificial Neural Networks Definition 

Neural networks are a type of machine learning that uses interconnected nodes to simulate the function of a human brain to solve complex problems. 


Introduction:

Artificial Neural Networks (ANN) is a type of machine learning algorithm that is inspired by the structure and functioning of biological neural networks in the human brain. ANNs are used for a variety of applications such as image recognition, natural language processing, and predictive modelling. 

Neural Network Representation:

An ANN consists of a set of interconnected neurons or nodes that are organized in layers. The input layer receives input data, and the output layer produces the output of the model. Between the input and output layers, there can be one or more hidden layers, which perform computations on the input data.

Perceptron:

The simplest kind of neural network that may be applied to binary classification issues is the perceptron. It consists of a single layer of neurons, where each neuron computes a weighted sum of the input features and applies a threshold function to produce the output.

Neural Network Perceptron

General Algorithm Steps:

  • Initialize the weights and biases of the perceptron randomly.
  • For each training example, compute the weighted sum of the input features.
  • Apply the threshold function to produce the output of the perceptron.
  • Adapt the weights and biases based on the discrepancy between the expected and actual results.
  • Repeat steps 2-4 for a fixed number of epochs or until convergence.

Python code for perceptron:

import numpy as np

class Perceptron:

    def __init__(self, n_features, learning_rate=0.01, n_epochs=100):

        self. weights = np. random.rand(n_features)

        self. bias = np. random.rand()

        self.learning_rate = learning_rate

        self.n_epochs = n_epochs

    def predict(self, X):

        linear_output = np.dot(X, self.weights) + self.bias

        return np.where(linear_output > 0, 1, 0)

       def train(self, X, y):

        for epoch in range(self.n_epochs):

            for xi, yi in zip(X, y):

                linear_output = np.dot(xi, self.weights) + self.bias

                prediction = np.where(linear_output > 0, 1, 0)

                error = yi - prediction

                self. weights += self.learning_rate * error * xi

                self.bias += self.learning_rate * error            

    def evaluate(self, X, y):

        y_pred = self.predict(X)

        accuracy = np.mean(y_pred == y)

        return accuracy

Multi-Layer Perceptron (MLP):

The MLP is a type of neural network that consists of multiple layers of neurons, including one or more hidden layers. Each neuron in the hidden layer computes a weighted sum of the inputs and applies an activation function to produce the output. The output of one layer becomes the input of the next layer, and the final layer produces the output of the model.

General Algorithm Steps:

  • Randomize the MLP's weights and biases upon initialization.
  • For each training example, compute the output of each neuron in the hidden layers and the output layer using forward propagation.
  • Calculate the difference in production between what was expected and what was produced.
  • Update the weights and biases using backpropagation.
  • Repeat steps 2-4 for a fixed number of epochs or until convergence.

Python code for MLP:

from sklearn.neural_network import MLPClassifier

# initialize MLP classifier

mlp = MLPClassifier(hidden_layer_sizes=(100, 50), activation='relu', solver='adam', max_iter=1000)

# train MLP on training data

mlp.fit(X_train, y_train)

# predict on test data

y_pred = mlp.predict(X_test)

# evaluate the performance of MLP

accuracy = np.mean(y_pred == y_test)

print(f"Accuracy: {accuracy}") 

Back Propagation Algorithm:

Back Propagation Algorithm is a supervised learning algorithm that is used to train Artificial Neural Networks. It is a method to update the weights and biases of a neural network to minimize the error between the predicted and actual output.

General Algorithm Steps:

  • Randomize the neural network's weights and biases upon initialization.
  • Feed an input example to the neural network and compute the output.
  • Calculate the difference in production between what was expected and what was produced.
  • Back propagate the error through the network to update the weights and biases using the chain rule of differentiation.
  • Repeat steps 2-4 for all input examples in the training set for multiple epochs or until convergence.

Remarks on Back Propagation Algorithm:

  • Back Propagation Algorithm is computationally expensive and requires a large amount of training data.
  • It may suffer from overfitting if the model is too complex or if the training data is noisy.
  • Various optimization techniques can be used to improve the convergence of the algorithm, such as momentum, learning rate decay, and weight decay.

Neural Network Propagation example

An Illustrative Example: Face Recognition

Face recognition is a classic example of using Artificial Neural Networks to perform image classification tasks. The general algorithm steps for face recognition using Artificial Neural Networks are:

  • Collect a dataset of face images and label them according to the person in the image.
  • Preprocess the images by resizing, cropping, and normalizing the pixel values.
  • Train an Artificial Neural Network using preprocessed images and labels.
  • Test the trained model on a set of new images to evaluate its accuracy.

Python code for Face Recognition using Artificial Neural Networks 

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# load dataset

X_train, y_train, X_test, y_test = load_face_data()

# initialize CNN

cnn = Sequential()

#Addd convolutional layer

cnn.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 1)))

# add max pooling layer

cnn.add(MaxPooling2D((2, 2)))

# add flatten layer

cnn.add(Flatten())

# adda a fully connected layer

cnn.add(Dense(128, activation='relu'))

cnn.add(Dense(10, activation='softmax'))

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

# train CNN on training data

cnn.fit(X_train, y_train, epochs=10, batch_size=32)

# evaluatethe  performance of CNN on test data

loss, accuracy = cnn.evaluate(X_test, y_test)

print(f"Accuracy: {accuracy}")

Advanced Topics in Artificial Neural Networks:

There are several advanced topics in Artificial Neural Networks, such as:

Transfer Learning: Using pre-trained neural networks to solve new problems with limited data.

Adversarial Training: Training neural networks to be robust against adversarial attacks.

Generative Adversarial Networks (GANs): A type of neural network that is used for generative tasks such as image synthesis and data augmentation.

Reinforcement Learning: A type of machine learning where an agent learns to make decisions by interacting with an environment and receiving rewards or punishments.

Python code for GANs:

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Reshape, Flatten, Conv2DTranspose, Conv2D

from tensorflow.keras.optimizers import Adam

# define generator model

generator = Sequential()

generator.add(Dense(128 * 7 * 7, input_dim=100))

generator.add(Reshape((7, 7, 128)))

generator.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))

generator.add(LeakyReLU(alpha=0.2))

generator.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))

generator.add(LeakyReLU(alpha=0.2))

generator.add(Conv2DTranspose(1, (7,7), activation='sigmoid', padding='same'))

# define discriminator model

discriminator = Sequential()

discriminator.add(Conv2D(64, (3,3), strides=(2,2), padding='same', input_shape=(28,28,1)))

discriminator.add(LeakyReLU(alpha=0.2))

discriminator.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))

discriminator.add(LeakyReLU(alpha=0.2))

discriminator.add(Flatten())

discriminator.add(Dense(1, activation='sigmoid'))

# compile the discriminator

discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5), metrics=['accuracy'])

# combine the generator and discriminator into a single model

gan = Sequential()

gan.add(generator)

gan.add(discriminator)

gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))

#Loadd the MNIST dataset

(X_train, _), (_, _) = mnist.load_data()

#Normalizee input images

X_train = X_train.astype('float32')

X_train = (X_train - 127.5) / 127.5

#Reshapee images to 4-dimensional tensor

X_train = np.expand_dims(X_train, axis=3)

# define the size of the batch

batch_size = 128

# define the number of training iterations

epochs = 100

# calculate the number of batches per training epoch

batch_per_epoch = int(X_train.shape[0] / batch_size

# generate samples and save them every epoch

for i in range(epochs):

    for j in range(batch_per_epoch):

        # generate a random sample of input noise

        noise = np.random.randn(batch_size, 100)

        # generate fake images using the generator

        fake_images = generator.predict(noise)

        # concatenate real and fake images

        images = np.concatenate((X_train[j * batch_size:(j + 1) * batch_size], fake_images))

        # assign target labels for real and fake images

        target = np.concatenate((np.ones((batch_size, 1)), np.zeros((batch_size, 1))))

        # train the discriminator on the real and fake images

        discriminator_loss = discriminator.train_on_batch(images, target)

        # generate a random sample of input noise

        noise = np.random.randn(batch_size, 100)

        # assign target labels for the fake images

        target = np.ones((batch_size, 1))

# train generator to fool the discriminator

        noise = np.random.normal(0, 1, (batch_size, 100))

        misleading_targets = np.ones((batch_size, 1))

        g_loss = gan.train_on_batch(noise, misleading_targets)

    # display progress

    print(f"Epoch {epoch+1}/{epochs}: D_loss_real = {d_loss_real:.4f}, D_loss_fake = {d_loss_fake:.4f}, G_loss = {g_loss:.4f}")

# generate new images with trained generator

import matplotlib.pyplot as plt

noise = np.random.normal(0, 1, (16, 100))

generated_images = generator.predict(noise)

fig, axs = plt.subplots(4, 4)

cnt = 0

for i in range(4):

    for j in range(4):

        axs[i,j].imshow(generated_images[cnt, :,enerated_images[cnt, :, :, 0], cmap='gray')

axs[i,j].axis('off')

cnt += 1

plt.show()

This code generates new images using the trained generator. The first line generates 16 random noise vectors of length 100 using the NumPy random. normal function. The generator then uses these noise vectors to generate 16 new images.

The next lines create a 4x4 grid of subplots using Matplotlib. The count variable is used to keep track of the current image index. The for loop iterates over the subplots and displays each generated image in grayscale using imshow. The axis function is called to remove the axis labels. Finally, the show function is called to display the generated images.                                                                                                

Back Propagation Algorithm:

Back Propagation Algorithm is a supervised learning algorithm that is used to train Artificial Neural Networks. It is a method to update the weights and biases of a neural network to minimize the error between the predicted and actual output.

General Algorithm Steps:

  • Randomize the neural network's weights and biases upon initialization.
  • Feed an input example to the neural network and compute the output.
  • Calculate the difference in production between what was expected and what was produced.
  • Back propagate the error through the network to update the weights and biases using the chain rule of differentiation.
  • Repeat steps 2-4 for all input examples in the training set for multiple epochs or until convergence.

Remarks on Back Propagation Algorithm:

  • Back Propagation Algorithm is computationally expensive and requires a large amount of training data.
  • It may suffer from overfitting if the model is too complex or if the training data is noisy.
  • Various optimization techniques can be used to improve the convergence of the algorithm, such as momentum, learning rate decay, and weight decay.

Neural Network Propagation example

An Illustrative Example: Face Recognition

Face recognition is a classic example of using Artificial Neural Networks to perform image classification tasks. The general algorithm steps for face recognition using Artificial Neural Networks are:

  • Collect a dataset of face images and label them according to the person in the image.
  • Preprocess the images by resizing, cropping, and normalizing the pixel values.
  • Train an Artificial Neural Network using preprocessed images and labels.
  • Test the trained model on a set of new images to evaluate its accuracy.

Python code for Face Recognition using Artificial Neural Networks 

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# load dataset

X_train, y_train, X_test, y_test = load_face_data()

# initialize CNN

cnn = Sequential()

#Addd convolutional layer

cnn.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 1)))

# add max pooling layer

cnn.add(MaxPooling2D((2, 2)))

# add flatten layer

cnn.add(Flatten())

# adda a fully connected layer

cnn.add(Dense(128, activation='relu'))

cnn.add(Dense(10, activation='softmax'))

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

# train CNN on training data

cnn.fit(X_train, y_train, epochs=10, batch_size=32)

# evaluatethe  performance of CNN on test data

loss, accuracy = cnn.evaluate(X_test, y_test)

print(f"Accuracy: {accuracy}")

Advanced Topics in Artificial Neural Networks:

There are several advanced topics in Artificial Neural Networks, such as:

Transfer Learning: Using pre-trained neural networks to solve new problems with limited data.

Adversarial Training: Training neural networks to be robust against adversarial attacks.

Generative Adversarial Networks (GANs): A type of neural network that is used for generative tasks such as image synthesis and data augmentation.

Reinforcement Learning: A type of machine learning where an agent learns to make decisions by interacting with an environment and receiving rewards or punishments.

Python code for GANs:

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Reshape, Flatten, Conv2DTranspose, Conv2D

from tensorflow.keras.optimizers import Adam

# define generator model

generator = Sequential()

generator.add(Dense(128 * 7 * 7, input_dim=100))

generator.add(Reshape((7, 7, 128)))

generator.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))

generator.add(LeakyReLU(alpha=0.2))

generator.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))

generator.add(LeakyReLU(alpha=0.2))

generator.add(Conv2DTranspose(1, (7,7), activation='sigmoid', padding='same'))

# define discriminator model

discriminator = Sequential()

discriminator.add(Conv2D(64, (3,3), strides=(2,2), padding='same', input_shape=(28,28,1)))

discriminator.add(LeakyReLU(alpha=0.2))

discriminator.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))

discriminator.add(LeakyReLU(alpha=0.2))

discriminator.add(Flatten())

discriminator.add(Dense(1, activation='sigmoid'))

# compile the discriminator

discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5), metrics=['accuracy'])

# combine the generator and discriminator into a single model

gan = Sequential()

gan.add(generator)

gan.add(discriminator)

gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))

#Loadd the MNIST dataset

(X_train, _), (_, _) = mnist.load_data()

#Normalizee input images

X_train = X_train.astype('float32')

X_train = (X_train - 127.5) / 127.5

#Reshapee images to 4-dimensional tensor

X_train = np.expand_dims(X_train, axis=3)

# define the size of the batch

batch_size = 128

# define the number of training iterations

epochs = 100

# calculate the number of batches per training epoch

batch_per_epoch = int(X_train.shape[0] / batch_size

# generate samples and save them every epoch

for i in range(epochs):

    for j in range(batch_per_epoch):

        # generate a random sample of input noise

        noise = np.random.randn(batch_size, 100)

        # generate fake images using the generator

        fake_images = generator.predict(noise)

        # concatenate real and fake images

        images = np.concatenate((X_train[j * batch_size:(j + 1) * batch_size], fake_images))

        # assign target labels for real and fake images

        target = np.concatenate((np.ones((batch_size, 1)), np.zeros((batch_size, 1))))

        # train the discriminator on the real and fake images

        discriminator_loss = discriminator.train_on_batch(images, target)

        # generate a random sample of input noise

        noise = np.random.randn(batch_size, 100)

        # assign target labels for the fake images

        target = np.ones((batch_size, 1))

# train generator to fool the discriminator

        noise = np.random.normal(0, 1, (batch_size, 100))

        misleading_targets = np.ones((batch_size, 1))

        g_loss = gan.train_on_batch(noise, misleading_targets)

    # display progress

    print(f"Epoch {epoch+1}/{epochs}: D_loss_real = {d_loss_real:.4f}, D_loss_fake = {d_loss_fake:.4f}, G_loss = {g_loss:.4f}")

# generate new images with trained generator

import matplotlib.pyplot as plt

noise = np.random.normal(0, 1, (16, 100))

generated_images = generator.predict(noise)

fig, axs = plt.subplots(4, 4)

cnt = 0

for i in range(4):

    for j in range(4):

        axs[i,j].imshow(generated_images[cnt, :,enerated_images[cnt, :, :, 0], cmap='gray')

axs[i,j].axis('off')

cnt += 1

plt.show()

This code generates new images using the trained generator. The first line generates 16 random noise vectors of length 100 using the NumPy random. normal function. The generator then uses these noise vectors to generate 16 new images.

The next lines create a 4x4 grid of subplots using Matplotlib. The count variable is used to keep track of the current image index. The for loop iterates over the subplots and displays each generated image in grayscale using imshow. The axis function is called to remove the axis labels. Finally, the show function is called to display the generated images.

previous(Unsupervised Machine Learning)

                                              Continue to (Reinforcement Machine Learning)


Comments

Popular posts from this blog

What is Machine Learning

Definition of  Machine Learning and Introduction Concepts of Machine Learning Introduction What is machine learning ? History of Machine Learning Benefits of Machine Learning Advantages of Machine Learning Disadvantages of Machine Learning

Know the Machine Learning Syllabus

Learn Machine Learning Step-by-step INDEX  1. Introduction to Machine Learning What is Machine Learning? Applications of Machine Learning Machine Learning Lifecycle Types of Machine Learning   2. Exploratory Data Analysis Data Cleaning and Preprocessing Data Visualization Techniques Feature Extraction and Feature Selection  

What is Analytical Machine Learning

Analytical  and  Explanation-based learning  with domain theories  Analytical Learning Concepts Introduction Learning with perfect domain theories: PROLOG-EBG Explanation-based learning Explanation-based learning of search control knowledge Analytical Learning Definition :  Analytical learning is a type of machine learning that uses statistical and mathematical techniques to analyze and make predictions based on data.

What is Well-posed learning

  Perspectives and Issues of Well-posed learning What is well-posed learning? Well-posed learning is a type of machine learning where the problem is well-defined, and there exists a unique solution to the problem.  Introduction Designing a learning system Perspectives and issues in machine learning

What is Bayes Theorem

Bayesian Theorem and Concept Learning  Bayesian learning Topics Introduction Bayes theorem Concept learning Maximum Likelihood and least squared error hypotheses Maximum likelihood hypotheses for predicting probabilities Minimum description length principle, Bayes optimal classifier, Gibs algorithm, Naïve Bayes classifier, an example: learning to classify text,  Bayesian belief networks, the EM algorithm. What is Bayesian Learning? Bayesian learning is a type of machine learning that uses Bayesian probability theory to make predictions and decisions based on data.

Total Pageviews

Followers