Alternating least squares algorithm to calculate a matrix factorization.
Alternating least squares algorithm to calculate a matrix factorization.
Given a matrix R, ALS calculates two matrices U and V such that R ~~ U^TV. The
unknown row dimension is given by the number of latent factors. Since matrix factorization
is often used in the context of recommendation, we'll call the first matrix the user and the
second matrix the item matrix. The ith column of the user matrix is u_i and the ith
column of the item matrix is v_i. The matrix R is called the ratings matrix and
(R)_{i,j} = r_{i,j}.
In order to find the user and item matrix, the following problem is solved:
with \lambda being the regularization factor, n_{u_i} being the number of items the user i
has rated and n_{v_j} being the number of times the item j has been rated. This
regularization scheme to avoid overfitting is called weighted-lambda-regularization. Details
can be found in the work of Zhou et al..
By fixing one of the matrices U or V one obtains a quadratic form which can be solved. The
solution of the modified problem is guaranteed to decrease the overall cost function. By
applying this step alternately to the matrices U and V, we can iteratively improve the
matrix factorization.
The matrix R is given in its sparse representation as a tuple of (i, j, r) where i is the
row index, j is the column index and r is the matrix value at position (i,j).
Example:
val inputDS: DataSet[(Int, Int, Double)] = env.readCsvFile[(Int, Int, Double)](
pathToTrainingFile)
val als = ALS()
.setIterations(10)
.setNumFactors(10)
als.fit(inputDS)
val data2Predict: DataSet[(Int, Int)] = env.readCsvFile[(Int, Int)](pathToData)
als.predict(data2Predict)
org.apache.flink.ml.recommendation.ALS.Blocks:
The number of blocks into which the user and item matrix a grouped. The fewer
blocks one uses, the less data is sent redundantly. However, bigger blocks entail bigger
update messages which have to be stored on the Heap. If the algorithm fails because of
an OutOfMemoryException, then try to increase the number of blocks. (Default value: None)
org.apache.flink.ml.recommendation.ALS.TemporaryPath:
Path to a temporary directory into which intermediate results are stored. If
this value is set, then the algorithm is split into two preprocessing steps, the ALS iteration
and a post-processing step which calculates a last ALS half-step. The preprocessing steps
calculate the org.apache.flink.ml.recommendation.ALS.OutBlockInformation and
org.apache.flink.ml.recommendation.ALS.InBlockInformation for the given rating matrix.
The result of the individual steps are stored in the specified directory. By splitting the
algorithm into multiple smaller steps, Flink does not have to split the available memory
amongst too many operators. This allows the system to process bigger individual messasges and
improves the overall performance. (Default value: None)
The ALS implementation is based on Spark's MLLib implementation of ALS which you can find
recommendation/ALS.scala here.
Alternating least squares algorithm to calculate a matrix factorization.
Given a matrix
R
, ALS calculates two matricesU
andV
such thatR ~~ U^TV
. The unknown row dimension is given by the number of latent factors. Since matrix factorization is often used in the context of recommendation, we'll call the first matrix the user and the second matrix the item matrix. The
ith column of the user matrix is
u_iand the
ith column of the item matrix is
v_i. The matrix
Ris called the ratings matrix and
(R)_{i,j} = r_{i,j}
.In order to find the user and item matrix, the following problem is solved:
argmin_{U,V} sum_(i,j\ with\ r_{i,j} != 0) (r_{i,j} - u_{i}Tv_{j})2 + lambda (sum_(i) n_{u_i} ||u_i||2 + sum_(j) n_{v_j} ||v_j||2)
with
\lambda
being the regularization factor,n_{u_i}
being the number of items the useri
has rated andn_{v_j}
being the number of times the itemj
has been rated. This regularization scheme to avoid overfitting is called weighted-lambda-regularization. Details can be found in the work of Zhou et al..By fixing one of the matrices
U
orV
one obtains a quadratic form which can be solved. The solution of the modified problem is guaranteed to decrease the overall cost function. By applying this step alternately to the matricesU
andV
, we can iteratively improve the matrix factorization.The matrix
R
is given in its sparse representation as a tuple of(i, j, r)
wherei
is the row index,j
is the column index andr
is the matrix value at position(i,j)
.Parameters