From da8e6ee81221c63a05a1a41bd4c5c51d0a0015e6 Mon Sep 17 00:00:00 2001 From: Alluxia-F <1665627261@qq.com> Date: Wed, 12 Jul 2017 13:53:02 -0400 Subject: [PATCH] Updates Updates --- ...adient Descent With Multiple Outputs.ipynb | 29 ++-- ...scent with Multiple Inputs & Outputs.ipynb | 136 ++++++++++++++++++ ...adient Descent With Multiple Outputs.ipynb | 105 -------------- 3 files changed, 155 insertions(+), 115 deletions(-) create mode 100644 Chapter 5/6. Gradient Descent with Multiple Inputs & Outputs.ipynb delete mode 100644 Chapter 5/Gradient Descent With Multiple Outputs.ipynb diff --git a/Chapter 5/5. Gradient Descent With Multiple Outputs.ipynb b/Chapter 5/5. Gradient Descent With Multiple Outputs.ipynb index 18c6e26..a6e5912 100644 --- a/Chapter 5/5. Gradient Descent With Multiple Outputs.ipynb +++ b/Chapter 5/5. Gradient Descent With Multiple Outputs.ipynb @@ -2,8 +2,10 @@ "cells": [ { "cell_type": "code", - "execution_count": 19, - "metadata": {}, + "execution_count": 27, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create an empty network with multiple outputs\n", @@ -22,14 +24,14 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Prediction: [ 0.195 0.13 0.585]\n", + "Prediction:[ 0.195 0.13 0.585]\n", "Error: [ 0.009025 0.7569 0.235225]\n", "Delta: [ 0.095 -0.87 0.485]\n", "weights_delta:[ 0.06175 -0.5655 0.31525]\n" @@ -47,7 +49,7 @@ "true=[hurt[0],win[0],sad[0]]\n", "\n", "pred=nerual_network(inputs,weights)\n", - "print 'Prediction: '+str(pred)\n", + "print 'Prediction:'+str(pred)\n", "error=(pred-true)**2\n", "print 'Error: '+str(error)\n", "delta=pred-true\n", @@ -71,11 +73,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m Note: There are so many mistakes on the diagram\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], "source": [ "Note: There are so many mistakes on the diagram" ] diff --git a/Chapter 5/6. Gradient Descent with Multiple Inputs & Outputs.ipynb b/Chapter 5/6. Gradient Descent with Multiple Inputs & Outputs.ipynb new file mode 100644 index 0000000..b21aa2e --- /dev/null +++ b/Chapter 5/6. Gradient Descent with Multiple Inputs & Outputs.ipynb @@ -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 +} diff --git a/Chapter 5/Gradient Descent With Multiple Outputs.ipynb b/Chapter 5/Gradient Descent With Multiple Outputs.ipynb deleted file mode 100644 index 18c6e26..0000000 --- a/Chapter 5/Gradient Descent With Multiple Outputs.ipynb +++ /dev/null @@ -1,105 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "# Create an empty network with multiple outputs\n", - "\n", - "import numpy as np\n", - "weights=[0.3,0.2,0.9]\n", - "\n", - "def nerual_network(inputs,weights):\n", - " prediction=ele_mul(inputs,weights)\n", - " return prediction\n", - "\n", - "def ele_mul(inputs,weights):\n", - " output=inputs*np.array(weights)\n", - " return output\n" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Prediction: [ 0.195 0.13 0.585]\n", - "Error: [ 0.009025 0.7569 0.235225]\n", - "Delta: [ 0.095 -0.87 0.485]\n", - "weights_delta:[ 0.06175 -0.5655 0.31525]\n" - ] - } - ], - "source": [ - "# Predict: make a prediction adn calculate error and delta\n", - "wlrec=[0.65,1.0,1.0,0.9]\n", - "hurt=[0.1,0.0,0.0,0.1]\n", - "win=[1,1,0,1]\n", - "sad=[0.1,0,0.1,0.2]\n", - "\n", - "inputs=wlrec[0]\n", - "true=[hurt[0],win[0],sad[0]]\n", - "\n", - "pred=nerual_network(inputs,weights)\n", - "print 'Prediction: '+str(pred)\n", - "error=(pred-true)**2\n", - "print 'Error: '+str(error)\n", - "delta=pred-true\n", - "print 'Delta: '+str(delta)\n", - "weights_delta=inputs*delta\n", - "print 'weights_delta:'+str(weights_delta)\n", - "\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Update weights\n", - "alpha=0.1\n", - "weights-=alpha*weights_delta\n", - "print 'Weights:'+str(weights)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "Note: There are so many mistakes on the diagram" - ] - } - ], - "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 -}