Created
July 28, 2015 14:47
-
-
Save jose-roberto-abreu/16dc9ed6d3a225fdd9d7 to your computer and use it in GitHub Desktop.
NN Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"cells": [{"metadata": {"trusted": true, "collapsed": false}, "cell_type": "code", "source": "import numpy as np\n\n#Utility Functions\ndef loadDataSet():\n #Last column Target\n dataset = np.array([[1,1,1,1],\n [1,1,0,1],\n [1,0,1,1],\n [1,0,0,0]])\n return dataset\n\ndef sigmoid(z,derivate=False):\n if derivate:\n #Derivate Sigmoid Function\n return z * (1-z)\n #Simoidal Function\n return 1.0 / (1.0 + np.e**(-z))\n\ndef classify(Test_X):\n a_1 = sigmoid(np.dot(Test_X,weights_1.T))\n a_1 = np.hstack((np.ones((1,1)),a_1))\n a_2 = sigmoid(np.dot(a_1,weights_2.T))\n print(\"Prob Of Pos : %f\"%a_2)\n if a_2 >= 0.5:\n print(\"1\")\n else:\n print(\"0\")\n\n#Arquitecture\n'''\n 3 nodes input layer - include bias\n 3 nodes hidden layer - include bias\n 1 node output layer\n'''\n \n#Calc - HardCode for the arquitecture above\ndataset = loadDataSet()\nnumber_observations = dataset.shape[0]\nweights_1 = np.random.random((2,3))\nweights_2 = np.random.random((1,3))\n\nmaxIter = 100000\nactivateDebug = False\nfor iterNumber in range(maxIter):\n data = np.random.permutation(dataset)\n X = data[:,:3]\n y = data[:,-1:]\n\n #FeedForward\n a_1 = sigmoid(np.dot(X,weights_1.T))\n a_1 = np.hstack((np.ones((number_observations,1)),a_1))\n a_2 = sigmoid(np.dot(a_1,weights_2.T))\n \n #BackPropagation\n delta_2 = (y - a_2) * sigmoid(a_2,True)\n delta_1 = delta_2.dot(weights_2) * sigmoid(a_1,True)\n \n if iterNumber % 100 == 0 and activateDebug:\n print(\"Error : %f - %d\"%(np.sum(delta_2),iterNumber))\n \n #DELTA1:\n '''\n 1)Product delta_1 * activation - in this case X o a_0\n 2)Sum by columns to get acumulate\n 3)Throw away first column\n 4)Make a reshape\n 5)Tile the vector, to be equal dimension of weights\n '''\n DELTA_1 = np.tile((np.sum(delta_1 * X,axis=0)[1:]).reshape(2,1),3)\n \n #DELTA2:\n DELTA_2 = np.sum(delta_2 * a_1,axis=0).reshape(1,3)\n \n weights_1 += DELTA_1\n weights_2 += DELTA_2\n\n#Classify\nclassify(np.array([[1,1,1]]))\nclassify(np.array([[1,1,0]]))\nclassify(np.array([[1,0,1]]))\nclassify(np.array([[1,0,0]]))\n", "outputs": [], "execution_count": null}, {"metadata": {"trusted": true, "collapsed": true}, "cell_type": "code", "source": "", "outputs": [], "execution_count": null}], "nbformat": 4, "metadata": {"language_info": {"file_extension": ".py", "name": "python", "nbconvert_exporter": "python", "mimetype": "text/x-python", "version": "3.4.3", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}, "nbformat_minor": 0} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment