# Sum of Flux.params

**URL:** <https://discourse.julialang.org/t/sum-of-flux-params/54608>\
**Category:** Machine Learning\
**Created:** [February 4, 2021, 10:37am UTC](https://discourse.julialang.org/t/sum-of-flux-params/54608 "2021-02-04T10:37:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [February 4, 2021, 10:37am UTC](https://discourse.julialang.org/t/sum-of-flux-params/54608/1 "2021-02-04T10:37:33Z")

</div>

I tried these two functions to get the l2 regularization term for a NN model:

```julia
function ℓ₂(a)
    s = zero(Float32)
    for i in a
        s += sum(i.^2)
    end
    s
end

```

```julia
function l₂(a)
    s = zero(Float32)
    Lₐ = length(a)
    for i in 1:Lₐ
        @inbounds Lᵢ = length(a[i])
        for j in 1:Lᵢ
            @inbounds s += a[i][j]^2    
        end
    end
    s
end

```

But I get different result, for the first function I get 469.8826f0 and for the second I get 469.87442f0.  
Why this happens?  
Moreover the second function is much slower than the first one. Why?  
What is the best way to compute the sum of params of a model in julia?

Sorry for my english.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [February 4, 2021, 8:32pm UTC](https://discourse.julialang.org/t/sum-of-flux-params/54608/2 "2021-02-04T20:32:58Z")

</div>

This can be done with a one-liner 🙂 . [Regularisation · Flux](https://fluxml.ai/Flux.jl/stable/models/regularisation/) has some good examples.
