Skip to content

Instantly share code, notes, and snippets.

@jose-roberto-abreu
Last active August 29, 2015 14:26
Show Gist options
  • Save jose-roberto-abreu/b6d5f1c5c6c6bf6d0fa4 to your computer and use it in GitHub Desktop.
Save jose-roberto-abreu/b6d5f1c5c6c6bf6d0fa4 to your computer and use it in GitHub Desktop.
NN Implementation
{"nbformat": 4, "nbformat_minor": 0, "cells": [{"cell_type": "code", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Probability Of Pos : 0.122164\n0\n"}], "source": "import numpy as np\n\n#Utility Functions\ndef loadDataSet():\n dataset = np.array([[1,1,1],\n [1,1,0],\n [1,0,1],\n [1,0,0]])\n label = np.array([[1],[0],[0],[0]])\n return dataset,label\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(value):\n print(\"Prob Of Pos : %f\"%value)\n if value >= 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\nX,y = loadDataSet()\nnumber_observations = X.shape[0]\nweights_1 = np.random.random((2,3))\nweights_2 = np.random.random((1,3))\n\nmaxIter = 1000\nactivateDebug = False\nfor iterNumber in range(maxIter):\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\nTest_X = np.array([[1,0,0]])\na_1 = sigmoid(np.dot(Test_X,weights_1.T))\na_1 = np.hstack((np.ones((1,1)),a_1))\na_2 = sigmoid(np.dot(a_1,weights_2.T))\n\nclassify(a_2)\n", "execution_count": 159, "metadata": {"trusted": true, "collapsed": false}}, {"cell_type": "code", "outputs": [], "source": "", "execution_count": null, "metadata": {"trusted": true, "collapsed": true}}], "metadata": {"language_info": {"mimetype": "text/x-python", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "version": "3.4.3", "name": "python", "pygments_lexer": "ipython3", "file_extension": ".py"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment