Skip to content

Instantly share code, notes, and snippets.

@sjstebbins
Last active September 1, 2016 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjstebbins/384cd9816df4ccfe032fc5975259cc61 to your computer and use it in GitHub Desktop.
Save sjstebbins/384cd9816df4ccfe032fc5975259cc61 to your computer and use it in GitHub Desktop.
NNET
# 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