ARTIFICIAL NEURAL NETWORK Notes from the AI Advance course-Class 25 by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
ARTIFICIAL NEURAL NETWORK
Notes from the AI Advance course-Class 25 by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
In Linear Models, two things vary with each other.
Neural networks are non-linear models.
Neural Network are made up of neurons.
Neuron has two parts:
Fully Connected Neural Network, Standard Neural Network, Feed Forward, Dense Neural Netrwork, Artificial Neural Network, MLP are all same.
CNN used for Videos etc.
RNN used for Time Series Analysis.
Auto Encoders are necessary to Understand GPT and Bert.
Attention Models (Transformers) are very popular in NLP.
We have to train neural network with data.
Neural network is used to get prediction.
Inference or Prediction from Neural Network, Forward pass is used.
To train neural network, both forward and backward propagation is used.
Learning with Discipline is very Important.
In the coming years, two types of peoples are very important.
1- Reliability
2- Critical Thinking
Don't go for shortcut.
Correct your mindset.
Communication & Critical Thinking is very important for success in life.
Critical Thinking is directly linked to Your Concepts.
You can earn money by grasping your concepts on tools.
Linear Regression works on dependent variables.
Business makes fortunes.
Un-Certainity is non-linearity.
Try to apply the things, that you have learned.
Store the concepts in your permanent memory.
Develop the habbit of Learning.
Get Direction & Do Practice.
In Remote Jobs, to clear technical interviews, you have to develop your strong concepts.
Neural network is non-linear complex problem.
Now for NLP, Transformers are used.
C2C (Customer to Candidate), remote jobs, works on a fixed salary.
Parameter (Neural Network learns with data)
Hyper-Parameters (Neural Network learns with Experience)
Learning Rate is also a Hyper Parameter.
Dataset is a collection of a data regarding specific problem.
Check out the code.
{
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
# Load the Fashion MNIST dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
# (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
}
{# splitting the data into test and train set
(X_train, Y_train), (X_test, Y_test) = fashion_mnist.load_data()}
{import matplotlib.pyplot as plt
# Number of images to display
n = 10
# Create a figure to display the images
plt.figure(figsize=(20, 4))
# Loop through the first 'n' images
for i in range(n):
# Create a subplot within the figure
ax = plt.subplot(2, n, i + 1)
# Display the original image
plt.imshow(X_test[i].reshape(28, 28))
# Set colormap to grayscale
plt.gray()
# Hide x-axis and y-axis labels and ticks
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Show the figure with the images
plt.show()
# Close the figure
plt.close()
}
{# Print the shapes of the original training data and labels
print("Previous X_train shape: {} \nPrevious Y_train shape:{}".format(X_train.shape, Y_train.shape))
# Reshape training and testing data to a flat format (flattening the images)
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)}
{# Convert the data type of the images to float32
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# Normalize the pixel values to a range between 0 and 1 # Zero is for Black #1 for White
X_train /= 255
X_test /= 255}
{# Number of classes in the dataset
classes = 10
# Convert the labels to one-hot encoded format
Y_train = to_categorical(Y_train, classes)
Y_test = to_categorical(Y_test, classes)
# Print the shapes of the preprocessed training data and labels
print("New X_train shape: {} \nNew Y_train shape:{}".format(X_train.shape, Y_train.shape))}
{# Define the input size for each data sample (e.g., image pixels)
input_size = 784
# Specify the number of data samples to process in each batch
batch_size = 200
# Define the number of neurons in the first hidden layer
hidden1 = 400
# Define the number of neurons in the second hidden layer
hidden2 = 20
# Define the total number of classes/categories in the dataset
classes = 10
# Set the number of complete passes through the dataset during training
epochs = 10
}
{### 4. Build the model ###
# Create a Sequential model, which allows us to build a neural network layer by layer
model = Sequential()
# Add the first hidden layer with 'hidden1' neurons, using ReLU activation function
# The 'input_dim' specifies the input size for this layer
model.add(Dense(hidden1, input_dim=input_size, activation='relu'))
# output = relu(dot(W, input) + bias)
# Add the second hidden layer with 'hidden2' neurons, also using ReLU activation function
model.add(Dense(hidden2, activation='relu'))
# Add the output layer with 'classes' neurons, using softmax activation function
# Softmax activation ensures that the output values represent probabilities of each class
model.add(Dense(classes, activation='softmax'))
### Compilation ###
# Compile the model by specifying the loss function, optimizer, and evaluation metrics
model.compile(loss='categorical_crossentropy',
metrics=['accuracy'], optimizer='sgd')
# Display a summary of the model architecture, showing the layers and parameter counts
model.summary()
}
{# Import necessary libraries
from time import time
# Record the current time to measure training time
tic = time()
# Fit the model on the training data
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)
# Record the time after model training
toc = time()
# Calculate and print the time taken for model training
print("Model training took {} secs".format(toc - tic))
# Testing the trained model
### 5. Test
# You can continue your code from here...
}
{# Import the necessary libraries
from sklearn.metrics import accuracy_score
Recommended by LinkedIn
import numpy as np
import matplotlib.pyplot as plt
# Predict probabilities for the test set using the trained model
y_pred_probs = model.predict(X_test, verbose=0)
y_pred = np.where(y_pred_probs > 0.5, 1, 0)
# Calculate and print the test accuracy using predicted and true labels
test_accuracy = accuracy_score(y_pred, Y_test)
print("\nTest accuracy: {}".format(test_accuracy))
}
{# Define a mask for selecting a range of indices (20 to 49)
mask = range(20, 50)
# Select the first 20 samples from the test set for visualization
X_valid = X_test[0:20]
actual_labels = Y_test[0:20]
# Predict probabilities for the selected validation samples
y_pred_probs_valid = model.predict(X_valid)
y_pred_valid = np.where(y_pred_probs_valid > 0.5, 1, 0)
}
{# Set up a figure to display images
n = len(X_valid)
plt.figure(figsize=(20, 4))
for i in range(n):
# Display the original image
ax = plt.subplot(2, n, i + 1)
plt.imshow(X_valid[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Display the predicted digit
predicted_digit = np.argmax(y_pred_probs_valid[i])
ax = plt.subplot(2, n, i + 1 + n)
plt.text(0.5, 0.5, str(predicted_digit), fontsize=12, ha='center', va='center')
plt.axis('off')
# Show the plotted images
plt.show()
# Close the plot
plt.close()
}
{
# Load the CIFAR-10 dataset
cifar10 = tf.keras.datasets.cifar10
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
}
{import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
# Function to display images from the CIFAR-10 dataset
def display_dataset_images(images, labels):
"""
Displays a grid of sample images from the CIFAR-10 dataset.
Args:
images: A NumPy array of images with shape (num_images, height, width, channels).
labels: A NumPy array of labels with shape (num_images,) where each element is the class index of the corresponding image.
"""
plt.figure(figsize=(10, 10)) # Create a figure with specified size
# Loop through 25 images (5 rows x 5 columns)
for i in range(25):
# Create a subplot at position (i + 1) in a 5x5 grid
plt.subplot(5, 5, i + 1)
# Turn off ticks (x and y axes) for cleaner visualization
plt.xticks([])
plt.yticks([])
# Remove grid lines for better focus on images
plt.grid(False)
# Display the image using grayscale colormap
plt.imshow(images[i], cmap=plt.cm.binary)
# Extract and display the class label for the image
plt.xlabel(labels[i][0])
# Display the entire figure with all subplots
plt.show()
}
{
# Normalize the images
train_images = train_images / 255.0
test_images = test_images / 255.0
# Class names in the CIFAR-10 dataset
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
}
{# Build the ANN model
model = models.Sequential([
layers.Flatten(input_shape=(32, 32, 3)), # Input layer: flatten the 32x32x3 images
layers.Dense(512, activation='relu'),
layers.Dense(1024, activation='relu'),
layers.Dense(512, activation='relu'),# Hidden layer: 512 neurons with ReLU activation
layers.Dense(10, activation='softmax') # Output layer: 10 neurons (one for each class) with softmax activation
])}
{# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
}
{pip install tensorflow pydot graphviz
from tensorflow.keras.utils import plot_model
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
}
{# Train the model
model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
}
{# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")
}
{def display_inference_results(images, predictions, actual_labels, class_names, num_samples=5):
"""
Displays inference results for a sample of images with their actual and predicted labels.
Args:
images: A NumPy array of images with shape (num_images, height, width, channels).
predictions: A NumPy array of predicted class labels with shape (num_images,).
actual_labels: A NumPy array of actual class labels with shape (num_images,).
class_names: A list of strings containing the class names for the CIFAR-10 dataset.
num_samples: The number of images to display (default is 5).
"""
# Create a figure with a specified size
plt.figure(figsize=(15, 3))
# Loop through the specified number of sample images
for i in range(num_samples):
# Create a subplot at position (i + 1) in a 1x(num_samples) grid
plt.subplot(1, num_samples, i + 1)
# Turn off ticks (x and y axes) for cleaner visualization
plt.xticks([])
plt.yticks([])
# Remove grid lines for better focus on images
plt.grid(False)
# Display the image using grayscale colormap
plt.imshow(images[i], cmap=plt.cm.binary)
# Extract the predicted class label using argmax
predicted_label = class_names[predictions[i].argmax()]
# Extract the actual class label (assuming first element in the array)
actual_label = class_names[actual_labels[i][0]]
# Combine and display actual and predicted labels in the subplot
plt.xlabel(f"Actual: {actual_label}\nPredicted: {predicted_label}")
# Display the entire figure with all subplots
plt.show()
}
{# Make predictions
predictions = model.predict(test_images)
# Display inference results for sample images with their actual and predicted labels
display_inference_results(test_images, predictions, test_labels, class_names)}
Single channel image means we are working on gray scale data.
Three channel image for colored data.
Every image is like a matrix.
Flattening means converting 2-D image into 1-D matrix.
#AI #artificialintelligence #datascience #neuralnetwork #irfanmalik #drsheraz #xevensolutions #hamzanadeem