-
Notifications
You must be signed in to change notification settings - Fork 1
/
params2stack.m
41 lines (32 loc) · 1.02 KB
/
params2stack.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function stack = params2stack(params, ei)
% Converts a flattened parameter vector into a nice "stack" structure
% This is useful when you're building multilayer networks.
%
% stack = params2stack(params, netconfig)
%
% params - flattened parameter vector
% ei - auxiliary variable containing
% the configuration of the network
% Maps the params (a vector into a stack of weights)
depth = numel(ei.layer_sizes);
stack = cell(depth,1);
% the size of the previous layer
prev_size = ei.input_dim;
% marks current position in parameter vector
cur_pos = 1;
for d = 1:depth
% Creates layer d
stack{d} = struct;
hidden = ei.layer_sizes(d);
% Extracts weights
wlen = double(hidden * prev_size);
stack{d}.W = reshape(params(cur_pos:cur_pos+wlen-1), hidden, prev_size);
cur_pos = cur_pos+wlen;
% Extracts bias
blen = hidden;
stack{d}.b = reshape(params(cur_pos:cur_pos+blen-1), hidden, 1);
cur_pos = cur_pos+blen;
% Sets previous layer size
prev_size = hidden;
end
end