public class ALS extends Estimator<ALSModel>
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 > 0 and 0 if r <= 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 |
---|---|
protected static <T> T |
$(Param<T> param) |
static DoubleParam |
alpha() |
DoubleParam |
alpha()
Param for the alpha parameter in the implicit preference formulation (>= 0).
|
static IntParam |
checkpointInterval() |
static Params |
clear(Param<?> param) |
ALS |
copy(ParamMap extra)
Creates a copy of this instance with the same UID and some extra params.
|
protected static <T extends Params> |
copyValues(T to,
ParamMap extra) |
protected static <T extends Params> |
copyValues$default$2() |
protected static <T extends Params> |
defaultCopy(ParamMap extra) |
static java.lang.String |
explainParam(Param<?> param) |
static java.lang.String |
explainParams() |
static ParamMap |
extractParamMap() |
static ParamMap |
extractParamMap(ParamMap extra) |
static Param<java.lang.String> |
finalStorageLevel() |
Param<java.lang.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 <T> scala.Option<T> |
getDefault(Param<T> param) |
static java.lang.String |
getFinalStorageLevel() |
java.lang.String |
getFinalStorageLevel() |
static boolean |
getImplicitPrefs() |
boolean |
getImplicitPrefs() |
static java.lang.String |
getIntermediateStorageLevel() |
java.lang.String |
getIntermediateStorageLevel() |
static java.lang.String |
getItemCol() |
java.lang.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<java.lang.Object> |
getParam(java.lang.String paramName) |
static java.lang.String |
getPredictionCol() |
static int |
getRank() |
int |
getRank() |
static java.lang.String |
getRatingCol() |
java.lang.String |
getRatingCol() |
static double |
getRegParam() |
static long |
getSeed() |
static java.lang.String |
getUserCol() |
java.lang.String |
getUserCol() |
static <T> boolean |
hasDefault(Param<T> param) |
static boolean |
hasParam(java.lang.String paramName) |
static BooleanParam |
implicitPrefs() |
BooleanParam |
implicitPrefs()
Param to decide whether to use implicit preference.
|
protected static void |
initializeLogIfNecessary(boolean isInterpreter) |
static Param<java.lang.String> |
intermediateStorageLevel() |
Param<java.lang.String> |
intermediateStorageLevel()
Param for StorageLevel for intermediate datasets.
|
static boolean |
isDefined(Param<?> param) |
static boolean |
isSet(Param<?> param) |
protected static boolean |
isTraceEnabled() |
static Param<java.lang.String> |
itemCol() |
Param<java.lang.String> |
itemCol()
Param for the column name for item ids.
|
static ALS |
load(java.lang.String path) |
protected static org.slf4j.Logger |
log() |
protected static void |
logDebug(scala.Function0<java.lang.String> msg) |
protected static void |
logDebug(scala.Function0<java.lang.String> msg,
java.lang.Throwable throwable) |
protected static void |
logError(scala.Function0<java.lang.String> msg) |
protected static void |
logError(scala.Function0<java.lang.String> msg,
java.lang.Throwable throwable) |
protected static void |
logInfo(scala.Function0<java.lang.String> msg) |
protected static void |
logInfo(scala.Function0<java.lang.String> msg,
java.lang.Throwable throwable) |
protected static java.lang.String |
logName() |
protected static void |
logTrace(scala.Function0<java.lang.String> msg) |
protected static void |
logTrace(scala.Function0<java.lang.String> msg,
java.lang.Throwable throwable) |
protected static void |
logWarning(scala.Function0<java.lang.String> msg) |
protected static void |
logWarning(scala.Function0<java.lang.String> msg,
java.lang.Throwable throwable) |
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 (>= 1).
|
static IntParam |
numUserBlocks() |
IntParam |
numUserBlocks()
Param for number of user blocks (>= 1).
|
static Param<?>[] |
params() |
static Param<java.lang.String> |
predictionCol() |
static IntParam |
rank() |
IntParam |
rank()
Param for rank of the matrix factorization (>= 1).
|
static Param<java.lang.String> |
ratingCol() |
Param<java.lang.String> |
ratingCol()
Param for the column name for ratings.
|
static DoubleParam |
regParam() |
static void |
save(java.lang.String path) |
static LongParam |
seed() |
static <T> Params |
set(Param<T> param,
T value) |
protected static Params |
set(ParamPair<?> paramPair) |
protected static Params |
set(java.lang.String param,
java.lang.Object value) |
ALS |
setAlpha(double value) |
ALS |
setCheckpointInterval(int value) |
protected static <T> Params |
setDefault(Param<T> param,
T value) |
protected static Params |
setDefault(scala.collection.Seq<ParamPair<?>> paramPairs) |
ALS |
setFinalStorageLevel(java.lang.String value) |
ALS |
setImplicitPrefs(boolean value) |
ALS |
setIntermediateStorageLevel(java.lang.String value) |
ALS |
setItemCol(java.lang.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(java.lang.String value) |
ALS |
setRank(int value) |
ALS |
setRatingCol(java.lang.String value) |
ALS |
setRegParam(double value) |
ALS |
setSeed(long value) |
ALS |
setUserCol(java.lang.String value) |
static java.lang.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 ::
|
java.lang.String |
uid()
An immutable unique ID for the object and its derivatives.
|
static Param<java.lang.String> |
userCol() |
Param<java.lang.String> |
userCol()
Param for the column name for user ids.
|
protected static StructType |
validateAndTransformSchema(StructType schema) |
StructType |
validateAndTransformSchema(StructType schema)
Validates and transforms the input schema.
|
static void |
validateParams() |
static MLWriter |
write() |
transformSchema
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
clear, copyValues, defaultCopy, defaultParamMap, explainParam, explainParams, extractParamMap, extractParamMap, get, getDefault, getOrDefault, getParam, hasDefault, hasParam, isDefined, isSet, paramMap, params, set, set, set, setDefault, setDefault, shouldOwn, validateParams
toString
public static ALS load(java.lang.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)
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 java.lang.String toString()
public static Param<?>[] params()
public static void validateParams()
public static java.lang.String explainParam(Param<?> param)
public static java.lang.String explainParams()
public static final boolean isSet(Param<?> param)
public static final boolean isDefined(Param<?> param)
public static boolean hasParam(java.lang.String paramName)
public static Param<java.lang.Object> getParam(java.lang.String paramName)
protected static final Params set(java.lang.String param, java.lang.Object value)
public static final <T> scala.Option<T> get(Param<T> param)
public static final <T> T getOrDefault(Param<T> param)
protected static final <T> T $(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()
protected static java.lang.String logName()
protected static org.slf4j.Logger log()
protected static void logInfo(scala.Function0<java.lang.String> msg)
protected static void logDebug(scala.Function0<java.lang.String> msg)
protected static void logTrace(scala.Function0<java.lang.String> msg)
protected static void logWarning(scala.Function0<java.lang.String> msg)
protected static void logError(scala.Function0<java.lang.String> msg)
protected static void logInfo(scala.Function0<java.lang.String> msg, java.lang.Throwable throwable)
protected static void logDebug(scala.Function0<java.lang.String> msg, java.lang.Throwable throwable)
protected static void logTrace(scala.Function0<java.lang.String> msg, java.lang.Throwable throwable)
protected static void logWarning(scala.Function0<java.lang.String> msg, java.lang.Throwable throwable)
protected static void logError(scala.Function0<java.lang.String> msg, java.lang.Throwable throwable)
protected static boolean isTraceEnabled()
protected static void initializeLogIfNecessary(boolean isInterpreter)
public static final Param<java.lang.String> predictionCol()
public static final java.lang.String getPredictionCol()
public static Param<java.lang.String> userCol()
public static java.lang.String getUserCol()
public static Param<java.lang.String> itemCol()
public static java.lang.String getItemCol()
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<java.lang.String> ratingCol()
public static java.lang.String getRatingCol()
public static BooleanParam nonnegative()
public static boolean getNonnegative()
public static Param<java.lang.String> intermediateStorageLevel()
public static java.lang.String getIntermediateStorageLevel()
public static Param<java.lang.String> finalStorageLevel()
public static java.lang.String getFinalStorageLevel()
protected static StructType validateAndTransformSchema(StructType schema)
public static void save(java.lang.String path) throws java.io.IOException
java.io.IOException
public static MLWriter write()
public java.lang.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(java.lang.String value)
public ALS setItemCol(java.lang.String value)
public ALS setRatingCol(java.lang.String value)
public ALS setPredictionCol(java.lang.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(java.lang.String value)
public ALS setFinalStorageLevel(java.lang.String value)
public ALS setNumBlocks(int value)
value
- (undocumented)public ALSModel fit(Dataset<?> dataset)
Estimator
public StructType transformSchema(StructType schema)
PipelineStage
Derives the output schema from the input schema.
transformSchema
in class PipelineStage
schema
- (undocumented)public ALS copy(ParamMap extra)
Params
public IntParam rank()
public int getRank()
public IntParam numUserBlocks()
public int getNumUserBlocks()
public IntParam numItemBlocks()
public int getNumItemBlocks()
public BooleanParam implicitPrefs()
public boolean getImplicitPrefs()
public DoubleParam alpha()
public double getAlpha()
public Param<java.lang.String> ratingCol()
public java.lang.String getRatingCol()
public BooleanParam nonnegative()
public boolean getNonnegative()
public Param<java.lang.String> intermediateStorageLevel()
StorageLevel
. Cannot be "NONE".
Default: "MEMORY_AND_DISK".
public java.lang.String getIntermediateStorageLevel()
public Param<java.lang.String> finalStorageLevel()
StorageLevel
.
Default: "MEMORY_AND_DISK".
public java.lang.String getFinalStorageLevel()
public StructType validateAndTransformSchema(StructType schema)
schema
- input schemapublic Param<java.lang.String> userCol()
public java.lang.String getUserCol()
public Param<java.lang.String> itemCol()
public java.lang.String getItemCol()