-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathModelLogregNnBatch_test.lua
307 lines (247 loc) · 9.05 KB
/
ModelLogregNnBatch_test.lua
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
-- ModelLogregNnbatch_test.lua
-- unit test
-- ISSUE: For some unknown reason, the error rate on the fitted model is high
require 'ConfusionMatrix'
require 'ModelLogregNnbatch'
require 'makeVp'
require 'ObjectivefunctionLogregNnbatch'
require 'printAllVariables'
require 'printTableVariable'
require 'printVariable'
require 'Random'
require 'torch'
torch.manualSeed(123)
-------------------------------------------------------------------------------
-- make test objects (fixtures in unit test frameworks)
-------------------------------------------------------------------------------
-- create random data about the same size as the problem of most interest
-- RETURNS
-- X, y, s, nCLasses : synthetic data
-- actualTheta : actual parameters used to generate y from X
local function makeTrainingData(dataSize)
local vp, verboseLevel = makeVp(0, 'makeTrainingData')
local nSamples = 60
local nFeatures = 8
local nClasses = 14
if dataSize == 'small' then
nSamples = 5
nFeatures = 2
nClasses = 3
end
local L2 = 0 -- arbitrary value needed for APIs
-- randomly generate data
local X = torch.rand(nSamples, nFeatures)
local y = Random():integer(nSamples, 1, nClasses) -- class numbers not derived from weights
local s = torch.abs(torch.rand(nSamples)) -- saliences must be non-negative
-- get random weights from the corresponding optimization function
local opfunc = ObjectivefunctionLogregNnbatch(X, y, s, nClasses, L2)
local initialTheta = opfunc:initialTheta() -- random weights
vp(2, 'initialTheta', initialTheta)
return X, y, s, nClasses, initialTheta
end
-- return model
local function makeModel(dataSize)
local X, y, s, nClasses, initialTheta = makeTrainingData(dataSize)
local model = ModelLogregNnbatch(X, y, s, nClasses)
return model
end
-------------------------------------------------------------------------------
-- TEST PUBLIC METHODS
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- test construction
-------------------------------------------------------------------------------
local function testConstruction()
local function testFields(m)
assert(m.X)
assert(m.y)
assert(m.s)
assert(m.nClasses)
end
local zeroSaliences = true
local lambda = 0.0001
local model = makeModel()
assert(model ~= nil)
testFields(model)
local model = makeModel('small')
assert(model ~= nil)
testFields(model)
end
testConstruction()
-------------------------------------------------------------------------------
-- test method fit
-------------------------------------------------------------------------------
-- converge based only on toleranceLoss
local function fitModelBottouEpoch(model, toleranceLoss, printLoss)
local vp = makeVp(0, 'fitModelBottouEpoch')
vp(1, 'model', model)
assert(model)
assert(toleranceLoss)
assert(printLoss ~= nil)
local function nextStepSizes(currentStepSize)
return {currentStepSize, 0.5 * currentStepSize, 1.5 * currentStepSize}
end
local convergence = {
toleranceLoss = toleranceLoss}
local regularizer = {
L2 = .001}
local bottouEpoch = {
initialStepSize = 1,
nEpochsBeforeAdjustingStepSize = 10,
nEpochsToAdjustStepSize = 2,
nextStepSizes = nextStepSizes}
local fittingOptions = {
method = 'bottouEpoch',
convergence = convergence,
regularizer = regularizer,
printLoss = printLoss,
bottouEpoch = bottouEpoch}
local optimalTheta, fitInfo = model:fit(fittingOptions)
return optimalTheta, fitInfo
end
-- converge based only on toleranceLoss
local function fitModelGradientDescent(model, toleranceLoss, printLoss)
local vp = makeVp(0, 'fitModelGradientDescent')
vp(1, 'model', model)
assert(model)
assert(toleranceLoss)
assert(printLoss ~= nil)
local convergence = {
toleranceLoss = toleranceLoss}
local regularizer = {
L2 = .001}
local gradientDescent = {
stepSize = 1}
local fittingOptions = {
method = 'gradientDescent',
convergence = convergence,
regularizer = regularizer,
printLoss = printLoss,
gradientDescent = gradientDescent}
local optimalTheta, fitInfo = model:fit(fittingOptions)
return optimalTheta, fitInfo
end
local function fitModelLbfgs(model, toleranceLoss, printLoss)
local vp = makeVp(0, 'fitModelLbfgs')
vp(1, 'model', model)
assert(model)
assert(toleranceLoss)
assert(printLoss ~= nil)
local convergence = {
maxEpochs = 1,
toleranceLoss = toleranceLoss}
local regularizer = {
L2 = .001}
local lbfgs = {
lineSearch = 'wolf'}
local fittingOptions = {
method = 'lbfgs',
convergence = convergence,
regularizer = regularizer,
printLoss = printLoss,
lbfgs = lbfgs}
local optimalTheta, fitInfo = model:fit(fittingOptions)
return optimalTheta, fitInfo
end
local function testFitInfo(fitInfo)
local vp = makeVp(0, 'testFitInfo')
assert(type(fitInfo) == 'table')
local function testFieldType(fieldName, expectedType)
local fieldValue = fitInfo[fieldName]
assert(fieldValue)
assert(type(fieldValue) == expectedType)
vp(2, fieldName, fieldValue)
end
testFieldType('convergedReason', 'string')
testFieldType('finalLoss', 'number')
testFieldType('nEpochsUntilConvergence', 'number')
assert(fitInfo.optimalTheta:nDimension() == 1)
assert(fitInfo.convergedReason == 'toleranceLoss' or
fitInfo.convergedReason == 'maxEpochs')
end
local function makeConfusionMatrix(actuals, predictions)
local vp = makeVp(0, 'makeConfusionMatrix')
vp(1, 'actuals', actuals, 'predictions', predictions)
local cm = ConfusionMatrix()
for i = 1, actuals:size(1) do
cm:add(actuals[i], predictions[i])
end
return cm
end
local function determineErrorRate(actuals, predictions)
local vp = makeVp(1, 'determineErrorRate')
local cm = makeConfusionMatrix(actuals, predictions)
return cm:errorRate()
end
local function testOptimalTheta(optimalTheta, model, newX, expectedY)
local vp = makeVp(0, 'testOptimalTheta')
vp(1, 'model', model)
assert(optimalTheta:nDimension() == 1)
assert(model)
assert(newX)
assert(expectedY)
-- test supposed optimalTheta by checking the predictions
local probabilities, predictInfo = model:predict(newX, optimalTheta)
local errorRate = determineErrorRate(expectedY, predictInfo.mostLikelyClasses)
vp(1, 'errorRate', errorRate)
-- NOTE: for the BottouEpoch method, the lowest error rate was 0.62 for toleranceLoss = 1e-6
assert(errorRate <= 1) -- for now
end
local function testFitDriver(howToFitModel, toleranceLoss, printLoss, dataSize)
local vp = makeVp(2, 'testFit')
local model = makeModel(dataSize)
local optimalTheta, fitInfo = howToFitModel(model, toleranceLoss, printLoss)
testOptimalTheta(optimalTheta, model, model.X, model.y)
testFitInfo(fitInfo)
end
local function testFit(dataSize)
local toleranceLoss = 1e-6
local printLoss = false
testFitDriver(fitModelLbfgs, toleranceLoss, printLoss, dataSize)
testFitDriver(fitModelBottouEpoch, toleranceLoss, printLoss, dataSize)
testFitDriver(fitModelGradientDescent, toleranceLoss, printLoss, dataSize)
end
if true then
local dataSize = 'small'
testFit(dataSize)
else
print('did not run testFit')
end
-------------------------------------------------------------------------------
-- test method predict
-------------------------------------------------------------------------------
local function testPredictValues(toleranceLoss)
local vp, verbose = makeVp(0, 'testPredictValues')
assert(toleranceLoss ~= nil)
-- make the model
local model = makeModel()
local actualX = model.X
local actualY = model.y
vp(2, 'actualY', actualY)
-- fit the model
local printLoss = false
local optimalTheta, fitInfo = fitModelBottouEpoch(model, toleranceLoss, printLoss)
-- check that we actually converged
assert(fitInfo.convergedReason ~= 'maxEpochs', fitInfo.convergedReason)
-- predict each X used as training data
local probabilities, predictInfo = model:predict(actualX, optimalTheta)
vp(2, 'probabilities', probabilities)
assert(probabilities:nDimension() == 2)
assert(probabilities:size(1) == model.nSamples)
assert(probabilities:size(2) == model.nClasses)
assert(type(predictInfo) == 'table')
local predictedClasses = predictInfo.mostLikelyClasses
vp(2, 'predictedClasses', predictedClasses)
assert(predictedClasses:size(1) == model.nSamples)
-- see how we did
local cm = makeConfusionMatrix(actualY, predictedClasses)
local errorRate = cm:errorRate()
vp(1, 'errorRate', errorRate)
if verbose > 0 then
cm:printTo(io.stdout,'confusion matrix')
end
assert(errorRate < .90) -- I don't know why the predictions are not more accurate
end
local toleranceLoss = 1e-6 -- tests show that this toleranceLoss value lead to lowest overall Loss
testPredictValues(toleranceLoss)
print('ok ModelLogregNnbatch')