Skip to content

Commit

Permalink
Put it all together
Browse files Browse the repository at this point in the history
  • Loading branch information
Alluxia-F committed Jul 15, 2017
1 parent 11405d1 commit e461dfb
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Chapter 6/4. Backpropagation in Code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
Expand Down
135 changes: 135 additions & 0 deletions Chapter 6/6. Put it all together.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 4)\n",
"(4, 1)\n"
]
}
],
"source": [
"# Input data \n",
"import numpy as np\n",
"np.random.seed(1)\n",
"streetlights=np.array([[1,0,1],\n",
" [0,1,1],\n",
" [0,0,1],\n",
" [1,1,1]])\n",
"\n",
"walk_vs_stop=np.array([[1,1,0,0]]).T\n",
"\n",
"alpha=0.2\n",
"hidden_size=4\n",
"\n",
"weights_0_1=2*np.random.random((3,hidden_size))-1\n",
"weights_1_2=2*np.random.random((hidden_size,1))-1\n",
"print weights_0_1.shape\n",
"print weights_1_2.shape"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def relu(x):\n",
" return (x>0)\n",
"\n",
"def relu2deriv(output):\n",
" return output>0"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 1.31255512793\n",
"Error: 0.857463601866\n",
"Error: 0.652266842474\n",
"Error: 0.203997142254\n",
"Error: 0.199295859099\n",
"Error: 0.0533385633385\n"
]
}
],
"source": [
"# Train the model\n",
"\n",
"for iteration in xrange(60):\n",
" layer_2_error=0\n",
" for i in xrange(len(streetlights)):\n",
" layer_0=streetlights[i:i+1]\n",
" layer_1=relu(layer_0.dot(weights_0_1))\n",
" layer_2=layer_1.dot(weights_1_2)\n",
" \n",
" layer_2_error+=np.sum((layer_2-walk_vs_stop[i:i+1])**2)\n",
" layer_2_delta=layer_2-walk_vs_stop[i:i+1]\n",
" layer_1_delta=(layer_2_delta.dot(weights_1_2.T))*relu2deriv(layer_1)\n",
" \n",
" weights_1_2-=alpha*layer_1.T.dot(layer_2_delta)\n",
" weights_0_1-=alpha*layer_0.T.dot(layer_1_delta)\n",
" \n",
" if (iteration%10==9):\n",
" print 'Error: '+str(layer_2_error)\n",
" \n",
" \n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit e461dfb

Please sign in to comment.