/
stump_training_core.h
1176 lines (940 loc) · 42.6 KB
/
stump_training_core.h
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* stump_training_core.h -*- C++ -*-
Jeremy Barnes, 20 February 2004
Copyright (c) 2004 Jeremy Barnes. All rights reserved.
$Source$
Core routines of the stump training. Header file only, since it is
templated on the parts which can be specialised.
*/
#ifndef __boosting__stump_training_core_h__
#define __boosting__stump_training_core_h__
#include "config.h"
#include <vector>
#include <boost/multi_array.hpp>
#include "training_data.h"
#include "feature_set.h"
#include "jml/arch/exception.h"
#include "jml/utils/vector_utils.h"
#include "jml/utils/pair_utils.h"
#include "stump.h"
#include "stump_training.h"
#include "training_index.h"
#include "jml/utils/guard.h"
#include <boost/bind.hpp>
#include "thread_context.h"
namespace ML {
extern size_t num_real, num_boolean, num_presence, num_categorical;
extern size_t num_bucketed, num_non_bucketed;
extern size_t num_real_early, num_real_not_early;
extern size_t num_bucket_early, num_bucket_not_early;
/** Compare two Z values, and return true if they are equal within a small
relative tolerance. */
inline bool z_equal(double z1, double z2, double tolerance = 1e-3)
{
double interval = tolerance * (1.0 - std::max(z1, z2) + 1e-3);
return std::abs(z1 - z2) < interval;
}
/** Very lightweight array that calculates its offsets much more easily than a
multi array. Can speed up some code by four times. */
template<typename T>
struct LW_Array {
template<typename T2>
LW_Array(const boost::multi_array<T2, 2> & array)
: base(array.data()), stride(array.shape()[1]) {}
T * base;
size_t stride;
JML_ALWAYS_INLINE T * operator [] (size_t i) const { return base + i * stride; }
};
/*****************************************************************************/
/* TRACING */
/*****************************************************************************/
/** When we want all tracing to turn into a no-op and go away, we use this
tracer object.
*/
struct No_Trace {
/** Are we tracing? */
JML_ALWAYS_INLINE operator bool () const { return false; }
/** Return the stream to which we trace.
\param module The name of the module we are tracing.
\param level The verbosity level of the message.
*/
std::ostream & operator () (const char * module, int level)
{
throw Exception("Tracing should never be called from No_Trace");
}
};
/** When we want to trace to an ostream, we use this object. */
struct Stream_Tracer {
/** Construct the tracer.
\param trace Are we actually tracing? Allows it to be enabled/
disabled at runtime.
\param stream Stream to dump the tracing information to.
*/
Stream_Tracer(bool trace = true, std::ostream & stream = std::cerr,
size_t start_message = 0)
: trace(trace), stream(stream), message(0),
start_message(start_message)
{
}
/** Are we tracing? */
JML_ALWAYS_INLINE operator bool () const
{
return trace && message++ >= start_message;
}
/** Return the stream to which we trace.
\param module The name of the module we are tracing.
\param level The verbosity level of the message.
*/
std::ostream & operator () (const char * module, int level)
{
return std::cerr << message << " " << module << " " << level << ": ";
}
bool trace; ///< Is tracing enabled?
std::ostream & stream; ///< Which stream do we write to?
mutable size_t message; ///< Which message number are we up to?
size_t start_message; ///< Which message number do we start with?
};
/*****************************************************************************/
/* STUMP_TRAINER */
/*****************************************************************************/
/** This class performs the overall training of a decision stumps object.
It presents candidate (feature, arg, W) tuples to a results object
(one of the template parameters), which is free to do with them as it
pleases.
\param W The type of the object which holds the weights
(broken down by label, predicate value, and
correct/incorrect).
*/
template<class T>
struct LW_Array;
/** What is our advance in memory to move from label to label within the
weights array? If we have a binary symmetric problem, we will only
store the weights for label 0, since label 1 will have the same
value. In this case, we use an advance of 0 which keeps us pointing
to the same value.
*/
JML_ALWAYS_INLINE int get_advance(const boost::multi_array<float, 2> & weights)
{
return (weights.shape()[1] == 1 ? 0 : 1);
}
template<class T>
JML_ALWAYS_INLINE int get_advance(const LW_Array<T> & weights)
{
return (weights.stride == 1 ? 0 : 1);
}
/** When the weights are like this, it's always regression or one
dimensional, so the advance doesn't matter. */
JML_ALWAYS_INLINE int get_advance(const std::vector<float> & weights)
{
return 1;
}
/** Ditto. */
JML_ALWAYS_INLINE int get_advance(const std::vector<const float *> & weights)
{
return 1; // assume not binsym
}
template<class W, class Z, class Tracer=No_Trace>
struct Stump_Trainer {
Stump_Trainer() {}
Stump_Trainer(const Tracer & tracer)
: tracer(tracer)
{
}
mutable Tracer tracer; ///< Object to which we trace
/** This is an object used for example weights which acts as a vector
of all 1s. It specifies that each example counts for the same
amount, without needing to use any memory.
*/
struct All_Examples {
float operator [] (int) const { return 1.0f; }
};
/** Test all of the given features. This will iterate over all features
given and test each of them, accumulating the results of each in the
results object.
\param features List of features to test.
\param data Training data to test over.
\param predicted Feature we are trying to predict.
\param weights Array of weights for each label for each example.
For a regression problem, there is one weight
per sample. Must be accessible via the syntax
weights[ex][label].
\param results Results object into which we accumulate possible
split points and their Z values.
*/
template<class Results, class Weights>
void test_all(const std::vector<Feature> & features,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
Results & results,
int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
/* Pre-calculate the bucket weights for each label. */
W default_w = calc_default_w(data, predicted, All_Examples(), weights,
advance);
if (tracer) {
tracer("stump training", 1)
<< "test all: " << features.size() << " features" << endl;
tracer("stump training", 2)
<< "default w: " << endl
<< default_w.print() << endl;
}
for (unsigned i = 0; i < features.size(); ++i)
test(features[i], data, predicted, weights, All_Examples(),
default_w, results, advance);
}
/** Test all of the given features. This will iterate over all features
given and test each of them, accumulating the results of each in the
results object.
An extra parameter is provided which allows us to give the weights
of each example. This can either be used to weight training examples,
or used as a boolean to indicate which examples are to be trained on
(with a weight of 1) and which are to be ignored.
\param features List of features to test.
\param data Training data to test over.
\param predicted Feature we are trying to predict.
\param weights Array of weights for each label for each example.
For a regression problem, there is one weight
per sample. Must be accessible via the syntax
weights[ex][label].
\param results Results object into which we accumulate possible
split points and their Z values.
*/
template<class Results, class Weights>
void test_all(const std::vector<Feature> & features,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const distribution<float> & in_class,
Results & results,
int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
W default_w = calc_default_w(data, predicted, in_class, weights,
advance);
if (tracer) {
tracer("stump training", 1)
<< "test all: " << features.size() << " features" << endl;
tracer("stump training", 2)
<< "default w: " << endl
<< default_w.print() << endl;
}
for (unsigned i = 0; i < features.size(); ++i)
test(features[i], data, predicted, weights, in_class, default_w,
results, advance);
}
template<class Results, class Weights>
struct Test_Feature_Job {
const Stump_Trainer * parent;
Feature feature;
const Training_Data & data;
const Feature & predicted;
const Weights & weights;
const distribution<float> & in_class;
Results & results;
int advance;
const W & default_w;
Test_Feature_Job(const Stump_Trainer * parent,
const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const distribution<float> & in_class,
const W & default_w,
Results & results,
int advance)
: parent(parent), feature(feature), data(data),
predicted(predicted),
weights(weights), in_class(in_class), results(results),
advance(advance), default_w(default_w)
{
}
void operator () ()
{
parent->test(feature, data, predicted, weights, in_class,
default_w, results, advance);
}
};
/** Like test_all, but in parallel using the worker task. */
template<class Results, class Weights>
void test_all(Thread_Context & context,
const std::vector<Feature> & features,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const distribution<float> & in_class,
Results & results,
int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
W default_w = calc_default_w(data, predicted, in_class, weights,
advance);
if (tracer) {
tracer("stump training", 1)
<< "test all: " << features.size() << " features" << endl;
tracer("stump training", 2)
<< "default w: " << endl
<< default_w.print() << endl;
}
Worker_Task & worker = context.worker();
int group = worker.get_group(NO_JOB,
"test all group",
context.group());
{
Call_Guard guard(boost::bind(&Worker_Task::unlock_group,
boost::ref(worker),
group));
for (unsigned i = 0; i < features.size(); ++i)
worker.add(Test_Feature_Job<Results, Weights>
(this, features[i], data, predicted,
weights, in_class, default_w,
results, advance),
"test feature job",
group);
}
worker.run_until_finished(group);
}
/* Test all of the given features, and return them sorted by their best
Z score. */
template<class Results, class Weights>
void test_all_and_sort(std::vector<Feature> & features,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
Results & results,
int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
W default_w = calc_default_w(data, predicted, All_Examples(), weights,
advance);
if (tracer) {
tracer("stump training", 1)
<< "test all: " << features.size() << " features" << endl;
tracer("stump training", 2)
<< "default w: " << endl
<< default_w.print() << endl;
}
std::vector<std::pair<int, float> > feature_scores;
feature_scores.reserve(features.size());
for (unsigned i = 0; i < features.size(); ++i) {
float z = test(features[i], data, predicted, weights, All_Examples(),
default_w, results, advance);
feature_scores.push_back(std::make_pair(i, z));
}
sort_on_second_ascending(feature_scores);
std::vector<Feature> new_features;
new_features.reserve(features.size());
for (unsigned i = 0; i < features.size(); ++i)
new_features.push_back(features[feature_scores[i].first]);
features.swap(new_features);
}
/* Test all of the given features, and return them sorted by their best
Z score. */
template<class Results, class Weights>
void test_all_and_sort(std::vector<Feature> & features,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const distribution<float> & in_class,
Results & results, int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
W default_w = calc_default_w(data, predicted, in_class, weights, advance);
if (tracer) {
tracer("stump training", 1)
<< "test all: " << features.size() << " features" << endl;
tracer("stump training", 2)
<< "default w: " << endl
<< default_w.print() << endl;
}
std::vector<std::pair<int, float> > feature_scores;
feature_scores.reserve(features.size());
for (unsigned i = 0; i < features.size(); ++i) {
float z = test(features[i], data, predicted, weights, in_class,
default_w, results);
//cerr << " feat " << features[i] << " z " << z << endl;
if (z != Z::none) feature_scores.push_back(std::make_pair(i, z));
}
sort_on_second_ascending(feature_scores);
std::vector<Feature> new_features;
new_features.reserve(feature_scores.size());
for (unsigned i = 0; i < feature_scores.size(); ++i)
new_features.push_back(features[feature_scores[i].first]);
features.swap(new_features);
}
/** Test the single given feature, calling the appropriate routine for the
type of feature.
\param feature Feature we are testing.
\param data Training data to test over.
\param predicted Feature we are trying to predict.
\param weights Array of weights for each label for each example.
For a regression problem, there is one weight
per sample. Must be accessible via the syntax
weights[ex][label].
\param ex_weights Array of weights for each sample. The weights
must be accessible via the syntax ex_weights[ex].
\param default_w The starting W value. Passed in as it can be
calculated once and used for each feature.
See calc_default_w.
\param results Results object into which we accumulate possible
split points and their Z values.
\returns The Z value of the best split point.
*/
template<class Results, class ExampleWeights, class Weights>
float test(const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const ExampleWeights & ex_weights,
const W & default_w, Results & results,
int advance = -1) const
{
using namespace std;
if (advance == -1) advance = get_advance(weights);
/* Don't predict the label with the label! */
if (feature == predicted) return Z::worst;
std::shared_ptr<const Feature_Space> fs = data.feature_space();
if (tracer)
tracer("stump training", 1)
<< "testing feature " << fs->print(feature)
<< "(" << feature << ") info "
<< fs->info(feature) << endl;
Feature_Info info = fs->info(feature);
switch (info.type()) {
case PRESENCE:
return test_presence(feature, data, predicted, weights, ex_weights,
default_w, results, advance);
case BOOLEAN:
return test_boolean(feature, data, predicted, weights, ex_weights,
default_w, results, advance);
case CATEGORICAL:
case STRING:
return test_categorical(feature, data, predicted, weights,
ex_weights, default_w, results, advance);
case REAL:
return test_real(feature, data, predicted, weights, ex_weights,
default_w, results, advance);
case INUTILE: {
double missing;
if (!results.start(feature, default_w, missing))
return Z::worst;
float z = results.add(feature, default_w, -INFINITY, missing);
results.finish(feature);
return z;
}
default:
throw Exception("Unknown feature info type " + info.print()
+ " in Stump::test");
}
}
/** Calculate the default weights of the buckets. This will accumulate
the example weights into the MISSING bucket for each label. This
is the starting point for all of the test_* routines; we only do it
once.
\param data Training data to test over.
\param predicted Feature we are trying to predict.
\param ex_weights Array of weights for each sample. The weights
must be accessible via the syntax ex_weights[ex].
Normally, this will be either a
distribution<float> or All_Examples object.
\param weights Array of weights for each label for each example.
For a regression problem, there is one weight
per sample. Must be accessible via the syntax
weights[ex][label]. Normally this will be a
boost::multi_array<float, 2>.
\returns The W object with all weight in the MISSING
buckets, distributed according to weights and
ex_weights.
*/
template<class Weights, class ExampleWeights>
W calc_default_w(const Training_Data & data,
const Feature & predicted,
const ExampleWeights & ex_weights,
const Weights & weights,
int advance = -1) const
{
if (advance == -1) advance = get_advance(weights);
int nl = data.label_count(predicted);
W result(nl);
const std::vector<Label> & labels = data.index().labels(predicted);
for (unsigned i = 0; i < data.example_count(); ++i) {
if (ex_weights[i] == 0.0) continue;
int correct_label = labels[i];
#if 0
using namespace std;
cerr << "default W: example " << i << " label " << correct_label
<< " weights " << ex_weights[i] << " " << weights[i][0]
<< endl;
#endif
result.add(correct_label, MISSING, ex_weights[i], &weights[i][0],
advance);
}
//using namespace std;
//cerr << "default w: " << endl
// << result.print() << endl;
return result;
}
/** Adjust the weight for a given feature before testing split points.
This function transfers weight from the MISSING bucket to the
def bucket (specified) for all examples where the given feature is
not missing. This is done before testing the split points for each
feature.
\param W W value to modify. This will probably be the
return from default_w. It is modified in place.
\param data Training data we are training over.
\param predicted Feature we are trying to predict.
\param weights Array of weights for each label for each example.
For a regression problem, there is one weight
per sample. Must be accessible via the syntax
weights[ex][label]. Normally this will be a
boost::multi_array<float, 2>.
\param ex_weights Array of weights for each sample. The weights
must be accessible via the syntax ex_weights[ex].
Normally, this will be either a
distribution<float> or All_Examples object.
\param feature Feature we are adjusting for (that we are about
to test).
\param def Bucket to transfer weight to for non-missing
examples. Weight is always transfered from the
MISSING bucket.
\returns Number of non-missing examples that contain this
feature in the training data.
*/
template<class Weights, class ExampleWeights>
int adjust_w(W & w,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const ExampleWeights & ex_weights,
const Feature & feature, bool def, int advance) const
{
using namespace std;
/* Fix it up for the ones where the feature occurs. */
Joint_Index index
= data.index().joint(predicted, feature, BY_EXAMPLE,
IC_EXAMPLE | IC_LABEL | IC_DIVISOR);
#if 0
if (tracer)
tracer("adjust_w", 3)
<< " feature_data.exactly_one = " << feature_index.exactly_one()
<< " feature_data.dense() = " << feature_index.dense()
<< " feature_data.size() = " << examples.size()
<< " data.example_count() = " << data.example_count()
<< endl;
#endif
unsigned i = 0;
int result = 0;
for (; i < index.size(); ++i) {
if (index[i].missing()) continue;
int example = index[i].example();
if (ex_weights[example] == 0.0) continue;
int label = index[i].label();
/* If we had the same feature multiple times in the dataset, then
we need to spread its weight out over all of them (otherwise
the one feature will have too much weight). */
double divisor = ex_weights[example] * index[i].divisor();
/* Transfer (weighted) the weight from the MISSING bucket to the
def bucket for this label. */
w.transfer(label, MISSING, def, divisor, &weights[example][0],
advance);
++result;
}
/* Compensate for any accumulated rounding errors. */
w.clip(MISSING);
return result;
}
/** Test a boolean variable. */
template<class Results, class Weights, class ExampleWeights>
float test_boolean(const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const ExampleWeights & ex_weights,
const W & default_w, Results & results, int advance) const
{
using namespace std;
++num_boolean;
/* See if we can do it by buckets. We only do so if more than 20% of
the examples include this feature (otherwise it will probably be
slower).
*/
if (data.index().density(feature) > 0.2)
return test_buckets(feature, data, predicted, weights, ex_weights,
default_w, results, 2,
false /* categorical; false since doesn't
matter and faster if false */,
advance);
/* Fix it up for the ones where the feature occurs. */
Joint_Index index
= data.index().joint(predicted, feature, BY_EXAMPLE,
IC_EXAMPLE | IC_LABEL | IC_DIVISOR | IC_VALUE);
if (index.empty()) return Z::none;
++num_non_bucketed;
W w(default_w);
unsigned i = 0;
for (; i < index.size(); ++i) {
if (index[i].missing()) continue;
int example = index[i].example();
if (ex_weights[example] == 0.0) continue;
int label = index[i].label();
bool bucket = index[i].value() > 0.5;
/* If we had the same feature multiple times in the dataset, then
we need to spread its weight out over all of them (otherwise
the one feature will have too much weight). */
double divisor = ex_weights[example] * index[i].divisor();
w.transfer(label, MISSING, bucket, divisor, &weights[example][0],
advance);
}
/* Compensate for any accumulated rounding errors. */
w.clip(MISSING);
double missing;
if (!results.start(feature, w, missing)) return Z::worst;
float Z = results.add(feature, w, 0.5, missing);
results.finish(feature);
return Z;
}
/** Test a presence variable. */
template<class Results, class ExampleWeights, class Weights>
float test_presence(const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
ExampleWeights ex_weights,
const W & default_w, Results & results,
int advance) const
{
using namespace std;
++num_presence;
W w(default_w);
int ex = adjust_w(w, data, predicted, weights, ex_weights, feature,
true, advance);
if (ex == 0) return Z::none; // no examples
if (tracer)
tracer("test_presence", 1)
<< "w = " << endl << w.print() << endl;
double missing;
if (!results.start(feature, w, missing)) return Z::worst;
float Z = results.add_presence(feature, w, 0.5, missing);
results.finish(feature);
return Z;
}
template<class Results, class Weights, class ExampleWeights>
float test_categorical(const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const ExampleWeights & ex_weights,
const W & default_w, Results & results,
int advance) const
{
++num_categorical;
/* See if we can do it by buckets. We only do so if more than 20% of
the examples include this feature (otherwise it will probably be
slower).
*/
//if (data.index().density(feature) > 0.2)
// return test_buckets(feature, data, predicted, weights, ex_weights,
// default_w, results,
// 255 /* num_buckets; TODO: configurable */,
// true /* categorical */, advance);
Joint_Index index
= data.index().joint(predicted, feature, BY_VALUE,
IC_EXAMPLE | IC_LABEL | IC_EXAMPLE
| IC_DIVISOR);
if (index.empty()) return Z::none;
W w(default_w);
int ex = adjust_w(w, data, predicted, weights, ex_weights, feature,
false, advance);
if (ex == 0) return Z::none;
double missing;
if (!results.start(feature, w, missing)) return Z::worst;
/* Save this W value so we can get back to it after each value. */
W w_start(w);
bool debug = false;
int i = 0;
/* Skim off any missing ones from the start. */
while (i < index.size() && index[i].missing()) ++i;
using namespace std;
if (debug) {
cerr << " feature "
<< data.feature_space()->print(feature)
<< endl;
cerr << "i = " << i << " of " << index.size() << endl;
}
/* One candidate split point is -INF, which lets us split only based
upon missing or not. */
float Z = Z::worst;
if (i != 0) {
Z = results.add(feature, w, -INFINITY, missing);
if (debug)
cerr << "added split " << -INFINITY << " with " << missing
<< " missing and score " << Z << endl;
}
float prev = index[i].value();
while (i < index.size()) {
int nex = 0;
/* Look for a unique split point. */
while (i < index.size() && index[i].value() == prev) {
int example = index[i].example();
if (ex_weights[example] == 0.0) { ++i; continue; }
int label = index[i].label();
float divisor = ex_weights[example] * index[i].divisor();
/* Transfer weight from predicate not holding to predicate
holding. */
w.transfer(label, false, true, divisor, &weights[example][0],
advance);
++i; ++nex;
}
/* Fix up any rounding errors that took it below zero. */
w.clip(false);
/* Add this split point. */
float arg = prev;
float new_Z = results.add(feature, w, arg, missing);
Z = std::min(Z, new_Z);
if (debug && new_Z == Z) {
cerr << "i = " << i << endl;
cerr << "added split "
<< data.feature_space()->print(feature, arg)
<< " with " << missing
<< " missing and score " << new_Z
<< (new_Z == Z ? " *** BEST ***" : "")
<< endl;
cerr << "nex = " << nex << endl;
cerr << "w_start = " << w_start.print() << endl;
cerr << "W = " << w.print() << endl;
}
/* Reset back to old values for next value */
w = w_start;
if (i < index.size()) prev = index[i].value();
}
results.finish(feature);
return Z;
}
template<class Results, class Weights, class ExampleWeights>
float test_real(const Feature & feature,
const Training_Data & data,
const Feature & predicted,
const Weights & weights,
const ExampleWeights & ex_weights,
const W & default_w,
Results & results,
int advance) const
{
using namespace std;
//cerr << "test_real" << endl;
/* See if we can do it by buckets. We only do so if more than 20% of
the examples include this feature (otherwise it will probably be
slower).
*/
if (data.index().density(feature) > 0.2)
return test_buckets(feature, data, predicted, weights, ex_weights,
default_w, results,
255 /* num_buckets; TODO: configurable */,
false /* categorical */,
advance);
++num_real;
bool debug = false;
//debug = (data.feature_space()->print(feature) == "language|all_diff_prob_lb");
//debug = (feature.type() == 10);
using namespace std;
if (debug) {
cerr << "feature " << data.feature_space()->print(feature)
<< endl;
}
++num_non_bucketed;
++num_real_early;
Joint_Index index
= data.index().joint(predicted, feature, BY_VALUE,
IC_VALUE | IC_LABEL | IC_EXAMPLE | IC_DIVISOR);
if (index.empty()) return Z::none;
W w(default_w);
int ex = adjust_w(w, data, predicted, weights, ex_weights, feature,
true, advance);
if (ex == 0) return Z::none;
if (debug)
cerr << "ex = " << ex << " adjusted w " << endl << w.print() << endl;
//cerr << "adjusted w = " << endl << w.print() << endl;
double missing;
if (!results.start(feature, w, missing)) return Z::worst;
--num_real_early;
++num_real_not_early;
int i = 0;
/* Skim off any missing ones from the start. */
while (i < index.size() && index[i].missing()) ++i;
if (debug) {
cerr << "i = " << i << " of " << index.size() << endl;
}
// TODO: not missing
float Z = Z::worst;
#if 0
/* One candidate split point is -INF, which lets us split only based
upon missing or not. */
float Z = results.add(feature, w, -INFINITY, missing);
#endif
float prev = index[i].value();
float max_value = index.back().value();
while (i < index.size() && index[i].value() < max_value) {
/* Look for a unique split point. */
for (; i < index.size() && index[i].value() < max_value
&& index[i].value() == prev; ++i) {
int example = index[i].example();
if (ex_weights[example] == 0.0) continue;
int label = index[i].label();
float divisor = ex_weights[example] * index[i].divisor();
/* Transfer weight from predicate not holding to predicate
holding. */
w.transfer(label, true, false, divisor, &weights[example][0],
advance);
}
/* Fix up any rounding errors that took it below zero. */
w.clip(true);
/* Add this split point. */
float arg = (index[i].value() + prev) * 0.5;
if (arg == prev || arg == index[i].value()) {
arg = index[i].value();
#if 0 // TODO: should be equal to lower or highest?
cerr << "feature: " << data.feature_space()->print(feature)
<< endl;
cerr << "arg: " << format("arg: %.10f (0x%08x) "
"prev: %.10f (0x%08x) "
"val: %.10f (0x%08x)",
arg, reinterpret_as_int(arg),
prev, reinterpret_as_int(prev),
index[i].value(),
reinterpret_as_int(index[i].value()))