Skip to content

Instantly share code, notes, and snippets.

@Erlemar
Created July 30, 2017 15:04
Show Gist options
  • Save Erlemar/360bddedb91c0b9500e65566de2cbab3 to your computer and use it in GitHub Desktop.
Save Erlemar/360bddedb91c0b9500e65566de2cbab3 to your computer and use it in GitHub Desktop.
3 layer FNN
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error:0.427202388574\n",
"Error:0.00806482848555\n",
"Error:0.00526593550698\n",
"Error:0.00415712122415\n",
"Error:0.00352802988241\n",
"Error:0.00311138599353\n",
"Error:0.00281016648331\n",
"Error:0.00257969133595\n",
"Error:0.00239619398074\n",
"Error:0.00224573335481\n",
"[[ 5.73112665e-03]\n",
" [ 9.97614085e-01]\n",
" [ 1.55373260e-03]\n",
" [ 1.24248700e-03]\n",
" [ 1.75679361e-03]\n",
" [ 9.97699121e-01]\n",
" [ 6.00341783e-04]\n",
" [ 7.05775684e-04]\n",
" [ 9.97748118e-01]\n",
" [ 9.97748118e-01]\n",
" [ 5.84046510e-04]\n",
" [ 9.97691529e-01]\n",
" [ 9.97739697e-01]\n",
" [ 9.97474914e-01]\n",
" [ 9.97474914e-01]\n",
" [ 9.97474914e-01]\n",
" [ 9.97474914e-01]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"x = np.array([\n",
" [0,0,0,0],\n",
" [0,0,0,1],\n",
" [0,0,1,0],\n",
" [0,1,0,0],\n",
" [1,0,0,0],\n",
" [0,0,1,1],\n",
" [0,1,1,0],\n",
" [1,1,0,0],\n",
" [1,0,0,1],\n",
" [1,0,0,1],\n",
" [1,1,1,0],\n",
" [1,1,0,1],\n",
" [1,0,1,1],\n",
" [1,1,1,1],\n",
" [1,1,1,1],\n",
" [1,1,1,1],\n",
" [1,1,1,1]\n",
" ])\n",
"\n",
"\n",
"y = np.array([\n",
" [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]\n",
" ]).T\n",
"\n",
"w = np.random.random((4, 4))\n",
"w2 = np.random.random((4, 4))\n",
"w3 = np.random.random((4, 1))\n",
"\n",
"for j in range(10000):\n",
" a2 = 1/(1 + np.exp(-(np.dot(x, w))))\n",
" a3 = 1/(1 + np.exp(-(np.dot(a2, w2))))\n",
" a4 = 1/(1 + np.exp(-(np.dot(a3, w3))))\n",
" a4delta = (y - a4) * (a4 * (1 - a4))\n",
" a3delta = a4delta.dot(w3.T) * (a3 * (1 - a3))\n",
" a2delta = a3delta.dot(w2.T) * (a2 * (1 - a2))\n",
" if (j% 1000) == 0:\n",
" print (\"Error:\" + str(np.mean(np.abs(y - a4))))\n",
" w3 += a3.T.dot(a4delta)\n",
" w2 += a2.T.dot(a3delta)\n",
" w += x.T.dot(a2delta)\n",
"print(a4)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Actual value: 0. Predicted value: 0.0057',\n",
" 'Actual value: 1. Predicted value: 0.9976',\n",
" 'Actual value: 0. Predicted value: 0.0016',\n",
" 'Actual value: 0. Predicted value: 0.0012',\n",
" 'Actual value: 0. Predicted value: 0.0018',\n",
" 'Actual value: 1. Predicted value: 0.9977',\n",
" 'Actual value: 0. Predicted value: 0.0006',\n",
" 'Actual value: 0. Predicted value: 0.0007',\n",
" 'Actual value: 1. Predicted value: 0.9977',\n",
" 'Actual value: 1. Predicted value: 0.9977',\n",
" 'Actual value: 0. Predicted value: 0.0006',\n",
" 'Actual value: 1. Predicted value: 0.9977',\n",
" 'Actual value: 1. Predicted value: 0.9977',\n",
" 'Actual value: 1. Predicted value: 0.9975',\n",
" 'Actual value: 1. Predicted value: 0.9975',\n",
" 'Actual value: 1. Predicted value: 0.9975',\n",
" 'Actual value: 1. Predicted value: 0.9975']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"['Actual value: ' + str(i[0][0]) + '. Predicted value: ' + str(np.round(i[1][0], 4)) for i in list(zip(y, a4))]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment