-
Notifications
You must be signed in to change notification settings - Fork 108
/
ModelTrainConf.java
614 lines (527 loc) · 18.9 KB
/
ModelTrainConf.java
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* Copyright [2012-2014] PayPal Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ml.shifu.shifu.container.obj;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import ml.shifu.shifu.core.alg.LogisticRegressionTrainer;
import ml.shifu.shifu.core.alg.SVMTrainer;
import ml.shifu.shifu.core.dtrain.CommonConstants;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* {@link ModelTrainConf} is train part in ModelConfig.json.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModelTrainConf {
/**
* Different training algorithms supported in Shifu. SVM actuall is not implemented well. DT is replaced by RF and
* GBT. TF_DNN is used for tensorflow dnn training, TENSORFLOW is used for generic tensorflow model evaluation.
*
* @author Zhang David (pengzhang@paypal.com)
*/
public static enum ALGORITHM {
NN, LR, SVM, DT, RF, GBT, TENSORFLOW, WDL
}
/**
* Multiple classification algorithm. NATIVE is supported in NN/RF. ONEVSALL/ONEVSREST is by enabling multiple
* regerssion running.
*
* @author Zhang David (pengzhang@paypal.com)
*/
@JsonDeserialize(using = MultipleClassificationDeserializer.class)
public static enum MultipleClassification {
NATIVE, // means using NN regression or RF classification, not one vs all or one vs one
ONEVSALL, ONEVSREST, // the same as ONEVSALL
ONEVSONE; // ONEVSONE is not impl yet.
/*
* Get {@link MultipleClassification} by string, case can be ignored.
*/
public static MultipleClassification of(String strategy) {
for(MultipleClassification element: values()) {
if(element.toString().equalsIgnoreCase(strategy)) {
return element;
}
}
throw new IllegalArgumentException("cannot find such enum in MULTIPLE_CLASSIFICATION");
}
}
/**
* How many bagging jobs in training.
*/
private Integer baggingNum = Integer.valueOf(1);
/**
* Bagging sampling with replacement, this is only works well in NN. In RF, bagging sampling with replacement is
* enabled no matter true or false. In GBT, bagging sampling with replacement is disabled no matter true or false
*/
private Boolean baggingWithReplacement = Boolean.FALSE;
/**
* In each bagging job to do sampling according to this sample rate.
*/
private Double baggingSampleRate = Double.valueOf(1.0);
/**
* After bagging sampling, current rate of records is used to do validation.
*/
private Double validSetRate = Double.valueOf(0.2);
/**
* Only sample negative records out, this works with {@link #baggingSampleRate}.
*/
private Boolean sampleNegOnly = Boolean.FALSE;
/**
* If training is converged. 0 means not enabled early stop feature.
*/
private Double convergenceThreshold = Double.valueOf(0.0);
/**
* Iterations used in training.
*/
private Integer numTrainEpochs = Integer.valueOf(100);
/**
* For NN only, how many epochs training in one iteration.
*/
private Integer epochsPerIteration = Integer.valueOf(1);
/**
* Train data located on disk or not, this parameter is deprecated because of in NN/LR MemoryDiskList is used if not
* enough memory, disk will be automatically used. In GBDT/RF, because of data with prediction is changed in each
* tree, only memory list is supported.
*/
@Deprecated
private Boolean trainOnDisk = Boolean.FALSE;
/**
* If enabled by true, training data and validation data will be fixed in training even another job is started.
*/
private Boolean fixInitInput = Boolean.FALSE;
/**
* Only works in regression, if enabled by true, both positive and negative records will be sampled independent.
*/
private Boolean stratifiedSample = Boolean.FALSE;
/**
* If continue model training based on existing model in model path, this is like warm-start in scikit-learn.
*/
private Boolean isContinuous = Boolean.FALSE;
/**
* Only works in NN and do swapping training, validation data in different epochs.
*/
private Boolean isCrossOver = Boolean.FALSE;
/**
* How many threads in each worker, this will enable multiple threading running in workers.
*/
private Integer workerThreadCount = 4;
/**
* If enabled by a value in (1 - 20], cross validation will be enabled. Jobs will be started to train according to
* k-fold training data. Final average validation error will be printed in console.
*/
private Integer numKFold = -1;
/**
* Random sample seed is used to generate Random instance when sampling.
* It's a hidden feature support in shifu. If user not configure this value, shifu will will fallback to generate
* random for bagging each time.
*/
@JsonIgnore
private Long baggingSampleSeed = CommonConstants.NOT_CONFIGURED_BAGGING_SEED;
/**
* Up sampling for positive tags, this is to solve class imbalance.
*/
private Double upSampleWeight = Double.valueOf(1d);
/**
* Algorithm: LR, NN, RF, GBT, TF-DNN
*/
private String algorithm = "NN";
/**
* Model params for training like learning rate, tree depth ...
*/
private Map<String, Object> params;
/**
* Grid search params config file path.
*/
private String gridConfigFile = null;
/**
* Grid search params in config file.
* Read from {@link #gridConfigFile} after loading {@link ModelConfig} from JSON file.
*/
@JsonIgnore
private List<String> gridConfigFileContent = null;
/**
* Multiple classification method: NATIVE or ONEVSALL(ONEVSREST)
*/
private MultipleClassification multiClassifyMethod = MultipleClassification.NATIVE;
private Map<String, String> customPaths;
public ModelTrainConf() {
customPaths = new HashMap<String, String>(1);
/**
* Since most user won't use this function,
* hidden the custom paths for creating new model.
*/
/*
* customPaths.put(Constants.KEY_PRE_TRAIN_STATS_PATH, null);
* customPaths.put(Constants.KEY_SELECTED_RAW_DATA_PATH, null);
* customPaths.put(Constants.KEY_NORMALIZED_DATA_PATH, null);
* customPaths.put(Constants.KEY_TRAIN_SCORES_PATH, null);
* customPaths.put(Constants.KEY_BIN_AVG_SCORE_PATH, null);
*/
}
public Integer getBaggingNum() {
return baggingNum;
}
public void setBaggingNum(Integer baggingNum) {
this.baggingNum = baggingNum;
}
public Boolean getBaggingWithReplacement() {
return baggingWithReplacement;
}
public void setBaggingWithReplacement(Boolean baggingWithReplacement) {
this.baggingWithReplacement = baggingWithReplacement;
}
public Double getBaggingSampleRate() {
return baggingSampleRate;
}
public void setBaggingSampleRate(Double baggingSampleRate) {
this.baggingSampleRate = baggingSampleRate;
}
public Double getValidSetRate() {
return validSetRate;
}
public void setValidSetRate(Double validSetRate) {
this.validSetRate = validSetRate;
}
@JsonIgnore
public Boolean getTrainOnDisk() {
return trainOnDisk;
}
public void setTrainOnDisk(Boolean trainOnDisk) {
this.trainOnDisk = trainOnDisk;
}
@JsonIgnore
public Boolean getFixInitInput() {
return fixInitInput;
}
public void setFixInitInput(Boolean fixInitInput) {
this.fixInitInput = fixInitInput;
}
public Integer getNumTrainEpochs() {
return numTrainEpochs;
}
public void setNumTrainEpochs(Integer numTrainEpochs) {
this.numTrainEpochs = numTrainEpochs;
}
public String getAlgorithm() {
return algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
@JsonIgnore
public String getGridConfigFile() {
return gridConfigFile;
}
@JsonProperty
public void setGridConfigFile(String gridConfigFile) {
this.gridConfigFile = gridConfigFile;
}
public List<String> getGridConfigFileContent() {
return gridConfigFileContent;
}
public void setGridConfigFileContent(List<String> gridConfigFileContent) {
this.gridConfigFileContent = gridConfigFileContent;
}
public Map<String, String> getCustomPaths() {
return customPaths;
}
public void setCustomPaths(Map<String, String> customPaths) {
this.customPaths = customPaths;
}
/**
* @return the epochsPerIteration
*/
@JsonIgnore
public Integer getEpochsPerIteration() {
return epochsPerIteration;
}
/**
* @param epochsPerIteration
* the epochsPerIteration to set
*/
@JsonProperty
public void setEpochsPerIteration(Integer epochsPerIteration) {
this.epochsPerIteration = epochsPerIteration;
}
/**
* As threshold is an optional setting, Use @{@link JsonIgnore} to ignore threshold when initially write
* out to ModelConfig.json.
*
* @return Convergence threshold.
*/
@JsonIgnore
public Double getConvergenceThreshold() {
return convergenceThreshold;
}
@JsonProperty
public void setConvergenceThreshold(Double convergenceThreshold) {
this.convergenceThreshold = convergenceThreshold;
}
@JsonIgnore
public Boolean getIsCrossOver() {
return isCrossOver;
}
/**
* @param isCrossOver
* the isCrossOver to set
*/
@JsonProperty
public void setIsCrossOver(Boolean isCrossOver) {
this.isCrossOver = isCrossOver;
}
/**
* @return the isContinuous
*/
public Boolean getIsContinuous() {
return isContinuous;
}
/**
* @param isContinuous
* the isContinuous to set
*/
public void setIsContinuous(Boolean isContinuous) {
this.isContinuous = isContinuous;
}
/**
* @return the workerThreadCount
*/
public Integer getWorkerThreadCount() {
return workerThreadCount;
}
/**
* @param workerThreadCount
* the workerThreadCount to set
*/
public void setWorkerThreadCount(Integer workerThreadCount) {
this.workerThreadCount = workerThreadCount;
}
/**
* @return the baggingSampleSeed
*/
public Long getBaggingSampleSeed() {
return baggingSampleSeed;
}
/**
* @param baggingSampleSeed
* the baggingSampleSeed to set
*/
public void setBaggingSampleSeed(Long baggingSampleSeed) {
this.baggingSampleSeed = baggingSampleSeed;
}
/**
* @return the upSampleWeight
*/
@JsonIgnore
public Double getUpSampleWeight() {
return upSampleWeight;
}
/**
* @param upSampleWeight
* the upSampleWeight to set
*/
public void setUpSampleWeight(Double upSampleWeight) {
this.upSampleWeight = upSampleWeight;
}
/**
* @return the multiClassifyMethod
*/
@JsonIgnore
public MultipleClassification getMultiClassifyMethod() {
return multiClassifyMethod;
}
/**
* @param multiClassifyMethod
* the multiClassifyMethod to set
*/
@JsonProperty
public void setMultiClassifyMethod(MultipleClassification multiClassifyMethod) {
this.multiClassifyMethod = multiClassifyMethod;
}
@JsonIgnore
public boolean isOneVsAll() {
return this.multiClassifyMethod == MultipleClassification.ONEVSALL
|| this.multiClassifyMethod == MultipleClassification.ONEVSREST;
}
/**
* @return the sampleNegOnly
*/
@JsonIgnore
public Boolean getSampleNegOnly() {
return sampleNegOnly;
}
/**
* @param sampleNegOnly
* the sampleNegOnly to set
*/
@JsonProperty
public void setSampleNegOnly(Boolean sampleNegOnly) {
this.sampleNegOnly = sampleNegOnly;
}
/**
* @return the stratifiedSample
*/
@JsonIgnore
public Boolean getStratifiedSample() {
return stratifiedSample;
}
/**
* @param stratifiedSample
* the stratifiedSampling to set
*/
@JsonProperty
public void setStratifiedSample(Boolean stratifiedSample) {
this.stratifiedSample = stratifiedSample;
}
/**
* @return the numKFold
*/
@JsonIgnore
public Integer getNumKFold() {
return numKFold;
}
/**
* @param numKFold
* the numKFold to set
*/
@JsonProperty
public void setNumKFold(Integer numKFold) {
this.numKFold = numKFold;
}
@Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof ModelTrainConf)) {
return false;
}
ModelTrainConf other = (ModelTrainConf) obj;
if(this == other) {
return true;
}
return this.algorithm.equals(other.getAlgorithm()) && this.baggingNum.equals(other.getBaggingNum())
&& this.getNumTrainEpochs().equals(other.getNumTrainEpochs())
&& this.validSetRate.equals(other.getValidSetRate());
}
@Override
public ModelTrainConf clone() {
ModelTrainConf other = new ModelTrainConf();
other.setAlgorithm(algorithm);
other.setBaggingNum(baggingNum);
other.setBaggingSampleRate(baggingSampleRate);
other.setBaggingSampleSeed(baggingSampleSeed);
other.setConvergenceThreshold(convergenceThreshold);
if(customPaths != null) {
other.setCustomPaths(new HashMap<String, String>(customPaths));
}
other.setEpochsPerIteration(epochsPerIteration);
other.setFixInitInput(fixInitInput);
other.setIsContinuous(isContinuous);
other.setMultiClassifyMethod(multiClassifyMethod);
other.setNumTrainEpochs(numTrainEpochs);
other.setParams(new HashMap<String, Object>(params));
other.setTrainOnDisk(trainOnDisk);
other.setUpSampleWeight(upSampleWeight);
other.setValidSetRate(validSetRate);
other.setWorkerThreadCount(workerThreadCount);
return other;
}
public static Map<String, Object> createParamsByAlg(ALGORITHM alg, ModelTrainConf trainConf) {
Map<String, Object> params = new HashMap<String, Object>();
if(ALGORITHM.NN.equals(alg)) {
params.put(CommonConstants.PROPAGATION, "R");
params.put(CommonConstants.LEARNING_RATE, 0.1);
params.put(CommonConstants.NUM_HIDDEN_LAYERS, 1);
List<Integer> nodes = new ArrayList<Integer>();
nodes.add(50);
params.put(CommonConstants.NUM_HIDDEN_NODES, nodes);
List<String> func = new ArrayList<String>();
func.add("tanh");
params.put(CommonConstants.ACTIVATION_FUNC, func);
params.put(CommonConstants.REGULARIZED_CONSTANT, 0.0);
} else if(ALGORITHM.SVM.equals(alg)) {
params.put(SVMTrainer.SVM_KERNEL, "linear");
params.put(SVMTrainer.SVM_GAMMA, 1.0);
params.put(SVMTrainer.SVM_CONST, 1.0);
} else if(ALGORITHM.RF.equals(alg)) {
params.put("TreeNum", "10");
params.put("FeatureSubsetStrategy", "TWOTHIRDS");
params.put("MaxDepth", 10);
params.put("MinInstancesPerNode", 1);
params.put("MinInfoGain", 0.0);
params.put("Impurity", "variance");
params.put("Loss", "squared");
} else if(ALGORITHM.GBT.equals(alg)) {
params.put("TreeNum", "100");
params.put("FeatureSubsetStrategy", "TWOTHIRDS");
params.put("MaxDepth", 7);
params.put("MinInstancesPerNode", 5);
params.put("MinInfoGain", 0.0);
params.put("DropoutRate", 0.0);
params.put("Impurity", "variance");
params.put(CommonConstants.LEARNING_RATE, 0.05);
params.put("Loss", "squared");
} else if(ALGORITHM.LR.equals(alg)) {
params.put(CommonConstants.PROPAGATION, "R");
params.put(LogisticRegressionTrainer.LEARNING_RATE, 0.1);
params.put(CommonConstants.REGULARIZED_CONSTANT, 0.0);
params.put(CommonConstants.REG_LEVEL_KEY, "NONE");
} else if(ALGORITHM.TENSORFLOW.equals(alg)) {
params.put(CommonConstants.LEARNING_RATE, 0.1);
params.put(CommonConstants.NUM_HIDDEN_LAYERS, 1);
params.put(CommonConstants.TF_ALG, "DNN");
params.put(CommonConstants.CHECKPOINT_INTERVAL, 0);
List<Integer> nodes = new ArrayList<Integer>();
nodes.add(50);
params.put(CommonConstants.NUM_HIDDEN_NODES, nodes);
List<String> func = new ArrayList<String>();
func.add("relu");
params.put(CommonConstants.ACTIVATION_FUNC, func);
params.put(CommonConstants.TF_OPTIMIZER, "Adam");
params.put(CommonConstants.TF_LOSS, "entropy");
} else if(ALGORITHM.WDL.equals(alg)) {
params.put(CommonConstants.LEARNING_RATE, 0.1);
List<Integer> embedColumnIds = new ArrayList<>(30);
embedColumnIds.add(629);
embedColumnIds.add(627);
embedColumnIds.add(555);
embedColumnIds.add(554);
embedColumnIds.add(553);
embedColumnIds.add(552);
embedColumnIds.add(550);
embedColumnIds.add(549);
embedColumnIds.add(547);
embedColumnIds.add(441);
params.put(CommonConstants.NUM_EMBED_COLUMN_IDS, embedColumnIds);
List<Integer> nodes = new ArrayList<Integer>();
nodes.add(50);
params.put(CommonConstants.NUM_HIDDEN_LAYERS, nodes);
List<String> func = new ArrayList<String>();
func.add("relu");
params.put(CommonConstants.ACTIVATION_FUNC, func);
params.put(CommonConstants.NUM_HIDDEN_NODES, 3);
params.put(CommonConstants.WDL_L2_REG, 1e-8f);
}
return params;
}
}