| 172 | |
| 173 | template <typename EXP1, typename EXP2> |
| 174 | void train ( |
| 175 | const matrix_exp<EXP1>& example_in, |
| 176 | const matrix_exp<EXP2>& example_out |
| 177 | ) |
| 178 | { |
| 179 | for (long i = 0; i < example_in.nr(); ++i) |
| 180 | z(i) = example_in(i); |
| 181 | // insert the bias |
| 182 | z(z.nr()-1) = -1; |
| 183 | |
| 184 | tmp1 = sigmoid(w1*z); |
| 185 | // insert the bias |
| 186 | tmp1(tmp1.nr()-1) = -1; |
| 187 | |
| 188 | |
| 189 | if (second_hidden_nodes == 0) |
| 190 | { |
| 191 | o = sigmoid(w3*tmp1); |
| 192 | |
| 193 | // now compute the errors and propagate them backwards though the network |
| 194 | e3 = pointwise_multiply(example_out-o, uniform_matrix<double>(output_nodes,1,1.0)-o, o); |
| 195 | e1 = pointwise_multiply(tmp1, uniform_matrix<double>(first_hidden_nodes+1,1,1.0) - tmp1, trans(w3)*e3 ); |
| 196 | |
| 197 | // compute the new weight updates |
| 198 | w3m = alpha * e3*trans(tmp1) + w3m*momentum; |
| 199 | w1m = alpha * e1*trans(z) + w1m*momentum; |
| 200 | |
| 201 | // now update the weights |
| 202 | w1 += w1m; |
| 203 | w3 += w3m; |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | tmp2 = sigmoid(w2*tmp1); |
| 208 | // insert the bias |
| 209 | tmp2(tmp2.nr()-1) = -1; |
| 210 | |
| 211 | o = sigmoid(w3*tmp2); |
| 212 | |
| 213 | |
| 214 | // now compute the errors and propagate them backwards though the network |
| 215 | e3 = pointwise_multiply(example_out-o, uniform_matrix<double>(output_nodes,1,1.0)-o, o); |
| 216 | e2 = pointwise_multiply(tmp2, uniform_matrix<double>(second_hidden_nodes+1,1,1.0) - tmp2, trans(w3)*e3 ); |
| 217 | e1 = pointwise_multiply(tmp1, uniform_matrix<double>(first_hidden_nodes+1,1,1.0) - tmp1, trans(w2)*e2 ); |
| 218 | |
| 219 | // compute the new weight updates |
| 220 | w3m = alpha * e3*trans(tmp2) + w3m*momentum; |
| 221 | w2m = alpha * e2*trans(tmp1) + w2m*momentum; |
| 222 | w1m = alpha * e1*trans(z) + w1m*momentum; |
| 223 | |
| 224 | // now update the weights |
| 225 | w1 += w1m; |
| 226 | w2 += w2m; |
| 227 | w3 += w3m; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | template <typename EXP> |