|
19 | 19 | },
|
20 | 20 | "outputs": [],
|
21 | 21 | "source": [
|
22 |
| - "from learning import *" |
| 22 | + "from learning import *\n", |
| 23 | + "\n", |
| 24 | + "from notebook import psource, pseudocode" |
23 | 25 | ]
|
24 | 26 | },
|
25 | 27 | {
|
|
56 | 58 | "\n",
|
57 | 59 | "After that we will create our neural network in the `network` function. This function will make the necessary connections between the input layer, hidden layer and output layer. With the network ready, we will use the `BackPropagationLearner` to train the weights of our network for the examples provided in the dataset.\n",
|
58 | 60 | "\n",
|
59 |
| - "The NeuralNetLearner returns the `predict` function, which can receive an example and feed-forward it into our network to generate a prediction." |
| 61 | + "The NeuralNetLearner returns the `predict` function which, in short, can receive an example and feed-forward it into our network to generate a prediction.\n", |
| 62 | + "\n", |
| 63 | + "In more detail, the example values are first passed to the input layer and then they are passed through the rest of the layers. Each node calculates the dot product of its inputs and its weights, activates it and pushes it to the next layer. The final prediction is the node with the maximum value from the output layer." |
60 | 64 | ]
|
61 | 65 | },
|
62 | 66 | {
|
63 | 67 | "cell_type": "code",
|
64 |
| - "execution_count": 2, |
65 |
| - "metadata": { |
66 |
| - "collapsed": true |
67 |
| - }, |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
68 | 70 | "outputs": [],
|
69 | 71 | "source": [
|
70 |
| - "%psource NeuralNetLearner" |
| 72 | + "psource(NeuralNetLearner)" |
71 | 73 | ]
|
72 | 74 | },
|
73 | 75 | {
|
|
101 | 103 | "We can use the same technique for the weights in the input layer as well. After we have the gradients for both weights, we use gradient descent to update the weights of the network."
|
102 | 104 | ]
|
103 | 105 | },
|
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "### Pseudocode" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 3, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [ |
| 118 | + { |
| 119 | + "data": { |
| 120 | + "text/markdown": [ |
| 121 | + "### AIMA3e\n", |
| 122 | + "__function__ BACK-PROP-LEARNING(_examples_, _network_) __returns__ a neural network \n", |
| 123 | + " __inputs__ _examples_, a set of examples, each with input vector __x__ and output vector __y__ \n", |
| 124 | + "    _network_, a multilayer network with _L_ layers, weights _w<sub>i,j</sub>_, activation function _g_ \n", |
| 125 | + " __local variables__: Δ, a vector of errors, indexed by network node \n", |
| 126 | + "\n", |
| 127 | + " __repeat__ \n", |
| 128 | + "   __for each__ weight _w<sub>i,j</sub>_ in _network_ __do__ \n", |
| 129 | + "     _w<sub>i,j</sub>_ ← a small random number \n", |
| 130 | + "   __for each__ example (__x__, __y__) __in__ _examples_ __do__ \n", |
| 131 | + "     /\\* _Propagate the inputs forward to compute the outputs_ \\*/ \n", |
| 132 | + "     __for each__ node _i_ in the input layer __do__ \n", |
| 133 | + "       _a<sub>i</sub>_ ← _x<sub>i</sub>_ \n", |
| 134 | + "     __for__ _l_ = 2 __to__ _L_ __do__ \n", |
| 135 | + "       __for each__ node _j_ in layer _l_ __do__ \n", |
| 136 | + "         _in<sub>j</sub>_ ← Σ<sub>_i_</sub> _w<sub>i,j</sub>_ _a<sub>i</sub>_ \n", |
| 137 | + "         _a<sub>j</sub>_ ← _g_(_in<sub>j</sub>_) \n", |
| 138 | + "     /\\* _Propagate deltas backward from output layer to input layer_ \\*/ \n", |
| 139 | + "     __for each__ node _j_ in the output layer __do__ \n", |
| 140 | + "       Δ\\[_j_\\] ← _g_′(_in<sub>j</sub>_) × (_y<sub>i</sub>_ − _a<sub>j</sub>_) \n", |
| 141 | + "     __for__ _l_ = _L_ − 1 __to__ 1 __do__ \n", |
| 142 | + "       __for each__ node _i_ in layer _l_ __do__ \n", |
| 143 | + "         Δ\\[_i_\\] ← _g_′(_in<sub>i</sub>_) Σ<sub>_j_</sub> _w<sub>i,j</sub>_ Δ\\[_j_\\] \n", |
| 144 | + "     /\\* _Update every weight in network using deltas_ \\*/ \n", |
| 145 | + "     __for each__ weight _w<sub>i,j</sub>_ in _network_ __do__ \n", |
| 146 | + "       _w<sub>i,j</sub>_ ← _w<sub>i,j</sub>_ + _α_ × _a<sub>i</sub>_ × Δ\\[_j_\\] \n", |
| 147 | + "  __until__ some stopping criterion is satisfied \n", |
| 148 | + "  __return__ _network_ \n", |
| 149 | + "\n", |
| 150 | + "---\n", |
| 151 | + "__Figure ??__ The back\\-propagation algorithm for learning in multilayer networks." |
| 152 | + ], |
| 153 | + "text/plain": [ |
| 154 | + "<IPython.core.display.Markdown object>" |
| 155 | + ] |
| 156 | + }, |
| 157 | + "execution_count": 3, |
| 158 | + "metadata": {}, |
| 159 | + "output_type": "execute_result" |
| 160 | + } |
| 161 | + ], |
| 162 | + "source": [ |
| 163 | + "pseudocode('Back-Prop-Learning')" |
| 164 | + ] |
| 165 | + }, |
104 | 166 | {
|
105 | 167 | "cell_type": "markdown",
|
106 | 168 | "metadata": {},
|
|
112 | 174 | },
|
113 | 175 | {
|
114 | 176 | "cell_type": "code",
|
115 |
| - "execution_count": 3, |
| 177 | + "execution_count": null, |
116 | 178 | "metadata": {},
|
117 | 179 | "outputs": [],
|
118 | 180 | "source": [
|
119 |
| - "%psource BackPropagationLearner" |
| 181 | + "psource(BackPropagationLearner)" |
120 | 182 | ]
|
121 | 183 | },
|
122 | 184 | {
|
|
0 commit comments