Last active
May 18, 2016 22:14
-
-
Save eristoddle/28e9e22c9f0f958e5c2e1a52b694d2d7 to your computer and use it in GitHub Desktop.
R script to take an H2o models weights and biases and send them through the NeuralNetTools olden variable importance function...and first time I have used R so I could be doing things totally wrong but it works.
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
#Rscript processModel.R | |
#http://rpackages.ianhowson.com/cran/NeuralNetTools/man/olden.html | |
#https://cran.r-project.org/web/packages/nnet/nnet.pdf | |
library(NeuralNetTools) | |
library(h2o) | |
library(jsonlite) | |
model_id <- 'deeplearning-f168835d-9e6e-4170-b881-fb5f214f302b' | |
localH2O <- h2o.init() | |
model <- h2o.getModel(model_id) | |
model_param <- slot(model, 'allparameters') | |
hidden_layers <- model_param[['hidden']] | |
layer_count <- length(hidden_layers) + 1 | |
struct <- c() | |
weights <- list() | |
wts_in <- c() | |
for(i in 1:layer_count){ | |
weights_df <- as.data.frame(h2o.weights(model, i)) | |
nueron_count <- length(weights_df) | |
struct <-c(struct, nueron_count) | |
biases_df <- as.data.frame(h2o.biases(model, i)) | |
#nnet structure: The first value in each element of the list is the weight from the bias layer | |
names(biases_df) <- c('B1') | |
weights_df <- cbind(biases_df, weights_df) | |
weights[[i]] = weights_df | |
wts_in <- c(wts_in, t(weights[[i]]), recursive = TRUE) | |
} | |
struct <-c(struct, 1) | |
x_names <- names(weights[[1]]) | |
x_names <- x_names[-1] | |
importances <- olden(wts_in, struct, x_lab=x_names, bar_plot=FALSE) | |
importance_json <- toJSON(importances, pretty=TRUE, dataframe='rows') | |
print(importance_json) | |
write(importance_json, file="./importances.json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment