{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "cc99d119-6597-4d49-b013-f31694aed38e", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "# Install the necessary dependencies\n", "\n", "import os\n", "import sys \n", "!{sys.executable} -m pip install --quiet pandas scikit-learn numpy matplotlib jupyterlab_myst ipython imageio scikit-image requests ucimlrepo seaborn keras\n", "# Neural Networks" ] }, { "cell_type": "markdown", "id": "52e69c41-73fc-43dd-a615-9db97592b588", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "---\n", "license:\n", " code: MIT\n", " content: CC-BY-4.0\n", "github: https://github.com/ocademy-ai/machine-learning\n", "venue: By Ocademy\n", "open_access: true\n", "bibliography:\n", " - https://raw.githubusercontent.com/ocademy-ai/machine-learning/main/open-machine-learning-jupyter-book/references.bib\n", "---" ] }, { "cell_type": "markdown", "id": "ec28bbd0-5ff6-48ab-bd15-f92783e6a183", "metadata": {}, "source": [ "# Neural Networks" ] }, { "cell_type": "markdown", "id": "85c9021c-c560-485d-91f2-1642c6402ac5", "metadata": {}, "source": [ "Neural Networks are the functional unit of [Deep Learning](https://press.ocademy.cc/deep-learning/dl-overview.html) and are known to mimic the behavior of the human brain to solve complex data-driven problems.\n", "The input data is processed through different layers of artificial neurons stacked together to produce the desired output.\n", "From speech recognition and person recognition to healthcare and marketing, Neural Networks have been used in a varied set of domains." ] }, { "cell_type": "markdown", "id": "cae8690c-88aa-4980-bff6-4b315ced4ca8", "metadata": {}, "source": [ "

Image: Neurons in human brain
" ] }, { "cell_type": "markdown", "id": "cc2d8e9d-ffb2-4562-a180-92e5e9a87d79", "metadata": {}, "source": [ "## Key Components of the Neural Network Architecture" ] }, { "cell_type": "markdown", "id": "460b8583-75ed-47ff-bea0-18913ac02aba", "metadata": {}, "source": [ "The Neural Network architecture is made of individual units called neurons that mimic the biological behavior of the brain. \n", "Here are the various components of a neuron." ] }, { "cell_type": "markdown", "id": "11568845-e197-4e99-be19-27509431a4c1", "metadata": {}, "source": [ "

Image: Neuron in Artificial Neural Network
" ] }, { "cell_type": "markdown", "id": "bd6cd958-b6af-4f18-ad97-83307a697c7c", "metadata": {}, "source": [ "**Input:** It is the set of features that are fed into the model for the learning process. For example, the input in object detection can be an array of pixel values pertaining to an image.\n", "\n", "**Weight:** Its main function is to give importance to those features that contribute more towards the learning. It does so by introducing scalar multiplication between the input value and the weight matrix. For example, a negative word would impact the decision of the sentiment analysis model more than a pair of neutral words.\n", "\n", "**Transfer function:** The job of the transfer function is to combine multiple inputs into one output value so that the activation function can be applied. It is done by a simple summation of all the inputs to the transfer function. " ] }, { "cell_type": "markdown", "id": "d87d58f8-445e-4381-aeb2-931dfc397992", "metadata": {}, "source": [ "**Activation Function:** It introduces non-linearity in the working of perceptrons to consider varying linearity with the inputs. Without this, the output would just be a linear combination of input values and would not be able to introduce non-linearity in the network." ] }, { "cell_type": "markdown", "id": "37a9359d-ade4-4218-aad8-eaba6712f77e", "metadata": {}, "source": [ "

Image: Different Activation Functions
" ] }, { "cell_type": "markdown", "id": "53f32b90-5cc1-41fb-93a5-7776d9b3d9c2", "metadata": {}, "source": [ "In the realm of deep learning, several common activation functions are widely used due to their impact on network training and performance. Here are some prevalent activation functions:\n", "\n", "**1. ReLU (Rectified Linear Activation):**\n", "ReLU is one of the most commonly used activation functions. It sets negative input values to zero and keeps positive values unchanged:\n", "\n", "$$\n", "f(x) = \\max(0, x) \n", "$$\n", "\n", "ReLU effectively mitigates the vanishing gradient problem and computes faster. However, it can cause neurons to \"die\" by setting negative outputs to zero.\n", "\n", "**2. Sigmoid Function:**\n", "The sigmoid function maps inputs to the range (0, 1):\n", "\n", "$$\n", "f(x) = \\frac{1}{1 + e^{-x}}\n", "$$\n", "\n", "It's often used in binary classification problems at the output layer but suffers from the vanishing gradient problem in deep neural networks.\n", "\n", "**3. Tanh Function:**\n", "Tanh function maps inputs to the range (-1, 1):\n", "\n", "$$\n", "f(x) = \\frac{e^x - e^{-x}}{e^x + e^{-x}}\n", "$$\n", "\n", "Similar to the sigmoid function but outputs in the range (-1, 1), aiding zero-centered data. However, it also faces the issue of vanishing gradients.\n", "\n", "**4. Leaky ReLU:**\n", "Leaky ReLU is an improvement over ReLU, allowing a small slope for negative input values:\n", "\n", "$$\n", "f(x) = \\begin{cases} x & \\text{if } x > 0 \\\\ \\alpha x & \\text{otherwise} \\end{cases}\n", "$$\n", "\n", "Where $\\alpha$ is a small positive number. This function addresses the \"neuron death\" problem in ReLU.\n", "\n", "**5. ELU (Exponential Linear Unit):**\n", "ELU is similar to Leaky ReLU but allows a slightly negative slope for negative values and tends to zero-center:\n", "\n", "$$\n", "f(x) = \\begin{cases} x & \\text{if } x > 0 \\\\ \\alpha (e^x - 1) & \\text{otherwise} \\end{cases}\n", "$$\n", " \n", "ELU helps reduce the vanishing gradient problem and improves training stability in some scenarios.\n", "\n", "Each activation function has its advantages and drawbacks. Their performance can vary in different network architectures and tasks. Research in deep learning continually explores new activation functions to enhance training effectiveness and network performance." ] }, { "cell_type": "markdown", "id": "5137abb6-81fa-4ab6-aa57-28d215074735", "metadata": {}, "source": [ "**Bias:**\n", "The role of bias is to shift the value produced by the activation function. Its role is similar to the role of a constant in a linear function.\n", "\n", "When multiple neurons are stacked together in a row, they constitute a layer, and multiple layers piled next to each other are called a multi-layer neural network.\n", "\n", "We've described the main components of this type of structure below." ] }, { "cell_type": "markdown", "id": "19aeaa65-4f77-47cf-8c98-9ce0ba304908", "metadata": {}, "source": [ "

Image: Multi-layer neural network
" ] }, { "cell_type": "markdown", "id": "bfb44613-7e13-41a9-a621-bec3327a9ed2", "metadata": {}, "source": [ "**Input Layer:**\n", "The data that we feed to the model is loaded into the input layer from external sources like a CSV file or a web service. It is the only visible layer in the complete Neural Network architecture that passes the complete information from the outside world without any computation.\n", "\n", "**Hidden Layers:**\n", "The hidden layers are what makes deep learning what it is today. They are intermediate layers that do all the computations and extract the features from the data.\n", "\n", "There can be multiple interconnected hidden layers that account for searching different hidden features in the data. For example, in image processing, the first hidden layers are responsible for higher-level features like edges, shapes, or boundaries. On the other hand, the later hidden layers perform more complicated tasks like identifying complete objects (a car, a building, a person).\n", "\n", "**Output Layer:**\n", "The output layer takes input from preceding hidden layers and comes to a final prediction based on the model’s learnings. It is the most important layer where we get the final result.\n", "\n", "In the case of classification/regression models, the output layer generally has a single node. However, it is completely problem-specific and dependent on the way the model was built." ] }, { "cell_type": "markdown", "id": "98b6b84a-b5cd-4cd9-89b6-3945ba0505ed", "metadata": {}, "source": [ "## Standard Neural Networks" ] }, { "cell_type": "markdown", "id": "b4e9a68e-2b30-44b6-b651-d80e871f5ddc", "metadata": {}, "source": [ "The following are several standard types of neural networks" ] }, { "cell_type": "markdown", "id": "6a8abe53-1148-41cc-9651-2bcb0a3e916e", "metadata": {}, "source": [ "### The Perceptron\n", "Perceptron is the simplest Neural Network architecture.\n", "\n", "It is a type of Neural Network that takes a number of inputs, applies certain mathematical operations on these inputs, and produces an output. It takes a vector of real values inputs, performs a linear combination of each attribute with the corresponding weight assigned to each of them.\n", "\n", "The weighted input is summed into a single value and passed through an activation function. \n", "\n", "These perceptron units are combined to form a bigger Artificial Neural Network architecture." ] }, { "cell_type": "markdown", "id": "02af3b8a-5251-4fff-8d16-87fbf1d7a87b", "metadata": {}, "source": [ "### Feed-Forward Networks\n", "Perceptron represents how a single neuron works.\n", "\n", "But—\n", "\n", "What about a series of perceptrons stacked in a row and piled in different layers? How does the model learn then?\n", "\n", "It is a multi-layer Neural Network, and, as the name suggests, the information is passed in the forward direction—from left to right.\n", "\n", "In the forward pass, the information comes inside the model through the input layer, passes through the series of hidden layers, and finally goes to the output layer. This Neural Networks architecture is forward in nature—the information does not loop with two hidden layers.\n", "\n", "The later layers give no feedback to the previous layers. The basic learning process of Feed-Forward Networks remain the same as the perceptron." ] }, { "cell_type": "markdown", "id": "84f7dc96-ab00-4b00-827a-5790918ed462", "metadata": {}, "source": [ "### Residual Networks (ResNet)\n", "Now that you know more about the Feed-Forward Networks, one question might have popped up in your head—how to decide on the number of layers in our neural network architecture?\n", "\n", "A naive answer would be: The greater the number of hidden layers, the better is the learning process.\n", "\n", "More layers enrich the levels of features.\n", "\n", "But—\n", "\n", "Is that so?\n", "\n", "Very deep Neural Networks are extremely difficult to train due to vanishing and exploding gradient problems.\n", "\n", "ResNets provide an alternate pathway for data to flow to make the training process much faster and easier.\n", "\n", "This is different from the feed-forward approach of earlier Neural Networks architectures. \n", "\n", "The core idea behind ResNet is that a deeper network can be made from a shallow network by copying weight from the shallow counterparts using identity mapping.\n", "\n", "The data from previous layers is fast-forwarded and copied much forward in the Neural Networks. This is what we call skip connections first introduced in Residual Networks to resolve vanishing gradients." ] }, { "cell_type": "markdown", "id": "c59bd0fe-75ec-473a-aacc-cac0e6eccb35", "metadata": {}, "source": [ "## Code\n", "Now, let's train a neural network model for Heart Disease Classification as an example to help you better understand.\n", "\n", "First, let's import the necessary libraries." ] }, { "cell_type": "code", "execution_count": 1, "id": "ed7d3c46-b1dd-43c2-9e72-09c929bccba2", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "import keras\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.metrics import confusion_matrix, accuracy_score\n", "from keras.models import Sequential\n", "from keras.layers import Dense\n", "from ucimlrepo import fetch_ucirepo\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "id": "214903a7-3974-4428-b550-2a053adde3b3", "metadata": {}, "source": [ "Let's start by importing the dataset from the UCI ML Repository. The UCI ML Repository is a comprehensive resource for machine learning datasets. Within this repository is a dataset specifically focused on heart disease, containing diverse data from multiple cardiac patients.However, it encompasses various types of heart conditions. For our task of building a binary classification neural network, we'll unify the heart disease types into a single category." ] }, { "cell_type": "code", "execution_count": 2, "id": "a3dc3e5b-a784-449d-b93f-336139d6d1f6", "metadata": {}, "outputs": [], "source": [ "# Fetch dataset from UCI ML Repository (Heart Disease dataset)\n", "heart_disease = fetch_ucirepo(id=45)\n", "\n", "# Suppress warnings about chained assignments in Pandas\n", "pd.options.mode.chained_assignment = None\n", "\n", "# Preprocess the dataset\n", "# Extract features and targets\n", "X = heart_disease.data.features \n", "y = heart_disease.data.targets\n", "\n", "# Convert multiclass labels to binary (0 or 1)\n", "y[y != 0] = 1" ] }, { "cell_type": "markdown", "id": "8e9ee95f-8cc1-47c6-9a01-7bf9deab1382", "metadata": {}, "source": [ "Now, let's perform specific encoding on the 'cp', 'slope', 'thal', and 'restecg' columns in the dataset to facilitate the handling of categorical data by the neural network model. " ] }, { "cell_type": "code", "execution_count": 3, "id": "ae66405b-04c8-4072-a09b-32a8f047ec8a", "metadata": {}, "outputs": [], "source": [ "# Perform one-hot encoding on categorical variables\n", "chest_pain = pd.get_dummies(X['cp'], prefix='cp', drop_first=True)\n", "X = pd.concat([X, chest_pain], axis=1)\n", "X.drop(['cp'], axis=1, inplace=True)\n", "\n", "# More one-hot encoding\n", "sp = pd.get_dummies(X['slope'], prefix='slope')\n", "th = pd.get_dummies(X['thal'], prefix='thal')\n", "rest_ecg = pd.get_dummies(X['restecg'], prefix='restecg')\n", "\n", "# Concatenate encoded columns\n", "frames = [X, sp, th, rest_ecg]\n", "X = pd.concat(frames, axis=1)\n", "X.drop(['slope', 'thal', 'restecg'], axis=1, inplace=True)" ] }, { "cell_type": "markdown", "id": "9e3ac8a7-bcb9-4a3a-9198-6d2023763ba4", "metadata": {}, "source": [ "Let's proceed with the final steps in data processing: splitting the data into training and testing sets, normalizing the data, and filling missing values with preceding data." ] }, { "cell_type": "code", "execution_count": 4, "id": "0df1f6ea-0162-4ee2-9579-e8057781179f", "metadata": {}, "outputs": [], "source": [ "# Split dataset into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n", "\n", "# Feature scaling using StandardScaler\n", "sc = StandardScaler()\n", "X_train = sc.fit_transform(X_train)\n", "X_test = sc.transform(X_test)\n", "\n", "# Handle missing values by forward filling\n", "X_train = pd.DataFrame(X_train).fillna(method='ffill')\n", "X_test = pd.DataFrame(X_test).fillna(method='ffill')" ] }, { "cell_type": "markdown", "id": "649a025e-84b1-4bcb-b008-05cbf1a551a9", "metadata": {}, "source": [ "Okay, we can now build the neural network structure and choose the activation function for each layer.\n", "\n", "From my code, you can see I've constructed an input layer with 11 units, limiting the input features to 21. There are three hidden layers, and an output layer with just one unit, using the sigmoid activation function. It will directly output the probability of a patient having heart disease." ] }, { "cell_type": "code", "execution_count": 5, "id": "824e4151-5dab-41cf-8c8d-b53f0448fa79", "metadata": {}, "outputs": [], "source": [ "# Build the neural network model\n", "classifier = Sequential()\n", "classifier.add(Dense(units=11, kernel_initializer='uniform', activation='relu', input_dim=21))\n", "classifier.add(Dense(units=10, kernel_initializer='uniform', activation='relu'))\n", "classifier.add(Dense(units=10, kernel_initializer='uniform', activation='relu'))\n", "classifier.add(Dense(units=5, kernel_initializer='uniform', activation='relu'))\n", "classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))" ] }, { "cell_type": "markdown", "id": "6ca77548-cbe4-4daf-86e1-749f8aee3a66", "metadata": {}, "source": [ "It is time for training our neural network." ] }, { "cell_type": "code", "execution_count": 10, "id": "6afa0b7b-80b0-431a-9f09-ee31aae1ef08", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2/2 [==============================] - 0s 2ms/step\n" ] } ], "source": [ "# Compile the model\n", "classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n", "\n", "# Train the model\n", "classifier.fit(X_train, y_train, batch_size=10, epochs=100, verbose=0)\n", "\n", "# Predict on the test set\n", "y_pred = classifier.predict(X_test)" ] }, { "cell_type": "markdown", "id": "399434e5-aa76-4926-8c2f-f1394c427a6f", "metadata": {}, "source": [ "Let's examine the model's confusion matrix and accuracy to assess its performance." ] }, { "cell_type": "code", "execution_count": 11, "id": "ecb94414-444d-4790-876e-2b96f1a711c7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "accuracy of the model: 0.7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K6+8suB1wJ5SKpXivvvui9NPP73oKfQR7mzQK95///1Ys2ZNTJs2bcexqqqqmDZtWqxcubLAZQBkExv0ira2tujq6or6+vpux+vr66OlpaWgVQD0BrEBAKQSG/SKESNGRHV1dbS2tnY73traGg0NDQWtAqA3iA16xaBBg6KxsTGWL1++49j27dtj+fLlceyxxxa4DIBsA4oewN6jqakpZs6cGRMnToxjjjkmbrnllujo6Ihzzjmn6GnAZ7Rly5Z45ZVXdrx+/fXXY+3atTF8+PAYO3ZsgcvoC/zpK71q/vz5MXfu3GhpaYmjjjoq5s2bF5MmTSp6FvAZrVixIk466aSdjs+cOTPuuuuu3h9EnyI2AIBUntkAAFKJDQAgldgAAFKJDQAgldgAAFKJDQAgldgAAFKJDQAgldgAAFKJDQAgldgAAFKJDQAg1f8AAgXg3cWxorsAAAAASUVORK5CYII=", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Create confusion matrix\n", "cm = confusion_matrix(y_test, y_pred.round())\n", "\n", "# Visualize confusion matrix using seaborn heatmap\n", "sns.heatmap(cm, annot=True, cmap=\"Blues\", fmt=\"d\", cbar=False)\n", "\n", "# Calculate and print accuracy score\n", "ac = accuracy_score(y_test, y_pred.round())\n", "print('accuracy of the model: ', ac)" ] }, { "cell_type": "markdown", "id": "fb10720d-9756-4ce8-a284-18a882e11f64", "metadata": {}, "source": [ "## Your turn! 🚀\n", "\n", "Try the exercises about [neural networks classify 15 fruits](../../assignments/deep-learning/nn-classify-15-fruits-assignment.ipynb) and [neural networks for classification](../../assignments/deep-learning/nn-for-classification-assignment.ipynb)." ] }, { "cell_type": "markdown", "id": "88ea6dc6", "metadata": {}, "source": [ "HIGHLY RECOMMENDED LECTURE : [The spelled-out intro to neural networks and backpropagation: building micrograd](https://www.youtube.com/watch?v=VMj-3S1tku0&t=5433s) by [Andrej Karpathy](https://karpathy.ai) It only assumes basic knowledge of Python and a vague recollection of calculus from high school. [Github Repo](https://github.com/karpathy/micrograd)" ] }, { "cell_type": "markdown", "id": "7f5b2350-2265-4919-9503-8f191fb33a52", "metadata": {}, "source": [ "## Acknowledgments\n", "\n", "Thanks to [Pragati Baheti](https://www.v7labs.com/authors/pragati-baheti) and [Rajesh kumar jha](https://www.kaggle.com/rajeshjnv) for creating the open-source course [The Essential Guide to Neural Network Architectures](https://www.v7labs.com/blog/neural-network-architectures-guide#standard-neural-networks) and [Heart Disease Classification - Neural Network](https://www.kaggle.com/code/rajeshjnv/heart-disease-classification-neural-network#Loading-appropriate-libraries), as well as [Andrej Karpathy](https://karpathy.ai) for sharing his brillant thoughts. It inspires the majority of the content in this chapter.\n" ] } ], "metadata": { "kernelspec": { "display_name": "open-machine-learning-jupyter-book", "language": "python", "name": "open-machine-learning-jupyter-book" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }