Last active
September 1, 2016 13:09
NNET
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
# function to find optimal hidden nodes and threshold | |
cv_nnet_models = function(train, validation, start, end) { | |
print(paste('Cross validating neural network on ams score with nodes from',start,'to',end)) | |
# create data frame to return best model for each hidden node level | |
neural_df = data.frame(hidden_nodes= NULL, threshold=NULL, ams_sum = NULL) | |
# iterate with increasing number of hidden nodes | |
for (hidden_nodes in seq(start,end,1)){ | |
print(paste('Training with', hidden_nodes,'hidden nodes')) | |
# train model | |
model = train_neural(train, hidden_nodes) | |
bst_ams= 0 | |
bst_model = NULL | |
# check which threhsold between 0 and 1, with intervasl of .025 yields highest ams | |
for (i in seq(0,1,.025)) { | |
print(paste('Predicting layer with',hidden_nodes,'nodes & threshold: ', i)) | |
predicted = get_prediction(validation, i, model, 'nnet') | |
ams_sum = get_ams(validation, predicted) | |
# if ams for threshold is highest, make it best model for this hidden node level | |
if (ams_sum > bst_ams) { | |
vec = c(hidden_nodes, i, ams_sum) | |
print(vec) | |
bst_ams = ams_sum | |
bst_model = vec | |
} | |
} | |
print(paste('Choosing best model with ams score:', bst_ams)) | |
# add best threshold model to data frame of models with different node level | |
neural_df = rbind(neural_df, bst_model) | |
} | |
return(neural_df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment