You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by du...@apache.org on 2017/06/28 19:25:20 UTC

[1/2] systemml git commit: [SYSTEMML-1678] Add a new 1D top_k utility function

Repository: systemml
Updated Branches:
  refs/heads/master 9f808c43e -> 5e7e57774


[SYSTEMML-1678] Add a new 1D top_k utility function

This function computes the top k values (i.e. probabilities) and
associated indices (i.e. classes) from the input matrix X.  A typical
use case is that in which X is the output of a softmax layer, and
values and indices will contain rows with the top k probabilities and
class indices.

Closes #551.


Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/2e78eb9a
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/2e78eb9a
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/2e78eb9a

Branch: refs/heads/master
Commit: 2e78eb9a56148f9e27ea1cb646e2d6dd528d251e
Parents: 9f808c4
Author: Fei Hu <hu...@gmail.com>
Authored: Wed Jun 28 12:23:08 2017 -0700
Committer: Mike Dusenberry <mw...@us.ibm.com>
Committed: Wed Jun 28 12:23:08 2017 -0700

----------------------------------------------------------------------
 scripts/nn/test/run_tests.dml |  2 ++
 scripts/nn/test/test.dml      | 66 +++++++++++++++++++++++++++++++++++++-
 scripts/nn/util.dml           | 59 ++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/2e78eb9a/scripts/nn/test/run_tests.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/test/run_tests.dml b/scripts/nn/test/run_tests.dml
index cca0d0d..4cc2875 100644
--- a/scripts/nn/test/run_tests.dml
+++ b/scripts/nn/test/run_tests.dml
@@ -98,6 +98,8 @@ test::padding()
 test::tanh()
 test::threshold()
 test::transpose_NCHW_to_CNHW()
+test::top_k_row()
+test::top_k()
 
 print("---")
 print("Other tests complete -- look for any ERRORs or WARNINGs.")

http://git-wip-us.apache.org/repos/asf/systemml/blob/2e78eb9a/scripts/nn/test/test.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/test/test.dml b/scripts/nn/test/test.dml
index b0899e3..b1190fc 100644
--- a/scripts/nn/test/test.dml
+++ b/scripts/nn/test/test.dml
@@ -768,7 +768,7 @@ tanh = function() {
 
 threshold = function() {
   /*
-   * Test for threshold function.
+   * Test for the threshold function.
    */
   print("Testing the threshold function.")
 
@@ -788,3 +788,67 @@ threshold = function() {
   out = test_util::check_all_equal(indicator_matrix, target_matrix)
 }
 
+top_k_row = function() {
+  /*
+   * Test for the top_k function.
+   */
+  print("Testing the top_k_row function.")
+
+  #Generate data
+  X = matrix("2 3 2 1 19 20 31 12 3 4 5 60 9 2
+              3 6 15 18 6 12 1 17 3 0 4 19 1 6", rows=2, cols=14)
+  r = 2
+  k = 3
+  expected_values = matrix("19
+                            18
+                            17", rows=1, cols=3)
+  expected_indices = matrix("12
+                             4
+                             8", rows=1, cols=3)
+
+  # Test the top 3 for the second row
+  [values, indices] = util::top_k_row(X, 2, 3)
+  check_values = test_util::check_all_equal(values, expected_values)
+  check_indices = test_util::check_all_equal(indices, expected_indices)
+}
+
+top_k = function() {
+   /*
+    * Test for the top_k function.
+   */
+   print("Testing the top_k function.")
+
+   # Generate data
+   X = matrix("0.1 0.3 0.2 0.4
+               0.1 0.3 0.3 0.2", rows=2, cols=4)
+   expected_values_top1 = matrix("0.4
+                                  0.3", rows=2, cols=1)
+   expected_indices_top1 = matrix("4
+                                   2", rows=2, cols=1)
+   expected_values_top2 = matrix("0.4 0.3
+                                  0.3 0.3", rows=2, cols=2)
+   expected_indices_top2 = matrix("4 2
+                                   2 3", rows=2, cols=2)
+   expected_values_topAll = matrix("0.4 0.3 0.2 0.1
+                                    0.3 0.3 0.2 0.1", rows=2, cols=4)
+   expected_indices_topAll = matrix("4 2 3 1
+                                     2 3 4 1", rows=2, cols=4)
+
+   # test top_1
+   print("Case 1: test top_1")
+   [values_top1, indices_top1] = util::top_k(X, 1)
+   check_values_top1 = test_util::check_all_equal(values_top1, expected_values_top1)
+   check_indices_top1 = test_util::check_all_equal(indices_top1, expected_indices_top1)
+
+   # test top_2
+   print("Case 2: test top_2")
+   [values_top2, indices_top2] = util::top_k(X, 2)
+   check_values_top2 = test_util::check_all_equal(values_top2, expected_values_top2)
+   check_indices_top2 = test_util::check_all_equal(indices_top2, expected_indices_top2)
+
+   # test top_All
+   print("Case 3: test top_All")
+   [values_topAll, indices_topAll] = util::top_k(X, 4)
+   check_values_topAll = test_util::check_all_equal(values_topAll, expected_values_topAll)
+   check_indices_topAll = test_util::check_all_equal(indices_topAll, expected_indices_topAll)
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/2e78eb9a/scripts/nn/util.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/util.dml b/scripts/nn/util.dml
index 329f22f..fb54c43 100644
--- a/scripts/nn/util.dml
+++ b/scripts/nn/util.dml
@@ -230,6 +230,8 @@ transpose_NCHW_to_CNHW = function(matrix[double] X, int C) return (matrix[double
    * Outputs:
    *  - out: Transposed output with C rows.
    */
+
+  if(1==1){}
   N = nrow(X)
   D = ncol(X) / C
 
@@ -268,3 +270,60 @@ transpose_NCHW_to_CNHW = function(matrix[double] X, int C) return (matrix[double
    */
   out = matrix(permut %*% matrix(X, rows=N*C, cols=D), rows=C, cols=N*D)
 }
+
+top_k_row = function(matrix[double] X, integer r, integer k)
+    return (matrix[double] values, matrix[double] indices) {
+  /*
+   * Computes the top k values (i.e. probabilities) and associated
+   * indices (i.e. classes) in the rth row of the input matrix X
+   *
+   * Inputs:
+   * - X: Inputs, of shape (N D).
+   * - r: Input row number of X to look for
+   * - k: Input number of top elements to look for
+   *
+   * Outputs:
+   * - values: The top k values at the rth row, of shape
+   *    (1, k)
+   * - indices: The class indices, of shape (1, k)
+   */
+
+  #TODO: do r & k need to be checked in the valid range
+  row = X[r, ]
+  row_t = t(row)
+  indices = order(target=row_t, by=1, decreasing=TRUE, index.return=TRUE)
+  indices = t(indices)
+  indices = indices[1, 1:k]
+
+  values = matrix(0, rows=1, cols=k)
+  for (i in 1:k) {
+    values[1, i] = row[1, as.scalar(indices[1, i])]
+  }
+}
+
+top_k = function(matrix[double] X, integer k)
+     return (matrix[double] values, matrix[double] indices) {
+  /*
+   * Computes the top k values (i.e. probabilities) and associated
+   * indices (i.e. classes) for the input matrix X.
+   *
+   * Inputs:
+   * - X: Inputs, of shape (N D).
+   * - k: Input number of top elements to look for
+   *
+   * Outputs:
+   * - values: The top k values along a certain dimension, of shape
+   *    (N, k)
+   * - indices: The indices of classes, of shape (N, K)
+   */
+  N = nrow(X)
+  D = ncol(X)
+  values = matrix(0, rows=N, cols=k)
+  indices = matrix(0, rows=N, cols=k)
+
+  parfor (r in 1:N) {
+    [value, index] = top_k_row(X, r, k)
+    values[r, ] = value
+    indices[r, ] = index
+  }
+}


[2/2] systemml git commit: [SYSTEMML-1736] Add a new 2D top_k utility function

Posted by du...@apache.org.
[SYSTEMML-1736] Add a new 2D top_k utility function

This function computes the top k values (i.e. probabilities) and
associated indices (i.e. classes) from the input matrix X. A typical use
case here is that in which X is the output of a 2D softmax layer, so
each channel contains a set of normalized class probalilities, and
values and indices will contain the top k probabilities and indices
along the channel axis. The scenario will be common in an image
segmentation problem.

Closes #551.


Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/5e7e5777
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/5e7e5777
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/5e7e5777

Branch: refs/heads/master
Commit: 5e7e57774b1936736d8551bacf3dffd60bc45071
Parents: 2e78eb9
Author: Fei Hu <hu...@gmail.com>
Authored: Wed Jun 28 12:23:13 2017 -0700
Committer: Mike Dusenberry <mw...@us.ibm.com>
Committed: Wed Jun 28 12:23:13 2017 -0700

----------------------------------------------------------------------
 scripts/nn/test/run_tests.dml |  1 +
 scripts/nn/test/test.dml      | 85 ++++++++++++++++++++++++++++++++++----
 scripts/nn/util.dml           | 62 ++++++++++++++++++++-------
 3 files changed, 127 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/5e7e5777/scripts/nn/test/run_tests.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/test/run_tests.dml b/scripts/nn/test/run_tests.dml
index 4cc2875..f4c33d8 100644
--- a/scripts/nn/test/run_tests.dml
+++ b/scripts/nn/test/run_tests.dml
@@ -100,6 +100,7 @@ test::threshold()
 test::transpose_NCHW_to_CNHW()
 test::top_k_row()
 test::top_k()
+test::top_k2d()
 
 print("---")
 print("Other tests complete -- look for any ERRORs or WARNINGs.")

http://git-wip-us.apache.org/repos/asf/systemml/blob/5e7e5777/scripts/nn/test/test.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/test/test.dml b/scripts/nn/test/test.dml
index b1190fc..a1bb6cd 100644
--- a/scripts/nn/test/test.dml
+++ b/scripts/nn/test/test.dml
@@ -493,17 +493,17 @@ transpose_NCHW_to_CNHW = function() {
    * Test for `transpose_NCHW_to_CNHW` function.
    */
   print("Testing transpose_NCHW_to_CNHW function.")
-  
+
   # Generate data
   N = 2
   C = 3
   H = 4
   W = 5
   X = matrix(seq(1, N*C*H*W), rows=N, cols=C*H*W)
-  
+
   out = util::transpose_NCHW_to_CNHW(X, C)
-  
-  target = 
+
+  target =
     matrix("1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20
             61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
             21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40
@@ -511,7 +511,7 @@ transpose_NCHW_to_CNHW = function() {
             41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60
             101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120",
            rows=C, cols=N*H*W)
-  
+
   # Equivalency check
   for (i in 1:nrow(out)) {
     for(j in 1:ncol(out)) {
@@ -790,7 +790,7 @@ threshold = function() {
 
 top_k_row = function() {
   /*
-   * Test for the top_k function.
+   * Test for the top_k_row function.
    */
   print("Testing the top_k_row function.")
 
@@ -806,7 +806,7 @@ top_k_row = function() {
                              4
                              8", rows=1, cols=3)
 
-  # Test the top 3 for the second row
+  # Test the top 3 for the second row.
   [values, indices] = util::top_k_row(X, 2, 3)
   check_values = test_util::check_all_equal(values, expected_values)
   check_indices = test_util::check_all_equal(indices, expected_indices)
@@ -852,3 +852,74 @@ top_k = function() {
    check_values_topAll = test_util::check_all_equal(values_topAll, expected_values_topAll)
    check_indices_topAll = test_util::check_all_equal(indices_topAll, expected_indices_topAll)
 }
+
+top_k2d = function() {
+  /*
+   * Test for the top_k2d function.
+  */
+  print("Testing the top_k2d function.")
+  # Generate data, of shape (2, 3, 3, 4)
+  k = 2
+  X = matrix("0.1 0.4 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.7 0.3 0.2
+
+              0.2 0.5 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.8 0.3 0.2
+
+              0.3 0.4 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.2 0.3 0.2
+
+              0.1 0.4 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.7 0.3 0.2
+
+              0.2 0.5 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.8 0.3 0.2
+
+              0.3 0.4 0.4 0.5
+              0.4 0.1 0.6 0.1
+              0.7 0.2 0.3 0.2", rows=2, cols=3*3*4)
+
+  expected_values = matrix("0.3  0.5  0.4  0.5
+                            0.4  0.1  0.6  0.1
+                            0.7  0.8  0.3  0.2
+
+                            0.2  0.4  0.4  0.5
+                            0.4  0.1  0.6  0.1
+                            0.7  0.7  0.3  0.2
+
+                            0.3  0.5  0.4  0.5
+                            0.4  0.1  0.6  0.1
+                            0.7  0.8  0.3  0.2
+
+                            0.2  0.4  0.4  0.5
+                            0.4  0.1  0.6  0.1
+                            0.7  0.7  0.3  0.2", rows=2, cols=2*3*4)
+
+  expected_indices = matrix("3  2  1  1
+                             1  1  1  1
+                             1  2  1  1
+
+                             2  1  2  2
+                             2  2  2  2
+                             2  1  2  2
+
+                             3  2  1  1
+                             1  1  1  1
+                             1  2  1  1
+
+                             2  1  2  2
+                             2  2  2  2
+                             2  1  2  2", rows=2, cols=24)
+
+  [values, indices] = util::top_k2d(X, k, 3, 3, 4)
+
+  # Equivalency check
+  check_values = test_util::check_all_equal(values, expected_values)
+  check_indices = test_util::check_all_equal(indices, expected_indices)
+}
+

http://git-wip-us.apache.org/repos/asf/systemml/blob/5e7e5777/scripts/nn/util.dml
----------------------------------------------------------------------
diff --git a/scripts/nn/util.dml b/scripts/nn/util.dml
index fb54c43..102a507 100644
--- a/scripts/nn/util.dml
+++ b/scripts/nn/util.dml
@@ -204,7 +204,7 @@ threshold = function(matrix[double] X, double thresh)
     return (matrix[double] out) {
   /*
    * Computes an indicator matrix with values in {0, 1} depending on
-   * whether or not the values in X are above the input threshold
+   * whether or not the values in X are above the input threshold.
    *
    * Inputs:
    *  - X: Inputs, of shape (any, any).
@@ -231,7 +231,6 @@ transpose_NCHW_to_CNHW = function(matrix[double] X, int C) return (matrix[double
    *  - out: Transposed output with C rows.
    */
 
-  if(1==1){}
   N = nrow(X)
   D = ncol(X) / C
 
@@ -275,17 +274,17 @@ top_k_row = function(matrix[double] X, integer r, integer k)
     return (matrix[double] values, matrix[double] indices) {
   /*
    * Computes the top k values (i.e. probabilities) and associated
-   * indices (i.e. classes) in the rth row of the input matrix X
+   * indices (i.e. classes) in the rth row of the input matrix X.
    *
    * Inputs:
-   * - X: Inputs, of shape (N D).
-   * - r: Input row number of X to look for
-   * - k: Input number of top elements to look for
+   *  - X: Inputs, of shape (N, D).
+   *  - r: Input row number of X to look for.
+   *  - k: Input number of top elements to look for.
    *
    * Outputs:
-   * - values: The top k values at the rth row, of shape
-   *    (1, k)
-   * - indices: The class indices, of shape (1, k)
+   *  - values: The top k values at the rth row, of shape
+   *    (1, k).
+   *  - indices: The class indices, of shape (1, k).
    */
 
   #TODO: do r & k need to be checked in the valid range
@@ -308,13 +307,13 @@ top_k = function(matrix[double] X, integer k)
    * indices (i.e. classes) for the input matrix X.
    *
    * Inputs:
-   * - X: Inputs, of shape (N D).
-   * - k: Input number of top elements to look for
+   *  - X: Inputs, of shape (N, D).
+   *  - k: Input number of top elements to look for.
    *
    * Outputs:
-   * - values: The top k values along a certain dimension, of shape
-   *    (N, k)
-   * - indices: The indices of classes, of shape (N, K)
+   *  - values: The top k values along a certain dimension, of shape
+   *    (N, k).
+   *  - indices: The indices of classes, of shape (N, K).
    */
   N = nrow(X)
   D = ncol(X)
@@ -327,3 +326,38 @@ top_k = function(matrix[double] X, integer k)
     indices[r, ] = index
   }
 }
+
+top_k2d = function(matrix[double] X, int k, int C, int Hin, int Win)
+     return (matrix[double] values, matrix[double] indices) {
+  /*
+   * Computes the top k values (i.e. probabilities) and associated
+   * indices (i.e. classes) for the input matrix X.
+   *
+   * Inputs:
+   *  - X: Inputs, of shape (N, C*Hin*Win).
+   *  - k: Input number of top elements to look for.
+   *  - C: Number of input channels (dimensionality of input depth).
+   *  - Hin: Input height.
+   *  - Win: Input width.
+   *
+   * Outputs:
+   *  - values: The top k values along a certain dimension, of shape
+   *    (N, k*Hin*Win).
+   *  - indices: The indices of classes, of shape (N, k*Hin*Win).
+   */
+  N = nrow(X)
+
+  # Reshape the input matrix (N, C*Hin*Win) to (N*Hin*Win, C)
+  X_C_NHW = transpose_NCHW_to_CNHW(X, C)
+  X_NHW_C = t(X_C_NHW)
+
+  # Compute the top k for the reshape matrix.
+  [values_NHW_K, indices_NHW_K] = top_k(X_NHW_C, k)  # shape: (N*Hin*Win, k)
+
+  values_K_NHW = t(values_NHW_K)
+  indices_K_NHW = t(indices_NHW_K)
+
+  values =  transpose_NCHW_to_CNHW(values_K_NHW, N)
+  indices = transpose_NCHW_to_CNHW(indices_K_NHW, N)
+}
+