neural networkR包中的计算函数在复制时不工作

我在neuralnet训练了一个模型,我想弄清楚如何在Excel中计算结果。 使用你从包中调用的computefunction一切正常。 但是我在Rstudio和github页面中使用F2进入源代码,并且函数不工作,并停止在relist()函数上,并给出了错误: Error in relist(weights, nrow.weights, ncol.weights) : unused argument (ncol.weights)

我认为问题是relist( )函数,但是我不知道如何在没有它的情况下转换权重。 neuralnet包不带有它自己的版本的relist() 。 如果忽略relist行,则会出现以下错误: Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments因为weights未正确转换,所以出现Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments 。 我在自己的数据上尝试了同样的事情,得到了同样的错误。

 library(neuralnet) normalize <-function(x) { return((x - min(x))/(max(x) - min(x))) } newdf <- Cars93 newdf = na.omit(newdf) newdf <- newdf[complete.cases(newdf),] newdf$Cylinders <- as.numeric(levels(newdf$Cylinders))[newdf$Cylinders] newdf$Horsepower <- normalize(newdf$Horsepower) newdf$EngineSize <- normalize(newdf$EngineSize) newdf$Cylinders <- normalize(newdf$Cylinders) smp_size <- floor(0.75 * nrow(newdf)) set.seed(12) train_ind <- sample(seq_len(nrow(newdf)), size = smp_size) train <- newdf[train_ind, ] test <- newdf[-train_ind, ] carsNN <- neuralnet(Horsepower ~ Cylinders+EngineSize, data = train,hidden = c(1)) cars_results = compute(carsNN,test[11:12]) #this is the source code using F2 in RStudio and on github sourceCodeCompute = function (x, covariate, rep = 1) { nn <- x linear.output <- nn$linear.output weights <- nn$weights[[rep]] nrow.weights <- sapply(weights, nrow) ncol.weights <- sapply(weights, ncol) weights <- unlist(weights) if (any(is.na(weights))) weights[is.na(weights)] <- 0 weights <- relist(weights, nrow.weights, ncol.weights) length.weights <- length(weights) covariate <- as.matrix(cbind(1, covariate)) act.fct <- nn$act.fct neurons <- list(covariate) if (length.weights > 1) for (i in 1:(length.weights - 1)) { temp <- neurons[[i]] %*% weights[[i]] act.temp <- act.fct(temp) neurons[[i + 1]] <- cbind(1, act.temp) } temp <- neurons[[length.weights]] %*% weights[[length.weights]] if (linear.output) net.result <- temp else net.result <- act.fct(temp) list(neurons = neurons, net.result = net.result) } sourceCodeCompute(carsNN,test[11:12]) 

您使用错误的重新启动function。 尝试明确调用neuralnet:::relist ,这是在package命名空间中自动使用的(未导出的)函数。

(我不知道这个问题与Excel有什么关系。)