public class ALS extends Estimator<ALSModel> implements DefaultParamsWritable
ALS attempts to estimate the ratings matrix R
as the product of two lower-rank matrices,
X
and Y
, i.e. X * Yt = R
. Typically these approximations are called 'factor' matrices.
The general approach is iterative. During each iteration, one of the factor matrices is held
constant, while the other is solved for using least squares. The newly-solved factor matrix is
then held constant while solving for the other factor matrix.
This is a blocked implementation of the ALS factorization algorithm that groups the two sets of factors (referred to as "users" and "products") into blocks and reduces communication by only sending one copy of each user vector to each product block on each iteration, and only for the product blocks that need that user's feature vector. This is achieved by pre-computing some information about the ratings matrix to determine the "out-links" of each user (which blocks of products it will contribute to) and "in-link" information for each product (which of the feature vectors it receives from each user block it will depend on). This allows us to send only an array of feature vectors between each user block and product block, and have the product block find the users' ratings and update the products based on these messages.
For implicit preference data, the algorithm used is based on "Collaborative Filtering for Implicit Feedback Datasets", available at http://dx.doi.org/10.1109/ICDM.2008.22, adapted for the blocked approach used here.
Essentially instead of finding the low-rank approximations to the rating matrix R
,
this finds the approximations for a preference matrix P
where the elements of P
are 1 if
r is greater than 0 and 0 if r is less than or equal to 0. The ratings then act as 'confidence'
values related to strength of indicated user
preferences rather than explicit ratings given to items.
Modifier and Type | Class and Description |
---|---|
static class |
ALS.InBlock$ |
static class |
ALS.Rating<ID>
:: DeveloperApi ::
Rating class for better code readability.
|
static class |
ALS.Rating$ |
static class |
ALS.RatingBlock$ |
Modifier and Type | Method and Description |
---|---|
static DoubleParam |
alpha() |
DoubleParam |
alpha()
Param for the alpha parameter in the implicit preference formulation (nonnegative).
|
UserDefinedFunction |
checkedCast()
Attempts to safely cast a user/item id to an Int.
|
static IntParam |
checkpointInterval() |
static Params |
clear(Param<?> param) |
static Param<String> |
coldStartStrategy() |
Param<String> |
coldStartStrategy()
Param for strategy for dealing with unknown or new users/items at prediction time.
|
ALS |
copy(ParamMap extra)
Creates a copy of this instance with the same UID and some extra params.
|
static String |
explainParam(Param<?> param) |
static String |
explainParams() |
static ParamMap |
extractParamMap() |
static ParamMap |
extractParamMap(ParamMap extra) |
static Param<String> |
finalStorageLevel() |
Param<String> |
finalStorageLevel()
Param for StorageLevel for ALS model factors.
|
ALSModel |
fit(Dataset<?> dataset)
Fits a model to the input data.
|
static <T> scala.Option<T> |
get(Param<T> param) |
static double |
getAlpha() |
double |
getAlpha() |
static int |
getCheckpointInterval() |
static String |
getColdStartStrategy() |
String |
getColdStartStrategy() |
static <T> scala.Option<T> |
getDefault(Param<T> param) |
static String |
getFinalStorageLevel() |
String |
getFinalStorageLevel() |
static boolean |
getImplicitPrefs() |
boolean |
getImplicitPrefs() |
static String |
getIntermediateStorageLevel() |
String |
getIntermediateStorageLevel() |
static String |
getItemCol() |
String |
getItemCol() |
static int |
getMaxIter() |
static boolean |
getNonnegative() |
boolean |
getNonnegative() |
static int |
getNumItemBlocks() |
int |
getNumItemBlocks() |
static int |
getNumUserBlocks() |
int |
getNumUserBlocks() |
static <T> T |
getOrDefault(Param<T> param) |
static Param<Object> |
getParam(String paramName) |
static String |
getPredictionCol() |
static int |
getRank() |
int |
getRank() |
static String |
getRatingCol() |
String |
getRatingCol() |
static double |
getRegParam() |
static long |
getSeed() |
static String |
getUserCol() |
String |
getUserCol() |
static <T> boolean |
hasDefault(Param<T> param) |
static boolean |
hasParam(String paramName) |
static BooleanParam |
implicitPrefs() |
BooleanParam |
implicitPrefs()
Param to decide whether to use implicit preference.
|
static Param<String> |
intermediateStorageLevel() |
Param<String> |
intermediateStorageLevel()
Param for StorageLevel for intermediate datasets.
|
static boolean |
isDefined(Param<?> param) |
static boolean |
isSet(Param<?> param) |
static Param<String> |
itemCol() |
Param<String> |
itemCol()
Param for the column name for item ids.
|
static ALS |
load(String path) |
static IntParam |
maxIter() |
static BooleanParam |
nonnegative() |
BooleanParam |
nonnegative()
Param for whether to apply nonnegativity constraints.
|
static IntParam |
numItemBlocks() |
IntParam |
numItemBlocks()
Param for number of item blocks (positive).
|
static IntParam |
numUserBlocks() |
IntParam |
numUserBlocks()
Param for number of user blocks (positive).
|
static Param<?>[] |
params() |
static Param<String> |
predictionCol() |
static IntParam |
rank() |
IntParam |
rank()
Param for rank of the matrix factorization (positive).
|
static Param<String> |
ratingCol() |
Param<String> |
ratingCol()
Param for the column name for ratings.
|
static DoubleParam |
regParam() |
static void |
save(String path) |
static LongParam |
seed() |
static <T> Params |
set(Param<T> param,
T value) |
ALS |
setAlpha(double value) |
ALS |
setCheckpointInterval(int value) |
ALS |
setColdStartStrategy(String value) |
ALS |
setFinalStorageLevel(String value) |
ALS |
setImplicitPrefs(boolean value) |
ALS |
setIntermediateStorageLevel(String value) |
ALS |
setItemCol(String value) |
ALS |
setMaxIter(int value) |
ALS |
setNonnegative(boolean value) |
ALS |
setNumBlocks(int value)
Sets both numUserBlocks and numItemBlocks to the specific value.
|
ALS |
setNumItemBlocks(int value) |
ALS |
setNumUserBlocks(int value) |
ALS |
setPredictionCol(String value) |
ALS |
setRank(int value) |
ALS |
setRatingCol(String value) |
ALS |
setRegParam(double value) |
ALS |
setSeed(long value) |
ALS |
setUserCol(String value) |
static String |
toString() |
static <ID> scala.Tuple2<RDD<scala.Tuple2<ID,float[]>>,RDD<scala.Tuple2<ID,float[]>>> |
train(RDD<ALS.Rating<ID>> ratings,
int rank,
int numUserBlocks,
int numItemBlocks,
int maxIter,
double regParam,
boolean implicitPrefs,
double alpha,
boolean nonnegative,
StorageLevel intermediateRDDStorageLevel,
StorageLevel finalRDDStorageLevel,
int checkpointInterval,
long seed,
scala.reflect.ClassTag<ID> evidence$1,
scala.math.Ordering<ID> ord)
:: DeveloperApi ::
Implementation of the ALS algorithm.
|
StructType |
transformSchema(StructType schema)
:: DeveloperApi ::
|
String |
uid()
An immutable unique ID for the object and its derivatives.
|
static Param<String> |
userCol() |
Param<String> |
userCol()
Param for the column name for user ids.
|
StructType |
validateAndTransformSchema(StructType schema)
Validates and transforms the input schema.
|
static MLWriter |
write() |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getPredictionCol, predictionCol
clear, copyValues, defaultCopy, defaultParamMap, explainParam, explainParams, extractParamMap, extractParamMap, get, getDefault, getOrDefault, getParam, hasDefault, hasParam, isDefined, isSet, paramMap, params, set, set, set, setDefault, setDefault, shouldOwn
toString
getMaxIter, maxIter
getRegParam, regParam
checkpointInterval, getCheckpointInterval
write
save
initializeLogging, initializeLogIfNecessary, initializeLogIfNecessary, isTraceEnabled, log_, log, logDebug, logDebug, logError, logError, logInfo, logInfo, logName, logTrace, logTrace, logWarning, logWarning
public static ALS load(String path)
public static <ID> scala.Tuple2<RDD<scala.Tuple2<ID,float[]>>,RDD<scala.Tuple2<ID,float[]>>> train(RDD<ALS.Rating<ID>> ratings, int rank, int numUserBlocks, int numItemBlocks, int maxIter, double regParam, boolean implicitPrefs, double alpha, boolean nonnegative, StorageLevel intermediateRDDStorageLevel, StorageLevel finalRDDStorageLevel, int checkpointInterval, long seed, scala.reflect.ClassTag<ID> evidence$1, scala.math.Ordering<ID> ord)
This implementation of the ALS factorization algorithm partitions the two sets of factors among
Spark workers so as to reduce network communication by only sending one copy of each factor
vector to each Spark worker on each iteration, and only if needed. This is achieved by
precomputing some information about the ratings matrix to determine which users require which
item factors and vice versa. See the Scaladoc for InBlock
for a detailed explanation of how
the precomputation is done.
In addition, since each iteration of calculating the factor matrices depends on the known
ratings, which are spread across Spark partitions, a naive implementation would incur
significant network communication overhead between Spark workers, as the ratings RDD would be
repeatedly shuffled during each iteration. This implementation reduces that overhead by
performing the shuffling operation up front, precomputing each partition's ratings dependencies
and duplicating those values to the appropriate workers before starting iterations to solve for
the factor matrices. See the Scaladoc for OutBlock
for a detailed explanation of how the
precomputation is done.
Note that the term "rating block" is a bit of a misnomer, as the ratings are not partitioned by contiguous blocks from the ratings matrix but by a hash function on the rating's location in the matrix. If it helps you to visualize the partitions, it is easier to think of the term "block" as referring to a subset of an RDD containing the ratings rather than a contiguous submatrix of the ratings matrix.
ratings
- (undocumented)rank
- (undocumented)numUserBlocks
- (undocumented)numItemBlocks
- (undocumented)maxIter
- (undocumented)regParam
- (undocumented)implicitPrefs
- (undocumented)alpha
- (undocumented)nonnegative
- (undocumented)intermediateRDDStorageLevel
- (undocumented)finalRDDStorageLevel
- (undocumented)checkpointInterval
- (undocumented)seed
- (undocumented)evidence$1
- (undocumented)ord
- (undocumented)public static String toString()
public static Param<?>[] params()
public static String explainParam(Param<?> param)
public static String explainParams()
public static final boolean isSet(Param<?> param)
public static final boolean isDefined(Param<?> param)
public static boolean hasParam(String paramName)
public static Param<Object> getParam(String paramName)
public static final <T> scala.Option<T> get(Param<T> param)
public static final <T> T getOrDefault(Param<T> param)
public static final <T> scala.Option<T> getDefault(Param<T> param)
public static final <T> boolean hasDefault(Param<T> param)
public static final ParamMap extractParamMap()
public static final Param<String> predictionCol()
public static final String getPredictionCol()
public static Param<String> userCol()
public static String getUserCol()
public static Param<String> itemCol()
public static String getItemCol()
public static Param<String> coldStartStrategy()
public static String getColdStartStrategy()
public static final IntParam maxIter()
public static final int getMaxIter()
public static final DoubleParam regParam()
public static final double getRegParam()
public static final IntParam checkpointInterval()
public static final int getCheckpointInterval()
public static final LongParam seed()
public static final long getSeed()
public static IntParam rank()
public static int getRank()
public static IntParam numUserBlocks()
public static int getNumUserBlocks()
public static IntParam numItemBlocks()
public static int getNumItemBlocks()
public static BooleanParam implicitPrefs()
public static boolean getImplicitPrefs()
public static DoubleParam alpha()
public static double getAlpha()
public static Param<String> ratingCol()
public static String getRatingCol()
public static BooleanParam nonnegative()
public static boolean getNonnegative()
public static Param<String> intermediateStorageLevel()
public static String getIntermediateStorageLevel()
public static Param<String> finalStorageLevel()
public static String getFinalStorageLevel()
public static void save(String path) throws java.io.IOException
java.io.IOException
public static MLWriter write()
public String uid()
Identifiable
uid
in interface Identifiable
public ALS setRank(int value)
public ALS setNumUserBlocks(int value)
public ALS setNumItemBlocks(int value)
public ALS setImplicitPrefs(boolean value)
public ALS setAlpha(double value)
public ALS setUserCol(String value)
public ALS setItemCol(String value)
public ALS setRatingCol(String value)
public ALS setPredictionCol(String value)
public ALS setMaxIter(int value)
public ALS setRegParam(double value)
public ALS setNonnegative(boolean value)
public ALS setCheckpointInterval(int value)
public ALS setSeed(long value)
public ALS setIntermediateStorageLevel(String value)
public ALS setFinalStorageLevel(String value)
public ALS setColdStartStrategy(String value)
public ALS setNumBlocks(int value)
value
- (undocumented)public ALSModel fit(Dataset<?> dataset)
Estimator
public StructType transformSchema(StructType schema)
PipelineStage
Check transform validity and derive the output schema from the input schema.
We check validity for interactions between parameters during transformSchema
and
raise an exception if any parameter value is invalid. Parameter value checks which
do not depend on other parameters are handled by Param.validate()
.
Typical implementation should first conduct verification on schema change and parameter validity, including complex parameter interaction checks.
transformSchema
in class PipelineStage
schema
- (undocumented)public ALS copy(ParamMap extra)
Params
defaultCopy()
.public DoubleParam alpha()
public Param<String> finalStorageLevel()
StorageLevel
.
Default: "MEMORY_AND_DISK".
public double getAlpha()
public String getFinalStorageLevel()
public boolean getImplicitPrefs()
public String getIntermediateStorageLevel()
public boolean getNonnegative()
public int getNumItemBlocks()
public int getNumUserBlocks()
public int getRank()
public String getRatingCol()
public BooleanParam implicitPrefs()
public Param<String> intermediateStorageLevel()
StorageLevel
. Cannot be "NONE".
Default: "MEMORY_AND_DISK".
public BooleanParam nonnegative()
public IntParam numItemBlocks()
public IntParam numUserBlocks()
public IntParam rank()
public Param<String> ratingCol()
public StructType validateAndTransformSchema(StructType schema)
schema
- input schemapublic UserDefinedFunction checkedCast()
public Param<String> coldStartStrategy()
public String getColdStartStrategy()
public String getItemCol()
public String getUserCol()
public Param<String> itemCol()
public Param<String> userCol()