# Sigmoid function in Julia

**URL:** <https://discourse.julialang.org/t/sigmoid-function-in-julia/29594>\
**Category:** New to Julia\
**Created:** [October 7, 2019, 2:17pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594 "2019-10-07T14:17:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 7, 2019, 2:17pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/1 "2019-10-07T14:17:22Z")

</div>

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

```julia
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:

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

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 7, 2019, 2:19pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/2 "2019-10-07T14:19:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [October 7, 2019, 2:19pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/3 "2019-10-07T14:19:37Z")

</div>

You could do worse than to copy this one:  
[https://github.com/FluxML/NNlib.jl/blob/master/src/activation.jl#L10](https://github.com/FluxML/NNlib.jl/blob/master/src/activation.jl#L10)

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

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 7, 2019, 2:24pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/4 "2019-10-07T14:24:25Z")

</div>

Did not understand your reply

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 7, 2019, 2:30pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/5 "2019-10-07T14:30:39Z")

</div>

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:

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

```

However, it is much better to write

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

```

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 7, 2019, 2:35pm UTC](https://discourse.julialang.org/t/sigmoid-function-in-julia/29594/6 "2019-10-07T14:35:48Z")

</div>

Thanks alot  
I wrote it now as:

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

```
