History

added KNN.weigh()

Ge Wang committed 3 years ago

Commit 11b3b3c1 parent 7cda3b24 files · +114 −16Browse files

Showing the changes to src (1 of 4 files). Show all

Unified · Split

src/core/ulib_ai.cpp+101−15
@@ -59,6 +59,7 @@ CK_DLL_CTOR( KNN_ctor ); · 58 unmodified lines
59CK_DLL_DTOR( KNN_dtor );59CK_DLL_DTOR( KNN_dtor );
60CK_DLL_MFUN( KNN_train );60CK_DLL_MFUN( KNN_train );
61CK_DLL_MFUN( KNN_search );61CK_DLL_MFUN( KNN_search );
62CK_DLL_MFUN( KNN_weigh );
62// 1.4.2.1 (yikai) added KNN263// 1.4.2.1 (yikai) added KNN2
63CK_DLL_CTOR( KNN2_ctor );64CK_DLL_CTOR( KNN2_ctor );
64CK_DLL_DTOR( KNN2_dtor );65CK_DLL_DTOR( KNN2_dtor );
@@ -67,6 +68,7 @@ CK_DLL_MFUN( KNN2_predict ); · 2 unmodified lines
67CK_DLL_MFUN( KNN2_search );68CK_DLL_MFUN( KNN2_search );
68CK_DLL_MFUN( KNN2_search2 );69CK_DLL_MFUN( KNN2_search2 );
69CK_DLL_MFUN( KNN2_search3 );70CK_DLL_MFUN( KNN2_search3 );
71CK_DLL_MFUN( KNN2_weigh );
70// offset72// offset
71static t_CKUINT KNN_offset_data = 0;73static t_CKUINT KNN_offset_data = 0;
72static t_CKUINT KNN2_offset_data = 0;74static t_CKUINT KNN2_offset_data = 0;
@@ -133,7 +135,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 60 unmodified lines
133 return FALSE;135 return FALSE;
134 136
135 // data offset137 // data offset
136 SVM_offset_data = type_engine_import_mvar( env, "float", "@SVM_data", FALSE );138 SVM_offset_data = type_engine_import_mvar( env, "int", "@SVM_data", FALSE );
137 if( SVM_offset_data == CK_INVALID_OFFSET ) goto error;139 if( SVM_offset_data == CK_INVALID_OFFSET ) goto error;
138 140
139 // train141 // train
@@ -166,7 +168,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 26 unmodified lines
166 return FALSE;168 return FALSE;
167 169
168 // data offset170 // data offset
169 KNN_offset_data = type_engine_import_mvar( env, "float", "@KNN_data", FALSE );171 KNN_offset_data = type_engine_import_mvar( env, "int", "@KNN_data", FALSE );
170 if( KNN_offset_data == CK_INVALID_OFFSET ) goto error;172 if( KNN_offset_data == CK_INVALID_OFFSET ) goto error;
171 173
172 // train174 // train
@@ -183,6 +185,12 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 10 unmodified lines
183 func->doc = "Search for the 'k' nearest neighbors of 'query' and return their respective indices.";185 func->doc = "Search for the 'k' nearest neighbors of 'query' and return their respective indices.";
184 if( !type_engine_import_mfun( env, func ) ) goto error;186 if( !type_engine_import_mfun( env, func ) ) goto error;
185 187
188 // weigh
189 func = make_new_mfun( "void", "weigh", KNN_weigh );
190 func->add_arg( "float[]", "weights" );
191 func->doc = "Set the weights for each dimension of the input vectors.";
192 if( !type_engine_import_mfun( env, func ) ) goto error;
193
186 // end the class import194 // end the class import
187 type_engine_import_class_end( env );195 type_engine_import_class_end( env );
188 196
@@ -199,7 +207,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 10 unmodified lines
199 return FALSE;207 return FALSE;
200 208
201 // data offset209 // data offset
202 KNN_offset_data = type_engine_import_mvar( env, "float", "@KNN2_data", FALSE );210 KNN_offset_data = type_engine_import_mvar( env, "int", "@KNN2_data", FALSE );
203 if( KNN_offset_data == CK_INVALID_OFFSET ) goto error;211 if( KNN_offset_data == CK_INVALID_OFFSET ) goto error;
204 212
205 // train213 // train
@@ -247,6 +255,12 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 41 unmodified lines
247 "Search for the 'k' nearest neighbors of 'query' and return their labels, indices, and feature vectors.";255 "Search for the 'k' nearest neighbors of 'query' and return their labels, indices, and feature vectors.";
248 if( !type_engine_import_mfun( env, func ) ) goto error;256 if( !type_engine_import_mfun( env, func ) ) goto error;
249 257
258 // weigh
259 func = make_new_mfun( "void", "weigh", KNN2_weigh );
260 func->add_arg( "float[]", "weights" );
261 func->doc = "Set the weights for each dimension of the input vectors.";
262 if( !type_engine_import_mfun( env, func ) ) goto error;
263
250 // end the class import264 // end the class import
251 type_engine_import_class_end( env );265 type_engine_import_class_end( env );
252 266
@@ -262,7 +276,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 9 unmodified lines
262 return FALSE;276 return FALSE;
263 277
264 // data offset278 // data offset
265 HMM_offset_data = type_engine_import_mvar( env, "float", "@HMM_data", FALSE );279 HMM_offset_data = type_engine_import_mvar( env, "int", "@HMM_data", FALSE );
266 if( HMM_offset_data == CK_INVALID_OFFSET ) goto error;280 if( HMM_offset_data == CK_INVALID_OFFSET ) goto error;
267 281
268 // init282 // init
@@ -310,7 +324,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 41 unmodified lines
310 return FALSE;324 return FALSE;
311 325
312 // data offset326 // data offset
313 Word2Vec_offset_data = type_engine_import_mvar( env, "float", "@Word2Vec_data", FALSE );327 Word2Vec_offset_data = type_engine_import_mvar( env, "int", "@Word2Vec_data", FALSE );
314 if( Word2Vec_offset_data == CK_INVALID_OFFSET ) goto error;328 if( Word2Vec_offset_data == CK_INVALID_OFFSET ) goto error;
315 329
316 // load330 // load
@@ -393,7 +407,7 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 76 unmodified lines
393 return FALSE;407 return FALSE;
394 408
395 // data offset409 // data offset
396 PCA_offset_data = type_engine_import_mvar( env, "float", "@PCA_data", FALSE );410 PCA_offset_data = type_engine_import_mvar( env, "int", "@PCA_data", FALSE );
397 if( PCA_offset_data == CK_INVALID_OFFSET ) goto error;411 if( PCA_offset_data == CK_INVALID_OFFSET ) goto error;
398 412
399 // transform413 // transform
@@ -401,7 +415,8 @@ DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) · 1 unmodified line
401 func->add_arg( "float[][]", "input" );415 func->add_arg( "float[][]", "input" );
402 func->add_arg( "int", "D" );416 func->add_arg( "int", "D" );
403 func->add_arg( "float[][]", "output" );417 func->add_arg( "float[][]", "output" );
404 func->doc = "dimension-reduce 'input' (NxM) to 'output' (NxD) as the projection of the input data onto its first 'D' principle components";418 func->doc =
419 "dimension-reduce 'input' (NxM) to 'output' (NxD) as the projection of the input data onto its first 'D' principle components";
405 if( !type_engine_import_sfun( env, func ) ) goto error;420 if( !type_engine_import_sfun( env, func ) ) goto error;
406 421
407 // end the class import422 // end the class import
@@ -673,27 +688,37 @@ public: · 265 unmodified lines
673 // constructor688 // constructor
674 KNN_Object()689 KNN_Object()
675 {690 {
676 X = new ChaiMatrixFast<t_CKFLOAT>();691 X = NULL;
677 Y = new ChaiVectorFast<t_CKUINT>();692 Y = NULL;
693 weights = NULL;
678 }694 }
679 695
680 // destructor696 // destructor
681 ~KNN_Object()697 ~KNN_Object()
698 {
699 clear();
700 }
701
702 // clear
703 void clear()
682 {704 {
683 SAFE_DELETE( X );705 SAFE_DELETE( X );
684 SAFE_DELETE( Y );706 SAFE_DELETE( Y );
707 SAFE_DELETE( weights );
685 }708 }
686 709
687 // train710 // train
688 t_CKINT train( Chuck_Array4 & x_ )711 t_CKINT train( Chuck_Array4 & x_ )
689 {712 {
713 clear();
690 // init714 // init
691 t_CKINT n_sample = x_.size();715 t_CKINT n_sample = x_.size();
692 t_CKUINT v;716 t_CKUINT v;
693 x_.get( 0, &v );717 x_.get( 0, &v );
694 Chuck_Array8 * x_i = (Chuck_Array8 *)v;718 Chuck_Array8 * x_i = (Chuck_Array8 *)v;
695 t_CKINT x_dim = x_i->size();719 t_CKINT x_dim = x_i->size();
696 X->allocate( n_sample, x_dim );720 X = new ChaiMatrixFast<t_CKFLOAT>( n_sample, x_dim );
721 weights = new ChaiVectorFast<t_CKFLOAT>( x_dim );
697 // copy722 // copy
698 for( t_CKINT i = 0; i < n_sample; i++ )723 for( t_CKINT i = 0; i < n_sample; i++ )
699 {724 {
@@ -704,20 +729,27 @@ public: · 4 unmodified lines
704 x_i->get( j, &X->v( i, j ) );729 x_i->get( j, &X->v( i, j ) );
705 }730 }
706 }731 }
732 // init weights
733 for( t_CKINT i = 0; i < x_dim; i++ )
734 {
735 weights->v( i ) = 1.0;
736 }
707 return 0;737 return 0;
708 }738 }
709 739
710 // train740 // train
711 t_CKINT train( Chuck_Array4 & x_, Chuck_Array4 & y_ )741 t_CKINT train( Chuck_Array4 & x_, Chuck_Array4 & y_ )
712 {742 {
743 clear();
713 // init744 // init
714 t_CKINT n_sample = x_.size();745 t_CKINT n_sample = x_.size();
715 t_CKUINT v;746 t_CKUINT v;
716 x_.get( 0, &v );747 x_.get( 0, &v );
717 Chuck_Array8 * x_i = (Chuck_Array8 *)v;748 Chuck_Array8 * x_i = (Chuck_Array8 *)v;
718 t_CKUINT x_dim = x_i->size();749 t_CKUINT x_dim = x_i->size();
719 X->allocate( n_sample, x_dim );750 X = new ChaiMatrixFast<t_CKFLOAT>( n_sample, x_dim );
720 Y->allocate( n_sample );751 Y = new ChaiVectorFast<t_CKUINT>( n_sample );
752 weights = new ChaiVectorFast<t_CKFLOAT>( x_dim );
721 // copy753 // copy
722 for( t_CKINT i = 0; i < n_sample; i++ )754 for( t_CKINT i = 0; i < n_sample; i++ )
723 {755 {
@@ -729,6 +761,11 @@ public: · 5 unmodified lines
729 }761 }
730 y_.get( i, &Y->v( i ) );762 y_.get( i, &Y->v( i ) );
731 }763 }
764 // init weights
765 for( t_CKINT i = 0; i < x_dim; i++ )
766 {
767 weights->v( i ) = 1.0;
768 }
732 return 0;769 return 0;
733 }770 }
734 771
@@ -760,7 +797,7 @@ public: · 25 unmodified lines
760 {797 {
761 // extra check in case query less than training dimensions798 // extra check in case query less than training dimensions
762 diff = ( j < query.size() ? query[j] : 0.0 ) - X->v( i, j );799 diff = ( j < query.size() ? query[j] : 0.0 ) - X->v( i, j );
763 distance += diff * diff;800 distance += diff * diff * weights->v( j );
764 }801 }
765 // check802 // check
766 for( t_CKINT j = 0; j < k; j++ )803 for( t_CKINT j = 0; j < k; j++ )
@@ -891,9 +928,32 @@ public: · 124 unmodified lines
891 }928 }
892 }929 }
893 930
931 // weigh
932 void weigh( const vector<t_CKFLOAT> & weights_ )
933 {
934 // sanity check
935 if( X == NULL )
936 {
937 EM_error3( "KNN: cannot weigh without training data" );
938 return;
939 }
940 if( weights_.size() != X->yDim() )
941 {
942 EM_error3( "KNN: invalid 'weights' vector provided: %d (expecting %d)",
943 weights_.size(), X->yDim() );
944 return;
945 }
946 // copy
947 for( t_CKINT i = 0; i < weights_.size(); i++ )
948 {
949 weights->v( i ) = weights_[i];
950 }
951 }
952
894private:953private:
895 ChaiMatrixFast<t_CKFLOAT> * X;954 ChaiMatrixFast<t_CKFLOAT> * X;
896 ChaiVectorFast<t_CKUINT> * Y;955 ChaiVectorFast<t_CKUINT> * Y;
956 ChaiVectorFast<t_CKFLOAT> * weights;
897};957};
898 958
899 959
@@ -953,6 +1013,16 @@ CK_DLL_MFUN( KNN_search ) · 53 unmodified lines
953 knn->search0( query->m_vector, k, *indices );1013 knn->search0( query->m_vector, k, *indices );
954}1014}
955 1015
1016CK_DLL_MFUN( KNN_weigh )
1017{
1018 // get object
1019 KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN_offset_data );
1020 // get args
1021 Chuck_Array8 * weights = (Chuck_Array8 *)GET_NEXT_OBJECT( ARGS );
1022 // weigh
1023 knn->weigh( weights->m_vector );
1024}
1025
956 1026
957 1027
958 1028
@@ -1074,6 +1144,16 @@ CK_DLL_MFUN( KNN2_search3 ) · 115 unmodified lines
1074 knn->search3( query->m_vector, k, *labels, *indices, *features );1144 knn->search3( query->m_vector, k, *labels, *indices, *features );
1075}1145}
1076 1146
1147CK_DLL_MFUN( KNN2_weigh )
1148{
1149 // get object
1150 KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data );
1151 // get args
1152 Chuck_Array8 * weights = (Chuck_Array8 *)GET_NEXT_OBJECT( ARGS );
1153 // weigh
1154 knn->weigh( weights->m_vector );
1155}
1156
1077//-----------------------------------------------------------------------------1157//-----------------------------------------------------------------------------
1078// name: struct HMM_Object1158// name: struct HMM_Object
1079// desc: hidden markov model implementation | added 1.4.2.0 (yikai)1159// desc: hidden markov model implementation | added 1.4.2.0 (yikai)
@@ -2065,7 +2145,10 @@ public: · 985 unmodified lines
2065 { }2145 { }
2066 2146
2067 /* Reduce a real, symmetric matrix to a symmetric, tridiag. matrix. */2147 /* Reduce a real, symmetric matrix to a symmetric, tridiag. matrix. */
2068 static void tred2( ChaiMatrixFast<t_CKFLOAT> & a, t_CKINT m, ChaiVectorFast<t_CKFLOAT> & d, ChaiVectorFast<t_CKFLOAT> & e )2148 static void tred2( ChaiMatrixFast<t_CKFLOAT> & a,
2149 t_CKINT m,
2150 ChaiVectorFast<t_CKFLOAT> & d,
2151 ChaiVectorFast<t_CKFLOAT> & e )
2069 /* Householder reductiom of matrix a to tridiagomal form.2152 /* Householder reductiom of matrix a to tridiagomal form.
2070 Algorithm: Martim et al., Num. Math. 11, 181-195, 1968.2153 Algorithm: Martim et al., Num. Math. 11, 181-195, 1968.
2071 Ref: Smith et al., Matrix Eigemsystem Routimes -- EISPACK Guide2154 Ref: Smith et al., Matrix Eigemsystem Routimes -- EISPACK Guide
@@ -2153,7 +2236,10 @@ public: · 81 unmodified lines
2153 }2236 }
2154 2237
2155 /* Tridiagonal QL algorithm -- Implicit */2238 /* Tridiagonal QL algorithm -- Implicit */
2156 static void tqli( ChaiVectorFast<t_CKFLOAT> & d, ChaiVectorFast<t_CKFLOAT> & e, t_CKINT m, ChaiMatrixFast<t_CKFLOAT> & z )2239 static void tqli( ChaiVectorFast<t_CKFLOAT> & d,
2240 ChaiVectorFast<t_CKFLOAT> & e,
2241 t_CKINT m,
2242 ChaiMatrixFast<t_CKFLOAT> & z )
2157 /*2243 /*
2158 Source code adapted from F. Murtagh, Munich, 6 June 19892244 Source code adapted from F. Murtagh, Munich, 6 June 1989
2159 http://astro.u-strasbg.fr/~fmurtagh/mda-sw/pca.c2245 http://astro.u-strasbg.fr/~fmurtagh/mda-sw/pca.c

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t