Search code examples
rneural-networksigmoid

Sigmoid function in the neuralnet package in R


I can't seem to find any documentation of how to apply a sigmoid function in the neuralnet package, I tried:

neuralnet(...,act.fct="sigmoid")

However this returned;

Error: ''act.fct' is not known

Solution

  • You are looking for the "logistic" for that package.

    neuralnet(..., act.fct = "logistic")
    

    That said though, if you have a function that isn't in there (and there aren't many in that package) you can pass the function yourself.

    library(neuralnet)
    
    data(infert)
    
    set.seed(123)
    net.infert <- neuralnet(case~parity+induced+spontaneous, infert, 
                            err.fct="ce", linear.output=FALSE, likelihood=TRUE)
    
    sigmoid = function(x) {
      1 / (1 + exp(-x))
    }
    
    set.seed(123)
    net.infert2 <- neuralnet(case~parity+induced+spontaneous, infert, 
                            err.fct="ce", linear.output=FALSE, likelihood=TRUE,
                            act.fct = sigmoid)
    
    all.equal(net.infert$weights, net.infert2$weights)
    [1] TRUE