Repository: incubator-systemml Updated Branches: refs/heads/master d926ea052 -> 27a6a2b2a
[SYSTEMML-618][SYSTEMML-803] Improving performance of the LeNet algorithm. This improves performance of the LeNet algorithm during training and prediction by batching the validation and testing data within the `predict` function. This allows one to compute model predictions over any size of data without incurring high memory usage due to large intermediates. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/27a6a2b2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/27a6a2b2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/27a6a2b2 Branch: refs/heads/master Commit: 27a6a2b2a1764ed176c5d3d58bb8e74296c85f81 Parents: d926ea0 Author: Mike Dusenberry <[email protected]> Authored: Wed Jul 13 16:14:54 2016 -0700 Committer: Mike Dusenberry <[email protected]> Committed: Wed Jul 13 16:14:54 2016 -0700 ---------------------------------------------------------------------- .../SystemML-NN/examples/mnist_lenet.dml | 46 +++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/27a6a2b2/scripts/staging/SystemML-NN/examples/mnist_lenet.dml ---------------------------------------------------------------------- diff --git a/scripts/staging/SystemML-NN/examples/mnist_lenet.dml b/scripts/staging/SystemML-NN/examples/mnist_lenet.dml index b9ece2f..3ae0d0f 100644 --- a/scripts/staging/SystemML-NN/examples/mnist_lenet.dml +++ b/scripts/staging/SystemML-NN/examples/mnist_lenet.dml @@ -222,6 +222,8 @@ predict = function(matrix[double] X, int C, int Hin, int Win, * Outputs: * - probs: Class probabilities, of shape (N, K). */ + N = nrow(X) + # Network: # conv1 -> relu1 -> pool1 -> conv2 -> relu2 -> pool2 -> affine3 -> relu3 -> affine4 -> softmax Hf = 5 # filter height @@ -234,21 +236,35 @@ predict = function(matrix[double] X, int C, int Hin, int Win, N3 = ncol(W3) # num nodes in affine3 K = ncol(W4) # num nodes in affine4, equal to number of target dimensions (num classes) - # Compute forward pass - ## layer 1: conv1 -> relu1 -> pool1 - [outc1, Houtc1, Woutc1] = conv::forward(X, W1, b1, C, Hin, Win, Hf, Wf, stride, stride, pad, pad) - outr1 = relu::forward(outc1) - [outp1, Houtp1, Woutp1] = max_pool::forward(outr1, F1, Houtc1, Woutc1, Hf=2, Wf=2, strideh=2, stridew=2) - ## layer 2: conv2 -> relu2 -> pool2 - [outc2, Houtc2, Woutc2] = conv::forward(outp1, W2, b2, F1, Houtp1, Woutp1, Hf, Wf, stride, stride, pad, pad) - outr2 = relu::forward(outc2) - [outp2, Houtp2, Woutp2] = max_pool::forward(outr2, F2, Houtc2, Woutc2, Hf=2, Wf=2, strideh=2, stridew=2) - ## layer 3: affine3 -> relu3 - outa3 = affine::forward(outp2, W3, b3) - outr3 = relu::forward(outa3) - ## layer 4: affine4 -> softmax - outa4 = affine::forward(outr3, W4, b4) - probs = softmax::forward(outa4) + # Compute predictions over mini-batches + probs = matrix(0, rows=N, cols=K) + batch_size = 64 + iters = ceil(N / batch_size) + for(i in 1:iters) { + # Get next batch + beg = ((i-1) * batch_size) %% N + 1 + end = min(N, beg + batch_size - 1) + X_batch = X[beg:end,] + + # Compute forward pass + ## layer 1: conv1 -> relu1 -> pool1 + [outc1, Houtc1, Woutc1] = conv::forward(X_batch, W1, b1, C, Hin, Win, Hf, Wf, stride, stride, pad, pad) + outr1 = relu::forward(outc1) + [outp1, Houtp1, Woutp1] = max_pool::forward(outr1, F1, Houtc1, Woutc1, Hf=2, Wf=2, strideh=2, stridew=2) + ## layer 2: conv2 -> relu2 -> pool2 + [outc2, Houtc2, Woutc2] = conv::forward(outp1, W2, b2, F1, Houtp1, Woutp1, Hf, Wf, stride, stride, pad, pad) + outr2 = relu::forward(outc2) + [outp2, Houtp2, Woutp2] = max_pool::forward(outr2, F2, Houtc2, Woutc2, Hf=2, Wf=2, strideh=2, stridew=2) + ## layer 3: affine3 -> relu3 + outa3 = affine::forward(outp2, W3, b3) + outr3 = relu::forward(outa3) + ## layer 4: affine4 -> softmax + outa4 = affine::forward(outr3, W4, b4) + probs_batch = softmax::forward(outa4) + + # Store predictions + probs[beg:end,] = probs_batch + } } eval = function(matrix[double] probs, matrix[double] y)
