Sigmoid function in Julia

I’m trying to write the sigmoid function as below, but did not work:

function sigmoid(z::Array)
    return 1.0 ./ (1.0 + exp(-z))
end;

Knowing that the below is working fine, but I’m looking for a short way to write it:

function sigmoid(z::Array)
    sig = []
    for (index, value) in enumerate(z)
         push!(sig, 1 / (1 + value))
    end
    return sig
end

It is already implemented as StatsFuns.logistic. Don’t define elementwise functions for arrays: use broadcasting.

You could do worse than to copy this one:

Edit: and apply it to an array by writing σ.(z).

Did not understand your reply

Instead of defining sigmoid(z::Array) define sigmoid(z::Real) and then use broadcasting to apply it to the elements of an array: sigmoid.(z) (note the dot, which indicates broadcasting)

This is also the problem you’re facing in your OP. If you were to define sigmoid(z::Array), which (again) you shouldn’t, you’ll need dots everywhere:

function sigmoid(z::Array)
    return 1.0 ./ (1.0 .+ exp.(-z))
end

However, it is much better to write

sigmoid(z::Real) = 1.0 / (1.0 + exp(-z))

Even better you can make this function type generic like so:

sigmoid(z::Real) = one(z) / (one(z) + exp(-z))

This way it would also work for number types that are not Float64: It will use the appropriate one element (the identity under multiplication) of that particular element type.

5 Likes

Thanks alot
I wrote it now as:

sigmoid(x::Real, derive::Bool=false) = 
(derive==true) ? x*(one(x)-x) : one(x)/(one(x) + exp(-x))
1 Like