Skip to content

Commit

Permalink
Merge branch 'reducedmemory'
Browse files Browse the repository at this point in the history
  • Loading branch information
vishrutg committed Oct 14, 2015
2 parents e0120d5 + f1cae11 commit b4d5c33
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
17 changes: 2 additions & 15 deletions algorithms/HPdclassifier/R/hpdRF_parallelTree.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ hpdrandomForest <- hpdRF_parallelTree <- function(formula, data,
model$type = "regression"
if(completeModel)
{
tryCatch(
{
model$mse = oob_predictions$mse
model$rsq = oob_predictions$rsq

Expand All @@ -439,10 +437,6 @@ hpdrandomForest <- hpdRF_parallelTree <- function(formula, data,
model$test$predicted,
na.rm = TRUE)
}
},
error = function(e){
stop(paste("could not compute additional statistics due to error:", e))
})
}
}
else
Expand All @@ -451,9 +445,7 @@ hpdrandomForest <- hpdRF_parallelTree <- function(formula, data,

if(completeModel)
{
tryCatch(
{
confusion = confusionMatrix(true_responses,
confusion = HPdutility::confusionMatrix(true_responses,
model$predicted)
model$err.rate = oob_predictions$err.rate
classErr <- model$err.rate[nrow(model$err.rate),-1]
Expand Down Expand Up @@ -486,7 +478,7 @@ hpdrandomForest <- hpdRF_parallelTree <- function(formula, data,
{
stop("Categories of 'ytest' are not the same as categories of response variable trained in model")
}
confusionTest = confusionMatrix(ytest,
confusionTest = HPdutility::confusionMatrix(ytest,
model$test$predicted)
model$test$err.rate = errorRate(ytest,
model$test$predicted)
Expand All @@ -497,11 +489,6 @@ hpdrandomForest <- hpdRF_parallelTree <- function(formula, data,
model$test$confusion <-cbind(confusionTest,
classErrTest)
}
},
error = function(e){
stop(paste("could not compute additional statistics due to error:", e))
})

}
}

Expand Down
50 changes: 13 additions & 37 deletions algorithms/HPdclassifier/R/hpdRFdistributed.R
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@
tree_ids = tree_ids[-1]
forest = .Call("unserializeForest",forest,
PACKAGE = "HPdclassifier")

forestparam = .Call("getForestParameters", forest,
PACKAGE = "HPdclassifier")

Expand All @@ -609,35 +608,16 @@
lapply(categorical_variables,
function(var)
observations[,var]+1)
predictions = matrix(as.numeric(NA),

predictions = matrix(as.double(NA),
ncol = nrow(observations),
length(tree_ids))

tree_ids = tree_ids - 1
temp_predictions = lapply(1:length(tree_ids),
function(tree_id_index)
{
tree_id = tree_ids[tree_id_index]
tree_oob_indices = oob_indices[[tree_id]]
tree_predictions = sapply(tree_oob_indices,
function(obs)
.Call("specificTreePredictObservation",
forest, as.integer(tree_id),
observations,
as.integer(obs),
PACKAGE = "HPdclassifier"))
return(cbind(rep(tree_id_index,
length(tree_oob_indices)),
tree_oob_indices,as.numeric(tree_predictions)))
})
if(length(temp_predictions)>0)
{
for(i in 1:length(temp_predictions))
{
predictions[temp_predictions[[i]][,c(1,2)]] =
temp_predictions[[i]][,3]
}
}
oob_indices = lapply(oob_indices, as.integer)
.Call("forestPredictOOB", forest, predictions, observations,
oob_indices, as.integer(tree_ids))

.Call("garbageCollectForest",forest)
update(predictions)
},progress = FALSE)
Expand Down Expand Up @@ -1097,16 +1077,12 @@
PACKAGE = "HPdclassifier")

forestparam=.Call("getForestParameters", forest)
response_cardinality = forestparam[[2]]
ntree = forestparam[[7]]
predictions = sapply(1:nrow(new_observations), function(index)
sapply(tree_ids, function(tree_id)
as.numeric(.Call("specificTreePredictObservation",
forest, as.integer(tree_id),
new_observations,
as.integer(index),
PACKAGE = "HPdclassifier"))))
predictions = matrix(predictions,ncol=nrow(new_observations))
predictions = matrix(as.double(NA),
ncol = nrow(new_observations),
length(tree_ids))
.Call("forestPredictObservations", forest, predictions,
new_observations, as.integer(tree_ids))

update(predictions)
.Call("garbageCollectForest",forest)
rm(list = ls())
Expand Down
45 changes: 45 additions & 0 deletions algorithms/HPdclassifier/src/hpdRFpredict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,49 @@ extern "C"
return R_new_errors;
}


void forestPredictOOB(SEXP R_forest, SEXP R_predictions, SEXP R_observations,
SEXP R_indices, SEXP R_tree_ids)
{
hpdRFforest *forest = (hpdRFforest *) R_ExternalPtrAddr(R_forest);
double *predictions = REAL(R_predictions);
for(int tree_index = 0; tree_index < length(R_tree_ids); tree_index++)
{
int tree_id = INTEGER(R_tree_ids)[tree_index]-1;
SEXP indices;
if(length(R_indices) > 1)
indices = VECTOR_ELT(R_indices,tree_id);
else
indices = R_indices;
for(int i = 0; i < length(indices); i++)
{
int obs_index = INTEGER(indices)[i]-1;
predictions[obs_index*length(R_tree_ids)+tree_index] =
treePredictObservation(forest->trees[tree_id],
R_observations,
forest->features_cardinality,
obs_index);
}
}
}
void forestPredictObservations(SEXP R_forest, SEXP R_predictions,
SEXP R_observations, SEXP R_tree_ids)
{
hpdRFforest *forest = (hpdRFforest *) R_ExternalPtrAddr(R_forest);
double *predictions = REAL(R_predictions);
int num_predictions = length(VECTOR_ELT(R_observations,0));
for(int tree_index = 0; tree_index < length(R_tree_ids); tree_index++)
{
int tree_id = INTEGER(R_tree_ids)[tree_index]-1;
for(int obs_index = 0; obs_index < num_predictions; obs_index++)
{
predictions[obs_index*length(R_tree_ids)+tree_index] =
treePredictObservation(forest->trees[tree_id],
R_observations,
forest->features_cardinality,
obs_index);
}
}
}

}

0 comments on commit b4d5c33

Please sign in to comment.