# Flux: different activation functions for different outputs

**URL:** <https://discourse.julialang.org/t/flux-different-activation-functions-for-different-outputs/41938>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [June 23, 2020, 2:30pm UTC](https://discourse.julialang.org/t/flux-different-activation-functions-for-different-outputs/41938 "2020-06-23T14:30:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmurray](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmurray/32/15806_2.png) [@jmurray](https://discourse.julialang.org/u/jmurray)\
**Post date:** [June 23, 2020, 2:30pm UTC](https://discourse.julialang.org/t/flux-different-activation-functions-for-different-outputs/41938/1 "2020-06-23T14:30:37Z")

</div>

I need to apply different activation functions to different outputs of a Flux layer. This requires a custom layer. I can modify an [existing custom layer](https://discourse.julialang.org/t/flux-custom-layer/41481) so that its only job is to apply a per-output activation function as shown:

```julia
function (a::Nonneg)(x::AbstractArray)
    x_out = x # or a.W * x .+ a.b
    return vcat(softplus.(x[1:1,:]), σ.(x[2:2,:]), σ.(x[3:3,:]))
end

```

Here, `σ` is the activation function that’s part of the layer’s `struct`, and `softplus` is hard-coded to a particular output to enforce non-negativity. But I would really like to not have to hard-code any functions or the (multiple) indices at which they are to be applied. What’s a good approach to this?

There was a [similar topic](https://discourse.julialang.org/t/how-to-apply-an-activation-function-to-a-subset-of-output-units/33202/2) a few months back that used a `struct` to apply an activation function only to a (hard-coded) subset of the outputs, but it reportedly didn’t work for arrays of dimension larger than one (as I’ve confirmed). There might be a simple fix. If so, then maybe one can replace the struct’s `activationfn` with an array of activation functions?
