-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* GH-15697: add R test to reproduce error. * add generic model to test as well. * Add fold column info to generic model. * add compareFrame to compare prediction results. * add prediction with fold column removed Co-authored-by: wendycwong <[email protected]> Co-authored-by: Veronika Maurerová <[email protected]>
- Loading branch information
1 parent
054a599
commit 0c60e3e
Showing
5 changed files
with
71 additions
and
1 deletion.
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
63 changes: 63 additions & 0 deletions
63
h2o-r/tests/testdir_algos/glm/runit_gh_15697_mojo_offset_cv.R
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,63 @@ | ||
setwd(normalizePath(dirname(R.utils::commandArgs(asValues=TRUE)$"f"))) | ||
source("../../../scripts/h2o-r-test-setup.R") | ||
library(data.table) | ||
|
||
test_glm_mojo_offset_fold_columns <- function() { | ||
mt <- as.h2o(mtcars) | ||
|
||
find_offset <- h2o.glm( | ||
x = c("cyl", "disp"), | ||
y = "mpg", | ||
training_frame = mt, | ||
lambda = 0 | ||
) | ||
|
||
h2o.residual_deviance(find_offset) | ||
# [1] 270.7403 | ||
|
||
# create offset to look exactly like it's in the model (easy comparison) | ||
mt$offset <- mt$cyl * find_offset@model$coefficients[["cyl"]] | ||
mt$fold <- h2o.kfold_column(data = mt, nfolds = 3, seed = 123) | ||
|
||
# move "cyl" from being modeled to be an offset | ||
# build with a fold column | ||
mod_w_offset <- h2o.glm( | ||
x = c("disp"), | ||
y = "mpg", | ||
training_frame = mt, | ||
offset = "offset", | ||
lambda = 0, | ||
fold_column = "fold" | ||
) | ||
|
||
h2o.residual_deviance(mod_w_offset) | ||
# [1] 270.7403 (match as expected) | ||
|
||
# save out models then immediately reimport | ||
mojo_path <- h2o.save_mojo(object = mod_w_offset, path = ".") | ||
biny_path <- h2o.saveModel(object = mod_w_offset, path = ".") | ||
|
||
mojo <- h2o.import_mojo(mojo_file_path = mojo_path) | ||
genericGLM <- h2o.genericModel(mojo_path) | ||
biny <- h2o.loadModel(path = biny_path) | ||
|
||
|
||
predict <- h2o.predict(object = mod_w_offset, newdata = mt) | ||
predict0 <- h2o.predict(object = mojo, newdata = mt) | ||
predict1 <- h2o.predict(object = biny, newdata = mt) | ||
predict2 <- h2o.predict(object = genericGLM, newdata = mt) | ||
|
||
# check all predicts match | ||
compareFrames(predict, predict0) | ||
compareFrames(predict, predict1) | ||
compareFrames(predict, predict2) | ||
|
||
# remove the fold column in the dataset for prediction | ||
predict3 <- h2o.predict(object = mojo, newdata = mt[-c(13)]) | ||
predict4 <- h2o.predict(object = genericGLM, newdata = mt[-c(13)]) | ||
# make sure prediction still matches after removing fold column when calling predict | ||
compareFrames(predict, predict3) | ||
compareFrames(predict, predict4) | ||
} | ||
|
||
doTest("Fix GLM mojo with offset and fold column", test_glm_mojo_offset_fold_columns) |