R is a programming language for statistical data processing and graphics, as well as a free open-source computing environment within the GNU project.
nn<-function() { print(" ") neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=20, threshold=0.0001) } nn()
within(benchmark(test.name=test.function(), # replications=c(3), # (+1 "") columns=c('test', 'replications', 'elapsed'), # order=c('elapsed', 'test')), # { average = elapsed/replications }) #
test replications elapsed average 1 _ 3 47.83 15.94333333
nn.s<-function() { print(" ") nets<-sapply(1:20, function(X) neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=1, threshold=0.0001)) }
test replications elapsed average 1 _ 3 46.05 15.35 2 _ 3 47.52 15.84
nn.p<-function() { print(" ") cl <- makeCluster(getOption("cl.cores", 4)) # clusterExport(cl,"infert") # clusterEvalQ(cl,library(neuralnet)) # neuralnet parSapply(cl, 1:20, function(X) # sapply neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=1, threshold=0.0001) ) stopCluster(cl) }
test replications elapsed average 3 _ 3 17.38 5.793333333 2 _ 3 45.88 15.293333333 1 _ 3 46.61 15.536666667
pneuralnet <- function(formula, data, rep=1, ..., cl) { clusterExport(cl,"data") clusterEvalQ(cl,library(neuralnet)) nets <- parLapply(cl, 1:rep, function(X) neuralnet(formula, data, rep=1, ...) ) # reached.threshold nets <- nets[order(sapply(1:rep,function(i){nets[[i]]$result.matrix["reached.threshold", ]}))] return(nets) } cl <- makeCluster(getOption("cl.cores", 4)) nets <- pneuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=4, threshold=0.0001, cl=cl) stopCluster(cl)
library(parallel) library(neuralnet) library(rbenchmark) data(infert, package="datasets") nn<-function() { print(" ") neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=20, threshold=0.0001) } nn.s<-function() { print(" ") nets<-sapply(1:20, function(X) neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=1, threshold=0.0001)) } nn.p<-function() { print(" ") cl <- makeCluster(getOption("cl.cores", 4)) clusterExport(cl,"infert") clusterEvalQ(cl,library(neuralnet)) parSapply(cl, 1:20, function(X) neuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=1, threshold=0.0001) ) stopCluster(cl) } pneuralnet <- function(formula, data, rep=1, ..., cl) { clusterExport(cl,"data") clusterEvalQ(cl,library(neuralnet)) nets <- parLapply(cl, 1:rep, function(X) neuralnet(formula, data, rep=1, ...) ) # reached.threshold nets <- nets[order(sapply(1:rep,function(i){nets[[i]]$result.matrix["reached.threshold", ]}))] return(nets) } within(benchmark(_=nn(), _=nn.s(), _=nn.p(), replications=c(3), columns=c('', 'replications', 'elapsed'), order=c('elapsed', 'test')), { average = elapsed/replications }) cl <- makeCluster(getOption("cl.cores", 4)) nets <- pneuralnet(case~parity+induced+spontaneous, infert, err.fct="ce", linear.output=FALSE, likelihood=TRUE,rep=4, threshold=0.0001, cl=cl) stopCluster(cl)
Source: https://habr.com/ru/post/168399/
All Articles