I have been trying to run linear regression with SGD that is found in Spark mllib for some time and I am experiencing huge performance problems. All examples that I was looking have number of iterations set to 100 and say that Spark mllib can run very fast for big data.
This the code I am having problems with:
def train(data: RDD[TrainFeature], scalerModel: StandardScalerModel): LinearRegressionModel = {
val labeledData = data map { trainFeature =>
LabeledPoint(trainFeature.relevance.value, toVector(trainFeature.feature, scalerModel))
}
labeledData.cache()
val algorithm = new LinearRegressionWithSGD()
algorithm.optimizer
.setNumIterations(10)
.setRegParam(0.01)
.setStepSize(0.1)
algorithm run labeledData
}
private def toVector(feature: Feature, scalerModel: StandardScalerModel): Vector = scalerModel transform toVector(feature)
private def toVector(feature: Feature): Vector = Vectors dense feature.coordinates.toArray
I have scaled the data first and then run the algorithm to train the model. Even when I use 10 iterations it takes around 10 minutes to train model for 70,000 entries with feature vector of size 2. And the results I get are not good at all. I start getting decent results after numberOfIterations = 1000, but that would take ages.
Is it normal for linear regression with SGD to be this slow for 70,000 vectors of size 2?
My JVM min and initial memory is set to 4g.
I have tried setting the following as well (devastated try): System.setProperty("spark.executor.memory", "3g")
I am running this locally and since normal LinearRegression written in MatLab would finish the job very fast, I am wondering what am I doing wrong?
Edit: When I look at the spark UI in jobs section, I see that it is creating way too many jobs for gradient descent. Is there a way that I could tell Spark to create very little jobs - i.e. don't split data, run everything in a single thread? Maybe that can help me debug the problem further.
coalesce
. $\endgroup$NumIterations
but it cannot be lowered manually. Regarding performance - it takes less than a fives seconds (~160 iterations) on a few years old box with random data of the same shape as yours. It would be useful if you show how you load and prepare the data. $\endgroup$