-
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.
Updates
- Loading branch information
Showing
3 changed files
with
155 additions
and
115 deletions.
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
136 changes: 136 additions & 0 deletions
136
Chapter 5/6. Gradient Descent with Multiple Inputs & Outputs.ipynb
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,136 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 215, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create an empty network with multiple inputs & outputs\n", | ||
"def nerual_network(inputs,weights):\n", | ||
" pred=mat_mul(inputs,weights) # Matrix multiplication\n", | ||
" return pred\n", | ||
"\n", | ||
"def mat_mul(inputs,weights):\n", | ||
" assert len(inputs)==len(weights)\n", | ||
" output=[]\n", | ||
" for index,weight in enumerate(weights):\n", | ||
" output.append(w_sum(inputs,weight))\n", | ||
" return output\n", | ||
"\n", | ||
"def w_sum(inputs,weights):\n", | ||
" sums=0\n", | ||
" for i,v in enumerate(inputs):\n", | ||
" sums+=inputs[i]*weights[i]\n", | ||
" return sums\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 216, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Data information\n", | ||
"\n", | ||
"toes=[8.5,9.5,9.9,9.0]\n", | ||
"wlrec=[0.65,0.8,0.8,0.9]\n", | ||
"nfans=[1.2,1.3,0.5,1.0]\n", | ||
"\n", | ||
"hurt=[0.1,0.0,0.0,0.1]\n", | ||
"win=[1,1,0,1]\n", | ||
"sad=[0.1,0.0,0.1,0.2]\n", | ||
"\n", | ||
"inputs=[toes[0],wlrec[0],nfans[0]]\n", | ||
"true=[hurt[0],win[0],sad[0]]\n", | ||
"\n", | ||
"weights=[[0.1,0.1,-0.3],[0.1,0.2,0.0],[0.0,1.3,0.1]]\n", | ||
"alpha=0.01" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 217, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Prediction:[ 0.555 0.98 0.965]\n", | ||
"Error:[ 2.07025000e-01 4.00000000e-04 7.48225000e-01]\n", | ||
"Delta:[ 0.455 -0.02 0.865]\n", | ||
"Weights_Delta: [[ 3.8675 -0.17 7.3525 ]\n", | ||
" [ 0.29575 -0.013 0.56225]\n", | ||
" [ 0.546 -0.024 1.038 ]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Run the model and make the prediciton\n", | ||
"import numpy as np\n", | ||
"pred=nerual_network(inputs,weights)\n", | ||
"pred=np.array(pred)\n", | ||
"weights=np.array(weights)\n", | ||
"inputs=np.array(inputs)\n", | ||
"print 'Prediction:'+str(pred)\n", | ||
"true=np.array(true)\n", | ||
"error=(pred-true)**2\n", | ||
"print 'Error:'+str((error))\n", | ||
"delta=pred-true\n", | ||
"print 'Delta:'+str(delta)\n", | ||
"\n", | ||
"weights_delta=np.zeros(weights.shape)\n", | ||
"for i,x in np.ndenumerate(inputs):\n", | ||
" for j,v in np.ndenumerate(delta):\n", | ||
" weights_delta[i][j]=inputs[i]*delta[j]\n", | ||
"print 'Weights_Delta: '+str(weights_delta)\n", | ||
" \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 218, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[ 0.061325 0.1017 -0.373525 ]\n", | ||
" [ 0.0970425 0.20013 -0.0056225]\n", | ||
" [-0.00546 1.30024 0.08962 ]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Update the weights\n", | ||
"weights-=alpha*weights_delta\n", | ||
"print weights" | ||
] | ||
} | ||
], | ||
"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 | ||
} |
This file was deleted.
Oops, something went wrong.