Permalink
Switch branches/tags
last_OK jenkins-tomk-hadoop-1 jenkins-tomas_jenkins-7 jenkins-tomas_jenkins-6 jenkins-tomas_jenkins-5 jenkins-tomas_jenkins-4 jenkins-tomas_jenkins-3 jenkins-tomas_jenkins-2 jenkins-tomas_jenkins-1 jenkins-sample-docs-3 jenkins-sample-docs-2 jenkins-sample-docs-1 jenkins-rel-wright-8 jenkins-rel-wright-7 jenkins-rel-wright-6 jenkins-rel-wright-5 jenkins-rel-wright-4 jenkins-rel-wright-3 jenkins-rel-wright-2 jenkins-rel-wright-1 jenkins-rel-wolpert-11 jenkins-rel-wolpert-10 jenkins-rel-wolpert-9 jenkins-rel-wolpert-8 jenkins-rel-wolpert-7 jenkins-rel-wolpert-6 jenkins-rel-wolpert-5 jenkins-rel-wolpert-4 jenkins-rel-wolpert-3 jenkins-rel-wolpert-2 jenkins-rel-wolpert-1 jenkins-rel-wheeler-12 jenkins-rel-wheeler-11 jenkins-rel-wheeler-10 jenkins-rel-wheeler-9 jenkins-rel-wheeler-8 jenkins-rel-wheeler-7 jenkins-rel-wheeler-6 jenkins-rel-wheeler-5 jenkins-rel-wheeler-4 jenkins-rel-wheeler-3 jenkins-rel-wheeler-2 jenkins-rel-wheeler-1 jenkins-rel-weierstrass-7 jenkins-rel-weierstrass-6 jenkins-rel-weierstrass-5 jenkins-rel-weierstrass-4 jenkins-rel-weierstrass-3 jenkins-rel-weierstrass-2 jenkins-rel-weierstrass-1 jenkins-rel-vapnik-1 jenkins-rel-vajda-4 jenkins-rel-vajda-3 jenkins-rel-vajda-2 jenkins-rel-vajda-1 jenkins-rel-ueno-12 jenkins-rel-ueno-11 jenkins-rel-ueno-10 jenkins-rel-ueno-9 jenkins-rel-ueno-8 jenkins-rel-ueno-7 jenkins-rel-ueno-6 jenkins-rel-ueno-5 jenkins-rel-ueno-4 jenkins-rel-ueno-3 jenkins-rel-ueno-2 jenkins-rel-ueno-1 jenkins-rel-tverberg-6 jenkins-rel-tverberg-5 jenkins-rel-tverberg-4 jenkins-rel-tverberg-3 jenkins-rel-tverberg-2 jenkins-rel-tverberg-1 jenkins-rel-tutte-2 jenkins-rel-tutte-1 jenkins-rel-turnbull-2 jenkins-rel-turnbull-1 jenkins-rel-turing-10 jenkins-rel-turing-9 jenkins-rel-turing-8 jenkins-rel-turing-7 jenkins-rel-turing-6 jenkins-rel-turing-5 jenkins-rel-turing-4 jenkins-rel-turing-3 jenkins-rel-turing-2 jenkins-rel-turing-1 jenkins-rel-turin-4 jenkins-rel-turin-3 jenkins-rel-turin-2 jenkins-rel-turin-1 jenkins-rel-turchin-11 jenkins-rel-turchin-10 jenkins-rel-turchin-9 jenkins-rel-turchin-8 jenkins-rel-turchin-7 jenkins-rel-turchin-6 jenkins-rel-turchin-5 jenkins-rel-turchin-4 jenkins-rel-turchin-3
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (57 sloc) 2.79 KB
library(glmnet)
setwd(normalizePath(dirname(R.utils::commandArgs(asValues=TRUE)$"f")))
source("../../../scripts/h2o-r-test-setup.R")
test.glm_reg_path <- function() {
# first run with no validation, compare against itself and glmnet
d <- h2o.importFile(path = locate("smalldata/logreg/prostate.csv"))
m = h2o.glm(training_frame=d,x=3:9,y=2,family='binomial',alpha=1,lambda_search = TRUE, solver='COORDINATE_DESCENT')
regpath = h2o.getGLMFullRegularizationPath(m)
expect_true(is.null(regpath$explained_deviance_valid))
coefs1 = coefficients(m@model)
coefs2 = regpath$coefficients[length(regpath$lambdas),]
expect_false(max(abs(coefs1[names(coefs2)] - coefs2)) > 1e-10)
# run glmnet
d2 = as.data.frame(d)
x = as.matrix(d2[,3:9])
y = as.matrix(d2[,2])
m_net = glmnet(x=x,y=y,family='binomial')
for(i in 1:length(regpath$lambdas)){
coefs_net = m_net$beta[,i]
coefs_h2o = regpath$coefficients[i,]
diff = max(abs((coefs_h2o[names(coefs_net)] - coefs_net)/max(1,coefs_net)))
expect_false(diff > 1e-3)
}
print("with validation")
# now make sure we can run with validation
splits = h2o.splitFrame(d)
d2 = as.data.frame(splits[[1]])
x = as.matrix(d2[,3:9])
y = as.matrix(d2[,2])
m_net = glmnet(x=x,y=y,family='binomial')
m = h2o.glm(training_frame=splits[[1]],validation_frame=splits[[2]],x=3:9,y=2,family='binomial',alpha=1,lambda_search = TRUE, solver='COORDINATE_DESCENT')
regpath = h2o.getGLMFullRegularizationPath(m)
regpath$explained_deviance_train
expect_false(is.null(regpath$explained_deviance_valid))
n = min(length(m_net$lambda),dim(regpath$coefficients)[1])
for(i in 1:n){
coefs_net = m_net$beta[,i]
coefs_h2o = regpath$coefficients[i,]
diff = max(abs((coefs_h2o[names(coefs_net)] - coefs_net)/max(1,coefs_net)))
expect_false(diff > 1e-3)
model2 <- h2o.makeGLMModel(m,coefs_h2o)
perf1 = h2o.performance(model2,splits[[1]])
explained_dev_train = (1-perf1@metrics$residual_deviance/perf1@metrics$null_deviance)
print(c(explained_dev_train,regpath$explained_deviance_train[i]))
diff = abs(explained_dev_train - regpath$explained_deviance_train[i])
cat('diff = ',diff,'\n')
expect_false(diff > 1e-4)
perf2 = h2o.performance(model2,splits[[2]])
explained_dev_test = (1-perf2@metrics$residual_deviance/perf2@metrics$null_deviance)
expect_false(is.null(regpath$explained_deviance_valid))
diff = abs(explained_dev_test - regpath$explained_deviance_valid[i])
print(c(explained_dev_test,regpath$explained_deviance_valid[i]))
cat('diff2 = ',diff,'\n')
expect_false(diff > 1e-4)
}
}
doTest("GLM Regularization Path extraction", test.glm_reg_path)