changeset 9:8dd249e99b5b default tip

Optimisations for backprop
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Fri, 11 Nov 2011 20:36:02 -0500
parents 5c236ac72938
children
files nnCostFunction.m
diffstat 1 files changed, 4 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/nnCostFunction.m
+++ b/nnCostFunction.m
@@ -42,19 +42,15 @@
       
       ## The regularisation term has to exclude the first column of the Thetas,
       ## because we don't regularise the bias nodes.
-      + lambda*(sum (Theta1(:, 2:end)(:).^2)                       \
-                + sum (Theta2(:, 2:end)(:).^2))/(2*m);
+      + lambda*(sumsq (Theta1(:, 2:end)(:))                         \
+                + sumsq (Theta2(:, 2:end)(:)))/(2*m);
 
   ## Backprop
   delta3 = a3 - y_idx;
   delta2 = (delta3*Theta2)(:, 2:end) .* sigmoidGradient (z2);
 
-  Theta2_grad = sum (bsxfun (@times, permute (delta3, [2, 3, 1]),
-                             permute ([one_vec, a2], [3, 2, 1])),
-                     3)/m;
-  Theta1_grad = sum (bsxfun (@times, permute (delta2, [2, 3, 1]),
-                             permute ([one_vec, a1], [3, 2, 1])),
-                     3)/m;
+  Theta2_grad = delta3' * [one_vec, a2] / m;
+  Theta1_grad = delta2' * [one_vec, a1] / m;
 
   ## Add regularisation terms
   Theta2_grad(:, 2:end) += Theta2(:, 2:end)*lambda/m;