Introduction to Artificial Intelligence, Machine Learning and Neural networks

Introduction to Artificial Intelligence, Machine Learning and Neural networks

ยท

4 min read

Table of contents

No heading

No headings in the article.

What is Artificial intelligence?
Artificial intelligence (AI) is the ability of machines to perform tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation.

What is Machine Learning?
Machine learning is a field of computer science and artificial intelligence (AI) that focuses on the development of algorithms and statistical models that enable computer systems to learn from and make predictions or decisions based on data. In essence, machine learning involves creating programs that can automatically improve their performance on a task by learning from experience or data.

What is a Neural Network?
A neural network is a type of machine learning algorithm that is designed to recognize patterns and make decisions or predictions based on input data. It is inspired by the structure and function of the human brain and is therefore also known as an artificial neural network.

Tensorflow and Keras
Tensorflow is an open-source software library for dataflow and differentiable programming across a range of tasks. It is developed by the Google Brain team and used extensively in machine learning and deep learning applications.

Keras is a high-level neural networks API written in Python and integrated with Tensorflow as its default backend. Keras provides an easy-to-use interface for building, training, and deploying deep learning models.

Demo project
Let's now walk through a demo project that uses Tensorflow and Keras to build a machine learning model. We will use the Fashion MNIST dataset, which contains 70,000 grayscale images of 10 different types of clothing items. The goal of our model is to predict the type of clothing item in a given image.

Here's the code for the demo project:

import tensorflow as tf
import keras as k
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

data = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ["T-shirt", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boots"]

train_images = train_images/255.0
test_images = test_images/255.0

model = k.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")  
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

model.fit(train_images, train_labels, epochs=5)

prediction = model.predict(test_images)
for i in range(5):
    plt.grid(False)
    plt.imshow(test_images[i], cmap=plt.cm.binary)
    plt.xlabel("Actual: " + class_names[test_labels[i]])
    plt.title("Prediction: "+ class_names[np.argmax(prediction[i])] )
    plt.show()

Let's go through the code step by step.

Libraries Import

import tensorflow as tf
import keras as k
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

The first few lines import the necessary modules: Tensorflow, Keras, Numpy and Matplotlib

Data loading

data = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()

This block loads the Fashion-MNIST dataset from Keras. This dataset contains 60,000 training images and 10,000 testing images of 10 different clothing items. The "load_data()" function returns the training and testing images as well as their corresponding labels.

Data Preprocessing

class_names = ["T-shirt", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boots"]
train_images = train_images/255.0
test_images = test_images/255.0

This block defines the names of the 10 clothing items in the dataset and scales the pixel values of the images to be between 0 and 1. This is a common preprocessing step for image data.

Model Building

model = k.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")  
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

This block defines the neural network model to be used and compiles it with an optimizer, loss function, and evaluation metric. The model has three layers: a flatten layer to convert the 2D image data into a 1D array, a dense layer with 128 neurons and ReLU activation function, and a dense output layer with 10 neurons and softmax activation function to output the probability of each class.

Model Training

model.fit(train_images, train_labels, epochs=5)

This block trains the neural network model using the training images and labels for a specified number of epochs. During training, the model adjusts its parameters to minimize the loss function and improve its accuracy on the training data.

Prediction and Visualization

prediction = model.predict(test_images)
for i in range(5):
    plt.grid(False)
    plt.imshow(test_images[i], cmap=plt.cm.binary)
    plt.xlabel("Actual: " + class_names[test_labels[i]])
    plt.title("Prediction: "+ class_names[np.argmax(prediction[i])] )
    plt.show()

This block makes predictions on the test images using the trained model and visualizes the first 5 test images with their corresponding actual labels and predicted labels. The "argmax()" function is used to get the index of the predicted class with the highest probability.

In summary, this code demonstrates how to build a simple neural network using Keras and Tensorflow to classify images of clothing from the Fashion MNIST dataset. The code showcases how to load the data, define the neural network architecture, compile and train the model, and make predictions on new data.

So, there we go we've just trained a basic neural network. Here is the link to the Github repository https://github.com/feranmiodugbemi/Demo-AI. ๐Ÿ‘‹

ย