|
2 | 2 | backprop2
|
3 | 3 | ~~~~~~~~~
|
4 | 4 |
|
5 |
| -This is a minor variation on the backprop module. The most |
6 |
| -significant changes are: (1) It uses the cross-entropy cost by |
7 |
| -default, instead of the quadratic cost; and (2) The weights and bias |
8 |
| -for each neuron are initialized as a Gaussian random variable whose |
9 |
| -mean is zero and standard deviation is one over the square-root of the |
10 |
| -neuron's fan-in (instead of having mean zero and standard deviation |
11 |
| -one). |
| 5 | +This is a minor variation on the backprop module. The main changes |
| 6 | +are: (1) It uses the cross-entropy cost by default, instead of the |
| 7 | +quadratic cost; (2) The weights and bias for each neuron are |
| 8 | +initialized as a Gaussian random variable whose mean is zero and |
| 9 | +standard deviation is one over the square-root of the neuron's fan-in |
| 10 | +(instead of having mean zero and standard deviation one); and (3) The |
| 11 | +feedforward method has been generalized to make it easier to propagate |
| 12 | +activations through part of the network. |
12 | 13 | """
|
13 | 14 |
|
14 | 15 | #### Libraries
|
@@ -37,9 +38,14 @@ def __init__(self, sizes):
|
37 | 38 | self.weights = [np.random.randn(y, x)/np.sqrt(y)
|
38 | 39 | for x, y in zip(sizes[:-1], sizes[1:])]
|
39 | 40 |
|
40 |
| - def feedforward(self, a): |
41 |
| - "Return the output of the network if ``a`` is input." |
42 |
| - for b, w in zip(self.biases, self.weights): |
| 41 | + def feedforward(self, a, start=0, end=None): |
| 42 | + """Return the result from feeding forward the activation ``a`` |
| 43 | + from layer ``start`` through to layer ``end``. Note that if |
| 44 | + ``end`` is ``None`` then this is interpreted as ``end = |
| 45 | + self.num_layers``, i.e., the default behaviour is to propagate |
| 46 | + through to the end of the network.""" |
| 47 | + end = self.num_layers if end == None |
| 48 | + for b, w in zip(self.biases, self.weights)[start:end]: |
43 | 49 | a = sigmoid_vec(np.dot(w, a)+b)
|
44 | 50 | return a
|
45 | 51 |
|
@@ -167,29 +173,6 @@ def evaluate_training_results(self, training_data):
|
167 | 173 | return sum(int(x == y)
|
168 | 174 | for x, y in zip(training_results, actual_training_results))
|
169 | 175 |
|
170 |
| - def initial_feedforward(self, input_data, j): |
171 |
| - """ |
172 |
| - Feedforward the elements ``x`` in the list ``input_data`` |
173 |
| - through the network until the ``j``th layer. Return the list |
174 |
| - of activations from the ``j``th layer. |
175 |
| - """ |
176 |
| - for k in range(j): |
177 |
| - intermediate_data = [ |
178 |
| - sigmoid_vec(np.dot(self.weights[k], x)+self.biases[k]) |
179 |
| - for x in input_data] |
180 |
| - return intermediate_data |
181 |
| - |
182 |
| - def final_feedforward(self, intermediate_data, j): |
183 |
| - """ |
184 |
| - Feedforward the elements ``x`` in the list |
185 |
| - ``intermediate_data`` through the network to the output. The |
186 |
| - elements in ``intermediate_data`` are assumed to be inputs to |
187 |
| - the ``j``th layer.""" |
188 |
| - for k in range(j, len(self.weights)): |
189 |
| - output_data = [ |
190 |
| - sigmoid_vec(np.dot(self.weights[k], a)+self.biases[k]) |
191 |
| - for a in intermediate_data] |
192 |
| - return output_data |
193 | 176 |
|
194 | 177 | #### Miscellaneous functions
|
195 | 178 | def minimal_cross_entropy(training_data):
|
|
0 commit comments