From cb4e946281f81929ab95c88d2103e5dcb45037db Mon Sep 17 00:00:00 2001 From: ramanjorewal <120960111+ramanjorewal@users.noreply.github.com> Date: Fri, 20 Jan 2023 23:58:40 +0530 Subject: [PATCH] Add files via upload --- .../210823_ RamanJorewal Assignment 4.ipynb | 1179 +++++++++++++++++ 1 file changed, 1179 insertions(+) create mode 100644 210823_Raman_Jorewal/210823_ RamanJorewal Assignment 4.ipynb diff --git a/210823_Raman_Jorewal/210823_ RamanJorewal Assignment 4.ipynb b/210823_Raman_Jorewal/210823_ RamanJorewal Assignment 4.ipynb new file mode 100644 index 0000000..fbfb42d --- /dev/null +++ b/210823_Raman_Jorewal/210823_ RamanJorewal Assignment 4.ipynb @@ -0,0 +1,1179 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "f0685c8c", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a0cebd1d", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_csv('data.csv')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d689d320", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ATVAPRHPE
014.9641.761024.0773.17463.26
125.1862.961020.0459.08444.37
25.1139.401012.1692.14488.56
320.8657.321010.2476.64446.48
410.8237.501009.2396.62473.90
..................
956316.6549.691014.0191.00460.03
956413.1939.181023.6766.78469.62
956531.3274.331012.9236.48429.57
956624.4869.451013.8662.39435.74
956721.6062.521017.2367.87453.28
\n", + "

9568 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " AT V AP RH PE\n", + "0 14.96 41.76 1024.07 73.17 463.26\n", + "1 25.18 62.96 1020.04 59.08 444.37\n", + "2 5.11 39.40 1012.16 92.14 488.56\n", + "3 20.86 57.32 1010.24 76.64 446.48\n", + "4 10.82 37.50 1009.23 96.62 473.90\n", + "... ... ... ... ... ...\n", + "9563 16.65 49.69 1014.01 91.00 460.03\n", + "9564 13.19 39.18 1023.67 66.78 469.62\n", + "9565 31.32 74.33 1012.92 36.48 429.57\n", + "9566 24.48 69.45 1013.86 62.39 435.74\n", + "9567 21.60 62.52 1017.23 67.87 453.28\n", + "\n", + "[9568 rows x 5 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "cc3f66b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(9568, 5)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d6654f80", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ATVAPRHPE
014.9641.761024.0773.17463.26
125.1862.961020.0459.08444.37
25.1139.401012.1692.14488.56
320.8657.321010.2476.64446.48
410.8237.501009.2396.62473.90
\n", + "
" + ], + "text/plain": [ + " AT V AP RH PE\n", + "0 14.96 41.76 1024.07 73.17 463.26\n", + "1 25.18 62.96 1020.04 59.08 444.37\n", + "2 5.11 39.40 1012.16 92.14 488.56\n", + "3 20.86 57.32 1010.24 76.64 446.48\n", + "4 10.82 37.50 1009.23 96.62 473.90" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3a3d1a71", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ATVAPRHPE
count9568.0000009568.0000009568.0000009568.0000009568.000000
mean19.65123154.3058041013.25907873.308978454.365009
std7.45247312.7078935.93878414.60026917.066995
min1.81000025.360000992.89000025.560000420.260000
25%13.51000041.7400001009.10000063.327500439.750000
50%20.34500052.0800001012.94000074.975000451.550000
75%25.72000066.5400001017.26000084.830000468.430000
max37.11000081.5600001033.300000100.160000495.760000
\n", + "
" + ], + "text/plain": [ + " AT V AP RH PE\n", + "count 9568.000000 9568.000000 9568.000000 9568.000000 9568.000000\n", + "mean 19.651231 54.305804 1013.259078 73.308978 454.365009\n", + "std 7.452473 12.707893 5.938784 14.600269 17.066995\n", + "min 1.810000 25.360000 992.890000 25.560000 420.260000\n", + "25% 13.510000 41.740000 1009.100000 63.327500 439.750000\n", + "50% 20.345000 52.080000 1012.940000 74.975000 451.550000\n", + "75% 25.720000 66.540000 1017.260000 84.830000 468.430000\n", + "max 37.110000 81.560000 1033.300000 100.160000 495.760000" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "fbf9cb23", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AT 0\n", + "V 0\n", + "AP 0\n", + "RH 0\n", + "PE 0\n", + "dtype: int64" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6cbfdb82", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['AT', 'V', 'AP', 'RH', 'PE'], dtype='object')\n" + ] + } + ], + "source": [ + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "463216fd", + "metadata": {}, + "outputs": [], + "source": [ + "# spiliiting the data\n", + "# taking 80 % for training and 10% for validation and 10% for the test" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7c8c5ea7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(9568, 5)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data=df.values\n", + "data.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "643375a0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7654 1914\n", + "True\n", + "(9568, 4)\n" + ] + } + ], + "source": [ + "split1=int(0.8*data.shape[0])\n", + "\n", + "#x=data[:split,:4]\n", + "split2=data.shape[0]-split1\n", + "print(split1,split2)\n", + "print((split1+split2)==data.shape[0])\n", + "x=data[:,0:4]\n", + "y=data[:,4]\n", + "print(x.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "21e90bf9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1914, 4)\n" + ] + } + ], + "source": [ + "x_train=x[:split1,:4]\n", + "x_test=x[split1:,:4]\n", + "x_train.shape\n", + "print(x_test.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9fb6bb3c", + "metadata": {}, + "outputs": [], + "source": [ + "#x[:5,:]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8bd6ee0d", + "metadata": {}, + "outputs": [], + "source": [ + "#y=data[split1:,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "aadbbdf9", + "metadata": {}, + "outputs": [], + "source": [ + "y_train=y[:split1]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "b10d27a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7654,)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_train.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "63c9a73f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1914,)\n" + ] + } + ], + "source": [ + "y_test=y[split1:]\n", + "print(y_test.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "404d0570", + "metadata": {}, + "outputs": [], + "source": [ + "y_test=y_test.reshape((y_test.shape[0],1))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "63613ae2", + "metadata": {}, + "outputs": [], + "source": [ + "y_train=y_train.reshape((y_train.shape[0],1))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "abe912d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1914, 1) (7654, 1)\n" + ] + } + ], + "source": [ + "print(y_test.shape,y_train.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "016ba69d", + "metadata": {}, + "outputs": [], + "source": [ + "# implementing the neural network here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa23d983", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70e04a22", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "8a679e61", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'area' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[22], line 28\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;66;03m# The line\u001b[39;00m\n\u001b[0;32m 27\u001b[0m y_hat \u001b[38;5;241m=\u001b[39m w_1\u001b[38;5;241m*\u001b[39mx_1 \u001b[38;5;241m+\u001b[39m w_0\u001b[38;5;241m*\u001b[39mx_0\n\u001b[1;32m---> 28\u001b[0m plt\u001b[38;5;241m.\u001b[39mscatter(x_1, y, s\u001b[38;5;241m=\u001b[39m\u001b[43marea\u001b[49m, c\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m, alpha\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0.5\u001b[39m)\n\u001b[0;32m 29\u001b[0m \u001b[38;5;66;03m# We can also show the error\u001b[39;00m\n\u001b[0;32m 30\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(x_1, y_hat, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-b\u001b[39m\u001b[38;5;124m'\u001b[39m, label \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSSE= \u001b[39m\u001b[38;5;132;01m%.2f\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39mSSE(y, y_hat))\n", + "\u001b[1;31mNameError\u001b[0m: name 'area' is not defined" + ] + } + ], + "source": [ + "def SSE(y, y_hat):\n", + " return 0.50*np.sum((y- y_hat)**2)\n", + "# The linear function\n", + "def Linear(Z):\n", + " return Z\n", + "\n", + "def dEdW_1 (y, y_hat,x_1):\n", + " return np.sum(-(y - y_hat) * x_1)\n", + " \n", + "def dEdW_0 (y, y_hat):\n", + " return np.sum(-(y - y_hat))\n", + "\n", + "# The number of training data\n", + "N = 200\n", + "# 200 random samples as our data\n", + "x_1 = np.random.rand(N)\n", + "# Define the line slope and the Gaussian noise parameters\n", + "slope = 3\n", + "mu, sigma = 0, 0.1 # mean and standard deviation\n", + "intercept = np.random.normal(mu, sigma, N)\n", + "# Define the coordinates of the data points using the line equation and the added Gaussian noise \n", + "y = slope*x_1 + intercept\n", + "w_1 = np.random.uniform(-2,-3,1)\n", + "w_0 = np.random.uniform(0,2,1)\n", + "x_0 = 1\n", + "# The line\n", + "y_hat = w_1*x_1 + w_0*x_0\n", + "plt.scatter(x_1, y, s=area, c='r', alpha=0.5)\n", + "# We can also show the error\n", + "plt.plot(x_1, y_hat, '-b', label = \"SSE= %.2f\" %SSE(y, y_hat))\n", + "plt.grid()\n", + "plt.title('Initial Line')\n", + "plt.xlabel('x')\n", + "plt.ylabel('y')\n", + "plt.legend()\n", + "plt.ylim(-3,5)\n", + "plt.show()\n", + "\n", + "epoch = 300\n", + "# Learning rate\n", + "eta = 0.001\n", + "# This list will save the error as training keeps going\n", + "E = []\n", + "for ep in range(epoch):\n", + " ##################################################### Forward pass starts #################################\n", + " Z = w_1 * x_1 + w_0*x_0\n", + " y_hat = Linear(Z)\n", + " error = SSE(y, y_hat)\n", + " E.append(error) # We save the error per epoch\n", + " ##################################################### Back propagation starts #############################\n", + " # Here we call our functions for computing the derivatives\n", + " dEdw_1 = dEdW_1(y, y_hat, x_1)\n", + " dEdw_0 = dEdW_0(y, y_hat)\n", + " # We update our parameters (i.e., the weights)\n", + " w_1 = w_1 - eta*dEdw_1\n", + " w_0 = w_0 - eta*dEdw_0\n", + " # Simply every 20 epochs plot the line using the updated weights\n", + " if ep % 20 == 0:\n", + " # First plot our data\n", + " plt.scatter(x_1, y, s=area, c='r', alpha=0.5)\n", + " # Now plot the improved line\n", + " plt.plot(x_1, y_hat, '-b', label=\"SSE= %.2f\" %error + '\\n' + \"Epoch Number= %d\" %(ep))\n", + " plt.title('Initial Line')\n", + " plt.xlabel('x (Our Data)')\n", + " plt.ylabel('y_hat (Network Outputs)')\n", + " plt.legend()\n", + " plt.ylim(-3, 5)\n", + " plt.grid()\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c1c20fd", + "metadata": {}, + "outputs": [], + "source": [ + "def SSE(y, y_hat):\n", + " return 0.50*np.sum((y - y_hat)**2)\n", + "def Linear(Z):\n", + " return Z\n", + "def dEdW_1 (y, y_hat,x_1):\n", + " return np.sum(-(y - y_hat) * x_1)\n", + "def dEdW_0 (y, y_hat):\n", + " return np.sum(-(y - y_hat))\n", + "N = 200\n", + "slope = 3\n", + "mu, sigma = 0, 0.1 # mean and standard deviation\n", + "intercept = np.random.normal(mu, sigma, N)\n", + "x_1 = np.random.rand(N)\n", + "y = slope*x_1 + intercept\n", + "area = np.pi*10\n", + "w_1 = np.random.uniform(-2,-3,1)\n", + "w_0 = np.random.uniform(0,2,1)\n", + "x_0 = 1\n", + "y_hat = w_1*x_1 + w_0*x_0\n", + "epoch = 300\n", + "eta = 0.001\n", + "E = []\n", + "for ep in range(epoch):\n", + " Z = w_1 * x_1 + w_0*x_0\n", + " y_hat = Linear(Z)\n", + " error = SSE(y, y_hat)\n", + " E.append(error)\n", + " dEdw_1 = dEdW_1(y, y_hat, x_1)\n", + " dEdw_0 = dEdW_0(y, y_hat)\n", + " w_1 = w_1 - eta*dEdw_1\n", + " w_0 = w_0 - eta*dEdw_0\n", + " if ep % 20 == 0:\n", + " plt.scatter(x_1, y, s=area, c='r', alpha=0.5)\n", + " plt.plot(x_1, y_hat, '-b', label=\"SSE= %.2f\" %error + '\\n' + \"Epoch Number= %d\" %(ep))\n", + " plt.xlabel('x (Our Data)')\n", + " plt.ylabel('y_hat (Network Outputs)')\n", + " plt.legend()\n", + " plt.ylim(-3, 5)\n", + " plt.grid()\n", + " plt.show()\n", + "plt.plot(E, 'r')\n", + "plt.grid()\n", + "plt.title(\"Error\")\n", + "plt.xlabel(\"Epochs\")\n", + "plt.ylabel(\"SSE\")\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f141e7e7", + "metadata": {}, + "outputs": [], + "source": [ + "class neuralnetwork:\n", + " def __init__(self,input_size,layers,output_size):\n", + " np.random.seed(0)\n", + " model={}\n", + " model['w1']=np.random.randn(input_size,layers[0]) # size of weight matrix is going to this check your notes\n", + " # this weight matrix is randomly initialized by this \n", + " # it is for the first hidden layer\n", + " model['b1']=np.zeros((1,layers[0])) # it will be the bias vector of the first layer\n", + " \n", + " # second layer\n", + " model['w2']=np.random.randn(layers[0],output_size)\n", + " model['b2']=np.zeros((1,output_size))\n", + " \n", + " # third layer\n", + " # model['w3']=np.random.randn(layers[1],output_size)\n", + " # model['b3']=np.zeros((1,output_size))\n", + " \n", + " # this will be the architecture of the neural network\n", + " self.model=model\n", + " def forward(self,x): # this function \n", + " \n", + " w1,w2=self.model[\"w1\"],self.model[\"w2\"] # these are the unpacked wieghts \n", + " b1,b2=self.model[\"b1\"],self.model[\"b2\"]# unpacking the \n", + " \n", + " z1=np.dot(x,w1)+b1\n", + " a1=z1 # same as sigmoid function but produces output in range(-1,1)\n", + " \n", + " z2=np.dot(a1,w2)+b2\n", + " y_=z2\n", + " \n", + " #z3=np.dot(a2,w3)+b3\n", + " #y_=softmax(z3)\n", + " \n", + " self.activation_outputs=(a1,y_) # this will be a tupple returned by this function of activation\n", + " # values calculated at each layer of this network\n", + " return (y_) # final output (mxc) no. of classes x no. of examples\n", + " \n", + " def backward(self,x,y,lr=0.001):\n", + " w1,w2=self.model[\"w1\"],self.model[\"w2\"] # these are the unpacked wieghts \n", + " b1,b2=self.model[\"b1\"],self.model[\"b2\"]\n", + " \n", + " a1,y_=self.activation_outputs\n", + " # computing error at the final layer (last layer) output layer\n", + " # y_ \n", + " delta2= y_-y # y is the matrix containing the each of the hot vector where as y_ is the matrix pred\n", + " m=x.shape[0]# m rows and n features\n", + " \n", + " \n", + " dw2=np.dot(a1,delta2)\n", + " db2=np.sum(delta2,axis=0)/float(m)\n", + " \n", + " #delta2=np.dot(delta3,w3.T) # as calculated in the notes \n", + " #dw2=np.dot(a1.T,delta2)\n", + " #db2=np.sum(delta2,axis=0)/float(m)\n", + " # because we doing back-propagation\n", + " \n", + " delta1=np.dot(delta2,w1.T) # as calculated in the notes \n", + " dw1=np.dot(x.T,delta1)\n", + " db1=np.sum(delta1,axis=0)/float(m)\n", + " \n", + " # update the model parameters using gradient descent\n", + " self.model[\"w1\"]-= lr*dw1\n", + " self.model[\"b1\"]-=lr*db1\n", + " \n", + " self.model[\"w2\"]-= lr*dw2\n", + " self.model[\"b2\"]-=lr*db2\n", + " \n", + " #self.model[\"w3\"]-= lr*dw3\n", + " #self.model[\"b3\"]-=lr*db3\n", + " \n", + " \n", + " def predict(self,x):\n", + " y_out=self.forward(x)\n", + " return y_out\n", + " \n", + " def summary(self):\n", + " \n", + " w1,w2=self.model[\"w1\"],self.model[\"w2\"] # these are the unpacked wieghts \n", + " #b1,b2,b3=self.model(\"b1\"),self.model(\"b2\"),self.model(\"b3\")\n", + " a1,y_=self.activation_outputs\n", + " \n", + " print(\"w1 \",w1.shape)\n", + " print(\"A1 \",a1.shape)\n", + " \n", + " #print(\"w2 \",w2.shape)\n", + " #print(\"A2 \",a1.shape)\n", + " \n", + " print(\"w2 \",w2.shape)\n", + " print(\"y_ \",y_.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f570f086", + "metadata": {}, + "outputs": [], + "source": [ + "model=neuralnetwork(x_train.shape[1],[10],y_train.shape[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e2429292", + "metadata": {}, + "outputs": [], + "source": [ + "x_train.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6c9fec6", + "metadata": {}, + "outputs": [], + "source": [ + "y_=model.forward(x_train)\n", + "y.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3717753b", + "metadata": {}, + "outputs": [], + "source": [ + "x_train.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40b77ad6", + "metadata": {}, + "outputs": [], + "source": [ + "x_train1.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d1f15f9", + "metadata": {}, + "outputs": [], + "source": [ + "data.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92c68718", + "metadata": {}, + "outputs": [], + "source": [ + "y_train.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c3a099b", + "metadata": {}, + "outputs": [], + "source": [ + "def sigmoid(x):\n", + " return(1/(1 + np.exp(-x)))\n", + " \n", + "# Creating the Feed forward neural network\n", + "# 1 Input layer(1, 30)\n", + "# 1 hidden layer (1, 5)\n", + "# 1 output layer(3, 3)\n", + " \n", + "def f_forward(x, w1, w2):\n", + " # hidden\n", + " z1 = x.dot(w1)# input from layer 1\n", + " a1 = z1# out put of layer 2\n", + " \n", + " # Output layer\n", + " z2 = a1.dot(w2)# input of out layer\n", + " a2 = z2# output of out layer\n", + " return(a2)\n", + " \n", + "# initializing the weights randomly\n", + "def generate_wt(x, y):\n", + " l =[]\n", + " for i in range(x * y):\n", + " l.append(np.random.randn())\n", + " return(np.array(l).reshape(x, y))\n", + " \n", + "# for loss we will be using mean square error(MSE)\n", + "def loss(out, Y):\n", + " s =(np.square(out-Y))\n", + " s = np.sum(s)/len(y)\n", + " return(s)\n", + " \n", + "# Back propagation of error\n", + "def back_prop(x, y, w1, w2, alpha):\n", + " \n", + " # hidden layer\n", + " z1 = x.dot(w1)# input from layer 1\n", + " a1 = z1# output of layer 2\n", + " \n", + " # Output layer\n", + " z2 = a1.dot(w2)# input of out layer\n", + " a2 = z2# output of out layer\n", + " # error in output layer\n", + " d2 =(a2-y)\n", + " print(d2.shape)\n", + " d1 = (np.dot(w2,d2.T))#np.multiply((w2.dot((d2.transpose()) \n", + " print(d1.shape)\n", + " \n", + " # Gradient for w1 and w2\n", + " w1_adj = x.transpose().dot(d1.T)\n", + " w2_adj = a1.transpose().dot(d2)\n", + " \n", + " # Updating parameters\n", + " w1 = w1-(alpha*(w1_adj))\n", + " w2 = w2-(alpha*(w2_adj))\n", + " \n", + " return(w1, w2)\n", + " \n", + "def train(x, Y, w1, w2, alpha = 0.01, epoch = 10):\n", + " acc =[]\n", + " losss =[]\n", + " for j in range(epoch):\n", + " l =[]\n", + " for i in range(len(x)):\n", + " out = f_forward(x[i], w1, w2)\n", + " #l.append((loss(out, Y[i])))\n", + " w1, w2 = back_prop(x[i].reshape((1,x[i].shape[0])), y[i].reshape((1,1)), w1, w2, alpha)\n", + " #print(\"epochs:\", j + 1, \"======== acc:\", (1-(sum(l)/len(x)))*100) \n", + " #acc.append((1-(sum(l)/len(x)))*100)\n", + " #losss.append(sum(l)/len(x))\n", + " return(acc, losss, w1, w2)\n", + " \n", + "def predict(x, w1, w2):\n", + " Out = f_forward(x, w1, w2)\n", + " return Out" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dbe5e7d3", + "metadata": {}, + "outputs": [], + "source": [ + "w1=generate_wt(4,10)\n", + "w2=generate_wt(10,1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e43db846", + "metadata": {}, + "outputs": [], + "source": [ + "w1,w2=train(x_train,y_train,w1,w2,0.1,100)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc3fd2a8", + "metadata": {}, + "outputs": [], + "source": [ + "x_train[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5643acf2", + "metadata": {}, + "outputs": [], + "source": [ + "w1.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "391363b6", + "metadata": {}, + "outputs": [], + "source": [ + "w2.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56c5cc09", + "metadata": {}, + "outputs": [], + "source": [ + "y1=predict(x_train,w1,w2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1019461", + "metadata": {}, + "outputs": [], + "source": [ + "y1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f57fb3c0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}