Skip to content

Instantly share code, notes, and snippets.

@arghyadeep99
Created August 15, 2020 10:59
Show Gist options
  • Save arghyadeep99/aeee03bcb9f8303cd8f3ca1a438fe675 to your computer and use it in GitHub Desktop.
Save arghyadeep99/aeee03bcb9f8303cd8f3ca1a438fe675 to your computer and use it in GitHub Desktop.
Gradient Descent from Scratch.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Gradient Descent from Scratch.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyP/LvgkKOxKcl+NTdbKf6PB",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/arghyadeep99/aeee03bcb9f8303cd8f3ca1a438fe675/gradient-descent-from-scratch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CxJJ6cXRamL9",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"\n",
"class NeuralNetwork:\n",
" def __init__(self):\n",
" np.random.seed(42)\n",
" self.Wij = np.random.rand(4,5)\n",
" self.Wjk = np.random.rand(5,1)\n",
"\n",
" def sigmoid(self, x, w):\n",
" z = np.dot(x,w)\n",
" return 1/(1 + np.exp(-z))\n",
" \n",
" def diff_sigmoid(self, x, w):\n",
" return self.sigmoid(x,w)*(1-self.sigmoid(x,w))\n",
" \n",
" def gradient_descent(self,x,y, epochs):\n",
" for i in range(epochs):\n",
" Xi = x #Input layer\n",
" Xj = self.sigmoid(Xi, self.Wij) #Hidden layer\n",
" Y_cap = self.sigmoid(Xj, self.Wjk) #Final layer\n",
" grad_Wjk = np.dot(Xj.T, (y-Y_cap)*self.diff_sigmoid(Xj, self.Wjk))\n",
" grad_Wij = np.dot(Xi.T, np.dot((y-Y_cap)* self.diff_sigmoid(Xj, self.Wjk), self.Wjk.T) * self.diff_sigmoid(Xi, self.Wij))\n",
"\n",
" self.Wjk += grad_Wjk\n",
" self.Wij += grad_Wij\n",
"\n",
" print('The final prediction from neural network are: ')\n",
" print(Y_cap)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6gYCR_7B1xM6",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 306
},
"outputId": "ed9ace23-2948-4cb0-c425-acf75d6fdec3"
},
"source": [
"if __name__ == '__main__':\n",
" NN = NeuralNetwork()\n",
" print('Random starting input to hidden weights: ')\n",
" print(NN.Wij)\n",
" print('Random starting hidden to output weights: ')\n",
" print(NN.Wjk)\n",
" X = np.array([[0, 0, 1, 1], [1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 0, 1], [0, 0, 0, 1]])\n",
" y = np.array([[0, 1, 1, 0, 1]]).T\n",
" neural_network.gradient_descent(X, y, 10000)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Random starting input to hidden weights: \n",
"[[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864]\n",
" [0.15599452 0.05808361 0.86617615 0.60111501 0.70807258]\n",
" [0.02058449 0.96990985 0.83244264 0.21233911 0.18182497]\n",
" [0.18340451 0.30424224 0.52475643 0.43194502 0.29122914]]\n",
"Random starting hidden to output weights: \n",
"[[0.61185289]\n",
" [0.13949386]\n",
" [0.29214465]\n",
" [0.36636184]\n",
" [0.45606998]]\n",
"The final prediction from neural network are: \n",
"[[0.00589178]\n",
" [0.99658221]\n",
" [0.99669157]\n",
" [0.00609915]\n",
" [0.99361295]]\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment