Alternating Least Squares

Description

The alternating least squares (ALS) algorithm factorizes a given matrix $R$ into two factors $U$ and $V$ such that $R \approx U^TV$.The unknown row dimension is given as a parameter to the algorithm and is called latent factors.Since matrix factorization can be used in the context of recommendation, the matrices $U$ and $V$ can be called user and item matrix, respectively.The $i$th column of the user matrix is denoted by $u_i$ and the $i$th column of the item matrix is $v_i$.The matrix $R$ can be called the ratings matrix with .

In order to find the user and item matrix, the following problem is solved:

with $\lambda$ being the regularization factor, being the number of items the user $i$ has rated and 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$, we obtain a quadratic form which can be solved directly.The solution of the modified problem is guaranteed to monotonically 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$ denotes the row index, $j$ the column index and $r$ is the matrix value at position $(i,j)$.

Operations

ALS is a Predictor.As such, it supports the fit and predict operation.

Fit

ALS is trained on the sparse representation of the rating matrix:

  • fit: DataSet[(Int, Int, Double)] => Unit

Predict

ALS predicts for each tuple of row and column index the rating:

  • predict: DataSet[(Int, Int)] => DataSet[(Int, Int, Double)]

Parameters

The alternating least squares implementation can be controlled by the following parameters:

ParametersDescription
NumFactorsThe number of latent factors to use for the underlying model.It is equivalent to the dimension of the calculated user and item vectors.(Default value: 10)
LambdaRegularization factor. Tune this value in order to avoid overfitting or poor performance due to strong generalization.(Default value: 1)
IterationsThe maximum number of iterations.(Default value: 10)
BlocksThe number of blocks into which the user and item matrix are 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)
SeedRandom seed used to generate the initial item matrix for the algorithm.(Default value: 0)
TemporaryPathPath 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 OutBlockInformation and InBlockInformation for the given rating matrix.The results 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 messages and improves the overall performance.(Default value: None)

Examples

  1. // Read input data set from a csv file
  2. val inputDS: DataSet[(Int, Int, Double)] = env.readCsvFile[(Int, Int, Double)](
  3. pathToTrainingFile)
  4. // Setup the ALS learner
  5. val als = ALS()
  6. .setIterations(10)
  7. .setNumFactors(10)
  8. .setBlocks(100)
  9. .setTemporaryPath("hdfs://tempPath")
  10. // Set the other parameters via a parameter map
  11. val parameters = ParameterMap()
  12. .add(ALS.Lambda, 0.9)
  13. .add(ALS.Seed, 42L)
  14. // Calculate the factorization
  15. als.fit(inputDS, parameters)
  16. // Read the testing data set from a csv file
  17. val testingDS: DataSet[(Int, Int)] = env.readCsvFile[(Int, Int)](pathToData)
  18. // Calculate the ratings according to the matrix factorization
  19. val predictedRatings = als.predict(testingDS)

Back to top