Sorry to be late to the party, catching up with my mailing list backlog.
> On Thursday, May 14, 2015 at 2:03:27 AM UTC-7, Tim Holy wrote:
>>
>> On Wednesday, May 13, 2015 11:50:00 PM Lyndon White wrote:
>> >
>> > function unpack!(nn::NN, θ::Vector)
>> > W_e_len = length(nn.W_e)
>> > b_e_len = length(nn.b_e)
>> > W_d_len = length(nn.W_d)
>> > b_d_len = length(nn.b_d)
>> > W_e_shape = size(nn.W_e)
>> > W_d_shape = size(nn.W_d)
>> >
>> > nn.W_e = reshape(θ[1: W_e_len],W_e_shape)
>> > nn.b_e = θ[W_e_len+1: W_e_len+b_e_len]
>> > nn.W_d = reshape(θ[W_e_len+b_e_len+1:
>> > W_e_len+b_e_len+W_d_len],W_d_shape
>> > )
>> > nn.b_d = θ[W_e_len+b_e_len+W_d_len+1: end]
>> >
>> > nn
>> > end
>> >
>> > function pack(nn::NN)
>> > pack(nn.W_e[:],nn.b_e, nn.W_d[:],nn.b_d[:]] _
>> > end
>> >
>> > pack(∇W_e::Matrix{Float64}, ∇b_e::Vector{Float64},
>> > ∇W_d::Matrix{Float64}, ∇
>> > b_d::Vector{Float64})
>> > [∇W_e[:], ∇b_e, ∇W_d[:], ∇b_d]
>> > end
I have been using pretty much the same strategy, but I exploit Julia
introspection a little bit to make things more generic under the
assumption that all the weights, and only the weights, are `Array`
fields of the model type.
import Base: length
function length(m::Model)
len = 0
for n in fieldnames(m)
issubtype(fieldtype(typeof(m), n), Array) || continue
len += length(getfield(m, n))
end
len
end
function flat!(v, m)
v = zeros(length(m))
Δ = 0
for n in fieldnames(m)
issubtype(fieldtype(typeof(m), n), Array) || continue
x = getfield(m, n)
for i in eachindex(x)
Δ += 1
v[Δ] = x[i]
end
end
v
end
flat(m) = flat!(zeros(length(m)), m)
function unflat!(m, v)
Δ = 0
for n in fieldnames(m)
issubtype(fieldtype(typeof(m), n), Array) || continue
x = getfield(m, n)
for i in eachindex(x)
Δ += 1
x[i] = v[Δ]
end
end
m
end
Potentially I should create an iterator `eacharray`, but so far I have
been too lazy to do it.
On 14 May 2015 at 14:40, John Myles White <[email protected]> wrote:
>
> In the long-term, the best way to do this will be to use SubArray and
> ReshapeArray. You'll allocate enough space for all parameters, then unpack
> them into separate objects when that helps.
Agreed, when we move closer to attain Array Nirvana [1] (views in
particular), this is exactly the direction I intend to go in.
[1]: https://github.com/JuliaLang/julia/issues/7941
Pontus