# Need help with example; Mixture Density Networks from Site

**URL:** <https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514>\
**Category:** Performance\
**Tags:** flux\
**Created:** [May 23, 2022, 1:49pm UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514 "2022-05-23T13:49:06Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 31, 2022, 5:05am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/21 "2022-05-31T05:05:15Z")

</div>

sorry, in your case model is `pi`, `sigma` and `mu` →

```julia
# lowest-level?
data = [(y, x)]

for epoch in 1:n_epochs
    
    # forward
    l = 0f0
    
    # backward
    gs = gradient(pars) do
        pi_out = pi(y)
        sigma_out = sigma(y)
        mu_out = mu(y)
        l = mdn_loss(pi_out, sigma_out, mu_out, x)
    end
    Flux.update!(opt, pars, gs)

    if epoch % 1000 == 0
        println("Epoch: ", epoch, " loss: ", l)
    end
end

```

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [May 31, 2022, 5:31am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/22 "2022-05-31T05:31:10Z")

</div>

> [@Karthik-d-k](#):
>
> ```julia
> gs = gradient(pars) do
> pi_out = pi(y)
> sigma_out = sigma(y)
> mu_out = mu(y)
> l = mdn_loss(pi_out, sigma_out, mu_out, x)
> end
> 
> ```

👍

```julia
Epoch: 1000 loss: 2.901283803535177
Epoch: 2000 loss: 2.683036585344914
Epoch: 3000 loss: 2.496097359141723
Epoch: 4000 loss: 1.8825208240140192
Epoch: 5000 loss: 1.5377693999566997
Epoch: 6000 loss: 1.4791544586544207
Epoch: 7000 loss: 1.4550877233830257
Epoch: 8000 loss: 1.4337247960818376

```

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [May 31, 2022, 6:29am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/23 "2022-05-31T06:29:17Z")

</div>

But the final graph is still not as expected:  
 ![graph](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d4e958824af08d46b62e108863919ceb1ad8e35.jpeg)

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 31, 2022, 7:12am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/24 "2022-05-31T07:12:25Z")

</div>

But I’m able to reproduce plots 🤔.  
I will try to clean-up notebook that i have created and share it once done !!

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 31, 2022, 7:24am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/25 "2022-05-31T07:24:53Z")

</div>

I have placed my code [in this gist](https://gist.github.com/Karthik-d-k/8b70ba97764c795d9e5e22c5525683ce)

**P.S:** I’m not using `Distributions.jl` package, if u want u can use already available functions instead of one that i have created…!!

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [May 31, 2022, 8:41am UTC](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514/26 "2022-05-31T08:41:00Z")

</div>

![graphok](https://global.discourse-cdn.com/julialang/original/3X/a/0/a072dbae3716e421cc46b5d12d8df896dc8b4483.jpeg)

[Previous page](https://discourse.julialang.org/t/need-help-with-example-mixture-density-networks-from-site/81514.md?page=1)
