-
Notifications
You must be signed in to change notification settings - Fork 394
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
Correct conversion of Spark model stages into MLeap local models #261
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
90f32a2
MLeap fixes: 1) contruct dataframe with schema + meta 2) use explicit…
tovbinm a77e53b
Fixes
tovbinm 05f0830
tests
tovbinm 5067d5a
Update README.md
tovbinm 9cd9973
Merge branch 'master' into mt/mleap-models
tovbinm 4b13d4d
forgot to commit a test
tovbinm 31f4982
Merge branch 'mt/mleap-models' of github.com:salesforce/TransmogrifAI…
tovbinm 4811ebd
Merge branch 'master' into mt/mleap-models
tovbinm e5fc929
Update README.md
tovbinm b03e76c
Update README.md
tovbinm f9c27e5
Merge branch 'master' into mt/mleap-models
tovbinm 1a3d9fa
Merge branch 'master' into mt/mleap-models
tovbinm 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
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
92 changes: 92 additions & 0 deletions
92
local/src/main/scala/com/salesforce/op/local/MLeapModelConverter.scala
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2017, Salesforce.com, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.salesforce.op.local | ||
|
||
import ml.combust.mleap.core.feature._ | ||
import org.apache.spark.ml.linalg.Vector | ||
|
||
/** | ||
* Converter of MLeap model instances to a model apply function | ||
*/ | ||
case object MLeapModelConverter { | ||
|
||
/** | ||
* Convert MLeap model instance to a model apply function | ||
* | ||
* @param model MLeap model | ||
* @throws RuntimeException if model type is not supported | ||
* @return runnable model apply function | ||
*/ | ||
def modelToFunction(model: Any): Array[Any] => Any = model match { | ||
case m: BinarizerModel => x => m.apply(x(0).asInstanceOf[Number].doubleValue()) | ||
case m: BucketedRandomProjectionLSHModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: BucketizerModel => x => m.apply(x(0).asInstanceOf[Number].doubleValue()) | ||
case m: ChiSqSelectorModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: CoalesceModel => x => m.apply(x: _*) | ||
case m: CountVectorizerModel => x => m.apply(x(0).asInstanceOf[Seq[String]]) | ||
case m: DCTModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: ElementwiseProductModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: FeatureHasherModel => x => m.apply(x(0).asInstanceOf[Seq[Any]]) | ||
case m: HashingTermFrequencyModel => x => m.apply(x(0).asInstanceOf[Seq[Any]]) | ||
case m: IDFModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: ImputerModel => x => m.apply(x(0).asInstanceOf[Number].doubleValue()) | ||
case m: InteractionModel => x => m.apply(x(0).asInstanceOf[Seq[Any]]) | ||
case m: MathBinaryModel => x => | ||
m.apply( | ||
x.headOption.map(_.asInstanceOf[Number].doubleValue()), | ||
x.lastOption.map(_.asInstanceOf[Number].doubleValue()) | ||
) | ||
case m: MathUnaryModel => x => m.apply(x(0).asInstanceOf[Number].doubleValue()) | ||
case m: MaxAbsScalerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: MinHashLSHModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: MinMaxScalerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: NGramModel => x => m.apply(x(0).asInstanceOf[Seq[String]]) | ||
case m: NormalizerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: OneHotEncoderModel => x => m.apply(x(0).asInstanceOf[Vector].toArray) | ||
case m: PcaModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: PolynomialExpansionModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: RegexIndexerModel => x => m.apply(x(0).toString) | ||
case m: RegexTokenizerModel => x => m.apply(x(0).toString) | ||
case m: ReverseStringIndexerModel => x => m.apply(x(0).asInstanceOf[Number].intValue()) | ||
case m: StandardScalerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: StopWordsRemoverModel => x => m.apply(x(0).asInstanceOf[Seq[String]]) | ||
case m: StringIndexerModel => x => m.apply(x(0)) | ||
case m: StringMapModel => x => m.apply(x(0).toString) | ||
case m: TokenizerModel => x => m.apply(x(0).toString) | ||
case m: VectorAssemblerModel => x => m.apply(x(0).asInstanceOf[Seq[Any]]) | ||
case m: VectorIndexerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: VectorSlicerModel => x => m.apply(x(0).asInstanceOf[Vector]) | ||
case m: WordLengthFilterModel => x => m.apply(x(0).asInstanceOf[Seq[String]]) | ||
case m: WordToVectorModel => x => m.apply(x(0).asInstanceOf[Seq[String]]) | ||
case m => throw new RuntimeException(s"Unsupported MLeap model: ${m.getClass.getName}") | ||
} | ||
|
||
} |
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
Oops, something went wrong.
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.
so every wrapped spark stage has to be in this list? we should add that the the docs on wrapping...
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 currently added all the stages from features package. We can also add models from classification, regression and recommendation packages, but we already have the first two of them covered as our own
OpTransformer
stages, so I did not see much of a point adding them.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.
so to your question - for right now I think we have everything covered, except recommenders, which I am planning to add once we are ready.
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.
Can you add a todo with the classification and regression models? I dont know that this will be much use without them...
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.
Adding those is very easy. the thing is we already have classification and regression models as OpTransformers so MLeap won’t be used to run them.
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.
good point :-)