-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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 | ||
} |