├── LICENSE ├── LeNetArchitecture └── LeNetArchitecture.ipynb ├── LogisticRegression ├── Data.csv └── LogisticRegression.ipynb └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Thinam Tamang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LeNetArchitecture/LeNetArchitecture.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "LeNetArchitecture.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | }, 16 | "accelerator": "GPU" 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "code", 21 | "metadata": { 22 | "id": "qNXFM06HOnBy" 23 | }, 24 | "source": [ 25 | "#@ IMPORTING THE LIBRARIES AND DEPENDENCIES: \n", 26 | "# !pip install -U d2l\n", 27 | "%matplotlib inline \n", 28 | "import torch\n", 29 | "from torch import nn\n", 30 | "from torch.nn import functional as F\n", 31 | "from d2l import torch as d2l" 32 | ], 33 | "execution_count": 1, 34 | "outputs": [] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "metadata": { 39 | "id": "q2nnTsgboSzk" 40 | }, 41 | "source": [ 42 | "#@ INITIALIZING MODEL PARAMETERS: \n", 43 | "scale = 0.01 # Initialization. \n", 44 | "W1 = torch.randn(size=(20, 1, 3, 3)) * scale # Initializing the Parameter. \n", 45 | "b1 = torch.zeros(20) # Initializing the Tensor of zeros. \n", 46 | "W2 = torch.randn(size=(50, 20, 5, 5)) * scale # Initializing the Parameter. \n", 47 | "b2 = torch.zeros(50) # Initializing the Tensor of zeros. \n", 48 | "W3 = torch.randn(size=(800, 128)) * scale # Initializing the Parameter. \n", 49 | "b3 = torch.zeros(128) # Initializing the Tensor of zeros. \n", 50 | "W4 = torch.randn(size=(128, 10)) * scale # Initializing the Parameter. \n", 51 | "b4 = torch.zeros(10) # Initializing the Tensor of zeros. \n", 52 | "params = [W1, b1, W2, b2, W3, b3, W4, b4] # Initializing a list of Parameters. " 53 | ], 54 | "execution_count": 2, 55 | "outputs": [] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "metadata": { 60 | "id": "V5Z391gWrDnO" 61 | }, 62 | "source": [ 63 | "#@ DEFINING THE LENET MODEL: \n", 64 | "def lenet(X, params): # Initializing the LENET Model. \n", 65 | " h1_conv = F.conv2d(input=X, weight=params[0], bias=params[1]) # Initializing the Convolutional Layer. \n", 66 | " h1_activation = F.relu(h1_conv) # Initializing the RELU Activation Function. \n", 67 | " h1 = F.avg_pool2d(input=h1_activation, kernel_size=(2, 2), stride=(2, 2)) # Initializing the Average Pooling Layer. \n", 68 | " h2_conv = F.conv2d(input=h1, weight=params[2], bias=params[3]) # Initializing the Convolutional Layer. \n", 69 | " h2_activation = F.relu(h2_conv) # Initializing the RELU Activation Function. \n", 70 | " h2 = F.avg_pool2d(input=h2_activation, kernel_size=(2, 2), stride=(2, 2)) # Initializing the Average Pooling Layer. \n", 71 | " h2 = h2.reshape(h2.shape[0], -1) # Changing the shape. \n", 72 | " h3_linear = torch.mm(h2, params[4] + params[5]) # Initializing the Matrix Multiplication. \n", 73 | " h3 = F.relu(h3_linear) # Initializing the RELU Activation Function. \n", 74 | " y_hat = torch.mm(h3, params[6] + params[7]) # Initializing the Matrix Multiplication. \n", 75 | " return y_hat\n", 76 | "#@ INITIALIZING CROSSENTROPY LOSS FUNCTION: \n", 77 | "loss = nn.CrossEntropyLoss(reduction=\"none\") # Initializing the Cross Entropy Loss Function. " 78 | ], 79 | "execution_count": 3, 80 | "outputs": [] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "metadata": { 85 | "id": "AYZHVTu1t-rH" 86 | }, 87 | "source": [ 88 | "#@ IMPLEMENTATION OF DATA SYNCHRONIZATION: \n", 89 | "def get_params(params, device): # Initializing the Parameters. \n", 90 | " new_params = [p.clone().to(device) for p in params]\n", 91 | " for p in new_params: \n", 92 | " p.requires_grad_()\n", 93 | " return new_params\n", 94 | "#@ IMPLEMENTATION DATA SYNCHRONIZATION: \n", 95 | "def allreduce(data): # Getting sum of the results. \n", 96 | " for i in range(1, len(data)):\n", 97 | " data[0][:] += data[i].to(data[0].device)\n", 98 | " for i in range(1, len(data)):\n", 99 | " data[i] = data[0].to(data[i].device)\n", 100 | "#@ IMPLEMENTATION OF DATA DISTRIBUTION: \n", 101 | "def split_data(X, y, devices): # Data Distribution. \n", 102 | " assert X.shape[0] == y.shape[0]\n", 103 | " return (nn.parallel.scatter(X, devices), nn.parallel.scatter(y, devices))" 104 | ], 105 | "execution_count": 4, 106 | "outputs": [] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "metadata": { 111 | "id": "Drroj8Ku1ttI", 112 | "colab": { 113 | "base_uri": "https://localhost:8080/", 114 | "height": 279 115 | }, 116 | "outputId": "87450df1-7d25-4de3-f7b2-ca4aa0db83cf" 117 | }, 118 | "source": [ 119 | "#@ IMPLEMENTATION OF TRAINING FUNCTION: \n", 120 | "def train_batch(X, y, device_params, devices, lr): # Function for Training Batches. \n", 121 | " X_shards, y_shards = split_data(X, y, devices) # Initializing the Data Distribution Function. \n", 122 | " losses = [loss(lenet(X_shard, device_W), y_shard).sum() for \n", 123 | " X_shard, y_shard, device_W in zip(X_shards, y_shards, device_params)] \n", 124 | " for l in losses: \n", 125 | " l.backward() # Initializing Back Propagation. \n", 126 | " with torch.no_grad():\n", 127 | " for i in range(len(device_params[0])):\n", 128 | " allreduce([device_params[c][i].grad for c in range(len(devices))])\n", 129 | " for param in device_params: \n", 130 | " d2l.sgd(param, lr, X.shape[0])\n", 131 | "#@ IMPLEMENTATION OF TRAINING FUNCTION: \n", 132 | "def train(num_gpus, batch_size, lr): # Function for Training the Model. \n", 133 | " train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) # Initializing the Training and Test Iterations. \n", 134 | " devices = [d2l.try_gpu(i) for i in range(num_gpus)]\n", 135 | " device_params = [get_params(params, d) for d in devices] # Initializing the Model Parameters. \n", 136 | " num_epochs = 10 # Initializing the Training Epochs. \n", 137 | " animator = d2l.Animator(\"epoch\", \"test acc\", xlim=[1, num_epochs]) # Initializing the Animation.\n", 138 | " timer = d2l.Timer() # Initializing the Timer. \n", 139 | " for epoch in range(num_epochs):\n", 140 | " timer.start()\n", 141 | " for X, y in train_iter: \n", 142 | " train_batch(X, y, device_params, devices, lr)\n", 143 | " torch.cuda.synchronize()\n", 144 | " timer.stop()\n", 145 | " animator.add(epoch + 1, (d2l.evaluate_accuracy_gpu(\n", 146 | " lambda x: lenet(x, device_params[0]), test_iter, devices[0]), ))\n", 147 | " print(f\"Test acc: {animator.Y[0][-1]:.2f}, {timer.avg():.1f} sec/epoch\"\n", 148 | " f\"on {str(devices)}\")\n", 149 | "#@ EXPERIMENTATION:\n", 150 | "train(num_gpus=1, batch_size=256, lr=0.2) # Training the Model. " 151 | ], 152 | "execution_count": 5, 153 | "outputs": [ 154 | { 155 | "output_type": "stream", 156 | "text": [ 157 | "Test acc: 0.10, 6.8 sec/epochon [device(type='cuda', index=0)]\n" 158 | ], 159 | "name": "stdout" 160 | }, 161 | { 162 | "output_type": "display_data", 163 | "data": { 164 | "text/plain": [ 165 | "
" 166 | ], 167 | "image/svg+xml": "\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 \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 \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" 168 | }, 169 | "metadata": { 170 | "tags": [], 171 | "needs_background": "light" 172 | } 173 | } 174 | ] 175 | } 176 | ] 177 | } -------------------------------------------------------------------------------- /LogisticRegression/Data.csv: -------------------------------------------------------------------------------- 1 | Pregnancies,Glucose,BloodPressure,SkinThickness,Insulin,BMI,DiabetesPedigreeFunction,Age,Outcome 2 | 6,148,72,35,0,33.6,0.627,50,1 3 | 1,85,66,29,0,26.6,0.351,31,0 4 | 8,183,64,0,0,23.3,0.672,32,1 5 | 1,89,66,23,94,28.1,0.167,21,0 6 | 0,137,40,35,168,43.1,2.288,33,1 7 | 5,116,74,0,0,25.6,0.201,30,0 8 | 3,78,50,32,88,31,0.248,26,1 9 | 10,115,0,0,0,35.3,0.134,29,0 10 | 2,197,70,45,543,30.5,0.158,53,1 11 | 8,125,96,0,0,0,0.232,54,1 12 | 4,110,92,0,0,37.6,0.191,30,0 13 | 10,168,74,0,0,38,0.537,34,1 14 | 10,139,80,0,0,27.1,1.441,57,0 15 | 1,189,60,23,846,30.1,0.398,59,1 16 | 5,166,72,19,175,25.8,0.587,51,1 17 | 7,100,0,0,0,30,0.484,32,1 18 | 0,118,84,47,230,45.8,0.551,31,1 19 | 7,107,74,0,0,29.6,0.254,31,1 20 | 1,103,30,38,83,43.3,0.183,33,0 21 | 1,115,70,30,96,34.6,0.529,32,1 22 | 3,126,88,41,235,39.3,0.704,27,0 23 | 8,99,84,0,0,35.4,0.388,50,0 24 | 7,196,90,0,0,39.8,0.451,41,1 25 | 9,119,80,35,0,29,0.263,29,1 26 | 11,143,94,33,146,36.6,0.254,51,1 27 | 10,125,70,26,115,31.1,0.205,41,1 28 | 7,147,76,0,0,39.4,0.257,43,1 29 | 1,97,66,15,140,23.2,0.487,22,0 30 | 13,145,82,19,110,22.2,0.245,57,0 31 | 5,117,92,0,0,34.1,0.337,38,0 32 | 5,109,75,26,0,36,0.546,60,0 33 | 3,158,76,36,245,31.6,0.851,28,1 34 | 3,88,58,11,54,24.8,0.267,22,0 35 | 6,92,92,0,0,19.9,0.188,28,0 36 | 10,122,78,31,0,27.6,0.512,45,0 37 | 4,103,60,33,192,24,0.966,33,0 38 | 11,138,76,0,0,33.2,0.42,35,0 39 | 9,102,76,37,0,32.9,0.665,46,1 40 | 2,90,68,42,0,38.2,0.503,27,1 41 | 4,111,72,47,207,37.1,1.39,56,1 42 | 3,180,64,25,70,34,0.271,26,0 43 | 7,133,84,0,0,40.2,0.696,37,0 44 | 7,106,92,18,0,22.7,0.235,48,0 45 | 9,171,110,24,240,45.4,0.721,54,1 46 | 7,159,64,0,0,27.4,0.294,40,0 47 | 0,180,66,39,0,42,1.893,25,1 48 | 1,146,56,0,0,29.7,0.564,29,0 49 | 2,71,70,27,0,28,0.586,22,0 50 | 7,103,66,32,0,39.1,0.344,31,1 51 | 7,105,0,0,0,0,0.305,24,0 52 | 1,103,80,11,82,19.4,0.491,22,0 53 | 1,101,50,15,36,24.2,0.526,26,0 54 | 5,88,66,21,23,24.4,0.342,30,0 55 | 8,176,90,34,300,33.7,0.467,58,1 56 | 7,150,66,42,342,34.7,0.718,42,0 57 | 1,73,50,10,0,23,0.248,21,0 58 | 7,187,68,39,304,37.7,0.254,41,1 59 | 0,100,88,60,110,46.8,0.962,31,0 60 | 0,146,82,0,0,40.5,1.781,44,0 61 | 0,105,64,41,142,41.5,0.173,22,0 62 | 2,84,0,0,0,0,0.304,21,0 63 | 8,133,72,0,0,32.9,0.27,39,1 64 | 5,44,62,0,0,25,0.587,36,0 65 | 2,141,58,34,128,25.4,0.699,24,0 66 | 7,114,66,0,0,32.8,0.258,42,1 67 | 5,99,74,27,0,29,0.203,32,0 68 | 0,109,88,30,0,32.5,0.855,38,1 69 | 2,109,92,0,0,42.7,0.845,54,0 70 | 1,95,66,13,38,19.6,0.334,25,0 71 | 4,146,85,27,100,28.9,0.189,27,0 72 | 2,100,66,20,90,32.9,0.867,28,1 73 | 5,139,64,35,140,28.6,0.411,26,0 74 | 13,126,90,0,0,43.4,0.583,42,1 75 | 4,129,86,20,270,35.1,0.231,23,0 76 | 1,79,75,30,0,32,0.396,22,0 77 | 1,0,48,20,0,24.7,0.14,22,0 78 | 7,62,78,0,0,32.6,0.391,41,0 79 | 5,95,72,33,0,37.7,0.37,27,0 80 | 0,131,0,0,0,43.2,0.27,26,1 81 | 2,112,66,22,0,25,0.307,24,0 82 | 3,113,44,13,0,22.4,0.14,22,0 83 | 2,74,0,0,0,0,0.102,22,0 84 | 7,83,78,26,71,29.3,0.767,36,0 85 | 0,101,65,28,0,24.6,0.237,22,0 86 | 5,137,108,0,0,48.8,0.227,37,1 87 | 2,110,74,29,125,32.4,0.698,27,0 88 | 13,106,72,54,0,36.6,0.178,45,0 89 | 2,100,68,25,71,38.5,0.324,26,0 90 | 15,136,70,32,110,37.1,0.153,43,1 91 | 1,107,68,19,0,26.5,0.165,24,0 92 | 1,80,55,0,0,19.1,0.258,21,0 93 | 4,123,80,15,176,32,0.443,34,0 94 | 7,81,78,40,48,46.7,0.261,42,0 95 | 4,134,72,0,0,23.8,0.277,60,1 96 | 2,142,82,18,64,24.7,0.761,21,0 97 | 6,144,72,27,228,33.9,0.255,40,0 98 | 2,92,62,28,0,31.6,0.13,24,0 99 | 1,71,48,18,76,20.4,0.323,22,0 100 | 6,93,50,30,64,28.7,0.356,23,0 101 | 1,122,90,51,220,49.7,0.325,31,1 102 | 1,163,72,0,0,39,1.222,33,1 103 | 1,151,60,0,0,26.1,0.179,22,0 104 | 0,125,96,0,0,22.5,0.262,21,0 105 | 1,81,72,18,40,26.6,0.283,24,0 106 | 2,85,65,0,0,39.6,0.93,27,0 107 | 1,126,56,29,152,28.7,0.801,21,0 108 | 1,96,122,0,0,22.4,0.207,27,0 109 | 4,144,58,28,140,29.5,0.287,37,0 110 | -------------------------------------------------------------------------------- /LogisticRegression/LogisticRegression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "LogisticRegression.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "code", 21 | "metadata": { 22 | "id": "19LYAr64HuOw" 23 | }, 24 | "source": [ 25 | "#@ IMPORTING THE LIBRARIES:\n", 26 | "import pandas as pd\n", 27 | "import numpy as np\n", 28 | "from sklearn.model_selection import train_test_split\n", 29 | "from sklearn.linear_model import LogisticRegression" 30 | ], 31 | "execution_count": 2, 32 | "outputs": [] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "metadata": { 37 | "id": "XqRfocAmIHrV" 38 | }, 39 | "source": [ 40 | "#@ IMPLEMENTATION OF LOGISTIC REGRESSION:\n", 41 | "class LogisticRegression(): # Implementation of Logistic Regression. \n", 42 | " def __init__(self, learning_rate, iterations): # Constructor Function. \n", 43 | " self.learning_rate = learning_rate # Initializing the Learning rate. \n", 44 | " self.iterations = iterations # Initializing the Iterations. \n", 45 | "\n", 46 | " def fit(self, features, labels): # Function for Training. \n", 47 | " self.features = features # Initializing Features. \n", 48 | " self.labels = labels # Initializing Labels.\n", 49 | " self.num_train = features.shape[0] # Number of Training examples. \n", 50 | " self.num_features = features.shape[1] # Number of Features. \n", 51 | " self.W, self.b = np.zeros(self.num_features), 0 # Initializing Weights. \n", 52 | "\n", 53 | " for i in range(self.iterations):\n", 54 | " self.update_weights()\n", 55 | " return self\n", 56 | " \n", 57 | " def update_weights(self): # Function for updating weights. \n", 58 | " A = 1 / (1 + np.exp(-(self.features.dot(self.W) + self.b)))\n", 59 | " pred = (A - self.labels.T)\n", 60 | " pred = np.reshape(pred, self.num_train)\n", 61 | " dW = np.dot(self.features.T, pred) / self.num_train\n", 62 | " db = np.sum(pred) / self.num_train # Average of the Predictions. \n", 63 | " self.W = self.W - self.learning_rate * dW # Updating Weights. \n", 64 | " self.b = self.b - self.learning_rate * db # Updating Weights. \n", 65 | " return self\n", 66 | " \n", 67 | " def predict(self, X): # Function for Predictions. \n", 68 | " Z = 1 / (1 + np.exp(-(X.dot(self.W) + self.b)))\n", 69 | " Y = np.where(Z > 0.5, 1, 0)\n", 70 | " return Y" 71 | ], 72 | "execution_count": 3, 73 | "outputs": [] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "metadata": { 78 | "colab": { 79 | "base_uri": "https://localhost:8080/" 80 | }, 81 | "id": "aEZlOSt4thQU", 82 | "outputId": "40a0db7f-067c-45c3-f753-252216bdcb79" 83 | }, 84 | "source": [ 85 | "#@ IMPLEMENTATION: \n", 86 | "def main():\n", 87 | " data = pd.read_csv(PATH) # Reading the Dataset. \n", 88 | " X = data.iloc[:, :-1].values\n", 89 | " Y = data.iloc[:, -1:].values\n", 90 | " X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, \n", 91 | " random_state=11)\n", 92 | " model = LogisticRegression(learning_rate=0.01, iterations=1000) # Initializing Logistic Regression. \n", 93 | " model.fit(X_train, y_train) # Training the Model. \n", 94 | " Y_pred = model.predict(X_test)\n", 95 | " correctly_classified = 0\n", 96 | " count = 0\n", 97 | " for count in range(np.size(Y_pred)):\n", 98 | " if y_test[count] == Y_pred[count]:\n", 99 | " correctly_classified += 1\n", 100 | " count += 1\n", 101 | " print(\"Accuracy:\", (correctly_classified / count) * 100)\n", 102 | "\n", 103 | "if __name__ == \"__main__\":\n", 104 | " main()" 105 | ], 106 | "execution_count": 6, 107 | "outputs": [ 108 | { 109 | "output_type": "stream", 110 | "text": [ 111 | "Accuracy: 42.42424242424242\n" 112 | ], 113 | "name": "stdout" 114 | } 115 | ] 116 | } 117 | ] 118 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **MACHINE LEARNING ALGORITHMS** 2 | 3 | **LOGISTIC REGRESSION** 4 | - Logistic Regression is a supervised learning algorithm which models the probabilities for classification problems. It is a statistical model which uses a logistic function to model a binary dependent variable. It is efficient method for binary and linear classification problems. 5 | - [**Implementation of Logistic Regression**](https://github.com/ThinamXx/MachineLearning__Algorithms/tree/main/LogisticRegression) 6 | 7 | ![Image](https://github.com/ThinamXx/300Days__MachineLearningDeepLearning/blob/main/Images/Day%20123a.PNG) 8 | --------------------------------------------------------------------------------