-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.m
144 lines (129 loc) · 5.19 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
% main loop
%% tunable constants
PARAMS.trainSamples = 28000;
PARAMS.batchSize = 15;
PARAMS.validatePercentage = .25;
PARAMS.maxEpoch = 7;
PARAMS.nodes = [500 500 2000];
PARAMS.learningRateW = 0.05; % Learning rate for RBM weights
PARAMS.learningRateBiasVis = 0.05; % Learning rate for biases of visible units
PARAMS.learningRateBiasHid = 0.05; % Learning rate for biases of hidden units
PARAMS.learningRateBiasLabel = 0.05; % Learning rate to fit the labels at the end
PARAMS.weightCost = 0.0002;
PARAMS.initialMomentum = 0.2;
PARAMS.finalMomentum = 0.5;
PARAMS.epochToChangeMomentum = 5;
PARAMS.maxBackPropEpoch = 10;
PARAMS.combo = 10; % for gradient descent
%PARAMS.numTargets = 2;
PARAMS.numberOfLineSearches = 3; % for conjugate gradient descent
PARAMS.displayVisualization = 1;
FIT_LAST_LAYER_TO_TARGETS = 0;
PARAMS.useFileBatches = 0;
PARAMS.nodeType = 'binary';
%% path definitions
% set this one
dirpath = 'C:\Users\william.cox\Documents\DBN\emotions_data\';
dH = matfile([dirpath 'trainXY.mat'],'Writable',true); % Should contain X NxD, Y Nx1
% these will get created
pathTrain = [dirpath 'processed/train/'];
pathTest = [dirpath 'processed/test/'];
pathBatch = [dirpath 'processed/batch'];
pathBatch1 = [pathBatch '1'];
pathValidate = [dirpath 'processed/validate/'];
% make output directories
if ~exist(pathTrain,'dir')
mkdir(pathTrain);
end
if ~exist(pathTest,'dir')
mkdir(pathTest);
end
if ~exist(pathValidate,'dir')
mkdir(pathValidate);
end
% make output directories
for ii = 1:(numel(PARAMS.nodes)+1)
newDir = [pathBatch num2str(ii)];
if ~exist(newDir,'dir')
mkdir(newDir);
end
end
%% fixed constants
% batches and validation set split
trainPercentage = 1 - PARAMS.validatePercentage;
totalTrainSamples = floor(PARAMS.trainSamples * trainPercentage);
PARAMS.numBatches = floor(totalTrainSamples/PARAMS.batchSize);
totalTrainSamples = PARAMS.numBatches * PARAMS.batchSize;
totalValidateSamples = PARAMS.trainSamples - totalTrainSamples;
PARAMS.numValidate = floor(totalValidateSamples/PARAMS.batchSize);
totalValidateSamples = PARAMS.numValidate * PARAMS.batchSize;
PARAMS.numCombinedBatches = floor(PARAMS.numBatches / PARAMS.combo);
numberOfLayers = numel(PARAMS.nodes);
% read onfile to get dimensions of original image
dataSize = size(dH,'X');
labelSize = size(dH,'Y');
PARAMS.dataLength = dataSize(2);
PARAMS.numTargets = labelSize(2);
fprintf(1, 'Begin DBN Training \n');
fprintf(1, 'Data dimension is %d \n', dataSize(2));
fprintf(1, 'Creating output subdirectories if they do not exist \n');
%% Reformat and Preprocess data
fprintf(1, 'Preprocess Training Data.\nUsing %f of data for training\n', (1-PARAMS.validatePercentage));
%DBN_FormatData(dirpath, pathTrain, pathTest, PARAMS);
%% train rbm
if PARAMS.useFileBatches == 1
%% make batches
fprintf(1, 'Create Batchfiles\nEach batch will have %d samples \n\n', PARAMS.batchSize);
offset = 0;
DBN_MakeBatches(dH, totalTrainSamples, PARAMS.numBatches,offset, pathBatch1, pathTrain, PARAMS);
end
%% train RBM
numNodes = [PARAMS.dataLength PARAMS.nodes];
offset = 0;
restart=1;
if restart == 0
disp('Are you sure you want restart zero???');
epoch = 4;
else
epoch = 0;
end
for ii = 1:numberOfLayers
fprintf(1,'Pretraining Layer %d with RBM: %d-%d \n',ii,numNodes(ii),numNodes(ii+1));
path1 = [pathBatch num2str(ii) '\'];
path2 = [pathBatch num2str(ii+1) '\'];
if ii == 1 % First layer activations are the input data
actH1 = dH;
else
actH1 = matfile([path1 'data.mat'],'Writable',true); % Visible layer activations file
end
actH2 = matfile([path2 'data.mat'],'Writable',true); % Hidden layer activations file
if ii < numberOfLayers || FIT_LAST_LAYER_TO_TARGETS == 0
[weights, biasesVis, biasesHid, errsum] = ...
DBN_RBM(actH1, actH2, numNodes(ii), numNodes(ii+1), restart, PARAMS,offset,epoch);
save([dirpath 'state' num2str(ii)], 'weights', 'biasesVis', 'biasesHid', 'errsum');
else
[weights, weightsC, biasesVis, biasesHid, biasesC, errsum] = ...
RBM_FIT(actH1, actH2, dH, numNodes(ii), numNodes(ii+1), restart, PARAMS,offset,epoch);
save([dirpath 'state' num2str(ii)], 'weights', 'biasesVis', 'biasesHid', 'errsum', 'weightsC','biasesC');
end
end
fprintf(1,'RBM training complete \n\n');
%% Fit last layer to labels?
%[testE,trainE,tEn,trainEN] = DBN_UNFOLD_NOBACKPROP(dH,dirpath,PARAMS);
%% backprop with labels
if PARAMS.useFileBatches == 1
%%
fprintf(1,'Create new batches for backprop training and validation\n');
% rebatch and validate data, for backprop
offset = 0;
DBN_MakeBatches(dH, totalTrainSamples, PARAMS.numBatches, offset, pathBatch1, pathTrain, PARAMS);
offset = totalTrainSamples;
DBN_MakeBatches(dH, totalValidateSamples, PARAMS.numValidate, offset, pathValidate, pathTrain, PARAMS);
end
%%
% backprop
fprintf(1,'Begin Backpropogation\n');
finalModelFileName = DBN_BackProp(dH,dirpath,PARAMS,pathBatch1,pathValidate )
%% test
%[pd, testout] = DBN_TEST(pathTest, PARAMS);
%save('temp','pd');