{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BUILDING NEURAL NETWORKS FROM SCRATCH PART 1: CODING NEURONS AND LAYERS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>CODING OUR FIRST NEURON: 3 INPUTS</b>\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "889de6b2-5333-4c4d-a82a-aa820b0eed8a.png": {
     "image/png": "[encoded data, 446748 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.14.13 PM.png](attachment:889de6b2-5333-4c4d-a82a-aa820b0eed8a.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.3\n"
     ]
    }
   ],
   "source": [
    "inputs = [1, 2, 3]\n",
    "weights = [0.2, 0.8, -0.5]\n",
    "bias = 2\n",
    "\n",
    "outputs = (inputs[0]*weights[0] + inputs[1]*weights[1] + inputs[2]*weights[2] + bias)\n",
    "\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>CODING OUR SECOND NEURON: 4 INPUTS</b>\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "96db3f0b-9cc8-4d44-ae28-29624144f3f5.png": {
     "image/png": "[encoded data, 578292 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.14.35 PM.png](attachment:96db3f0b-9cc8-4d44-ae28-29624144f3f5.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.8\n"
     ]
    }
   ],
   "source": [
    "inputs = [1.0, 2.0, 3.0, 2.5]\n",
    "weights = [0.2, 0.8, -0.5, 1.0]\n",
    "bias = 2.0\n",
    "output = (inputs[0]*weights[0] +\n",
    " inputs[1]*weights[1] +\n",
    " inputs[2]*weights[2] +\n",
    " inputs[3]*weights[3] + bias)\n",
    "\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>CODING OUR FIRST LAYER</b>\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "0a573927-c2fe-415b-9534-85f6510f635b.png": {
     "image/png": "[encoded data, 1223084 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.15.01 PM.png](attachment:0a573927-c2fe-415b-9534-85f6510f635b.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.8, 1.21, 2.385]\n"
     ]
    }
   ],
   "source": [
    "inputs = [1, 2, 3, 2.5]\n",
    "\n",
    "weights = [[0.2, 0.8, -0.5, 1],\n",
    " [0.5, -0.91, 0.26, -0.5],\n",
    " [-0.26, -0.27, 0.17, 0.87]]\n",
    "\n",
    "weights1 = weights[0] #LIST OF WEIGHTS ASSOCIATED WITH 1ST NEURON : W11, W12, W13, W14\n",
    "weights2 = weights[1] #LIST OF WEIGHTS ASSOCIATED WITH 2ND NEURON : W21, W22, W23, W24\n",
    "weights3 = weights[2] #LIST OF WEIGHTS ASSOCIATED WITH 3RD NEURON : W31, W32, W33, W34\n",
    "\n",
    "biases = [2, 3, 0.5]\n",
    "\n",
    "bias1 = 2\n",
    "bias2 = 3\n",
    "bias3 = 0.5\n",
    "\n",
    "outputs = [\n",
    " # Neuron 1:\n",
    " inputs[0]*weights1[0] +\n",
    " inputs[1]*weights1[1] +\n",
    " inputs[2]*weights1[2] +\n",
    " inputs[3]*weights1[3] + bias1,\n",
    " # Neuron 2:\n",
    " inputs[0]*weights2[0] +\n",
    " inputs[1]*weights2[1] +\n",
    " inputs[2]*weights2[2] +\n",
    " inputs[3]*weights2[3] + bias2,\n",
    " # Neuron 3:\n",
    " inputs[0]*weights3[0] +\n",
    " inputs[1]*weights3[1] +\n",
    " inputs[2]*weights3[2] +\n",
    " inputs[3]*weights3[3] + bias3]\n",
    "\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-danger\">\n",
    "\n",
    "USING LOOPS FOR BETTER AND EASIER CODING</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.8, 1.21, 2.385]\n"
     ]
    }
   ],
   "source": [
    "inputs = [1, 2, 3, 2.5]\n",
    "\n",
    "##LIST OF WEIGHTS\n",
    "weights = [[0.2, 0.8, -0.5, 1],\n",
    " [0.5, -0.91, 0.26, -0.5],\n",
    " [-0.26, -0.27, 0.17, 0.87]]\n",
    "\n",
    "##LIST OF BIASES\n",
    "biases = [2, 3, 0.5]\n",
    "\n",
    "# Output of current layer\n",
    "layer_outputs = []\n",
    "\n",
    "# For each neuron\n",
    "for neuron_weights, neuron_bias in zip(weights, biases):\n",
    " # Zeroed output of given neuron\n",
    " neuron_output = 0\n",
    " # For each input and weight to the neuron\n",
    " for n_input, weight in zip(inputs, neuron_weights):\n",
    " # Multiply this input by associated weight\n",
    " # and add to the neuron's output variable\n",
    "   neuron_output += n_input*weight ## W31*X1 + W32*X2 + W33*X3 + W34*X4\n",
    "   # Add bias\n",
    " neuron_output += neuron_bias ## ## W31*X1 + W32*X2 + W33*X3 + W34*X4 + B3\n",
    " # Put neuron's result to the layer's output list\n",
    " layer_outputs.append(neuron_output)\n",
    "print(layer_outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-success\">\n",
    "<b>USING NUMPY</b>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-warning\">\n",
    "SINGLE NEURON USING NUMPY\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "15dc20d7-a498-4291-a8d1-546adb19f1df.png": {
     "image/png": "[encoded data, 1204688 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.21.35 PM.png](attachment:15dc20d7-a498-4291-a8d1-546adb19f1df.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.8\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "inputs = [1.0, 2.0, 3.0, 2.5]\n",
    "weights = [0.2, 0.8, -0.5, 1.0]\n",
    "bias = 2.0\n",
    "outputs = np.dot(weights, inputs) + bias\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-warning\">\n",
    "LAYER OF NEURONS USING NUMPY\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "1baffeb8-236f-4476-a62a-2faf2ef006af.png": {
     "image/png": "[encoded data, 1149836 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.28.51 PM.png](attachment:1baffeb8-236f-4476-a62a-2faf2ef006af.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "##  In plain Python, we wrote this as a list of lists. With NumPy, this will be a 2-dimensional array, which we’ll call a matrix. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.8   1.21  2.385]\n"
     ]
    }
   ],
   "source": [
    "inputs = [1.0, 2.0, 3.0, 2.5]\n",
    "weights = [[0.2, 0.8, -0.5, 1],\n",
    " [0.5, -0.91, 0.26, -0.5],\n",
    " [-0.26, -0.27, 0.17, 0.87]]\n",
    "biases = [2.0, 3.0, 0.5]\n",
    "\n",
    "# A dot product of a matrix and a vector results in a list of dot products. \n",
    "#The np.dot() method treats the matrix as a list of vectors and performs a dot product of each of those vectors with the other vector\n",
    "\n",
    "layer_outputs = np.dot(weights, inputs) + biases\n",
    "print(layer_outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-warning\">\n",
    "LAYER OF NEURONS AND BATCH OF DATA USING NUMPY\n",
    "</div>"
   ]
  },
  {
   "attachments": {
    "a823d9f6-e42c-451f-adee-be153cc12000.png": {
     "image/png": "[encoded data, 1116740 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.40.55 PM.png](attachment:a823d9f6-e42c-451f-adee-be153cc12000.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-danger\">\n",
    "\n",
    "NEED TO TAKE TRANSPOSE OF WEIGHT MATRIX</div>"
   ]
  },
  {
   "attachments": {
    "45a2d43b-5bdc-4752-8e55-9a60c2e459ca.png": {
     "image/png": "[encoded data, 1099828 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Screenshot 2024-05-16 at 12.41.11 PM.png](attachment:45a2d43b-5bdc-4752-8e55-9a60c2e459ca.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4.8    1.21   2.385]\n",
      " [ 8.9   -1.81   0.2  ]\n",
      " [ 1.41   1.051  0.026]]\n"
     ]
    }
   ],
   "source": [
    "inputs = [[1.0, 2.0, 3.0, 2.5], [2.0, 5.0, -1.0, 2.0], [-1.5, 2.7, 3.3, -0.8]]\n",
    "weights = [[0.2, 0.8, -0.5, 1],\n",
    " [0.5, -0.91, 0.26, -0.5],\n",
    " [-0.26, -0.27, 0.17, 0.87]]\n",
    "biases = [2.0, 3.0, 0.5]\n",
    "\n",
    "# NOTE: WE CAN'T TRANSPOSE LISTS IN PYTHON, SO WE HAVE THE CONVERT THE WEIGHTS MATRIX INTO AN ARRAY FIRST\n",
    "outputs = np.dot(inputs, np.array(weights).T) + biases\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
