Class 23 - MULTI-CLASS CLASSIFICATION

Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Class 23 - MULTI-CLASS CLASSIFICATION Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Class 23 - MULTI-CLASS CLASSIFICATION

Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Hyper Parameters:

Parameters that are not learned from Data.

You learn Hyper Parameters along with the Practice.

Learning Rate:

Going towards the low points (converge)

Activation Function (Neurons):

Activation & Neurons are interchangeable for now.

ReLU is used in Hidden Layers.

Softmax is used in Output, when you have more than two classes (means multi classes)

In case of two classes, you can use binary classification (called sigmoid)

Batch Size:

Divide the data into sub classes.

Epcohs:

No of revisions of data

Google Colab Link:

https://colab.research.google.com/drive/1mA1-Qp4wLfJ9heprE0KR-X_UIhfrieNN#scrollTo=Al0gZIXGrzta

You can NLP on your own. But for neural networks you need help.

In Industry, there are lot of medium size projects that involve NLP, LLM'S etc.

You cann't learn Neural Networks by ratta.

Also Learn NN'S from Andrew Ng.

Then, do alot of Practice.

PRACTICE

PRACTICE

PRACTICE

This time is for alot of HARDWORK. Because Industry is Rapidly Changing on AI Side.

{###1. Load Data and Splot Data

from tensorflow.keras.datasets import mnist

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense

from tensorflow.keras.utils import to_categorical

}

{# splitting the data into test and train set

(X_train, Y_train), (X_test, Y_test) = mnist.load_data()}

{import matplotlib.pyplot as plt

# Number of digits 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 = 5

}

{### 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

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()

}

REVIEW THE BELOW NOTES, AFTER RUNNING THE CODE LINE BY LINE.

INCASE, YOU DON'T KNOW WHAT'S GOING ON IN THE CODE BEHIND THE SCENE.

DON'T WORRY, TRUST ME, GO THROUGH THE CODE.

YOU WILL BE FAMILAR WITH THE CODE AFTER RUNNING IT 2,3 TIMES.

One Hot Encoding:

Converting data into binary classification (means 0,1 form)

You can set Hyper Parameters based on your experience and Practice.

NN means Neural Network

NN has alot of Types

Fundamental type of NN is Standard NN or Fully Connected NN (Sequential)

Imagine Sequential like a box that contain layers of neurons.

Dense is used to add layer in Standard NN in tensorflow.

It is interconnected with the previous layers as well, that's why it is called Fully Connected NN.

ReLU, Sigmoid is Single Neuron

Softmax is group of Neurons

To make connections with these layer, we will call

model.compile()

Remember model = Sequential()

In Model Traning, our focus is the minimize the loss.

For loss function concept, go to tensorflow playground & make NN'S.

In case of multiclass classification, we use

{loss = "categorical_crossentropy"}

In case of binary classification, we use

{loss = "binary_crossentropy"}

Mutil-class classification is independent of classes, means class be 10, 50, 5 etc.

Here,

optimizer = "sgd"

stocastic gradient descent

For now, just remember it as a gradient descent.

Usually, input layer is not counted.

Input layer is a separate layer & it is necessary.

model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)

Here, verbose = 1 (means it will show you, what's going on, you can track the progress)

#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #neuralnetworks #multiclass #classification #hamzanadeem

要查看或添加评论,请登录

Hamza Nadeem的更多文章

社区洞察

其他会员也浏览了