-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SparseFeatures.h
1637 lines (1401 loc) · 41.9 KB
/
SparseFeatures.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2010 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Subset support written (W) 2011 Heiko Strathmann
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
* Copyright (C) 2010 Berlin Institute of Technology
*/
#ifndef _SPARSEFEATURES__H__
#define _SPARSEFEATURES__H__
#include <string.h>
#include <stdlib.h>
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/Cache.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Cache.h>
#include <shogun/io/File.h>
#include <shogun/lib/DataType.h>
#include <shogun/features/Labels.h>
#include <shogun/features/Features.h>
#include <shogun/features/DotFeatures.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/preprocessor/SparsePreprocessor.h>
namespace shogun
{
class CFile;
class CLabels;
class CFeatures;
class CDotFeatures;
template <class ST> class CSimpleFeatures;
template <class ST> class CSparsePreprocessor;
/** @brief Template class SparseFeatures implements sparse matrices.
*
* Features are an array of SGSparseVector, sorted w.r.t. vec_index (increasing) and
* withing same vec_index w.r.t. feat_index (increasing);
*
* Sparse feature vectors can be accessed via get_sparse_feature_vector() and
* should be freed (this operation is a NOP in most cases) via
* free_sparse_feature_vector().
*
* As this is a template class it can directly be used for different data types
* like sparse matrices of real valued, integer, byte etc type.
*
* (Partly) subset access is supported for this feature type.
* Simple use the (inherited) set_subset(), remove_subset() functions.
* If done, all calls that work with features are translated to the subset.
* See comments to find out whether it is supported for that method
*/
template <class ST> class CSparseFeatures : public CDotFeatures
{
public:
/** constructor
*
* @param size cache size
*/
CSparseFeatures(int32_t size=0)
: CDotFeatures(size), num_vectors(0), num_features(0),
sparse_feature_matrix(NULL), feature_cache(NULL)
{ init(); }
/** convenience constructor that creates sparse features from
* the ones passed as argument
*
* @param src dense feature matrix
* @param num_feat number of features
* @param num_vec number of vectors
* @param copy true to copy feature matrix
*/
CSparseFeatures(SGSparseVector<ST>* src, int32_t num_feat, int32_t num_vec, bool copy=false)
: CDotFeatures(0), num_vectors(0), num_features(0),
sparse_feature_matrix(NULL), feature_cache(NULL)
{
init();
if (!copy)
set_sparse_feature_matrix(SGSparseMatrix<ST>(src, num_feat, num_vec));
else
{
sparse_feature_matrix = new SGSparseVector<ST>[num_vec];
memcpy(sparse_feature_matrix, src, sizeof(SGSparseVector<ST>)*num_vec);
for (int32_t i=0; i< num_vec; i++)
{
sparse_feature_matrix[i].features = new SGSparseVectorEntry<ST>[sparse_feature_matrix[i].num_feat_entries];
memcpy(sparse_feature_matrix[i].features, src[i].features, sizeof(SGSparseVectorEntry<ST>)*sparse_feature_matrix[i].num_feat_entries);
}
}
}
/** convenience constructor that creates sparse features from
* sparse features
*
* @param sparse sparse matrix
*/
CSparseFeatures(SGSparseMatrix<ST> sparse)
: CDotFeatures(0), num_vectors(0), num_features(0),
sparse_feature_matrix(NULL), feature_cache(NULL)
{
init();
set_sparse_feature_matrix(sparse);
}
/** convenience constructor that creates sparse features from
* dense features
*
* @param dense dense feature matrix
*/
CSparseFeatures(SGMatrix<ST> dense)
: CDotFeatures(0), num_vectors(0), num_features(0),
sparse_feature_matrix(NULL), feature_cache(NULL)
{
init();
set_full_feature_matrix(dense);
}
/** copy constructor */
CSparseFeatures(const CSparseFeatures & orig)
: CDotFeatures(orig), num_vectors(orig.num_vectors),
num_features(orig.num_features),
sparse_feature_matrix(orig.sparse_feature_matrix),
feature_cache(orig.feature_cache)
{
init();
if (orig.sparse_feature_matrix)
{
free_sparse_feature_matrix();
sparse_feature_matrix=new SGSparseVector<ST>[num_vectors];
memcpy(sparse_feature_matrix, orig.sparse_feature_matrix, sizeof(SGSparseVector<ST>)*num_vectors);
for (int32_t i=0; i< num_vectors; i++)
{
sparse_feature_matrix[i].features=new SGSparseVectorEntry<ST>[sparse_feature_matrix[i].num_feat_entries];
memcpy(sparse_feature_matrix[i].features, orig.sparse_feature_matrix[i].features, sizeof(SGSparseVectorEntry<ST>)*sparse_feature_matrix[i].num_feat_entries);
}
}
m_subset=orig.m_subset->duplicate();
}
/** constructor loading features from file
*
* @param loader File object to load data from
*/
CSparseFeatures(CFile* loader)
: CDotFeatures(loader), num_vectors(0), num_features(0),
sparse_feature_matrix(NULL), feature_cache(NULL)
{
init();
load(loader);
}
/** default destructor */
virtual ~CSparseFeatures()
{
free_sparse_features();
}
/** free sparse feature matrix
*
* any subset is removed
*/
void free_sparse_feature_matrix()
{
clean_tsparse(sparse_feature_matrix, num_vectors);
sparse_feature_matrix = NULL;
num_vectors=0;
num_features=0;
remove_subset();
}
/** free sparse feature matrix and cache
*
* any subset is removed
*/
void free_sparse_features()
{
free_sparse_feature_matrix();
delete feature_cache;
feature_cache = NULL;
}
/** duplicate feature object
*
* @return feature object
*/
virtual CFeatures* duplicate() const
{
return new CSparseFeatures<ST>(*this);
}
/** get a single feature
*
* possible with subset
*
* @param num number of feature vector to retrieve
* @param index index of feature in this vector
*
* @return sum of features that match dimension index and 0 if none is found
*/
ST get_feature(int32_t num, int32_t index)
{
ASSERT(index>=0 && index<num_features) ;
ASSERT(num>=0 && num<get_num_vectors()) ;
bool vfree;
int32_t num_feat;
int32_t i;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(num, num_feat, vfree);
ST ret = 0 ;
if (sv)
{
for (i=0; i<num_feat; i++)
if (sv[i].feat_index==index)
ret += sv[i].entry ;
}
free_sparse_feature_vector(sv, num, vfree);
return ret ;
}
/** converts a sparse feature vector into a dense one
* preprocessed compute_feature_vector
* caller cleans up
*
* @param num index of feature vector
* @param len length is returned by reference
* @return dense feature vector
*/
ST* get_full_feature_vector(int32_t num, int32_t& len)
{
bool vfree;
int32_t num_feat;
int32_t i;
len=0;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(num, num_feat, vfree);
ST* fv=NULL;
if (sv)
{
len=num_features;
fv=new ST[num_features];
for (i=0; i<num_features; i++)
fv[i]=0;
for (i=0; i<num_feat; i++)
fv[sv[i].feat_index]= sv[i].entry;
}
free_sparse_feature_vector(sv, num, vfree);
return fv;
}
/** get the fully expanded dense feature vector num
*
* @return dense feature vector
* @param num index of feature vector
*/
SGVector<ST> get_full_feature_vector(int32_t num)
{
if (num>=num_vectors)
{
SG_ERROR("Index out of bounds (number of vectors %d, you "
"requested %d)\n", num_vectors, num);
}
bool vfree;
int32_t num_feat=0;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(num, num_feat, vfree);
SGVector<ST> dense;
if (sv)
{
dense.do_free=true;
dense.vlen=num_features;
dense.vector=new ST[num_features];
memset(dense.vector, 0, sizeof(ST)*num_features);
for (int32_t i=0; i<num_feat; i++)
dense.vector[sv[i].feat_index]= sv[i].entry;
}
free_sparse_feature_vector(sv, num, vfree);
return dense;
}
/** get number of non-zero features in vector
*
* @param num which vector
* @return number of non-zero features in vector
*/
virtual inline int32_t get_nnz_features_for_vector(int32_t num)
{
bool vfree;
int32_t len;
SGSparseVectorEntry<ST>* sv = get_sparse_feature_vector(num, len, vfree);
free_sparse_feature_vector(sv, num, vfree);
return len;
}
/** get sparse feature vector
* for sample num from the matrix as it is if matrix is initialized,
* else return preprocessed compute_feature_vector
*
* possible with subset
*
* @param num index of feature vector
* @param len number of sparse entries is returned by reference
* @param vfree whether returned vector must be freed by caller via
* free_sparse_feature_vector
* @return sparse feature vector
*/
SGSparseVectorEntry<ST>* get_sparse_feature_vector(int32_t num, int32_t& len, bool& vfree)
{
ASSERT(num<get_num_vectors());
index_t real_num=subset_idx_conversion(num);
if (sparse_feature_matrix)
{
len=sparse_feature_matrix[real_num].num_feat_entries;
vfree=false ;
return sparse_feature_matrix[real_num].features;
}
else
{
SGSparseVectorEntry<ST>* feat=NULL;
vfree=false;
if (feature_cache)
{
feat=feature_cache->lock_entry(num);
if (feat)
return feat;
else
{
feat=feature_cache->set_entry(num);
}
}
if (!feat)
vfree=true;
feat=compute_sparse_feature_vector(num, len, feat);
if (get_num_preprocessors())
{
int32_t tmp_len=len;
SGSparseVectorEntry<ST>* tmp_feat_before = feat;
SGSparseVectorEntry<ST>* tmp_feat_after = NULL;
for (int32_t i=0; i<get_num_preprocessors(); i++)
{
//tmp_feat_after=((CSparsePreprocessor<ST>*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len);
if (i!=0) // delete feature vector, except for the the first one, i.e., feat
delete[] tmp_feat_before;
tmp_feat_before=tmp_feat_after;
}
memcpy(feat, tmp_feat_after, sizeof(SGSparseVectorEntry<ST>)*tmp_len);
delete[] tmp_feat_after;
len=tmp_len ;
SG_DEBUG( "len: %d len2: %d\n", len, num_features);
}
return feat ;
}
}
/** compute the dot product between two sparse feature vectors
* alpha * vec^T * vec
*
* @param alpha scalar to multiply with
* @param avec first sparse feature vector
* @param alen avec's length
* @param bvec second sparse feature vector
* @param blen bvec's length
* @return dot product between the two sparse feature vectors
*/
static ST sparse_dot(ST alpha, SGSparseVectorEntry<ST>* avec, int32_t alen, SGSparseVectorEntry<ST>* bvec, int32_t blen)
{
ST result=0;
//result remains zero when one of the vectors is non existent
if (avec && bvec)
{
if (alen<=blen)
{
int32_t j=0;
for (int32_t i=0; i<alen; i++)
{
int32_t a_feat_idx=avec[i].feat_index;
while ( (j<blen) && (bvec[j].feat_index < a_feat_idx) )
j++;
if ( (j<blen) && (bvec[j].feat_index == a_feat_idx) )
{
result+= avec[i].entry * bvec[j].entry;
j++;
}
}
}
else
{
int32_t j=0;
for (int32_t i=0; i<blen; i++)
{
int32_t b_feat_idx=bvec[i].feat_index;
while ( (j<alen) && (avec[j].feat_index < b_feat_idx) )
j++;
if ( (j<alen) && (avec[j].feat_index == b_feat_idx) )
{
result+= bvec[i].entry * avec[j].entry;
j++;
}
}
}
result*=alpha;
}
return result;
}
/** compute the dot product between dense weights and a sparse feature vector
* alpha * sparse^T * w + b
*
* possible with subset
*
* @param alpha scalar to multiply with
* @param num index of feature vector
* @param vec dense vector to compute dot product with
* @param dim length of the dense vector
* @param b bias
* @return dot product between dense weights and a sparse feature vector
*/
ST dense_dot(ST alpha, int32_t num, ST* vec, int32_t dim, ST b)
{
ASSERT(vec);
ASSERT(dim==num_features);
ST result=b;
bool vfree;
int32_t num_feat;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(num, num_feat, vfree);
if (sv)
{
for (int32_t i=0; i<num_feat; i++)
result+=alpha*vec[sv[i].feat_index]*sv[i].entry;
}
free_sparse_feature_vector(sv, num, vfree);
return result;
}
/** add a sparse feature vector onto a dense one
* dense+=alpha*sparse
*
* possible with subset
*
@param alpha scalar to multiply with
@param num index of feature vector
@param vec dense vector
@param dim length of the dense vector
@param abs_val if true, do dense+=alpha*abs(sparse)
*/
void add_to_dense_vec(float64_t alpha, int32_t num, float64_t* vec, int32_t dim, bool abs_val=false)
{
ASSERT(vec);
if (dim!=num_features)
{
SG_ERROR("dimension of vec (=%d) does not match number of features (=%d)\n",
dim, num_features);
}
bool vfree;
int32_t num_feat;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(num, num_feat, vfree);
if (sv)
{
if (abs_val)
{
for (int32_t i=0; i<num_feat; i++)
vec[sv[i].feat_index]+= alpha*CMath::abs(sv[i].entry);
}
else
{
for (int32_t i=0; i<num_feat; i++)
vec[sv[i].feat_index]+= alpha*sv[i].entry;
}
}
free_sparse_feature_vector(sv, num, vfree);
}
/** free sparse feature vector
*
* possible with subset
*
* @param feat_vec feature vector to free
* @param num index of this vector in the cache
* @param free if vector should be really deleted
*/
void free_sparse_feature_vector(SGSparseVectorEntry<ST>* feat_vec, int32_t num, bool free)
{
if (feature_cache)
feature_cache->unlock_entry(subset_idx_conversion(num));
if (free)
delete[] feat_vec ;
}
/** get the pointer to the sparse feature matrix
* num_feat,num_vectors are returned by reference
*
* not possible with subset
*
* @param num_feat number of features in matrix
* @param num_vec number of vectors in matrix
* @return feature matrix
*/
SGSparseVector<ST>* get_sparse_feature_matrix(int32_t &num_feat, int32_t &num_vec)
{
if (m_subset)
SG_ERROR("get_sparse_feature_matrix() not allowed with subset\n");
num_feat=num_features;
num_vec=num_vectors;
return sparse_feature_matrix;
}
/** get the sparse feature matrix
*
* not possible with subset
*
* @return sparse matrix
*
*/
SGSparseMatrix<ST> get_sparse_feature_matrix()
{
if (m_subset)
SG_ERROR("get_sparse_feature_matrix() not allowed with subset\n");
SGSparseMatrix<ST> sm;
sm.sparse_matrix=get_sparse_feature_matrix(sm.num_features, sm.num_vectors);
return sm;
}
/** clean SGSparseVector
*
* @param sfm sparse feature matrix
* @param num_vec number of vectors in matrix
*/
static void clean_tsparse(SGSparseVector<ST>* sfm, int32_t num_vec)
{
if (sfm)
{
for (int32_t i=0; i<num_vec; i++)
delete[] sfm[i].features;
delete[] sfm;
}
}
/** get a transposed copy of the features
*
* possible with subset
*
* @return transposed copy
*/
CSparseFeatures<ST>* get_transposed()
{
int32_t num_feat;
int32_t num_vec;
SGSparseVector<ST>* s=get_transposed(num_feat, num_vec);
return new CSparseFeatures<ST>(s, num_feat, num_vec);
}
/** compute and return the transpose of the sparse feature matrix
* which will be prepocessed.
* num_feat, num_vectors are returned by reference
* caller has to clean up
*
* possible with subset
*
* @param num_feat number of features in matrix
* @param num_vec number of vectors in matrix
* @return transposed sparse feature matrix
*/
SGSparseVector<ST>* get_transposed(int32_t &num_feat, int32_t &num_vec)
{
num_feat=get_num_vectors();
num_vec=num_features;
int32_t* hist=new int32_t[num_features];
memset(hist, 0, sizeof(int32_t)*num_features);
// count how lengths of future feature vectors
for (int32_t v=0; v<num_feat; v++)
{
int32_t vlen;
bool vfree;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(v, vlen, vfree);
for (int32_t i=0; i<vlen; i++)
hist[sv[i].feat_index]++;
free_sparse_feature_vector(sv, v, vfree);
}
// allocate room for future feature vectors
SGSparseVector<ST>* sfm=new SGSparseVector<ST>[num_vec];
for (int32_t v=0; v<num_vec; v++)
{
sfm[v].features= new SGSparseVectorEntry<ST>[hist[v]];
sfm[v].num_feat_entries=hist[v];
sfm[v].vec_index=v;
}
// fill future feature vectors with content
memset(hist,0,sizeof(int32_t)*num_features);
for (int32_t v=0; v<num_feat; v++)
{
int32_t vlen;
bool vfree;
SGSparseVectorEntry<ST>* sv=get_sparse_feature_vector(v, vlen, vfree);
for (int32_t i=0; i<vlen; i++)
{
int32_t vidx=sv[i].feat_index;
int32_t fidx=v;
sfm[vidx].features[hist[vidx]].feat_index=fidx;
sfm[vidx].features[hist[vidx]].entry=sv[i].entry;
hist[vidx]++;
}
free_sparse_feature_vector(sv, v, vfree);
}
delete[] hist;
return sfm;
}
/** set sparse feature matrix
*
* not possible with subset
*
* @param sm sparse feature matrix
*
*/
void set_sparse_feature_matrix(SGSparseMatrix<ST> sm)
{
if (m_subset)
SG_ERROR("set_sparse_feature_matrix() not allowed with subset\n");
free_sparse_feature_matrix();
sparse_feature_matrix=sm.sparse_matrix;
num_features=sm.num_features;
num_vectors=sm.num_vectors;
}
/** gets a copy of a full feature matrix
*
* possible with subset
*
* @return full dense feature matrix
*/
SGMatrix<ST> get_full_feature_matrix()
{
SGMatrix<ST> full;
SG_INFO( "converting sparse features to full feature matrix of %ld x %ld entries\n", num_vectors, num_features);
full.num_rows=num_features;
full.num_cols=get_num_vectors();
full.do_free=true;
full.matrix=new ST[int64_t(num_features)*get_num_vectors()];
memset(full.matrix, 0, size_t(num_features)*size_t(get_num_vectors())*sizeof(ST));
for (int32_t v=0; v<full.num_cols; v++)
{
SGSparseVector<ST> current=
sparse_feature_matrix[subset_idx_conversion(v)];
for (int32_t f=0; f<current.num_feat_entries; f++)
{
int64_t offs=(current.vec_index*num_features)
+current.features[f].feat_index;
full.matrix[offs]=current.features[f].entry;
}
}
return full;
}
/** creates a sparse feature matrix from a full dense feature matrix
* necessary to set feature_matrix, num_features and num_vectors
* where num_features is the column offset, and columns are linear in memory
* see above for definition of sparse_feature_matrix
*
* any subset is removed before
*
* @param full full feature matrix
*/
virtual bool set_full_feature_matrix(SGMatrix<ST> full)
{
remove_subset();
ST* src=full.matrix;
int32_t num_feat=full.num_rows;
int32_t num_vec=full.num_cols;
free_sparse_feature_matrix();
bool result=true;
num_features=num_feat;
num_vectors=num_vec;
SG_INFO("converting dense feature matrix to sparse one\n");
int32_t* num_feat_entries=new int[num_vectors];
if (num_feat_entries)
{
int64_t num_total_entries=0;
// count nr of non sparse features
for (int32_t i=0; i< num_vec; i++)
{
num_feat_entries[i]=0;
for (int32_t j=0; j< num_feat; j++)
{
if (src[i*((int64_t) num_feat) + j] != 0)
num_feat_entries[i]++;
}
}
if (num_vec>0)
{
sparse_feature_matrix=new SGSparseVector<ST>[num_vec];
if (sparse_feature_matrix)
{
for (int32_t i=0; i< num_vec; i++)
{
sparse_feature_matrix[i].vec_index=i;
sparse_feature_matrix[i].num_feat_entries=0;
sparse_feature_matrix[i].features= NULL;
if (num_feat_entries[i]>0)
{
sparse_feature_matrix[i].features= new SGSparseVectorEntry<ST>[num_feat_entries[i]];
if (!sparse_feature_matrix[i].features)
{
SG_INFO( "allocation of features failed\n");
return false;
}
sparse_feature_matrix[i].num_feat_entries=num_feat_entries[i];
int32_t sparse_feat_idx=0;
for (int32_t j=0; j< num_feat; j++)
{
int64_t pos= i*num_feat + j;
if (src[pos] != 0)
{
sparse_feature_matrix[i].features[sparse_feat_idx].entry=src[pos];
sparse_feature_matrix[i].features[sparse_feat_idx].feat_index=j;
sparse_feat_idx++;
num_total_entries++;
}
}
}
}
}
else
{
SG_ERROR( "allocation of sparse feature matrix failed\n");
result=false;
}
SG_INFO( "sparse feature matrix has %ld entries (full matrix had %ld, sparsity %2.2f%%)\n",
num_total_entries, int64_t(num_feat)*num_vec, (100.0*num_total_entries)/(int64_t(num_feat)*num_vec));
}
else
{
SG_ERROR( "huh ? zero size matrix given ?\n");
result=false;
}
}
delete[] num_feat_entries;
return result;
}
/** apply preprocessor
*
* possible with subset
*
* @param force_preprocessing if preprocssing shall be forced
* @return if applying was successful
*/
virtual bool apply_preprocessor(bool force_preprocessing=false)
{
SG_INFO( "force: %d\n", force_preprocessing);
if ( sparse_feature_matrix && get_num_preprocessors() )
{
for (int32_t i=0; i<get_num_preprocessors(); i++)
{
if ( (!is_preprocessed(i) || force_preprocessing) )
{
set_preprocessed(i);
SG_INFO( "preprocessing using preproc %s\n", get_preprocessor(i)->get_name());
if (((CSparsePreprocessor<ST>*) get_preprocessor(i))->apply_to_sparse_feature_matrix(this) == NULL)
return false;
}
return true;
}
return true;
}
else
{
SG_WARNING( "no sparse feature matrix available or features already preprocessed - skipping.\n");
return false;
}
}
/** get memory footprint of one feature
*
* @return memory footprint of one feature
*/
virtual int32_t get_size() { return sizeof(ST); }
/** obtain sparse features from simple features
*
* subset on input is ignored, subset of this instance is removed
*
* @param sf simple features
* @return if obtaining was successful
*/
bool obtain_from_simple(CSimpleFeatures<ST>* sf)
{
SGMatrix<ST> fm=sf->get_feature_matrix();
ASSERT(fm.matrix && fm.num_cols>0 && fm.num_rows>0);
return set_full_feature_matrix(fm);
}
/** get number of feature vectors, possibly of subset
*
* @return number of feature vectors
*/
virtual inline int32_t get_num_vectors() const
{
return m_subset ? m_subset->get_size() : num_vectors;
}
/** get number of features
*
* @return number of features
*/
inline int32_t get_num_features() { return num_features; }
/** set number of features
*
* Sometimes when loading sparse features not all possible dimensions
* are used. This may pose a problem to classifiers when being applied
* to higher dimensional test-data. This function allows to
* artificially explode the feature space
*
* @param num the number of features, must be larger
* than the current number of features
* @return previous number of features
*/
inline int32_t set_num_features(int32_t num)
{
int32_t n=num_features;
ASSERT(n<=num);
num_features=num;
return num_features;
}
/** get feature class
*
* @return feature class SPARSE
*/
inline virtual EFeatureClass get_feature_class() { return C_SPARSE; }
/** get feature type
*
* @return templated feature type
*/
inline virtual EFeatureType get_feature_type();
/** free feature vector
*
* possible with subset
*
* @param feat_vec feature vector to free
* @param num index of vector in cache
* @param free if vector really should be deleted
*/
void free_feature_vector(SGSparseVectorEntry<ST>* feat_vec, int32_t num, bool free)
{
if (feature_cache)
feature_cache->unlock_entry(subset_idx_conversion(num));
if (free)
delete[] feat_vec ;
}
/** get number of non-zero entries in sparse feature matrix
*
* @return number of non-zero entries in sparse feature matrix
*/
int64_t get_num_nonzero_entries()
{
int64_t num=0;
index_t num_vec=get_num_vectors();
for (int32_t i=0; i<num_vec; i++)
num+=sparse_feature_matrix[subset_idx_conversion(i)].num_feat_entries;
return num;
}
/** compute a^2 on all feature vectors
*
* possible with subset
*
* @param sq the square for each vector is stored in here
* @return the square for each vector
*/
float64_t* compute_squared(float64_t* sq)
{
ASSERT(sq);
int32_t len=0;
bool do_free=false;
index_t num_vec=get_num_vectors();
for (int32_t i=0; i<num_vec; i++)
{
sq[i]=0;
SGSparseVectorEntry<float64_t>* vec = ((CSparseFeatures<float64_t>*) this)->get_sparse_feature_vector(i, len, do_free);
for (int32_t j=0; j<len; j++)
sq[i] += vec[j].entry * vec[j].entry;
((CSparseFeatures<float64_t>*) this)->free_feature_vector(vec, i, do_free);
}
return sq;
}
/** compute (a-b)^2 (== a^2+b^2-2ab)
* usually called by kernels'/distances' compute functions
* works on two feature vectors, although it is a member of a single
* feature: can either be called by lhs or rhs.
*
* possible wiht subsets on lhs or rhs
*
* @param lhs left-hand side features
* @param sq_lhs squared values of left-hand side
* @param idx_a index of left-hand side's vector to compute
* @param rhs right-hand side features