Rauf

AI and ML

Neural Net

Neural networks are a subset of machine learning, utilizing artificial neural networks inspired by human brains. Often called deep learning, neural networks excel in identifying patterns and making predictions. In this webnote, I will cover topics such as Artificial Neural Networks, Activation Functions, Gradient Descent, Backpropagation, Overfitting, TensorFlow, Image Convolution, Convolutional Neural Networks, and Recurrent Neural Networks, one by one. Let's start 😊

Artificial Neural Networks

Artificial Neural Networks (ANNs) are inspired by biological neurons. They consist of layers of neurons that work together to learn patterns from data.


# A simple implementation of a single neuron without libraries
inputs = [1.0, 2.0, 3.0]  # Input features
weights = [0.2, 0.8, -0.5]  # Weights for each input
bias = 2.0  # Bias term

# Calculate the output of the neuron
def simple_neuron(inputs, weights, bias):
    output = sum(i * w for i, w in zip(inputs, weights)) + bias
    return output

output = simple_neuron(inputs, weights, bias)
print("Output of the single neuron:", output)
        

In the code above, you can see a single neuron implemented with weights, an activation function, and a bias. Let’s break down these terms:

Gradient Descent

Gradient Descent is an optimization algorithm used to minimize the loss function by iteratively adjusting the model parameters.


# Gradient Descent example with a simple loss function
import numpy as np

# Loss function: f(x) = (x-3)^2
# Gradient: f'(x) = 2*(x-3)
def gradient_descent(learning_rate=0.1, iterations=100):
    x = 0  # Initial value of x
    for i in range(iterations):
        gradient = 2 * (x - 3)
        x = x - learning_rate * gradient
    return x

result = gradient_descent()
print("Minimum value of x:", result)
        

The code above shows a simple implementation of Gradient Descent. It iteratively updates the parameter x based on the gradient until it reaches the minimum value of the loss function.

Backpropagation

Backpropagation is a method used to calculate gradients in a neural network. It adjusts weights by propagating the error backward from the output layer to the input layer.


# Simplified example of backpropagation
import numpy as np

# Inputs and target output
inputs = np.array([1, 2, 3])
weights = np.array([0.5, -0.1, 0.2])
bias = 0.1

target = 0.5
learning_rate = 0.01

# Forward pass
def forward(inputs, weights, bias):
    return np.dot(inputs, weights) + bias

# Loss function
def loss(output, target):
    return (output - target) ** 2

# Gradient calculation
def backward(inputs, weights, bias, output, target):
    error = output - target
    d_weights = 2 * error * inputs
    d_bias = 2 * error
    return d_weights, d_bias

# Training loop
output = forward(inputs, weights, bias)
l = loss(output, target)
d_weights, d_bias = backward(inputs, weights, bias, output, target)
weights -= learning_rate * d_weights
bias -= learning_rate * d_bias

print("Updated weights:", weights)
print("Updated bias:", bias)
        

This example demonstrates how backpropagation works to adjust weights and biases to minimize the error.

TensorFlow

TensorFlow is a popular library for implementing deep learning models. It simplifies building, training, and deploying neural networks.


# TensorFlow example: A basic neural network
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

# Create a simple feedforward neural network
model = Sequential([
    Dense(8, activation='relu', input_shape=(3,)),  # Input layer with ReLU activation
    Dense(1, activation='sigmoid')  # Output layer with sigmoid activation
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Dummy dataset
inputs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
targets = [0, 1, 0]

# Train the model
model.fit(inputs, targets, epochs=10, verbose=1)
        

This code builds a simple neural network using TensorFlow and trains it on a dummy dataset. The network consists of an input layer and an output layer with sigmoid activation.

Convolutional Neural Network (CNN)

A Convolutional Neural Network (CNN) is a type of neural network primarily used to process and analyze image data. It identifies patterns in images, such as edges, textures, and shapes, using operations like convolutions and pooling. Finally, it flattens these features and passes them through a fully connected neural network for predictions.

Complete Code of CNN with TensorFlow


    import tensorflow as tf
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
    
    # Define the CNN model
    model = Sequential([
        Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
        MaxPooling2D(pool_size=(2, 2)),
        Flatten(),
        Dense(units=128, activation='relu'),
        Dense(units=10, activation='softmax')  # Output layer for 10 classes
    ])
    
    # Compile the model
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    
    # Summary of the model
    model.summary()
    
    # Example of training the model (using dummy data here)
    # Replace this with actual dataset like MNIST for real use
    import numpy as np
    x_train = np.random.random((100, 28, 28, 1))  # 100 samples, 28x28 size, 1 channel
    y_train = np.random.randint(0, 10, 100)  # 100 random labels for 10 classes
    
    # Train the model
    model.fit(x_train, y_train, epochs=5, batch_size=32)
        

The above code demonstrates a complete CNN pipeline with TensorFlow. It starts with defining the model, including convolutional, pooling, and fully connected layers. It then compiles and trains the model. Replace the dummy data with actual datasets like MNIST for real-world applications.

Recurrent Neural Network (RNN)

A Recurrent Neural Network (RNN) is a type of neural network designed to process sequential data. Unlike traditional neural networks, RNNs have connections that allow information to persist, making them ideal for tasks involving sequences like time series, natural language processing, and speech recognition.

Complete Code of RNN with TensorFlow


        import tensorflow as tf
        from tensorflow.keras.models import Sequential
        from tensorflow.keras.layers import SimpleRNN, Dense
    
        # Define the RNN model
        model = Sequential([
            SimpleRNN(units=32, activation='relu', input_shape=(5, 8)),
            Dense(units=10, activation='softmax')  # Output layer for 10 classes
        ])
    
        # Compile the model
        model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    
        # Summary of the model
        model.summary()
    
        # Example of training the model (using dummy data here)
        # Replace this with actual dataset for real use
        import numpy as np
        x_train = np.random.random((100, 5, 8))  # 100 samples, 5 timesteps, 8 features
        y_train = np.random.randint(0, 10, 100)  # 100 random labels for 10 classes
    
        # Train the model
        model.fit(x_train, y_train, epochs=5, batch_size=16)
        

The above code demonstrates a complete RNN pipeline with TensorFlow. It starts with defining the model, including recurrent and dense layers. It then compiles and trains the model. Replace the dummy data with actual datasets for real-world applications.