-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Allow JVM-Package to access inplace predict method #9167
Changes from 9 commits
c5d448e
ee0a029
5ce8d65
058aec3
79ce356
86e38d4
0c5b2df
312c874
bed3035
57d88da
b923c95
3b88aaf
e132136
d623163
e4f5ef8
bccd539
8b4885d
30ed3ca
509c880
272ed03
cc8e79b
81c23c2
dec1375
e91026a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,159 @@ private synchronized float[][] predict(DMatrix data, | |
return predicts; | ||
} | ||
|
||
/** | ||
* Perform thread-safe prediction. Calls | ||
* <code>inplace_predict(data, num_rows, num_features, Float.NaN, false, 0, false, false)</code>. | ||
* | ||
* @param data Flattened input matrix of features for prediction | ||
* @param num_rows The number of preditions to make (count of input matrix rows) | ||
* @param num_features The number of features in the model (count of input matrix columns) | ||
* | ||
* @return predict Result matrix | ||
* | ||
* @see #inplace_predict(float[] data, int num_rows, int num_features, float missing, | ||
* boolean outputMargin, int treeLimit, boolean predLeaf, | ||
* boolean predContribs) | ||
*/ | ||
public float[][] inplace_predict(float[] data, | ||
int num_rows, | ||
int num_features) throws XGBoostError { | ||
return this.inplace_predict(data, num_rows, num_features, | ||
Float.NaN, false, 0, false, false); | ||
} | ||
|
||
/** | ||
* Perform thread-safe prediction. Calls | ||
* <code>inplace_predict(data, num_rows, num_features, missing, false, 0, false, false)</code>. | ||
* | ||
* @param data Flattened input matrix of features for prediction | ||
* @param num_rows The number of preditions to make (count of input matrix rows) | ||
* @param num_features The number of features in the model (count of input matrix columns) | ||
* @param missing Value indicating missing element in the <code>data</code> input matrix | ||
* | ||
* @return predict Result matrix | ||
* | ||
* @see #inplace_predict(float[] data, int num_rows, int num_features, float missing, | ||
* boolean outputMargin, int treeLimit, boolean predLeaf, | ||
* boolean predContribs) | ||
*/ | ||
public float[][] inplace_predict(float[] data, | ||
int num_rows, | ||
int num_features, | ||
float missing) throws XGBoostError { | ||
return this.inplace_predict(data, num_rows, num_features, | ||
missing, false, 0, false, false); | ||
} | ||
|
||
/** | ||
* Perform thread-safe prediction. Calls | ||
* <code>inplace_predict(data, num_rows, num_features, missing, | ||
* outputMargin, 0, false, false)</code>. | ||
* | ||
* @param data Flattened input matrix of features for prediction | ||
* @param num_rows The number of preditions to make (count of input matrix rows) | ||
* @param num_features The number of features in the model (count of input matrix columns) | ||
* @param missing Value indicating missing element in the <code>data</code> input matrix | ||
* @param outputMargin Whether to only predict margin value instead of transformed prediction | ||
* | ||
* @return predict Result matrix | ||
* | ||
* @see #inplace_predict(float[] data, int num_rows, int num_features, float missing, | ||
* boolean outputMargin, int treeLimit, boolean predLeaf, | ||
* boolean predContribs) | ||
*/ | ||
|
||
public float[][] inplace_predict(float[] data, | ||
int num_rows, | ||
int num_features, | ||
float missing, | ||
boolean outputMargin) throws XGBoostError { | ||
return this.inplace_predict(data, num_rows, num_features, missing, | ||
outputMargin, 0, false, false); | ||
} | ||
|
||
/** | ||
* Perform thread-safe prediction. Calls | ||
* <code>inplace_predict(data, num_rows, num_features, missing, | ||
* outputMargin, treeLimit, false, false)</code>. | ||
* | ||
* @param data Flattened input matrix of features for prediction | ||
* @param num_rows The number of preditions to make (count of input matrix rows) | ||
* @param num_features The number of features in the model (count of input matrix columns) | ||
* @param missing Value indicating missing element in the <code>data</code> input matrix | ||
* @param outputMargin Whether to only predict margin value instead of transformed prediction | ||
* @param treeLimit limit number of trees, 0 means all trees. | ||
* | ||
* @return predict Result matrix | ||
* | ||
* @see #inplace_predict(float[] data, int num_rows, int num_features, float missing, | ||
* boolean outputMargin, int treeLimit, boolean predLeaf, | ||
* boolean predContribs) | ||
*/ | ||
public float[][] inplace_predict(float[] data, | ||
int num_rows, | ||
int num_features, | ||
float missing, | ||
boolean outputMargin, | ||
int treeLimit) throws XGBoostError { | ||
return this.inplace_predict(data, num_rows, num_features, missing, | ||
outputMargin, treeLimit, false, false); | ||
} | ||
|
||
/** | ||
* Perform thread-safe prediction. | ||
* | ||
* @param data Flattened input matrix of features for prediction | ||
* @param num_rows The number of preditions to make (count of input matrix rows) | ||
* @param num_features The number of features in the model (count of input matrix columns) | ||
* @param d_matrix_h The handle for a dmatrix | ||
* @param missing Value indicating missing element in the <code>data</code> input matrix | ||
* @param outputMargin Whether to only predict margin value instead of transformed prediction | ||
* @param treeLimit limit number of trees, 0 means all trees. | ||
* @param predLeaf prediction minimum to keep leafs | ||
* @param predContribs prediction feature contributions | ||
* | ||
* @return predict Result matrix | ||
*/ | ||
public float[][] inplace_predict(float[] data, | ||
int num_rows, | ||
int num_features, | ||
float missing, | ||
boolean outputMargin, | ||
int treeLimit, | ||
boolean predLeaf, | ||
boolean predContribs) throws XGBoostError { | ||
int optionMask = 0; | ||
if (outputMargin) { | ||
optionMask = 1; | ||
} | ||
if (predLeaf) { | ||
optionMask = 2; | ||
yoquinjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (predContribs) { | ||
optionMask = 4; | ||
yoquinjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
DMatrix d_mat = new DMatrix(data, num_rows, num_features, missing); | ||
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. Hmm, 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. Sorry for the delay - I'm looking through the python code that calls predictFromDense to see how that works - should we be implementing and using proxy DMatrix on the Java side of things? I'll start looking at making that change if you can just let me know if that would be the correct approach. Historically, we didn't have DMatrix here when we initially made our changes in 1.4 - this addition of the DMatrix here was due to changes in 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. Thank you for sharing! We unified the interface under the proxy DMatrix class. Let me do some more investigation into the JVM implementation first. I think I can help with the C++ code. |
||
float[][] rawPredicts = new float[1][]; | ||
XGBoostJNI.checkCall(XGBoostJNI.XGBoosterInplacePredict(handle, data, num_rows, num_features, | ||
d_mat.getHandle(), missing, | ||
optionMask, treeLimit, rawPredicts)); // pass missing and treelimit here? | ||
|
||
// System.out.println("Booster.inplace_predict rawPredicts[0].length = " + | ||
// rawPredicts[0].length); | ||
|
||
int row = num_rows; | ||
int col = rawPredicts[0].length / row; | ||
float[][] predicts = new float[row][col]; | ||
int r, c; | ||
for (int i = 0; i < rawPredicts[0].length; i++) { | ||
r = i / col; | ||
c = i % col; | ||
predicts[r][c] = rawPredicts[0][i]; | ||
} | ||
return predicts; | ||
} | ||
|
||
/** | ||
* Predict leaf indices given the data | ||
* | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
@wbo4958 I just learned about the existence of
BigDenseMatrix
, do you think we should return the prediction in that?