# Optimizable parameters

**URL:** https://discourse.julialang.org/t/optimizable-parameters/14689
**Category:** General Usage
**Created:** [September 8, 2018, 12:46am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689 "2018-09-08T00:46:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [September 8, 2018, 12:46am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/1 "2018-09-08T00:46:55Z")

</div>

**TLDR: I want to use `Parameters.jl` and convert a fraction of its fields to a vector and back. I can convert to the vector, but I don’t know how to convert back - please help**

Here is my MWE `Para` type

```julia
using Parameters
@with_kw struct Para{T}
    a::T = 1.0
    b::T = 2.0
    c::T = 3.0
end

```

with three parameters (`a`, `b`, and `c`). Some examples of concrete `Para`s:

```julia
julia> p1 = Para()
Para{Float64}
  a: Float64 1.0
  b: Float64 2.0
  c: Float64 3.0

julia> p2 = Para(a = 4.0, c = 5.0)
Para{Float64}
  a: Float64 4.0
  b: Float64 2.0
  c: Float64 5.0

```

Note that the `Para` constructor nicely works by supplying it assignments (like `a = 4.0` - Thanks to [`Parameters.jl`](https://github.com/mauro3/Parameters.jl)! ).

I want to optimize only `a` and `b`. For that, I want to create a vector of the values in `a` and `b` for a given `Para`. So I create a tuple of the symbol names

```julia
optimizable_Para = (:a, :b)

```

which lists the “optimizable” parameters. To create the vector of the optimizable parameters, I have the function

```julia
function convert_Para_to_Vector(p::Para)
    [getfield(p, s) for s in optimizable_Para]
end

```

Some examples of conversion from `Para` to `Vector`:

```julia
julia> x1 = convert_Para_to_Vector(p1)
2-element Array{Float64,1}:
 1.0
 2.0

julia> x2 = convert_Para_to_Vector(p2)
2-element Array{Float64,1}:
 4.0
 2.0

```

Now I want to do the opposite, i.e., create a `Para` from a `Vector` (and assign the default values to the parameters not listed in `optimizable_Para`, the _non-optimizable_ parameters).  
Thus I am looking for a function `convert_Vector_to_Para` that achieves

```julia
function convert_Vector_to_Para(x)
    Para(a = x[1], b = x[2])
end

```

But, instead of explicitly writing the assignments (`a = x[1], b = x[2]`), I would like to use the symbols in `oPara`. So that if I change `oPara` in the future, I don’t have to rewrite `convert_Vector_to_Para`. **I have not been able to figure how to do that so far… Please help?**

My guess is I need a macro, but the closest I have been is via a function which does not work:

```julia
function convert_Vector_to_Para(x)
    eval(Expr(:call, :Para, [Expr(:(=), oPara[i], x[i]) for i in 1:length(optimizable_Para)]...))
end

```

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [September 8, 2018, 1:46am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/2 "2018-09-08T01:46:23Z")

</div>

```julia
function Para(obj::AbstractVector,
              overwrite::AbstractVector{<:Symbol})
    fn = fieldnames(Para)
    default = Para()
    T = eltype(obj)
    return Para(NamedTuple{fn}(
                    name in overwrite ?
                        obj[idx] :
                        T(getfield(default, name)) for
                    (idx, name) in enumerate(fn))...)
end

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 8, 2018, 1:59am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/3 "2018-09-08T01:59:48Z")

</div>

[Flatten.jl](https://github.com/rafaqz/Flatten.jl) was made entirely to do this! Even for nested structs.

Like:

```julia
using Flatten
import Flatten: flattenable

@flattenable @with_kw struct Para{T}
    a::T = 1.0 | true
    b::T = 2.0 | true
    c::T = 3.0 | false
end

julia> para = Para()
Para{Float64}
  a: Float64 1.0
  b: Float64 2.0
  c: Float64 3.0

julia> fieldnameflatten(para)
(:a, :b)

julia> data = flatten(Vector,para)
2-element Array{Float64,1}:
 1.0
 2.0

julia> data[2] = 5.0
5.0

julia> Flatten.reconstruct(para, data)
Para{Float64}
  a: Float64 1.0
  b: Float64 5.0
  c: Float64 3.0

```

It becomes increasingly useful the larger and more complicated the struct, and it generates pretty fast code.

To use the fieldnames in the array, make an AxisArray

```julia
data = flatten(Vector,para)
names = fieldnameflatten(Vector, para)
a = AxisArray(data, Axis{:parameters}(names))                                                                                                                                                                                             

julia> a[:b]                                                                                                               
2.0                

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 8, 2018, 2:37am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/4 "2018-09-08T02:37:03Z")

</div>

Make it a subtype of FieldVector from StaticArrays.jl

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 8, 2018, 2:43am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/5 "2018-09-08T02:43:37Z")

</div>

That’s the simplest way, but he needs to exclude fields from the vector and rebuild ignoring those excluded fields

---

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [September 8, 2018, 3:30am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/6 "2018-09-08T03:30:40Z")

</div>

> [@Raf](#):
>
> Flatten.jl

Thank you very much, this is even better than what I was looking for! I’ll try it right away!

---

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [September 8, 2018, 3:51am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/7 "2018-09-08T03:51:52Z")

</div>

@Raf It seems Flatten.jl (and Tags.jl) are not in the reigistry. Is that normal?

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 8, 2018, 3:57am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/8 "2018-09-08T03:57:36Z")

</div>

That’s in the process of happening right now! except some people thought Tags.jl wasn’t a clear enough name (after I already renamed it from MetaFields.jl), I’m considering FieldTags.jl or FieldMetadata.jl, if you have any opinions now is the time!

So for now use `add "https://github.com/rafaqz/Tags.jl"` and `add "https://github.com/rafaqz/Flatten.jl"` but also expect some changes in the coming month.

Any feedback or feature requests you have would be really useful too, I’m not sure how people will actually use these packages.

---

<div class="post-metadata">

### Author: ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)
#### Post date: [September 14, 2018, 7:27am UTC](https://discourse.julialang.org/t/optimizable-parameters/14689/9 "2018-09-14T07:27:53Z")

</div>

> I’m considering FieldTags.jl or FieldMetadata.jl, if you have any opinions now is the time!

My uneducated vote would go to `FieldMetadata.jl`. It is longer but I think it is more descriptive. I’ll got there to ask things about it 🙂
