-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnnInitParams.m
43 lines (34 loc) · 1.46 KB
/
cnnInitParams.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
42
43
function theta = cnnInitParams(imageDim,filterDim,numFilters,...
poolDim,numClasses)
% Initialize parameters for a single layer convolutional neural
% network followed by a softmax layer.
%
% Parameters:
% imageDim - height/width of image
% filterDim - dimension of convolutional filter
% numFilters - number of convolutional filters
% poolDim - dimension of pooling area
% numClasses - number of classes to predict
%
%
% Returns:
% theta - unrolled parameter vector with initialized weights
%% Initialize parameters randomly based on layer sizes.
assert(filterDim < imageDim,'filterDim must be less that imageDim');
outDim = imageDim - filterDim + 1; % dimension of convolved image
% assume outDim is multiple of poolDim
assert(mod(outDim, poolDim)==0,...
'poolDim must divide imageDim - filterDim + 1');
Wc = 1e-1*randn(filterDim,filterDim,numFilters);
outDim = outDim/poolDim;
hiddenSize = outDim^2*numFilters;
% we'll choose weights uniformly from the interval [-r, r]
r = sqrt(6) / sqrt(numClasses+hiddenSize+1);
Wd = rand(numClasses, hiddenSize) * 2 * r - r;
bc = 0.001*randn(numFilters, 1);
bd = 0.001*randn(numClasses, 1);
% Convert weights and bias gradients to the vector form.
% This step will "unroll" (flatten and concatenate together) all
% your parameters into a vector, which can then be used with minFunc.
theta = [Wc(:) ; Wd(:) ; bc(:) ; bd(:)];
end