r R脚本采用H2o模型权重和偏差,并通过NeuralNetTools旧的变量重要性函数发送它们......并且第一个ti

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r R脚本采用H2o模型权重和偏差,并通过NeuralNetTools旧的变量重要性函数发送它们......并且第一个ti相关的知识,希望对你有一定的参考价值。

#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")

以上是关于r R脚本采用H2o模型权重和偏差,并通过NeuralNetTools旧的变量重要性函数发送它们......并且第一个ti的主要内容,如果未能解决你的问题,请参考以下文章