You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@systemds.apache.org by GitBox <gi...@apache.org> on 2020/09/30 10:39:00 UTC

[GitHub] [systemds] gabrielaozegovic commented on a change in pull request #1070: Rewriting ALS functions

gabrielaozegovic commented on a change in pull request #1070:
URL: https://github.com/apache/systemds/pull/1070#discussion_r497410970



##########
File path: scripts/builtin/als_cg.dml
##########
@@ -0,0 +1,172 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+#  
+# THIS SCRIPT COMPUTES AN APPROXIMATE FACTORIZATION OF A LOW-RANK MATRIX X INTO TWO MATRICES U AND V
+# USING THE ALTERNATING-LEAST-SQUARES (ALS) ALGORITHM WITH CONJUGATE GRADIENT.
+# MATRICES U AND V ARE COMPUTED BY MINIMIZING A LOSS FUNCTION (WITH REGULARIZATION).
+#
+# INPUT   PARAMETERS:
+# ---------------------------------------------------------------------------------------------
+# NAME    TYPE     DEFAULT  MEANING
+# ---------------------------------------------------------------------------------------------
+# X       String   ---      Location to read the input matrix X to be factorized
+# U       String   ---      Location to write the factor matrix U
+# V       String   ---      Location to write the factor matrix V
+# rank    Int      10       Rank of the factorization
+# reg     String   "L2"	    Regularization: 
+#                           "L2" = L2 regularization;
+#                           "wL2" = weighted L2 regularization
+# lambda  Double   0.000001 Regularization parameter, no regularization if 0.0
+# maxi    Int      50       Maximum number of iterations
+# check   Boolean  TRUE     Check for convergence after every iteration, i.e., updating U and V once
+# thr     Double   0.0001   Assuming check is set to TRUE, the algorithm stops and convergence is declared 
+#                           if the decrease in loss in any two consecutive iterations falls below this threshold; 
+#                           if check is FALSE thr is ignored
+# fmt     String   "text"   The output format of the factor matrices L and R, such as "text" or "csv"
+# ---------------------------------------------------------------------------------------------
+# OUTPUT: 
+# 1- An m x r matrix U, where r is the factorization rank 
+# 2- An r x n matrix V
+#
+# HOW TO INVOKE THIS SCRIPT - EXAMPLE:
+# hadoop jar SystemDS.jar -f ALS-CG.dml -nvargs X=INPUT_DIR/X U=OUTPUT_DIR/U V=OUTPUT_DIR/V rank=10 reg="L2" lambda=0.0001 fmt=csv
+
+
+m_als_cg = function(Matrix[Double] X, Integer rank = 10, String reg = "L2", Double lambda = 0.000001, Integer maxi = 50, Boolean check = TRUE, Double thr = 0.0001, String fmt = "text")
+    return (Matrix[Double] U, Matrix[Double] V)
+    {
+
+    r = rank;
+    max_iter = maxi;
+    fmt0 = fmt;
+
+    ###### MAIN PART ######
+    m = nrow (X);
+    n = ncol (X);
+
+    # initializing factor matrices
+    U = rand (rows = m, cols = r, min = -0.5, max = 0.5); # mxr
+    V = rand (rows = n, cols = r, min = -0.5, max = 0.5); # nxr
+
+    W = (X != 0);
+
+    # check for regularization
+    row_nonzeros = matrix(0,rows=1,cols=1);
+    col_nonzeros = matrix(0,rows=1,cols=1);
+    if( reg == "L2" ) {
+      print ("BEGIN ALS-CG SCRIPT WITH NONZERO SQUARED LOSS + L2 WITH LAMBDA - " + lambda);
+      row_nonzeros = matrix(1, nrow(W), 1);
+      col_nonzeros = matrix(1, ncol(W), 1);
+    }
+    else if( reg == "wL2" ) {
+      print ("BEGIN ALS-CG SCRIPT WITH NONZERO SQUARED LOSS + WEIGHTED L2 WITH LAMBDA - " + lambda);
+      row_nonzeros = rowSums(W);
+      col_nonzeros = t(colSums(W));
+    }
+    else {
+      stop ("wrong regularization! " + reg);
+    }
+
+    # Loss Function with L2:
+    # f (U, V) = 0.5 * sum (W * (U %*% V - X) ^ 2)
+    #          + 0.5 * lambda * (sum (U ^ 2) + sum (V ^ 2))
+    # Loss Function with weighted L2:
+    # f (U, V) = 0.5 * sum (W * (U %*% V - X) ^ 2)
+    #          + 0.5 * lambda * (sum (U ^ 2 * row_nonzeros) + sum (V ^ 2 * col_nonzeros))

Review comment:
       Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org