Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a529c01
document plans
holdenk May 22, 2015
f9e2635
Some progress.
holdenk May 22, 2015
7ebbd56
Keep track of the number of requested classes so that if its more tha…
holdenk May 22, 2015
ef2a9b0
Expose a train on instances method within Spark, use numOfLinearPredi…
holdenk May 22, 2015
407491e
tests are fun
holdenk May 24, 2015
e02bf3a
Start updating the tests to run with different updaters.
holdenk May 24, 2015
8517539
get the tests compiling
holdenk May 24, 2015
a619d42
style fixed
holdenk May 24, 2015
4febcc3
make the test method private
holdenk May 24, 2015
e8e03a1
CR feedback, pass RDD of Labeled points to ml implemetnation. Also fr…
holdenk May 24, 2015
38a024b
Convert it to a df and use set for the inital params
holdenk May 25, 2015
478b8c5
Handle non-dense weights
holdenk May 25, 2015
08589f5
CR feedback: make the setInitialWeights function private, don't mess …
holdenk May 26, 2015
f40c401
style fix up
holdenk May 26, 2015
f35a16a
Copy the number of iterations, convergence tolerance, and if we are f…
holdenk Jun 2, 2015
4d431a3
scala style check issue
holdenk Jun 3, 2015
7e41928
Only the weights if we need to.
holdenk Jun 3, 2015
ed351ff
Use appendBias for adding intercept to initial weights , fix generate…
holdenk Jun 3, 2015
3ac02d7
Merge in master
holdenk Jun 8, 2015
1793ff9
Try and avoid doing the round trip through dataframes
holdenk Jun 11, 2015
513a856
Attempt to update against master
holdenk Jan 16, 2016
eb2de97
And resolving the logical conflicts. The update to instances from lab…
holdenk Jan 16, 2016
6b3ebc2
long line
holdenk Jan 16, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.apache.spark.ml.util._
import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.linalg.BLAS._
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.stat.MultivariateOnlineSummarizer
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.rdd.RDD
Expand Down Expand Up @@ -247,15 +248,66 @@ class LogisticRegression @Since("1.2.0") (
@Since("1.5.0")
override def getThresholds: Array[Double] = super.getThresholds

override protected def train(dataset: DataFrame): LogisticRegressionModel = {
// Extract columns from data. If dataset is persisted, do not persist oldDataset.
private var optInitialWeights: Option[Vector] = None
/** @group setParam */
private[spark] def setInitialWeights(value: Vector): this.type = {
this.optInitialWeights = Some(value)
this
}

/** Validate the initial weights, return an Option, if not the expected size return None
* and log a warning.
*/
private def validateWeights(vectorOpt: Option[Vector], numFeatures: Int): Option[Vector] = {
vectorOpt.flatMap(vec =>
if (vec.size == numFeatures) {
Some(vec)
} else {
logWarning(
s"""Initial weights provided (${vec})did not match the expected size ${numFeatures}""")
None
})
}

/**
* Extract [[labelCol]] and [[featuresCol]] along with optional [[weightCol]] from the given
* dataset, and put it in an RDD with strong types.
*/
private def extractInstances(dataset: DataFrame): RDD[Instance] = {
val w = if ($(weightCol).isEmpty) lit(1.0) else col($(weightCol))
val instances: RDD[Instance] = dataset.select(col($(labelCol)), w, col($(featuresCol))).map {
case Row(label: Double, weight: Double, features: Vector) =>
dataset.select(col($(labelCol)), w, col($(featuresCol)))
.map { case Row(label: Double, weight: Double, features: Vector) =>
Instance(label, weight, features)
}
}

override protected[spark] def train(dataset: DataFrame): LogisticRegressionModel = {
val handlePersistence = dataset.rdd.getStorageLevel == StorageLevel.NONE
val (model, objectiveHistory) = train(extractInstances(dataset), handlePersistence)
val (summaryModel, probabilityColName) = model.findSummaryModelAndProbabilityCol()
val logRegSummary = new BinaryLogisticRegressionTrainingSummary(
summaryModel.transform(dataset),
probabilityColName,
$(labelCol),
$(featuresCol),
objectiveHistory)
model.setSummary(logRegSummary)
}

/**
* Internal train method, return the model and the objective history
*/
protected[spark] def train(points: RDD[LabeledPoint], handlePersistence: Boolean):
LogisticRegressionModel = {
val instances = points.map{case LabeledPoint(l, f) => Instance(l, 1.0, f)}
train(instances, handlePersistence)._1
}

/**
* Internal train method, return the model and the objective history
*/
protected[spark] def train(instances: RDD[Instance], handlePersistence: Boolean):
(LogisticRegressionModel, Array[Double]) = {
if (handlePersistence) instances.persist(StorageLevel.MEMORY_AND_DISK)

val (summarizer, labelSummarizer) = {
Expand Down Expand Up @@ -322,12 +374,14 @@ class LogisticRegression @Since("1.2.0") (
new BreezeOWLQN[Int, BDV[Double]]($(maxIter), 10, regParamL1Fun, $(tol))
}

val initialCoefficientsWithIntercept =
Vectors.zeros(if ($(fitIntercept)) numFeatures + 1 else numFeatures)
val numFeaturesWithIntercept = if ($(fitIntercept)) numFeatures + 1 else numFeatures
val userSuppliedWeights = validateWeights(optInitialWeights, numFeaturesWithIntercept)
val initialWeightsWithIntercept = userSuppliedWeights.getOrElse(
Vectors.zeros(numFeaturesWithIntercept))

if ($(fitIntercept)) {
/*
For binary logistic regression, when we initialize the coefficients as zeros,
if ($(fitIntercept) && !userSuppliedWeights.isDefined) {
/**
For binary logistic regression, when we initialize the weights as zeros,
it will converge faster if we initialize the intercept such that
it follows the distribution of the labels.

Expand All @@ -339,12 +393,12 @@ class LogisticRegression @Since("1.2.0") (
b = \log{P(1) / P(0)} = \log{count_1 / count_0}
}}}
*/
initialCoefficientsWithIntercept.toArray(numFeatures)
initialWeightsWithIntercept.toArray(numFeatures)
= math.log(histogram(1) / histogram(0))
}

val states = optimizer.iterations(new CachedDiffFunction(costFun),
initialCoefficientsWithIntercept.toBreeze.toDenseVector)
initialWeightsWithIntercept.toBreeze.toDenseVector)

val (coefficients, intercept, objectiveHistory) = {
/*
Expand Down Expand Up @@ -389,14 +443,7 @@ class LogisticRegression @Since("1.2.0") (
if (handlePersistence) instances.unpersist()

val model = copyValues(new LogisticRegressionModel(uid, coefficients, intercept))
val (summaryModel, probabilityColName) = model.findSummaryModelAndProbabilityCol()
val logRegSummary = new BinaryLogisticRegressionTrainingSummary(
summaryModel.transform(dataset),
probabilityColName,
$(labelCol),
$(featuresCol),
objectiveHistory)
model.setSummary(logRegSummary)
(model, objectiveHistory)
}

@Since("1.4.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ package org.apache.spark.mllib.classification
import org.apache.spark.SparkContext
import org.apache.spark.annotation.Since
import org.apache.spark.mllib.classification.impl.GLMClassificationModel
import org.apache.spark.mllib.linalg.{DenseVector, Vector}
import org.apache.spark.mllib.linalg.{DenseVector, Vector, Vectors}
import org.apache.spark.mllib.linalg.BLAS.dot
import org.apache.spark.mllib.optimization._
import org.apache.spark.mllib.pmml.PMMLExportable
import org.apache.spark.mllib.regression._
import org.apache.spark.mllib.util.{DataValidators, Loader, Saveable}
import org.apache.spark.mllib.util.{DataValidators, Loader, MLUtils, Saveable}
import org.apache.spark.rdd.RDD

import org.apache.spark.sql.SQLContext
import org.apache.spark.storage.StorageLevel

/**
* Classification model trained using Multinomial/Binary Logistic Regression.
Expand Down Expand Up @@ -332,6 +333,13 @@ object LogisticRegressionWithSGD {
* Limited-memory BFGS. Standard feature scaling and L2 regularization are used by default.
* NOTE: Labels used in Logistic Regression should be {0, 1, ..., k - 1}
* for k classes multi-label classification problem.
*
* Earlier implementations of LogisticRegressionWithLBFGS applies a regularization
* penalty to all elements including the intercept. If this is called with one of
* standard updaters (L1Updater, or SquaredL2Updater) this is translated
* into a call to ml.LogisticRegression, otherwise this will use the existing mllib
* GeneralizedLinearAlgorithm trainer, resulting in a regularization penalty to the
* intercept.
*/
@Since("1.1.0")
class LogisticRegressionWithLBFGS
Expand Down Expand Up @@ -374,4 +382,72 @@ class LogisticRegressionWithLBFGS
new LogisticRegressionModel(weights, intercept, numFeatures, numOfLinearPredictor + 1)
}
}


/**
* Run the algorithm with the configured parameters on an input RDD
* of LabeledPoint entries starting from the initial weights provided.
* If a known updater is used calls the ml implementation, to avoid
* applying a regularization penalty to the intercept, otherwise
* defaults to the mllib implementation. If more than two classes
* or feature scaling is disabled, always uses mllib implementation.
* If using ml implementation, uses ml code to generate initial weights.
*/
override def run(input: RDD[LabeledPoint]): LogisticRegressionModel = {
run(input, generateInitialWeights(input), false)
}

/**
* Run the algorithm with the configured parameters on an input RDD
* of LabeledPoint entries starting from the initial weights provided.
* If a known updater is used calls the ml implementation, to avoid
* applying a regularization penalty to the intercept, otherwise
* defaults to the mllib implementation. If more than two classes
* or feature scaling is disabled, always uses mllib implementation.
* Uses user provided weights.
*/
override def run(input: RDD[LabeledPoint], initialWeights: Vector): LogisticRegressionModel = {
run(input, initialWeights, true)
}

private def run(input: RDD[LabeledPoint], initialWeights: Vector, userSuppliedWeights: Boolean):
LogisticRegressionModel = {
// ml's Logisitic regression only supports binary classifcation currently.
if (numOfLinearPredictor == 1 && useFeatureScaling) {
def runWithMlLogisitcRegression(elasticNetParam: Double) = {
// Prepare the ml LogisticRegression based on our settings
val lr = new org.apache.spark.ml.classification.LogisticRegression()
lr.setRegParam(optimizer.getRegParam())
lr.setElasticNetParam(elasticNetParam)
if (userSuppliedWeights) {
val initialWeightsWithIntercept = if (addIntercept) {
MLUtils.appendBias(initialWeights)
} else {
initialWeights
}
lr.setInitialWeights(initialWeightsWithIntercept)
}
lr.setFitIntercept(addIntercept)
lr.setMaxIter(optimizer.getNumIterations())
lr.setTol(optimizer.getConvergenceTol())
// Determine if we should cache the input
val handlePersistence = input.getStorageLevel == StorageLevel.NONE
// Train our model
val mlLogisticRegresionModel = lr.train(input, handlePersistence)
// convert the model
val weights = mlLogisticRegresionModel.weights match {
case x: DenseVector => x
case y: Vector => Vectors.dense(y.toArray)
}
createModel(weights, mlLogisticRegresionModel.intercept)
}
optimizer.getUpdater() match {
case x: SquaredL2Updater => runWithMlLogisitcRegression(1.0)
case x: L1Updater => runWithMlLogisitcRegression(0.0)
case _ => super.run(input, initialWeights)
}
} else {
super.run(input, initialWeights)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class LBFGS(private var gradient: Gradient, private var updater: Updater)
this
}

/*
* Get the convergence tolerance of iterations.
*/
private[mllib] def getConvergenceTol(): Double = {
this.convergenceTol
}

/**
* Set the maximal number of iterations for L-BFGS. Default 100.
* @deprecated use [[LBFGS#setNumIterations]] instead
Expand All @@ -86,6 +93,13 @@ class LBFGS(private var gradient: Gradient, private var updater: Updater)
this
}

/**
* Get the maximum number of iterations for L-BFGS. Defaults to 100.
*/
private[mllib] def getNumIterations(): Int = {
this.maxNumIterations
}

/**
* Set the regularization parameter. Default 0.0.
*/
Expand All @@ -94,6 +108,13 @@ class LBFGS(private var gradient: Gradient, private var updater: Updater)
this
}

/**
* Get the regularization parameter.
*/
private[mllib] def getRegParam(): Double = {
this.regParam
}

/**
* Set the gradient function (of the loss function of one single data example)
* to be used for L-BFGS.
Expand All @@ -113,6 +134,13 @@ class LBFGS(private var gradient: Gradient, private var updater: Updater)
this
}

/**
* Returns the updater, limited to internal use.
*/
private[mllib] def getUpdater(): Updater = {
updater
}

override def optimize(data: RDD[(Double, Vector)], initialWeights: Vector): Vector = {
val (weights, _) = LBFGS.runLBFGS(
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ abstract class GeneralizedLinearAlgorithm[M <: GeneralizedLinearModel]
* translated back to resulting model weights, so it's transparent to users.
* Note: This technique is used in both libsvm and glmnet packages. Default false.
*/
private var useFeatureScaling = false
private[mllib] var useFeatureScaling = false

/**
* The dimension of training features.
Expand Down Expand Up @@ -196,12 +196,9 @@ abstract class GeneralizedLinearAlgorithm[M <: GeneralizedLinearModel]
}

/**
* Run the algorithm with the configured parameters on an input
* RDD of LabeledPoint entries.
*
* Generate the initial weights when the user does not supply them
*/
@Since("0.8.0")
def run(input: RDD[LabeledPoint]): M = {
protected def generateInitialWeights(input: RDD[LabeledPoint]): Vector = {
if (numFeatures < 0) {
numFeatures = input.map(_.features.size).first()
}
Expand All @@ -217,16 +214,21 @@ abstract class GeneralizedLinearAlgorithm[M <: GeneralizedLinearModel]
* TODO: See if we can deprecate `intercept` in `GeneralizedLinearModel`, and always
* have the intercept as part of weights to have consistent design.
*/
val initialWeights = {
if (numOfLinearPredictor == 1) {
Vectors.zeros(numFeatures)
} else if (addIntercept) {
Vectors.zeros((numFeatures + 1) * numOfLinearPredictor)
} else {
Vectors.zeros(numFeatures * numOfLinearPredictor)
}
if (numOfLinearPredictor == 1) {
Vectors.zeros(numFeatures)
} else if (addIntercept) {
Vectors.zeros((numFeatures + 1) * numOfLinearPredictor)
} else {
Vectors.zeros(numFeatures * numOfLinearPredictor)
}
run(input, initialWeights)
}

/**
* Run the algorithm with the configured parameters on an input
* RDD of LabeledPoint entries.
*/
def run(input: RDD[LabeledPoint]): M = {
run(input, generateInitialWeights(input))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private class MockLogisticRegression(uid: String) extends LogisticRegression(uid

setMaxIter(1)

override protected def train(dataset: DataFrame): LogisticRegressionModel = {
override protected[spark] def train(dataset: DataFrame): LogisticRegressionModel = {
val labelSchema = dataset.schema($(labelCol))
// check for label attribute propagation.
assert(MetadataUtils.getNumClasses(labelSchema).forall(_ == 2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.apache.spark.mllib.linalg.{Vector, Vectors}
import org.apache.spark.mllib.regression._
import org.apache.spark.mllib.util.{LocalClusterSparkContext, MLlibTestSparkContext}
import org.apache.spark.mllib.util.TestingUtils._
import org.apache.spark.mllib.optimization.{SquaredL2Updater, L1Updater, Updater, LBFGS, LogisticGradient}
import org.apache.spark.util.Utils


Expand Down Expand Up @@ -215,6 +216,11 @@ class LogisticRegressionSuite extends SparkFunSuite with MLlibTestSparkContext w

// Test if we can correctly learn A, B where Y = logistic(A + B*X)
test("logistic regression with LBFGS") {
val updaters: List[Updater] = List(new SquaredL2Updater(), new L1Updater())
updaters.foreach(testLBFGS)
}

private def testLBFGS(myUpdater: Updater): Unit = {
val nPoints = 10000
val A = 2.0
val B = -1.5
Expand All @@ -223,7 +229,15 @@ class LogisticRegressionSuite extends SparkFunSuite with MLlibTestSparkContext w

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()
val lr = new LogisticRegressionWithLBFGS().setIntercept(true)

// Override the updater
class LogisticRegressionWithLBFGSCustomUpdater
extends LogisticRegressionWithLBFGS {
override val optimizer =
new LBFGS(new LogisticGradient, myUpdater)
}

val lr = new LogisticRegressionWithLBFGSCustomUpdater().setIntercept(true)

val model = lr.run(testRDD)

Expand Down