-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack2params.m
38 lines (34 loc) · 1.52 KB
/
stack2params.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
function [params, numParams] = stack2params(stack)
% Converts a "stack" structure into a flattened parameter vector and also
% stores the network configuration. This is useful when working with
% optimization toolboxes such as minFunc.
%
% [params, netconfig] = stack2params(stack)
%
% stack - the stack structure, where stack{1}.w = weights of first layer
% stack{1}.b = weights of first layer
% stack{2}.w = weights of second layer
% stack{2}.b = weights of second layer
% ... etc.
% This is a non-standard version of the code to support conv nets
% it allows higher layers to have window sizes >= 1 of the previous layer
% If using a gpu pass inParams as your gpu datatype
% Setup the compressed param vector
params = [];
for d = 1:numel(stack)
% This can be optimized. But since our stacks are relatively short, it
% is okay
params = [params ; stack{d}.W(:) ; stack{d}.b(:) ];
% Check that stack is of the correct form
assert(size(stack{d}.W, 1) == size(stack{d}.b, 1), ...
['The bias should be a *column* vector of ' ...
int2str(size(stack{d}.W, 1)) 'x1']);
% no layer size constrain with conv nets
if d < numel(stack)
assert(mod(size(stack{d+1}.W, 2), size(stack{d}.W, 1)) == 0, ...
['The adjacent layers L' int2str(d) ' and L' int2str(d+1) ...
' should have matching sizes.']);
end
end
numParams = length(params);
end