# Bayesian neural networks in Flux

**URL:** <https://discourse.julialang.org/t/bayesian-neural-networks-in-flux/39065>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [May 8, 2020, 7:57am UTC](https://discourse.julialang.org/t/bayesian-neural-networks-in-flux/39065 "2020-05-08T07:57:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeepQ](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@DeepQ](https://discourse.julialang.org/u/DeepQ)\
**Post date:** [May 8, 2020, 7:57am UTC](https://discourse.julialang.org/t/bayesian-neural-networks-in-flux/39065/1 "2020-05-08T07:57:12Z")

</div>

I am trying to implement a Bayesian neural network in Flux. I have custom layers which return two outputs. One is the output of the layer and another is a conditional probability. I use these probabilities to define a loss function using the negative log-likelihood. My question is that how can I define a custom layer with two outputs instead of one?

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [May 8, 2020, 9:40am UTC](https://discourse.julialang.org/t/bayesian-neural-networks-in-flux/39065/2 "2020-05-08T09:40:29Z")

</div>

just return both: `return out1, out2`  
If the layer is stacked into a Chain, make sure the following layer can work with an input tuple of length 2

---

<div class="post-metadata">

**Author:** ![DeepQ](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@DeepQ](https://discourse.julialang.org/u/DeepQ)\
**Post date:** [May 8, 2020, 9:50am UTC](https://discourse.julialang.org/t/bayesian-neural-networks-in-flux/39065/3 "2020-05-08T09:50:13Z")

</div>

So is this code true? All the layers in the network are of the same type.

`

```julia
> struct BayesianLayer
> model
> end
> 
> function (l::BayesianLayer)(x)
> len = length(x)
> if len == 1
> out1 = l.model(x) 
> out2 = somefunction(out1)
> elseif len == 2
> out1 = l.model(x[1])
> out2 = somefunction(out1)*x[2]
> end
> return out1, out2
> end

```

`
