-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tweaks to the SparkLR example #872
Open
wannabeast
wants to merge
2
commits into
mesos:master
Choose a base branch
from
wannabeast:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,33 @@ import java.util.Random | |
import scala.math.exp | ||
import spark.util.Vector | ||
import spark._ | ||
import com.esotericsoftware.kryo.Kryo | ||
|
||
/** | ||
* Logistic regression based classification. | ||
*/ | ||
object SparkLR { | ||
val N = 10000 // Number of data points | ||
case class DataPoint(x: Vector, y: Double) | ||
|
||
class MyRegistrator extends KryoRegistrator { | ||
override def registerClasses(kryo: Kryo) { | ||
kryo.setRegistrationRequired(true) | ||
|
||
kryo.register(classOf[scala.collection.mutable.WrappedArray.ofRef[_]]) | ||
kryo.register(classOf[java.lang.Class[_]]) | ||
kryo.register(classOf[DataPoint]) | ||
kryo.register(classOf[Array[DataPoint]]) | ||
kryo.register(classOf[Vector]) | ||
kryo.register(classOf[Array[Double]]) | ||
} | ||
} | ||
|
||
var N = 10000 // Number of data points | ||
val D = 10 // Numer of dimensions | ||
val R = 0.7 // Scaling factor | ||
val ITERATIONS = 5 | ||
val rand = new Random(42) | ||
|
||
case class DataPoint(x: Vector, y: Double) | ||
|
||
def generateData = { | ||
def generatePoint(i: Int) = { | ||
val y = if(i % 2 == 0) -1 else 1 | ||
|
@@ -45,12 +59,17 @@ object SparkLR { | |
|
||
def main(args: Array[String]) { | ||
if (args.length == 0) { | ||
System.err.println("Usage: SparkLR <master> [<slices>]") | ||
System.err.println("Usage: SparkLR <master> [<slices> [<points>]]") | ||
System.exit(1) | ||
} | ||
|
||
System.setProperty("spark.serializer", "spark.KryoSerializer") | ||
System.setProperty("spark.kryo.registrator", "spark.examples.SparkLR$MyRegistrator") | ||
val sc = new SparkContext(args(0), "SparkLR", | ||
System.getenv("SPARK_HOME"), Seq(System.getenv("SPARK_EXAMPLES_JAR"))) | ||
|
||
val numSlices = if (args.length > 1) args(1).toInt else 2 | ||
if (args.length > 2) N = args(2).toInt | ||
val points = sc.parallelize(generateData, numSlices).cache() | ||
|
||
// Initialize w to a random value | ||
|
@@ -59,9 +78,10 @@ object SparkLR { | |
|
||
for (i <- 1 to ITERATIONS) { | ||
println("On iteration " + i) | ||
val zero = Vector.zeros(D) | ||
val gradient = points.map { p => | ||
(1 / (1 + exp(-p.y * (w dot p.x))) - 1) * p.y * p.x | ||
}.reduce(_ + _) | ||
((1 / (1 + exp(-p.y * (w dot p.x))) - 1) * p.y, p.x) | ||
}.aggregate(zero)((sum,v) => sum saxpy (v._1,v._2), _ += _) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the second argument to |
||
w -= gradient | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Mike, why do you need to register WrappedArray and Class? Doesn't seem like they'll occur in our data here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came up with that list by adding
setRegistrationRequired
, then fixing each exception that was thrown. So Kryo said they were used; I haven't considered why.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Did Kryo actually make a performance difference for you at all? We are not caching data in serialized form here, so it would only be used to send back task results, but that's just one Vector per partition. I think the WrappedArrays are because we somehow send that back within an array.