# Mamba Chains declaration for matrix of parameters

**URL:** <https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 8, 2019, 5:35pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663 "2019-10-08T17:35:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_xu/32/10688_2.png) [@Steven\_Xu](https://discourse.julialang.org/u/Steven_Xu)\
**Post date:** [October 8, 2019, 5:35pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/1 "2019-10-08T17:35:07Z")

</div>

This might be a stupid question but I just want to make sure I am doing it correctly. I have read the document but is still unsure.

I am trying to use Mamba’s NUTSVariate to implement a Bayesian neural network. My parameters are then the weight matrices and bias vectors.

Suppose I have weight matrix \alpha\in\mathbb{R}^{6\times5}, \beta\in\mathbb{R}^5, my codes look roughly like

```julia
α = rand(Normal(),6,5)
β = rand(Normal(),1,5)
Θ = [vec(α),vec(β)] 
function logfgrad(Θ::DenseVector)
    θ = reshape(Θ[1],J,K)
    α = reshape(Θ[2],J,K)
    loglik = ...
    Δα = ... #A 6 by 5 array of all gradients α[i,j]
    Δβ = ... #A 1 by 5 array of all gradients β[j]
    grad = [Δα, Δβ]
    return loglik, grad
end

n_samp = 10000
burnin = 5000
sim = Chains(iters = n_samp, params=?, start=(burnin+1), names=?)
samp = NUTSVariate(Θ, logfgrad)

```

My questions are:

Are the input vector \Theta and output vector **grad** specified correctly? Currently they are in the form \Theta = [[\alpha\_{1,1},\alpha\_{2,1},...,\alpha\_{6,5}] ,[\beta\_1,...\beta\_5]] and \textbf{grad}=[[\Delta\alpha\_{1,1},\Delta\alpha\_{2,1},...,\Delta\alpha\_{6,5}], [\Delta\beta\_1,...\Delta\beta\_5]], should I instead reshape them into \Theta = [\alpha\_{1,1},\alpha\_{2,1},...,\alpha\_{6,5},\beta\_1,...\beta\_5], etc?

I think the field **params** is the number of parameters right? In this case it should be `length(Θ)`?  
For **names** do I need to give a name for each single parameter, like `["α[1,1]","α[2,1]",...,"β[5]"]`?

Thank you for all the help!

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 8, 2019, 9:49pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/2 "2019-10-08T21:49:42Z")

</div>

This doesn’t answer your question directly, but I recommend [Turing](https://github.com/TuringLang/Turing.jl) or [DynamicHMC](https://github.com/tpapp/DynamicHMC.jl), as they are more activity maintained and developed. Turing in particular is geared towards machine learning. You might find some useful information in this [thread](https://discourse.julialang.org/t/bayesian-neural-network-multiclass-classification-in-turing-jl-and-flux-jl/21134/3).

---

<div class="post-metadata">

**Author:** ![Steven\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_xu/32/10688_2.png) [@Steven\_Xu](https://discourse.julialang.org/u/Steven_Xu)\
**Post date:** [October 8, 2019, 10:28pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/3 "2019-10-08T22:28:49Z")

</div>

Hi Christopher,

Thank you for the recommendations. DynamicHMC seems interesting, however is there any resource that provide examples with self-coded gradient function? I would like to avoid automatic differentiation.

@Tamas_Papp

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 8, 2019, 11:05pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/4 "2019-10-08T23:05:37Z")

</div>

Also see [https://github.com/TuringLang/AdvancedHMC.jl](https://github.com/TuringLang/AdvancedHMC.jl).

---

<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 9, 2019, 6:04am UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/5 "2019-10-09T06:04:49Z")

</div>

It is very simple, see

[https://tamaspapp.eu/LogDensityProblems.jl/dev/#Manually-calculated-derivatives-1](https://tamaspapp.eu/LogDensityProblems.jl/dev/#Manually-calculated-derivatives-1)

for a template. This, however, applies to log density functions of \mathbb{R}^n \to \mathbb{R} — if your domain is constrained you have to account for transformations yourself.

Feel free to ping me if you need help with this.

---

<div class="post-metadata">

**Author:** ![Steven\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_xu/32/10688_2.png) [@Steven\_Xu](https://discourse.julialang.org/u/Steven_Xu)\
**Post date:** [October 9, 2019, 7:58pm UTC](https://discourse.julialang.org/t/mamba-chains-declaration-for-matrix-of-parameters/29663/6 "2019-10-09T19:58:33Z")

</div>

Thanks! This is very useful.
