| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: ulib_ai.cpp |
| 28 | // desc: Music and AI class library; part of ChAI (ChucK for AI) |
| 29 | // |
| 30 | // date: Fall 2022 -- present |
| 31 | // authors: Ge Wang ([email protected]) |
| 32 | // Yikai Li ([email protected]) |
| 33 | // |
| 34 | // (HISTORICAL NOTE) |
| 35 | // Much of ChAI and its ways of thinking and doing can be foundationally |
| 36 | // attributed to the work of Dr. Rebecca Fiebrink, her landmark Ph.D. Thesis, |
| 37 | // /Real-time Human Interaction With Supervised Learning Algorithms for Music |
| 38 | // Composition and Performance/ (2011) [1], the Wekinator framework [2], her |
| 39 | // teaching at the intersections of AI, HCI, and Music [3], as well as earlier |
| 40 | // collaborations between Rebecca and Ge [4,5] including SMIRK [6] (Small Musical |
| 41 | // Information Retrieval toolKit; 2008; MARSYAS and the MIR work of Dr. George |
| 42 | // Tzanetakis; early efforts in on-the-fly learning), the unit analyzer |
| 43 | // framework [7] (2007; afford real-time audio feature extraction). All of |
| 44 | // these have directly or indirectly contributed the creation of ChAI. |
| 45 | // Additionally, ChAI benefitted from the teaching and design philosophy of |
| 46 | // Dr. Perry R. Cook (Ph.D. advisor to Rebecca, George, and Ge at Princeton, |
| 47 | // and ChucK co-author), who argued for real-time human interaction, parametric |
| 48 | // sound synthesis, and the importance of play in the design of musical tools. |
| 49 | // |
| 50 | // [1] Fiebrink, Rebecca. 2011. /Real-time Human Interaction With Supervised |
| 51 | // Learning Algorithms for Music Composition and Performance/ Ph.D. Thesis. |
| 52 | // Princeton University. https://www.cs.princeton.edu/techreports/2010/891.pdf |
| 53 | // |
| 54 | // [2] http://www.wekinator.org/ |
| 55 | // Wekinator | Software for real-time, interactive machine learning |
| 56 | // |
| 57 | // [3] Fiebrink, Rebecca. "Machine Learning for Musicians and Artists" MOOC |
| 58 | // https://www.kadenze.com/courses/machine-learning-for-musicians-and-artists/info |
| 59 | // |
| 60 | // [4] Fiebrink, R., G. Wang, P. R. Cook. 2008. "Foundations for On-the-fly |
| 61 | // Learning in the ChucK Programming Language." /International Computer Music |
| 62 | // Conference/. https://mcd.stanford.edu/publish/files/2008-icmc-learning.pdf |
| 63 | // |
| 64 | // [5] Fiebrink, R., G. Wang, P. R. Cook. 2008. "Support for Music Information |
| 65 | // Retrieval in the ChucK Programming Language." /International Conference |
| 66 | // on Music Information Retrieval/ (ISMIR). |
| 67 | // https://mcd.stanford.edu/publish/files/2008-ismir-proto.pdf |
| 68 | // |
| 69 | // [6] http://smirk.cs.princeton.edu/ |
| 70 | // sMIRk | Small Music Information Retrieval toolKit |
| 71 | // |
| 72 | // [7] Tzanetakis, G. and P. R. Cook. 2000. "MARSYAS: A Framework for Audio |
| 73 | // Analysis." Organised Sound. 4:(3) |
| 74 | // |
| 75 | // [8] Tzanetakis, G. and P. R. Cook. 2002. "Musical Genre Classification of |
| 76 | // Audio Signals." IEEE Transaction on Speech and Audio Processing. 10(5). |
| 77 | // |
| 78 | // [9] Wang, G., R. Fiebrink, P. R. Cook. 2007. "Combining Analysis and |
| 79 | // Synthesis in the ChucK Programming Language." /International Computer |
| 80 | // Music Conference/. https://mcd.stanford.edu/publish/files/2007-icmc-uana.pdf |
| 81 | //----------------------------------------------------------------------------- |
| 82 | #include "ulib_ai.h" |
| 83 | #include "chuck_type.h" |
| 84 | #include "chuck_instr.h" |
| 85 | #include "chuck_vm.h" |
| 86 | #include "chuck_errmsg.h" |
| 87 | #include "util_string.h" |
| 88 | #include <math.h> |
| 89 | |
| 90 | #include <string> |
| 91 | #include <vector> |
| 92 | #include <map> |
| 93 | #include <iostream> |
| 94 | #include <fstream> |
| 95 | using namespace std; |
| 96 | |
| 97 | // global model type |
| 98 | static t_CKINT g_mt_mlp = 0; |
| 99 | static t_CKINT g_mt_knn = 1; |
| 100 | static t_CKINT g_mt_svm = 2; |
| 101 | static t_CKINT g_mt_lr = 3; |
| 102 | static t_CKINT g_mt_pr = 4; |
| 103 | static t_CKINT g_mt_dt = 5; |
| 104 | static t_CKINT g_mt_boost = 6; |
| 105 | static t_CKINT g_mt_naivebayes = 7; |
| 106 | // global activation type |
| 107 | static t_CKINT g_at_linear = 0; |
| 108 | static t_CKINT g_at_sigmoid = 1; |
| 109 | static t_CKINT g_at_relu = 2; |
| 110 | static t_CKINT g_at_tanh = 3; |
| 111 | static t_CKINT g_at_softmax = 4; |
| 112 | // global task type |
| 113 | static t_CKINT g_tt_regression = -1; |
| 114 | static t_CKINT g_tt_classification = -2; |
| 115 | |
| 116 | |
| 117 | |
| 118 | // 1.4.2.0 (yikai) added SVM |
| 119 | CK_DLL_CTOR( SVM_ctor ); |
| 120 | CK_DLL_DTOR( SVM_dtor ); |
| 121 | CK_DLL_MFUN( SVM_train ); |
| 122 | CK_DLL_MFUN( SVM_predict ); |
| 123 | // offset |
| 124 | static t_CKUINT SVM_offset_data = 0; |
| 125 | |
| 126 | // 1.4.2.0 (yikai) added KNN |
| 127 | CK_DLL_CTOR( KNN_ctor ); |
| 128 | CK_DLL_DTOR( KNN_dtor ); |
| 129 | CK_DLL_MFUN( KNN_train ); |
| 130 | CK_DLL_MFUN( KNN_search ); |
| 131 | CK_DLL_MFUN( KNN_search2 ); |
| 132 | CK_DLL_MFUN( KNN_weigh ); |
| 133 | // 1.5.0.0 (yikai) added KNN2 |
| 134 | CK_DLL_CTOR( KNN2_ctor ); |
| 135 | CK_DLL_DTOR( KNN2_dtor ); |
| 136 | CK_DLL_MFUN( KNN2_train ); |
| 137 | CK_DLL_MFUN( KNN2_predict ); |
| 138 | CK_DLL_MFUN( KNN2_search ); |
| 139 | CK_DLL_MFUN( KNN2_search2 ); |
| 140 | CK_DLL_MFUN( KNN2_search3 ); |
| 141 | CK_DLL_MFUN( KNN2_weigh ); |
| 142 | // offset |
| 143 | static t_CKUINT KNN_offset_data = 0; |
| 144 | static t_CKUINT KNN2_offset_data = 0; |
| 145 | |
| 146 | // 1.4.2.0 (yikai) added HMM |
| 147 | CK_DLL_CTOR( HMM_ctor ); |
| 148 | CK_DLL_DTOR( HMM_dtor ); |
| 149 | CK_DLL_MFUN( HMM_init ); |
| 150 | CK_DLL_MFUN( HMM_train ); |
| 151 | CK_DLL_MFUN( HMM_generate ); |
| 152 | // offset |
| 153 | static t_CKUINT HMM_offset_data = 0; |
| 154 | |
| 155 | // 1.5.0.0 (yikai) added Word2Vec |
| 156 | CK_DLL_CTOR( Word2Vec_ctor ); |
| 157 | CK_DLL_DTOR( Word2Vec_dtor ); |
| 158 | CK_DLL_MFUN( Word2Vec_load ); |
| 159 | CK_DLL_MFUN( Word2Vec_load2 ); |
| 160 | CK_DLL_MFUN( Word2Vec_getMostSimilarByWord ); |
| 161 | CK_DLL_MFUN( Word2Vec_getMostSimilarByVector ); |
| 162 | CK_DLL_MFUN( Word2Vec_getVector ); |
| 163 | CK_DLL_MFUN( Word2Vec_getDictionarySize ); |
| 164 | CK_DLL_MFUN( Word2Vec_getDictionaryDim ); |
| 165 | CK_DLL_MFUN( Word2Vec_getUseKDTree ); |
| 166 | CK_DLL_MFUN( Word2Vec_getDimMinMax ); |
| 167 | CK_DLL_MFUN( Word2Vec_contains ); |
| 168 | // offset |
| 169 | static t_CKUINT Word2Vec_offset_data = 0; |
| 170 | |
| 171 | // 1.4.2.0 (yikai) added PCA |
| 172 | CK_DLL_CTOR( PCA_ctor ); |
| 173 | CK_DLL_DTOR( PCA_dtor ); |
| 174 | CK_DLL_SFUN( PCA_reduce ); |
| 175 | // offset |
| 176 | static t_CKUINT PCA_offset_data = 0; |
| 177 | |
| 178 | // 1.5.0.0 (yikai) added Wekinator |
| 179 | CK_DLL_CTOR( Wekinator_ctor ); |
| 180 | CK_DLL_DTOR( Wekinator_dtor ); |
| 181 | CK_DLL_MFUN( Wekinator_set_num_inputs ); |
| 182 | CK_DLL_MFUN( Wekinator_get_num_inputs ); |
| 183 | CK_DLL_MFUN( Wekinator_set_num_outputs ); |
| 184 | CK_DLL_MFUN( Wekinator_get_num_outputs ); |
| 185 | CK_DLL_MFUN( Wekinator_set_task_type ); |
| 186 | CK_DLL_MFUN( Wekinator_get_task_type ); |
| 187 | CK_DLL_MFUN( Wekinator_get_task_type_name ); |
| 188 | CK_DLL_MFUN( Wekinator_set_property ); |
| 189 | CK_DLL_MFUN( Wekinator_set_property1 ); |
| 190 | CK_DLL_MFUN( Wekinator_get_property_int ); |
| 191 | CK_DLL_MFUN( Wekinator_get_property_float ); |
| 192 | CK_DLL_MFUN( Wekinator_set_model_type ); |
| 193 | CK_DLL_MFUN( Wekinator_get_model_type ); |
| 194 | CK_DLL_MFUN( Wekinator_get_model_type_name ); |
| 195 | CK_DLL_MFUN( Wekinator_set_output_property ); |
| 196 | CK_DLL_MFUN( Wekinator_get_output_property_int ); |
| 197 | CK_DLL_MFUN( Wekinator_set_output_property1 ); |
| 198 | CK_DLL_MFUN( Wekinator_set_output_property2 ); |
| 199 | CK_DLL_MFUN( Wekinator_get_output_property_int1 ); |
| 200 | CK_DLL_MFUN( Wekinator_get_output_property_float ); |
| 201 | CK_DLL_MFUN( Wekinator_set_output_property3 ); |
| 202 | CK_DLL_MFUN( Wekinator_get_output_property ); |
| 203 | CK_DLL_MFUN( Wekinator_set_inputs ); |
| 204 | CK_DLL_MFUN( Wekinator_set_outputs ); |
| 205 | CK_DLL_MFUN( Wekinator_randomize_outputs ); |
| 206 | CK_DLL_MFUN( Wekinator_get_num_obs ); |
| 207 | CK_DLL_MFUN( Wekinator_get_num_obs1 ); |
| 208 | CK_DLL_MFUN( Wekinator_get_obs ); |
| 209 | CK_DLL_MFUN( Wekinator_get_obs1 ); |
| 210 | CK_DLL_MFUN( Wekinator_clear_obs ); |
| 211 | CK_DLL_MFUN( Wekinator_clear_obs1 ); |
| 212 | CK_DLL_MFUN( Wekinator_save ); |
| 213 | CK_DLL_MFUN( Wekinator_load ); |
| 214 | CK_DLL_MFUN( Wekinator_export_obs ); |
| 215 | CK_DLL_MFUN( Wekinator_export_obs1 ); |
| 216 | CK_DLL_MFUN( Wekinator_import_obs ); |
| 217 | CK_DLL_MFUN( Wekinator_clear_all_obs ); |
| 218 | CK_DLL_MFUN( Wekinator_clear_all_obs1 ); |
| 219 | CK_DLL_MFUN( Wekinator_set_all_record_status ); |
| 220 | CK_DLL_MFUN( Wekinator_set_output_record_status ); |
| 221 | CK_DLL_MFUN( Wekinator_get_output_record_status ); |
| 222 | CK_DLL_MFUN( Wekinator_set_all_run_status ); |
| 223 | CK_DLL_MFUN( Wekinator_set_output_run_status ); |
| 224 | CK_DLL_MFUN( Wekinator_get_output_run_status ); |
| 225 | CK_DLL_MFUN( Wekinator_add ); |
| 226 | CK_DLL_MFUN( Wekinator_add1 ); |
| 227 | CK_DLL_MFUN( Wekinator_add2 ); |
| 228 | CK_DLL_MFUN( Wekinator_next_round ); |
| 229 | CK_DLL_MFUN( Wekinator_train ); |
| 230 | CK_DLL_MFUN( Wekinator_predict ); |
| 231 | CK_DLL_MFUN( Wekinator_delete_last_round ); |
| 232 | CK_DLL_MFUN( Wekinator_clear ); |
| 233 | CK_DLL_MFUN( Wekinator_get_round ); |
| 234 | CK_DLL_MFUN( Wekinator_get_all_record_status ); |
| 235 | CK_DLL_MFUN( Wekinator_get_all_run_status ); |
| 236 | // offset |
| 237 | static t_CKUINT Wekinator_offset_data = 0; |
| 238 | |
| 239 | // 1.4.2.0 (yikai) added MLP |
| 240 | CK_DLL_CTOR( MLP_ctor ); |
| 241 | CK_DLL_DTOR( MLP_dtor ); |
| 242 | CK_DLL_MFUN( MLP_init ); |
| 243 | CK_DLL_MFUN( MLP_init2 ); |
| 244 | CK_DLL_MFUN( MLP_init3 ); |
| 245 | CK_DLL_MFUN( MLP_train ); |
| 246 | CK_DLL_MFUN( MLP_train2 ); |
| 247 | CK_DLL_MFUN( MLP_predict ); |
| 248 | CK_DLL_MFUN( MLP_get_weights ); |
| 249 | CK_DLL_MFUN( MLP_get_biases ); |
| 250 | CK_DLL_MFUN( MLP_get_gradients ); |
| 251 | CK_DLL_MFUN( MLP_get_activations ); |
| 252 | CK_DLL_MFUN( MLP_forward ); |
| 253 | CK_DLL_MFUN( MLP_backprop ); |
| 254 | CK_DLL_MFUN( MLP_save ); |
| 255 | CK_DLL_MFUN( MLP_load ); |
| 256 | CK_DLL_SFUN( MLP_shuffle ); |
| 257 | // offset |
| 258 | static t_CKUINT MLP_offset_data = 0; |
| 259 | |
| 260 | |
| 261 | |
| 262 | //----------------------------------------------------------------------------- |
| 263 | // name: libai_query() |
| 264 | // desc: add class definition to chuck internal type system |
| 265 | //----------------------------------------------------------------------------- |
| 266 | DLL_QUERY libai_query( Chuck_DL_Query * QUERY ) |
| 267 | { |
| 268 | // get environment reference |
| 269 | Chuck_Env * env = QUERY->env(); |
| 270 | // set module name |
| 271 | QUERY->setname( QUERY, "AI" ); |
| 272 | // model type |
| 273 | // add class |
| 274 | QUERY->begin_class( QUERY, "AI", "Object" ); |
| 275 | // add documentatiopn |
| 276 | QUERY->doc_class( QUERY, "AI class library." ); |
| 277 | // add svar |
| 278 | QUERY->add_svar( QUERY, "int", "MLP", TRUE, &g_mt_mlp ); |
| 279 | QUERY->doc_var( QUERY, "Model type: Multi-layer perceptron" ); |
| 280 | QUERY->add_svar( QUERY, "int", "KNN", TRUE, &g_mt_knn ); |
| 281 | QUERY->doc_var( QUERY, "Model type: K-nearest neighbor" ); |
| 282 | QUERY->add_svar( QUERY, "int", "SVM", TRUE, &g_mt_svm ); |
| 283 | QUERY->doc_var( QUERY, "Model type: Support vector machine" ); |
| 284 | QUERY->add_svar( QUERY, "int", "LR", TRUE, &g_mt_lr ); |
| 285 | QUERY->doc_var( QUERY, "Model type: Logistic regression" ); |
| 286 | QUERY->add_svar( QUERY, "int", "PR", TRUE, &g_mt_pr ); |
| 287 | QUERY->doc_var( QUERY, "Model type: Polynomial regression" ); |
| 288 | QUERY->add_svar( QUERY, "int", "DT", TRUE, &g_mt_dt ); |
| 289 | QUERY->doc_var( QUERY, "Model type: Decision tree" ); |
| 290 | QUERY->add_svar( QUERY, "int", "BOOST", TRUE, &g_mt_boost ); |
| 291 | QUERY->doc_var( QUERY, "Model type: Boosting" ); |
| 292 | QUERY->add_svar( QUERY, "int", "NAIVEBAYES", TRUE, &g_mt_naivebayes ); |
| 293 | QUERY->doc_var( QUERY, "Model type: Naive Bayes" ); |
| 294 | QUERY->add_svar( QUERY, "int", "ReLU", TRUE, &g_at_relu ); |
| 295 | QUERY->doc_var( QUERY, "Activation type: ReLU" ); |
| 296 | QUERY->add_svar( QUERY, "int", "Sigmoid", TRUE, &g_at_sigmoid ); |
| 297 | QUERY->doc_var( QUERY, "Activation type: Sigmoid" ); |
| 298 | QUERY->add_svar( QUERY, "int", "Tanh", TRUE, &g_at_tanh ); |
| 299 | QUERY->doc_var( QUERY, "Activation type: Tanh" ); |
| 300 | QUERY->add_svar( QUERY, "int", "Softmax", TRUE, &g_at_softmax ); |
| 301 | QUERY->doc_var( QUERY, "Activation type: Softmax" ); |
| 302 | QUERY->add_svar( QUERY, "int", "Linear", TRUE, &g_at_linear ); |
| 303 | QUERY->doc_var( QUERY, "Activation type: Linear" ); |
| 304 | QUERY->add_svar( QUERY, "int", "Regression", TRUE, &g_tt_regression ); |
| 305 | QUERY->doc_var( QUERY, "Task type: Regression" ); |
| 306 | QUERY->add_svar( QUERY, "int", "Classification", TRUE, &g_tt_classification ); |
| 307 | QUERY->doc_var( QUERY, "Task type: Classification" ); |
| 308 | // done |
| 309 | QUERY->end_class( QUERY ); |
| 310 | |
| 311 | |
| 312 | // function |
| 313 | Chuck_DL_Func * func = NULL; |
| 314 | // documentation string |
| 315 | std::string doc; |
| 316 | |
| 317 | //--------------------------------------------------------------------- |
| 318 | // init as base class: SVM |
| 319 | // 1.4.1.2 added by Yikai Li, Fall 2022 |
| 320 | //--------------------------------------------------------------------- |
| 321 | // doc string |
| 322 | doc = "a support vector machine (SVM) utility trains a model and predicts output based on new input"; |
| 323 | |
| 324 | // begin class definition |
| 325 | if( !type_engine_import_class_begin( env, "SVM", "Object", env->global(), SVM_ctor, SVM_dtor, doc.c_str() ) ) |
| 326 | return FALSE; |
| 327 | |
| 328 | // data offset |
| 329 | SVM_offset_data = type_engine_import_mvar( env, "int", "@SVM_data", FALSE ); |
| 330 | if( SVM_offset_data == CK_INVALID_OFFSET ) goto error; |
| 331 | |
| 332 | // train |
| 333 | func = make_new_mfun( "int", "train", SVM_train ); |
| 334 | func->add_arg( "float[][]", "x" ); |
| 335 | func->add_arg( "float[][]", "y" ); |
| 336 | func->doc = "Train the SVM model with the given samples 'x' and 'y'."; |
| 337 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 338 | |
| 339 | // predict |
| 340 | func = make_new_mfun( "int", "predict", SVM_predict ); |
| 341 | func->add_arg( "float[]", "x" ); |
| 342 | func->add_arg( "float[]", "y" ); |
| 343 | func->doc = "Predict the output 'y' given the input 'x'."; |
| 344 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 345 | |
| 346 | // add example |
| 347 | if( !type_engine_import_add_ex( env, "ai/svm/svm-basic.ck" ) ) goto error; |
| 348 | |
| 349 | // end the class import |
| 350 | type_engine_import_class_end( env ); |
| 351 | |
| 352 | //--------------------------------------------------------------------- |
| 353 | // init as base class: KNN |
| 354 | // 1.4.1.2 added by Yikai Li, Fall 2022 |
| 355 | //--------------------------------------------------------------------- |
| 356 | // doc string |
| 357 | doc = |
| 358 | "a basic k-NN utility that searches for k nearest neighbors from a set of observations / feature vectors. (Also see KNN2. The differrence between KNN and KNN2 is that KNN does not deal with labels whereas KNN2 is designed to work with labels.)"; |
| 359 | |
| 360 | // begin class definition |
| 361 | if( !type_engine_import_class_begin( env, "KNN", "Object", env->global(), KNN_ctor, KNN_dtor, doc.c_str() ) ) |
| 362 | return FALSE; |
| 363 | |
| 364 | // data offset |
| 365 | KNN_offset_data = type_engine_import_mvar( env, "int", "@KNN_data", FALSE ); |
| 366 | if( KNN_offset_data == CK_INVALID_OFFSET ) goto error; |
| 367 | |
| 368 | // train |
| 369 | func = make_new_mfun( "int", "train", KNN_train ); |
| 370 | func->add_arg( "float[][]", "x" ); |
| 371 | func->doc = "Train the KNN model with the given observations 'x'"; |
| 372 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 373 | |
| 374 | // search |
| 375 | func = make_new_mfun( "void", "search", KNN_search ); |
| 376 | func->add_arg( "float[]", "query" ); |
| 377 | func->add_arg( "int", "k" ); |
| 378 | func->add_arg( "int[]", "indices" ); |
| 379 | func->doc = "Search for the 'k' nearest neighbors of 'query' and return their corresponding indices."; |
| 380 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 381 | |
| 382 | // search |
| 383 | func = make_new_mfun( "void", "search", KNN_search2 ); |
| 384 | func->add_arg( "float[]", "query" ); |
| 385 | func->add_arg( "int", "k" ); |
| 386 | func->add_arg( "int[]", "indices" ); |
| 387 | func->add_arg( "float[][]", "observations" ); |
| 388 | func->doc = |
| 389 | "Search for the 'k' nearest neighbors of 'query' and return their corresponding indices and observations"; |
| 390 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 391 | |
| 392 | // weigh |
| 393 | func = make_new_mfun( "void", "weigh", KNN_weigh ); |
| 394 | func->add_arg( "float[]", "weights" ); |
| 395 | func->doc = "Set the weights for each dimension in the data."; |
| 396 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 397 | |
| 398 | // add example |
| 399 | if( !type_engine_import_add_ex( env, "ai/knn/knn-search.ck" ) ) goto error; |
| 400 | |
| 401 | // end the class import |
| 402 | type_engine_import_class_end( env ); |
| 403 | |
| 404 | //--------------------------------------------------------------------- |
| 405 | // init as base class: KNN2 |
| 406 | // 1.4.1.2 added by Yikai Li, Fall 2022 |
| 407 | //--------------------------------------------------------------------- |
| 408 | // doc string |
| 409 | doc = |
| 410 | "a k-NN utility that predicts probabilities of class membership based on distances from a test input to its k nearest neighbors. (Also see KNN. The differrence between KNN and KNN2 is that KNN does not deal with labels whereas KNN2 is designed to work with labels.)"; |
| 411 | |
| 412 | // begin class definition |
| 413 | if( !type_engine_import_class_begin( env, "KNN2", "Object", env->global(), KNN2_ctor, KNN2_dtor, doc.c_str() ) ) |
| 414 | return FALSE; |
| 415 | |
| 416 | // data offset |
| 417 | KNN_offset_data = type_engine_import_mvar( env, "int", "@KNN2_data", FALSE ); |
| 418 | if( KNN_offset_data == CK_INVALID_OFFSET ) goto error; |
| 419 | |
| 420 | // train |
| 421 | func = make_new_mfun( "int", "train", KNN2_train ); |
| 422 | func->add_arg( "float[][]", "x" ); |
| 423 | func->add_arg( "int[]", "labels" ); |
| 424 | func->doc = "Train the KNN model with the given observations 'x' and corresponding labels."; |
| 425 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 426 | |
| 427 | // predict |
| 428 | func = make_new_mfun( "int", "predict", KNN2_predict ); |
| 429 | func->add_arg( "float[]", "query" ); |
| 430 | func->add_arg( "int", "k" ); |
| 431 | func->add_arg( "float[]", "prob" ); |
| 432 | func->doc = |
| 433 | "Predict the output probabilities 'prob' given unlabeled test input 'query' based on distances to 'k' nearest neighbors."; |
| 434 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 435 | |
| 436 | // search |
| 437 | func = make_new_mfun( "void", "search", KNN2_search ); |
| 438 | func->add_arg( "float[]", "query" ); |
| 439 | func->add_arg( "int", "k" ); |
| 440 | func->add_arg( "int[]", "labels" ); |
| 441 | func->doc = "Search for the 'k' nearest neighbors of 'query' and return their labels."; |
| 442 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 443 | |
| 444 | // search |
| 445 | func = make_new_mfun( "void", "search", KNN2_search2 ); |
| 446 | func->add_arg( "float[]", "query" ); |
| 447 | func->add_arg( "int", "k" ); |
| 448 | func->add_arg( "int[]", "labels" ); |
| 449 | func->add_arg( "int[]", "indices" ); |
| 450 | func->doc = |
| 451 | "Search for the 'k' nearest neighbors of 'query' and return their labels and indices."; |
| 452 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 453 | |
| 454 | // search |
| 455 | func = make_new_mfun( "void", "search", KNN2_search3 ); |
| 456 | func->add_arg( "float[]", "query" ); |
| 457 | func->add_arg( "int", "k" ); |
| 458 | func->add_arg( "int[]", "labels" ); |
| 459 | func->add_arg( "int[]", "indices" ); |
| 460 | func->add_arg( "float[][]", "observations" ); |
| 461 | func->doc = |
| 462 | "Search for the 'k' nearest neighbors of 'query' and return their labels, indices, and observations."; |
| 463 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 464 | |
| 465 | // weigh |
| 466 | func = make_new_mfun( "void", "weigh", KNN2_weigh ); |
| 467 | func->add_arg( "float[]", "weights" ); |
| 468 | func->doc = "Set the weights for each dimension in the data."; |
| 469 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 470 | |
| 471 | // add example |
| 472 | if( !type_engine_import_add_ex( env, "ai/knn/knn2-classify.ck" ) ) goto error; |
| 473 | if( !type_engine_import_add_ex( env, "ai/knn/knn2-search.ck" ) ) goto error; |
| 474 | |
| 475 | // end the class import |
| 476 | type_engine_import_class_end( env ); |
| 477 | |
| 478 | //--------------------------------------------------------------------- |
| 479 | // init as base class: HMM |
| 480 | // 1.4.1.2 added by Yikai Li, Fall 2022 |
| 481 | //--------------------------------------------------------------------- |
| 482 | // doc string |
| 483 | doc = "a hidden markov model (HMM) utility that generates a sequence of observations based on the training data."; |
| 484 | |
| 485 | // begin class definition |
| 486 | if( !type_engine_import_class_begin( env, "HMM", "Object", env->global(), HMM_ctor, HMM_dtor, doc.c_str() ) ) |
| 487 | return FALSE; |
| 488 | |
| 489 | // data offset |
| 490 | HMM_offset_data = type_engine_import_mvar( env, "int", "@HMM_data", FALSE ); |
| 491 | if( HMM_offset_data == CK_INVALID_OFFSET ) goto error; |
| 492 | |
| 493 | // init |
| 494 | func = make_new_mfun( "int", "load", HMM_init ); |
| 495 | func->add_arg( "float[]", "initiailDistribution" ); |
| 496 | func->add_arg( "float[][]", "transitionMatrix" ); |
| 497 | func->add_arg( "float[][]", "emissionMatrix" ); |
| 498 | func->doc = |
| 499 | "Initialize the HMM model with the given initial state distribution, transition matrix, and emission matrix."; |
| 500 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 501 | |
| 502 | // train |
| 503 | func = make_new_mfun( "int", "train", HMM_train ); |
| 504 | func->add_arg( "int", "numStates" ); |
| 505 | func->add_arg( "int", "numEmissions" ); |
| 506 | func->add_arg( "int[]", "observations" ); |
| 507 | func->doc = "Train the HMM model with the given observations."; |
| 508 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 509 | |
| 510 | // generate |
| 511 | func = make_new_mfun( "int", "generate", HMM_generate ); |
| 512 | func->add_arg( "int", "length" ); |
| 513 | func->add_arg( "int[]", "output" ); |
| 514 | func->doc = "Generate a sequence of observations of the given length."; |
| 515 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 516 | |
| 517 | // add examples |
| 518 | if( !type_engine_import_add_ex( env, "ai/hmm/hmm-load.ck" ) ) goto error; |
| 519 | if( !type_engine_import_add_ex( env, "ai/hmm/hmm-train.ck" ) ) goto error; |
| 520 | |
| 521 | // end the class import |
| 522 | type_engine_import_class_end( env ); |
| 523 | |
| 524 | //--------------------------------------------------------------------- |
| 525 | // init as base class: Word2Vec |
| 526 | // 1.5.0.0 added by Yikai Li, Spring 2023 |
| 527 | //--------------------------------------------------------------------- |
| 528 | // doc string |
| 529 | doc = "a word embeddings utility that maps words to vectors; can load a model and perform similarity retrieval."; |
| 530 | |
| 531 | // begin class definition |
| 532 | if( !type_engine_import_class_begin( env, |
| 533 | "Word2Vec", |
| 534 | "Object", |
| 535 | env->global(), |
| 536 | Word2Vec_ctor, |
| 537 | Word2Vec_dtor, |
| 538 | doc.c_str() ) ) |
| 539 | return FALSE; |
| 540 | |
| 541 | // data offset |
| 542 | Word2Vec_offset_data = type_engine_import_mvar( env, "int", "@Word2Vec_data", FALSE ); |
| 543 | if( Word2Vec_offset_data == CK_INVALID_OFFSET ) goto error; |
| 544 | |
| 545 | // load |
| 546 | func = make_new_mfun( "int", "load", Word2Vec_load ); |
| 547 | func->add_arg( "string", "path" ); |
| 548 | func->doc = "Load pre-trained word embedding model from the given path."; |
| 549 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 550 | |
| 551 | // load |
| 552 | func = make_new_mfun( "int", "load", Word2Vec_load2 ); |
| 553 | func->add_arg( "string", "path" ); |
| 554 | func->add_arg( "int", "useKDTreeDim" ); |
| 555 | func->doc = |
| 556 | "Load pre-trained word embedding model from the given path; will use KDTree for similarity searches if the data dimension is less than or equal to 'useKDTreeDim'. Set 'useKDTreeDim' to 0 to use linear (brute force) similarity search; set 'useKDTreeDim' to less than 0 to always use KDTree."; |
| 557 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 558 | |
| 559 | // getMostSimilar |
| 560 | func = make_new_mfun( "int", "getSimilar", Word2Vec_getMostSimilarByWord ); |
| 561 | func->add_arg( "string", "word" ); |
| 562 | func->add_arg( "int", "k" ); |
| 563 | func->add_arg( "string[]", "output" ); |
| 564 | func->doc = "Get the k most similar words to the given word; return false if 'word' is not in model."; |
| 565 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 566 | |
| 567 | // getByVector |
| 568 | func = make_new_mfun( "int", "getSimilar", Word2Vec_getMostSimilarByVector ); |
| 569 | func->add_arg( "float[]", "vec" ); |
| 570 | func->add_arg( "int", "k" ); |
| 571 | func->add_arg( "string[]", "output" ); |
| 572 | func->doc = "Get the k most similar words to the given vector."; |
| 573 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 574 | |
| 575 | // getVector |
| 576 | func = make_new_mfun( "int", "getVector", Word2Vec_getVector ); |
| 577 | func->add_arg( "string", "word" ); |
| 578 | func->add_arg( "float[]", "output" ); |
| 579 | func->doc = "Get the vector of the given word; returns false if 'word' is not in model."; |
| 580 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 581 | |
| 582 | // contains |
| 583 | func = make_new_mfun( "int", "contains", Word2Vec_contains ); |
| 584 | func->add_arg( "string", "word" ); |
| 585 | func->doc = "Query if 'word' is in the current model."; |
| 586 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 587 | |
| 588 | // size |
| 589 | func = make_new_mfun( "int", "size", Word2Vec_getDictionarySize ); |
| 590 | func->doc = "Get number of words in dictionary."; |
| 591 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 592 | |
| 593 | // dim |
| 594 | func = make_new_mfun( "int", "dim", Word2Vec_getDictionaryDim ); |
| 595 | func->doc = "Get number of dimensions for word embedding."; |
| 596 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 597 | |
| 598 | // dim |
| 599 | func = make_new_mfun( "void", "minMax", Word2Vec_getDimMinMax ); |
| 600 | func->add_arg( "float[]", "mins" ); |
| 601 | func->add_arg( "float[]", "maxs" ); |
| 602 | func->doc = "Retrieve the minimums and maximums for each dimension."; |
| 603 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 604 | |
| 605 | // dim |
| 606 | func = make_new_mfun( "int", "useKDTree", Word2Vec_getUseKDTree ); |
| 607 | func->doc = "Get whether a KDTree is used for similarity search."; |
| 608 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 609 | |
| 610 | // add examples |
| 611 | if( !type_engine_import_add_ex( env, "ai/word2vec/word2vec-basic.ck" ) ) goto error; |
| 612 | if( !type_engine_import_add_ex( env, "ai/word2vec/poem-i-feel.ck" ) ) goto error; |
| 613 | if( !type_engine_import_add_ex( env, "ai/word2vec/poem-randomwalk.ck" ) ) goto error; |
| 614 | if( !type_engine_import_add_ex( env, "ai/word2vec/poem-spew.ck" ) ) goto error; |
| 615 | if( !type_engine_import_add_ex( env, "ai/word2vec/poem-ungenerate.ck" ) ) goto error; |
| 616 | if( !type_engine_import_add_ex( env, "ai/word2vec/word2vec-prompt.ck" ) ) goto error; |
| 617 | |
| 618 | // end the class import |
| 619 | type_engine_import_class_end( env ); |
| 620 | |
| 621 | //--------------------------------------------------------------------- |
| 622 | // init as base class: PCA |
| 623 | // 1.4.1.2 added by Yikai Li, Fall 2022 |
| 624 | //--------------------------------------------------------------------- |
| 625 | // doc string |
| 626 | doc = "a principle component analysis (PCA) utility, commonly used for dimensionality reduction."; |
| 627 | |
| 628 | // begin class definition |
| 629 | if( !type_engine_import_class_begin( env, "PCA", "Object", env->global(), PCA_ctor, PCA_dtor, doc.c_str() ) ) |
| 630 | return FALSE; |
| 631 | |
| 632 | // data offset |
| 633 | PCA_offset_data = type_engine_import_mvar( env, "int", "@PCA_data", FALSE ); |
| 634 | if( PCA_offset_data == CK_INVALID_OFFSET ) goto error; |
| 635 | |
| 636 | // transform |
| 637 | func = make_new_sfun( "void", "reduce", PCA_reduce ); |
| 638 | func->add_arg( "float[][]", "input" ); |
| 639 | func->add_arg( "int", "D" ); |
| 640 | func->add_arg( "float[][]", "output" ); |
| 641 | func->doc = |
| 642 | "dimension-reduce 'input' (NxM) to 'output' (NxD) as the projection of the input data onto its first 'D' principle components"; |
| 643 | if( !type_engine_import_sfun( env, func ) ) goto error; |
| 644 | |
| 645 | // add example |
| 646 | if( !type_engine_import_add_ex( env, "ai/pca/pca.ck" ) ) goto error; |
| 647 | |
| 648 | // end the class import |
| 649 | type_engine_import_class_end( env ); |
| 650 | |
| 651 | //--------------------------------------------------------------------- |
| 652 | // init as base class: Wekinator |
| 653 | // 1.5.0.0 added by Yikai Li, Winter 2023 |
| 654 | //--------------------------------------------------------------------- |
| 655 | // doc string |
| 656 | doc = |
| 657 | "a Wekinator utility that maps input vectors to output vectors, commonly used for interactive machine learning combining human-computer interaction and ML. Based on Rebecca Fiebrink's Wekinator framework."; |
| 658 | |
| 659 | // begin class definition |
| 660 | if( !type_engine_import_class_begin( env, |
| 661 | "Wekinator", |
| 662 | "Object", |
| 663 | env->global(), |
| 664 | Wekinator_ctor, |
| 665 | Wekinator_dtor, |
| 666 | doc.c_str() ) ) |
| 667 | return FALSE; |
| 668 | |
| 669 | // data offset |
| 670 | Wekinator_offset_data = type_engine_import_mvar( env, "float", "@Wekinator_data", FALSE ); |
| 671 | if( Wekinator_offset_data == CK_INVALID_OFFSET ) goto error; |
| 672 | |
| 673 | // set_num_inputs |
| 674 | func = make_new_mfun( "int", "inputDims", Wekinator_set_num_inputs ); |
| 675 | func->add_arg( "int", "n" ); |
| 676 | func->doc = "Set the number of input dimensions to Wekinator."; |
| 677 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 678 | |
| 679 | // get_num_inputs |
| 680 | func = make_new_mfun( "int", "inputDims", Wekinator_get_num_inputs ); |
| 681 | func->doc = "Get the number of input dimensions to Wekinator."; |
| 682 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 683 | |
| 684 | // set_num_outputs |
| 685 | func = make_new_mfun( "int", "outputDims", Wekinator_set_num_outputs ); |
| 686 | func->add_arg( "int", "n" ); |
| 687 | func->doc = "Set the number of output dimensions to Wekinator."; |
| 688 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 689 | |
| 690 | // get_num_outputs |
| 691 | func = make_new_mfun( "int", "outputDims", Wekinator_get_num_outputs ); |
| 692 | func->doc = "Get the number of output dimensions to Wekinator."; |
| 693 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 694 | |
| 695 | // set_task_type |
| 696 | func = make_new_mfun( "int", "taskType", Wekinator_set_task_type ); |
| 697 | func->add_arg( "int", "task_type" ); |
| 698 | func->doc = "Set the task type of the Wekinator. Options: AI.Regression, AI.Classification"; |
| 699 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 700 | |
| 701 | // get_task_type |
| 702 | func = make_new_mfun( "int", "taskType", Wekinator_get_task_type ); |
| 703 | func->doc = "Get the task type id of the Wekinator."; |
| 704 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 705 | |
| 706 | // get_task_type_name |
| 707 | func = make_new_mfun( "string", "taskTypeName", Wekinator_get_task_type_name ); |
| 708 | func->doc = "Get the task type name of the Wekinator."; |
| 709 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 710 | |
| 711 | // set_property |
| 712 | func = make_new_mfun( "void", "setProperty", Wekinator_set_property ); |
| 713 | func->add_arg( "int", "property_type" ); |
| 714 | func->add_arg( "string", "property_name" ); |
| 715 | func->add_arg( "int", "property_value" ); |
| 716 | func->doc = "Set the property of the Wekinator. See the Wekinator documentation for more information."; |
| 717 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 718 | |
| 719 | // set_property1 |
| 720 | func = make_new_mfun( "void", "setProperty", Wekinator_set_property1 ); |
| 721 | func->add_arg( "int", "property_type" ); |
| 722 | func->add_arg( "string", "property_name" ); |
| 723 | func->add_arg( "float", "property_value" ); |
| 724 | func->doc = "Set the property of the Wekinator. See the Wekinator documentation for more information."; |
| 725 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 726 | |
| 727 | // get_property_int |
| 728 | func = make_new_mfun( "int", "getPropertyInt", Wekinator_get_property_int ); |
| 729 | func->add_arg( "int", "property_type" ); |
| 730 | func->add_arg( "string", "property_name" ); |
| 731 | func->doc = "Get the property of the Wekinator. See the Wekinator documentation for more information."; |
| 732 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 733 | |
| 734 | // get_property_float |
| 735 | func = make_new_mfun( "float", "getPropertyFloat", Wekinator_get_property_float ); |
| 736 | func->add_arg( "int", "property_type" ); |
| 737 | func->add_arg( "string", "property_name" ); |
| 738 | func->doc = "Get the property of the Wekinator. See the Wekinator documentation for more information."; |
| 739 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 740 | |
| 741 | // set_model_type |
| 742 | func = make_new_mfun( "int", "modelType", Wekinator_set_model_type ); |
| 743 | func->add_arg( "int", "model_type" ); |
| 744 | func->doc = |
| 745 | "Set the model type of the Wekinator. Options: AI.Regression: AI.MLP, AI.LR, AI.Classification: AI.KNN, AI.SVM, AI.DT"; |
| 746 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 747 | |
| 748 | // get_model_type |
| 749 | func = make_new_mfun( "int", "modelType", Wekinator_get_model_type ); |
| 750 | func->doc = "Get the model type id of the Wekinator."; |
| 751 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 752 | |
| 753 | // get_model_type_name |
| 754 | func = make_new_mfun( "string", "modelTypeName", Wekinator_get_model_type_name ); |
| 755 | func->doc = "Get the model type name of the Wekinator."; |
| 756 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 757 | |
| 758 | // set_output_property |
| 759 | func = make_new_mfun( "void", "setOutputProperty", Wekinator_set_output_property ); |
| 760 | func->add_arg( "int", "output_index" ); |
| 761 | func->add_arg( "string", "property_name" ); |
| 762 | func->add_arg( "int", "property_value" ); |
| 763 | func->doc = "Set the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 764 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 765 | |
| 766 | // get_output_property_int |
| 767 | func = make_new_mfun( "int", "getOutputPropertyInt", Wekinator_get_output_property_int ); |
| 768 | func->add_arg( "int", "output_index" ); |
| 769 | func->add_arg( "string", "property_name" ); |
| 770 | func->doc = "Get the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 771 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 772 | |
| 773 | // set_output_property1 |
| 774 | func = make_new_mfun( "void", "setOutputProperty", Wekinator_set_output_property1 ); |
| 775 | func->add_arg( "int", "output_index" ); |
| 776 | func->add_arg( "int", "property_type" ); |
| 777 | func->add_arg( "string", "property_name" ); |
| 778 | func->add_arg( "int", "property_value" ); |
| 779 | func->doc = "Set the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 780 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 781 | |
| 782 | // set_output_property2 |
| 783 | func = make_new_mfun( "void", "setOutputProperty", Wekinator_set_output_property2 ); |
| 784 | func->add_arg( "int", "output_index" ); |
| 785 | func->add_arg( "int", "property_type" ); |
| 786 | func->add_arg( "string", "property_name" ); |
| 787 | func->add_arg( "float", "property_value" ); |
| 788 | func->doc = "Set the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 789 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 790 | |
| 791 | // get_output_property_int1 |
| 792 | func = make_new_mfun( "int", "getOutputPropertyInt", Wekinator_get_output_property_int1 ); |
| 793 | func->add_arg( "int", "output_index" ); |
| 794 | func->add_arg( "int", "property_type" ); |
| 795 | func->add_arg( "string", "property_name" ); |
| 796 | func->doc = "Get the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 797 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 798 | |
| 799 | // get_output_property_float |
| 800 | func = make_new_mfun( "float", "getOutputPropertyFloat", Wekinator_get_output_property_float ); |
| 801 | func->add_arg( "int", "output_index" ); |
| 802 | func->add_arg( "int", "property_type" ); |
| 803 | func->add_arg( "string", "property_name" ); |
| 804 | func->doc = "Get the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 805 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 806 | |
| 807 | // set_output_property3 |
| 808 | func = make_new_mfun( "void", "setOutputProperty", Wekinator_set_output_property3 ); |
| 809 | func->add_arg( "int", "output_index" ); |
| 810 | func->add_arg( "string", "property_name" ); |
| 811 | func->add_arg( "int[]", "property_value" ); |
| 812 | func->doc = "Set the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 813 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 814 | |
| 815 | // get_output_property |
| 816 | func = make_new_mfun( "void", "getOutputProperty", Wekinator_get_output_property ); |
| 817 | func->add_arg( "int", "output_index" ); |
| 818 | func->add_arg( "string", "property_name" ); |
| 819 | func->add_arg( "int[]", "property_value" ); |
| 820 | func->doc = "Get the output property of the Wekinator. See the Wekinator documentation for more information."; |
| 821 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 822 | |
| 823 | // set_inputs |
| 824 | func = make_new_mfun( "void", "input", Wekinator_set_inputs ); |
| 825 | func->add_arg( "float[]", "inputs" ); |
| 826 | func->doc = "Set the inputs of the Wekinator."; |
| 827 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 828 | |
| 829 | // set_outputs |
| 830 | func = make_new_mfun( "void", "output", Wekinator_set_outputs ); |
| 831 | func->add_arg( "float[]", "outputs" ); |
| 832 | func->doc = "Set the outputs of the Wekinator."; |
| 833 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 834 | |
| 835 | // randomize_outputs |
| 836 | func = make_new_mfun( "void", "randomizeOutputs", Wekinator_randomize_outputs ); |
| 837 | func->doc = "Randomize the outputs of the Wekinator."; |
| 838 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 839 | |
| 840 | // get_num_obs |
| 841 | func = make_new_mfun( "int", "numObs", Wekinator_get_num_obs ); |
| 842 | func->doc = "Get the number of observations in the Wekinator."; |
| 843 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 844 | |
| 845 | // get_num_obs1 |
| 846 | func = make_new_mfun( "int", "numObs", Wekinator_get_num_obs1 ); |
| 847 | func->add_arg( "int", "output_index" ); |
| 848 | func->doc = "Get the number of observations for the specified output in the Wekinator."; |
| 849 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 850 | |
| 851 | // get_obs |
| 852 | func = make_new_mfun( "void", "getObs", Wekinator_get_obs ); |
| 853 | func->add_arg( "float[][]", "obs" ); |
| 854 | func->doc = "Get the observations in the Wekinator."; |
| 855 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 856 | |
| 857 | // get_obs1 |
| 858 | func = make_new_mfun( "void", "getObs", Wekinator_get_obs1 ); |
| 859 | func->add_arg( "int", "output_index" ); |
| 860 | func->add_arg( "float[][]", "obs" ); |
| 861 | func->doc = "Get the observations for the specified output in the Wekinator."; |
| 862 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 863 | |
| 864 | // clear_obs |
| 865 | func = make_new_mfun( "void", "clearObs", Wekinator_clear_obs ); |
| 866 | func->add_arg( "int", "lo" ); |
| 867 | func->add_arg( "int", "hi" ); |
| 868 | func->doc = "Clear the observations by id range."; |
| 869 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 870 | |
| 871 | // clear_obs1 |
| 872 | func = make_new_mfun( "void", "clearObs", Wekinator_clear_obs1 ); |
| 873 | func->add_arg( "int", "output_index" ); |
| 874 | func->add_arg( "int", "lo" ); |
| 875 | func->add_arg( "int", "hi" ); |
| 876 | func->doc = "Clear the observations by id range for the specified output."; |
| 877 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 878 | |
| 879 | // save |
| 880 | func = make_new_mfun( "void", "save", Wekinator_save ); |
| 881 | func->add_arg( "string", "filename" ); |
| 882 | func->doc = "Save the Wekinator to a file."; |
| 883 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 884 | |
| 885 | // load |
| 886 | func = make_new_mfun( "void", "load", Wekinator_load ); |
| 887 | func->add_arg( "string", "filename" ); |
| 888 | func->doc = "Load the Wekinator from a file."; |
| 889 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 890 | |
| 891 | // export_obs |
| 892 | func = make_new_mfun( "void", "exportObs", Wekinator_export_obs ); |
| 893 | func->add_arg( "string", "filename" ); |
| 894 | func->doc = "Export the observations to a file."; |
| 895 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 896 | |
| 897 | // export_obs1 |
| 898 | func = make_new_mfun( "void", "exportObs", Wekinator_export_obs1 ); |
| 899 | func->add_arg( "int", "output_index" ); |
| 900 | func->add_arg( "string", "filename" ); |
| 901 | func->doc = "Export the observations for the specified output to a file."; |
| 902 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 903 | |
| 904 | // import_obs |
| 905 | func = make_new_mfun( "void", "importObs", Wekinator_import_obs ); |
| 906 | func->add_arg( "string", "filename" ); |
| 907 | func->doc = "Import the observations from a file."; |
| 908 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 909 | |
| 910 | // clear_all_obs |
| 911 | func = make_new_mfun( "void", "clearAllObs", Wekinator_clear_all_obs ); |
| 912 | func->doc = "Clear all observations."; |
| 913 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 914 | |
| 915 | // clear_all_obs1 |
| 916 | func = make_new_mfun( "void", "clearAllObs", Wekinator_clear_all_obs1 ); |
| 917 | func->add_arg( "int", "output_index" ); |
| 918 | func->doc = "Clear all observations for the specified output."; |
| 919 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 920 | |
| 921 | // set_all_record_status |
| 922 | func = make_new_mfun( "void", "setAllRecordStatus", Wekinator_set_all_record_status ); |
| 923 | func->add_arg( "int", "status" ); |
| 924 | func->doc = "Set the record status for all outputs."; |
| 925 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 926 | |
| 927 | // set_output_record_status |
| 928 | func = make_new_mfun( "void", "setOutputRecordStatus", Wekinator_set_output_record_status ); |
| 929 | func->add_arg( "int", "output_index" ); |
| 930 | func->add_arg( "int", "status" ); |
| 931 | func->doc = "Set the record status for the specified output."; |
| 932 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 933 | |
| 934 | // get_output_record_status |
| 935 | func = make_new_mfun( "int", "getOutputRecordStatus", Wekinator_get_output_record_status ); |
| 936 | func->add_arg( "int", "output_index" ); |
| 937 | func->doc = "Get the record status for the specified output."; |
| 938 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 939 | |
| 940 | // set_all_run_status |
| 941 | func = make_new_mfun( "void", "setAllRunStatus", Wekinator_set_all_run_status ); |
| 942 | func->add_arg( "int", "status" ); |
| 943 | func->doc = "Set the run status for all outputs."; |
| 944 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 945 | |
| 946 | // set_output_run_status |
| 947 | func = make_new_mfun( "void", "setOutputRunStatus", Wekinator_set_output_run_status ); |
| 948 | func->add_arg( "int", "output_index" ); |
| 949 | func->add_arg( "int", "status" ); |
| 950 | func->doc = "Set the run status for the specified output."; |
| 951 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 952 | |
| 953 | // get_output_run_status |
| 954 | func = make_new_mfun( "int", "getOutputRunStatus", Wekinator_get_output_run_status ); |
| 955 | func->add_arg( "int", "output_index" ); |
| 956 | func->doc = "Get the run status for the specified output."; |
| 957 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 958 | |
| 959 | // add |
| 960 | func = make_new_mfun( "void", "add", Wekinator_add ); |
| 961 | func->doc = "Add current inputs and outputs to the observations."; |
| 962 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 963 | |
| 964 | // add1 |
| 965 | func = make_new_mfun( "void", "add", Wekinator_add1 ); |
| 966 | func->add_arg( "float[]", "inputs" ); |
| 967 | func->add_arg( "float[]", "outputs" ); |
| 968 | func->doc = "Add given inputs and outputs to the observations."; |
| 969 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 970 | |
| 971 | // add2 |
| 972 | func = make_new_mfun( "void", "add", Wekinator_add2 ); |
| 973 | func->add_arg( "int", "output_index" ); |
| 974 | func->add_arg( "float[]", "inputs" ); |
| 975 | func->add_arg( "float[]", "outputs" ); |
| 976 | func->doc = "Add given inputs and outputs to the observations for the specified output."; |
| 977 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 978 | |
| 979 | // next_round |
| 980 | func = make_new_mfun( "void", "nextRound", Wekinator_next_round ); |
| 981 | func->doc = "Bump the recording round."; |
| 982 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 983 | |
| 984 | // train |
| 985 | func = make_new_mfun( "void", "train", Wekinator_train ); |
| 986 | func->doc = "Train models for all outputs."; |
| 987 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 988 | |
| 989 | // predict |
| 990 | func = make_new_mfun( "void", "predict", Wekinator_predict ); |
| 991 | func->add_arg( "float[]", "inputs" ); |
| 992 | func->add_arg( "float[]", "outputs" ); |
| 993 | func->doc = "Predict outputs for the given inputs."; |
| 994 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 995 | |
| 996 | // delete_last_round |
| 997 | func = make_new_mfun( "void", "deleteLastRound", Wekinator_delete_last_round ); |
| 998 | func->doc = "Delete the last round of observations."; |
| 999 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1000 | |
| 1001 | // clear |
| 1002 | func = make_new_mfun( "void", "clear", Wekinator_clear ); |
| 1003 | func->doc = "Clear everything except the global properties."; |
| 1004 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1005 | |
| 1006 | // get_round |
| 1007 | func = make_new_mfun( "int", "getRound", Wekinator_get_round ); |
| 1008 | func->doc = "Get the current recording round."; |
| 1009 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1010 | |
| 1011 | // get_all_record_status |
| 1012 | func = make_new_mfun( "int", "getAllRecordStatus", Wekinator_get_all_record_status ); |
| 1013 | func->doc = "Get the record status for all outputs."; |
| 1014 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1015 | |
| 1016 | // get_all_run_status |
| 1017 | func = make_new_mfun( "int", "getAllRunStatus", Wekinator_get_all_run_status ); |
| 1018 | func->doc = "Get the run status for all outputs."; |
| 1019 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1020 | |
| 1021 | // add examples |
| 1022 | if( !type_engine_import_add_ex( env, "ai/wekinator/wekinator-basic.ck" ) ) goto error; |
| 1023 | if( !type_engine_import_add_ex( env, "ai/wekinator/wekinator-customize.ck" ) ) goto error; |
| 1024 | if( !type_engine_import_add_ex( env, "ai/wekinator/wekinator-import.ck" ) ) goto error; |
| 1025 | |
| 1026 | // end the class import |
| 1027 | type_engine_import_class_end( env ); |
| 1028 | |
| 1029 | //--------------------------------------------------------------------- |
| 1030 | // init as base class: MLP |
| 1031 | // 1.5.0.0 added by Yikai Li, Winter 2023 |
| 1032 | //--------------------------------------------------------------------- |
| 1033 | // doc string |
| 1034 | doc = |
| 1035 | "a multilayer perceptron (MLP)--a basic artificial neural network--that maps an input layer to an output layer across a number of fully-connected hidden layers. This implementation can be trained either 1) by using one of the comprehensive .train() functions OR 2) by iteratively calling .forward() and .backprop() for each input-output observation, and using .shuffle() for each epoch. Commonly used for regression or classification."; |
| 1036 | |
| 1037 | // begin class definition |
| 1038 | if( !type_engine_import_class_begin( env, "MLP", "Object", env->global(), MLP_ctor, MLP_dtor, doc.c_str() ) ) |
| 1039 | return FALSE; |
| 1040 | |
| 1041 | // data offset |
| 1042 | MLP_offset_data = type_engine_import_mvar( env, "float", "@MLP_data", FALSE ); |
| 1043 | if( MLP_offset_data == CK_INVALID_OFFSET ) goto error; |
| 1044 | |
| 1045 | // init |
| 1046 | func = make_new_mfun( "void", "init", MLP_init ); |
| 1047 | func->add_arg( "int[]", "nodesPerLayer" ); |
| 1048 | func->doc = "Initialize the MLP with the given number of nodes per layer."; |
| 1049 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1050 | |
| 1051 | // init2 |
| 1052 | func = make_new_mfun( "void", "init", MLP_init2 ); |
| 1053 | func->add_arg( "int[]", "nodesPerLayer" ); |
| 1054 | func->add_arg( "int[]", "activationPerLayer" ); |
| 1055 | func->doc = |
| 1056 | "Initialize the MLP with the given number of nodes per layer and the given activation function per layer, as specified in 'activationPerLayer' (options: AI.Linear, AI.Sigmoid, AI.ReLU, AI.Tanh, or AI.Softmax)."; |
| 1057 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1058 | |
| 1059 | // init3 |
| 1060 | func = make_new_mfun( "void", "init", MLP_init3 ); |
| 1061 | func->add_arg( "int[]", "nodesPerLayer" ); |
| 1062 | func->add_arg( "int", "activationFunction" ); |
| 1063 | func->doc = |
| 1064 | "Initialize the MLP with the given number of nodes per layer and the given activation function for all layers (options: AI.Linear, AI.Sigmoid, AI.ReLU, AI.Tanh, or AI.Softmax)."; |
| 1065 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1066 | |
| 1067 | // train |
| 1068 | func = make_new_mfun( "void", "train", MLP_train ); |
| 1069 | func->add_arg( "float[][]", "inputs" ); |
| 1070 | func->add_arg( "float[][]", "outputs" ); |
| 1071 | func->doc = |
| 1072 | "Train the MLP with the given input and output observations with default learning rate=.01 and epochs=100. (Also see MLP.train(inputs,outputs,learningRate,epochs).)"; |
| 1073 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1074 | |
| 1075 | // train2 |
| 1076 | func = make_new_mfun( "void", "train", MLP_train2 ); |
| 1077 | func->add_arg( "float[][]", "inputs" ); |
| 1078 | func->add_arg( "float[][]", "outputs" ); |
| 1079 | func->add_arg( "float", "learningRate" ); |
| 1080 | func->add_arg( "int", "epochs" ); |
| 1081 | func->doc = "Train the MLP with the given input and output observations, the learning rate, and number of epochs."; |
| 1082 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1083 | |
| 1084 | // predict |
| 1085 | func = make_new_mfun( "int", "predict", MLP_predict ); |
| 1086 | func->add_arg( "float[]", "input" ); |
| 1087 | func->add_arg( "float[]", "output" ); |
| 1088 | func->doc = "Predict the output layer from an input layer."; |
| 1089 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1090 | |
| 1091 | // get_weights |
| 1092 | func = make_new_mfun( "void", "getWeights", MLP_get_weights ); |
| 1093 | func->add_arg( "int", "layer" ); |
| 1094 | func->add_arg( "float[][]", "weights" ); |
| 1095 | func->doc = "Get the weights of the given layer."; |
| 1096 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1097 | |
| 1098 | // get_biases |
| 1099 | func = make_new_mfun( "void", "getBiases", MLP_get_biases ); |
| 1100 | func->add_arg( "int", "layer" ); |
| 1101 | func->add_arg( "float[]", "biases" ); |
| 1102 | func->doc = "Get the biases of the given layer."; |
| 1103 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1104 | |
| 1105 | // get_gradients |
| 1106 | func = make_new_mfun( "void", "getGradients", MLP_get_gradients ); |
| 1107 | func->add_arg( "int", "layer" ); |
| 1108 | func->add_arg( "float[]", "gradients" ); |
| 1109 | func->doc = "Get the gradients of the given layer, after a manual .backprop()."; |
| 1110 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1111 | |
| 1112 | // get_activations |
| 1113 | func = make_new_mfun( "void", "getActivations", MLP_get_activations ); |
| 1114 | func->add_arg( "int", "layer" ); |
| 1115 | func->add_arg( "float[]", "activations" ); |
| 1116 | func->doc = "Get the activations of the given layer, after a manual .forward()."; |
| 1117 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1118 | |
| 1119 | // forward |
| 1120 | func = make_new_mfun( "void", "forward", MLP_forward ); |
| 1121 | func->add_arg( "float[]", "input" ); |
| 1122 | func->doc = "(Manually) forward-propagate the input vector through the network."; |
| 1123 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1124 | |
| 1125 | // backprop |
| 1126 | func = make_new_mfun( "void", "backprop", MLP_backprop ); |
| 1127 | func->add_arg( "float[]", "output" ); |
| 1128 | func->add_arg( "float", "learningRate" ); |
| 1129 | func->doc = |
| 1130 | "(Manually) backpropagate from the output layer, for a single input-output observation; compute the gradient of the loss function with respect to the weights in the network, one layer at a time."; |
| 1131 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1132 | |
| 1133 | // shuffle |
| 1134 | func = make_new_sfun( "void", "shuffle", MLP_shuffle ); |
| 1135 | func->add_arg( "float[][]", "X" ); |
| 1136 | func->add_arg( "float[][]", "Y" ); |
| 1137 | func->doc = "(Manually) shuffle the given input and output vectors."; |
| 1138 | if( !type_engine_import_sfun( env, func ) ) goto error; |
| 1139 | |
| 1140 | // save |
| 1141 | func = make_new_mfun( "int", "save", MLP_save ); |
| 1142 | func->add_arg( "string", "filename" ); |
| 1143 | func->doc = "Save the MLP model to file."; |
| 1144 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1145 | |
| 1146 | // load |
| 1147 | func = make_new_mfun( "int", "load", MLP_load ); |
| 1148 | func->add_arg( "string", "filename" ); |
| 1149 | func->doc = "Load a MLP model from file."; |
| 1150 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1151 | |
| 1152 | // add examples |
| 1153 | if( !type_engine_import_add_ex( env, "ai/mlp/mlp.ck" ) ) goto error; |
| 1154 | if( !type_engine_import_add_ex( env, "ai/mlp/mlp-manual.ck" ) ) goto error; |
| 1155 | if( !type_engine_import_add_ex( env, "ai/mlp/model-load.ck" ) ) goto error; |
| 1156 | if( !type_engine_import_add_ex( env, "ai/mlp/model-save.ck" ) ) goto error; |
| 1157 | |
| 1158 | // end the class import |
| 1159 | type_engine_import_class_end( env ); |
| 1160 | |
| 1161 | return TRUE; |
| 1162 | |
| 1163 | error: |
| 1164 | // end the class import |
| 1165 | type_engine_import_class_end( env ); |
| 1166 | |
| 1167 | return FALSE; |
| 1168 | } |
| 1169 | |
| 1170 | //----------------------------------------------------------------------------- |
| 1171 | // name: chuck2chai() |
| 1172 | // desc: chuck array (assuming float[][]) conversion to new chai matrix |
| 1173 | //----------------------------------------------------------------------------- |
| 1174 | ChaiMatrixFast<t_CKFLOAT> * chuck2chai( Chuck_ArrayInt & array ) |
| 1175 | { |
| 1176 | t_CKINT rows = array.size(); |
| 1177 | Chuck_ArrayFloat * row = (Chuck_ArrayFloat *)array.m_vector[0]; |
| 1178 | t_CKINT cols = row->size(); |
| 1179 | ChaiMatrixFast<t_CKFLOAT> * matrix = new ChaiMatrixFast<t_CKFLOAT>( rows, cols ); |
| 1180 | for( t_CKINT i = 0; i < rows; i++ ) |
| 1181 | { |
| 1182 | row = (Chuck_ArrayFloat *)array.m_vector[i]; |
| 1183 | for( t_CKINT j = 0; j < cols; j++ ) |
| 1184 | { |
| 1185 | matrix->v( i, j ) = row->m_vector[j]; |
| 1186 | } |
| 1187 | } |
| 1188 | return matrix; |
| 1189 | } |
| 1190 | |
| 1191 | //----------------------------------------------------------------------------- |
| 1192 | // name: chuck2chai() |
| 1193 | // desc: chuck array (float []) conversion to new chai vector |
| 1194 | //----------------------------------------------------------------------------- |
| 1195 | ChaiVectorFast<t_CKFLOAT> * chuck2chai( Chuck_ArrayFloat & array ) |
| 1196 | { |
| 1197 | t_CKINT size = array.size(); |
| 1198 | ChaiVectorFast<t_CKFLOAT> * vector = new ChaiVectorFast<t_CKFLOAT>( size ); |
| 1199 | for( t_CKINT i = 0; i < size; i++ ) |
| 1200 | { |
| 1201 | vector->v( i ) = array.m_vector[i]; |
| 1202 | } |
| 1203 | return vector; |
| 1204 | } |
| 1205 | |
| 1206 | //----------------------------------------------------------------------------- |
| 1207 | // name: shuffle() |
| 1208 | // desc: shuffle training data |
| 1209 | //----------------------------------------------------------------------------- |
| 1210 | void shuffle( Chuck_ArrayInt & X, Chuck_ArrayInt & Y ) |
| 1211 | { |
| 1212 | t_CKUINT temp, j; |
| 1213 | for( t_CKINT i = X.size() - 1; i > 0; i-- ) |
| 1214 | { |
| 1215 | j = ck_random() % ( i + 1 ); |
| 1216 | temp = X.m_vector[i]; |
| 1217 | X.m_vector[i] = X.m_vector[j]; |
| 1218 | X.m_vector[j] = temp; |
| 1219 | temp = Y.m_vector[i]; |
| 1220 | Y.m_vector[i] = Y.m_vector[j]; |
| 1221 | Y.m_vector[j] = temp; |
| 1222 | } |
| 1223 | } |
| 1224 | //----------------------------------------------------------------------------- |
| 1225 | // name: nextline() |
| 1226 | // desc: read next non-empty or commented line |
| 1227 | //----------------------------------------------------------------------------- |
| 1228 | t_CKBOOL nextline( std::ifstream & fin, string & line, t_CKBOOL commentIsHash ) |
| 1229 | { |
| 1230 | // skip over empty lines and commented lines (starts with #) |
| 1231 | do |
| 1232 | { |
| 1233 | // get line |
| 1234 | if( !std::getline( fin, line ) ) |
| 1235 | { |
| 1236 | line = ""; |
| 1237 | return FALSE; |
| 1238 | } |
| 1239 | // ltrim leading white spaces |
| 1240 | line = ltrim( line ); |
| 1241 | } while( line == "" || ( commentIsHash && line[0] == '#' ) ); |
| 1242 | |
| 1243 | return TRUE; |
| 1244 | } |
| 1245 | |
| 1246 | //----------------------------------------------------------------------------- |
| 1247 | // name: struct SVM_Object |
| 1248 | // desc: SVM implementation | added 1.4.2.0 (yikai) |
| 1249 | //----------------------------------------------------------------------------- |
| 1250 | struct SVM_Object |
| 1251 | { |
| 1252 | public: |
| 1253 | |
| 1254 | // constructor |
| 1255 | SVM_Object() |
| 1256 | { |
| 1257 | w = NULL; |
| 1258 | } |
| 1259 | |
| 1260 | // destructor |
| 1261 | ~SVM_Object() |
| 1262 | { |
| 1263 | CK_SAFE_DELETE( w ); |
| 1264 | } |
| 1265 | |
| 1266 | // train |
| 1267 | t_CKINT train( Chuck_ArrayInt & x_, Chuck_ArrayInt & y_ ) |
| 1268 | { |
| 1269 | ChaiMatrixFast<t_CKFLOAT> * x = chuck2chai( x_ ); |
| 1270 | ChaiMatrixFast<t_CKFLOAT> * y = chuck2chai( y_ ); |
| 1271 | train( *x, *y, x->xDim() ); |
| 1272 | CK_SAFE_DELETE( x ); |
| 1273 | CK_SAFE_DELETE( y ); |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | void train( ChaiMatrixFast<t_CKFLOAT> & x, ChaiMatrixFast<t_CKFLOAT> & y, t_CKINT n_sample ) |
| 1278 | { |
| 1279 | // init |
| 1280 | CK_SAFE_DELETE( w ); |
| 1281 | t_CKINT x_dim = x.yDim(), y_dim = y.yDim(); |
| 1282 | w = new ChaiMatrixFast<t_CKFLOAT>( x_dim + 1, y_dim ); |
| 1283 | // compute svm |
| 1284 | ChaiMatrixFast<t_CKFLOAT> xtx( x_dim, x_dim ); |
| 1285 | for( int i = 0; i < x_dim; i++ ) |
| 1286 | { |
| 1287 | for( int j = 0; j < x_dim; j++ ) |
| 1288 | { |
| 1289 | xtx.v( i, j ) = 0.0; |
| 1290 | for( int k = 0; k < n_sample; k++ ) |
| 1291 | { |
| 1292 | xtx.v( i, j ) += x.v( k, i ) * x.v( k, j ); |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | // inverse matrix |
| 1297 | ChaiMatrixFast<t_CKFLOAT> xtx_inv( x_dim, x_dim ); |
| 1298 | for( int i = 0; i < x_dim; i++ ) |
| 1299 | { |
| 1300 | for( int j = 0; j < x_dim; j++ ) |
| 1301 | { |
| 1302 | xtx_inv.v( i, j ) = 0.0; |
| 1303 | if( i == j ) |
| 1304 | { |
| 1305 | xtx_inv.v( i, j ) = 1.0; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | for( int i = 0; i < x_dim; i++ ) |
| 1310 | { |
| 1311 | t_CKFLOAT pivot = xtx.v( i, i ); |
| 1312 | for( int j = 0; j < x_dim; j++ ) |
| 1313 | { |
| 1314 | xtx.v( i, j ) /= pivot; |
| 1315 | xtx_inv.v( i, j ) /= pivot; |
| 1316 | } |
| 1317 | } |
| 1318 | for( int i = 0; i < x_dim; i++ ) |
| 1319 | { |
| 1320 | for( int j = 0; j < x_dim; j++ ) |
| 1321 | { |
| 1322 | if( i != j ) |
| 1323 | { |
| 1324 | t_CKFLOAT factor = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 1325 | for( int k = 0; k < x_dim; k++ ) |
| 1326 | { |
| 1327 | xtx.v( j, k ) -= factor * xtx.v( i, k ); |
| 1328 | xtx_inv.v( j, k ) -= factor * xtx_inv.v( i, k ); |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | // compute w |
| 1334 | for( int i = 0; i < x_dim; i++ ) |
| 1335 | { |
| 1336 | for( int j = 0; j < y_dim; j++ ) |
| 1337 | { |
| 1338 | w->v( i, j ) = 0.0; |
| 1339 | for( int k = 0; k < x_dim; k++ ) |
| 1340 | { |
| 1341 | for( int l = 0; l < n_sample; l++ ) |
| 1342 | { |
| 1343 | w->v( i, j ) += xtx_inv.v( i, k ) * x.v( l, k ) * y.v( l, j ); |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | // compute b |
| 1349 | ChaiVectorFast<t_CKFLOAT> b( y_dim ); |
| 1350 | for( int i = 0; i < y_dim; i++ ) |
| 1351 | { |
| 1352 | b.v( i ) = 0.0; |
| 1353 | for( int j = 0; j < n_sample; j++ ) |
| 1354 | { |
| 1355 | b.v( i ) += y.v( j, i ); |
| 1356 | for( int k = 0; k < x_dim; k++ ) |
| 1357 | { |
| 1358 | b.v( i ) -= w->v( k, i ) * x.v( j, k ); |
| 1359 | } |
| 1360 | } |
| 1361 | b.v( i ) /= n_sample; |
| 1362 | w->v( x_dim, i ) = b.v( i ); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // predict |
| 1367 | t_CKBOOL predict( Chuck_ArrayFloat & x_, Chuck_ArrayFloat & y_ ) |
| 1368 | { |
| 1369 | // init |
| 1370 | t_CKINT x_dim = x_.size(); |
| 1371 | t_CKINT y_dim = y_.size(); |
| 1372 | // copy |
| 1373 | ChaiVectorFast<t_CKFLOAT> x( x_dim ); |
| 1374 | for( t_CKINT i = 0; i < x_dim; i++ ) |
| 1375 | { |
| 1376 | x_.get( i, &x.v( i ) ); |
| 1377 | } |
| 1378 | // compute |
| 1379 | ChaiVectorFast<t_CKFLOAT> y( y_dim ); |
| 1380 | for( int i = 0; i < y_dim; i++ ) |
| 1381 | { |
| 1382 | y.v( i ) = 0.0; |
| 1383 | for( int j = 0; j < x_dim; j++ ) |
| 1384 | { |
| 1385 | y.v( i ) += w->v( j, i ) * x.v( j ); |
| 1386 | } |
| 1387 | y.v( i ) += w->v( x_dim, i ); |
| 1388 | } |
| 1389 | // copy |
| 1390 | for( t_CKINT i = 0; i < y_dim; i++ ) |
| 1391 | { |
| 1392 | y_.set( i, y.v( i ) ); |
| 1393 | } |
| 1394 | |
| 1395 | return TRUE; |
| 1396 | } |
| 1397 | |
| 1398 | // predict |
| 1399 | t_CKBOOL predict( ChaiVectorFast<t_CKFLOAT> & x_, ChaiVectorFast<t_CKFLOAT> & y_ ) |
| 1400 | { |
| 1401 | // init |
| 1402 | t_CKINT x_dim = x_.size(); |
| 1403 | t_CKINT y_dim = y_.size(); |
| 1404 | |
| 1405 | // check |
| 1406 | if( x_dim != w->xDim() - 1 || y_dim != w->yDim() ) |
| 1407 | { |
| 1408 | EM_error3( "SVM predict: input dimension mismatch: %d vs %d", x_dim, w->xDim() - 1 ); |
| 1409 | return FALSE; |
| 1410 | } |
| 1411 | |
| 1412 | // compute |
| 1413 | for( int i = 0; i < y_dim; i++ ) |
| 1414 | { |
| 1415 | y_.v( i ) = 0.0; |
| 1416 | for( int j = 0; j < x_dim; j++ ) |
| 1417 | { |
| 1418 | y_.v( i ) += w->v( j, i ) * x_.v( j ); |
| 1419 | } |
| 1420 | y_.v( i ) += w->v( x_dim, i ); |
| 1421 | } |
| 1422 | |
| 1423 | return TRUE; |
| 1424 | } |
| 1425 | |
| 1426 | private: |
| 1427 | ChaiMatrixFast<t_CKFLOAT> * w; |
| 1428 | }; |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | //----------------------------------------------------------------------------- |
| 1434 | // SVM c++ hooks |
| 1435 | //----------------------------------------------------------------------------- |
| 1436 | CK_DLL_CTOR( SVM_ctor ) |
| 1437 | { |
| 1438 | SVM_Object * svm = new SVM_Object(); |
| 1439 | OBJ_MEMBER_UINT( SELF, SVM_offset_data ) = (t_CKUINT)svm; |
| 1440 | } |
| 1441 | |
| 1442 | CK_DLL_DTOR( SVM_dtor ) |
| 1443 | { |
| 1444 | SVM_Object * svm = (SVM_Object *)OBJ_MEMBER_UINT( SELF, SVM_offset_data ); |
| 1445 | CK_SAFE_DELETE( svm ); |
| 1446 | OBJ_MEMBER_UINT( SELF, SVM_offset_data ) = 0; |
| 1447 | } |
| 1448 | |
| 1449 | CK_DLL_MFUN( SVM_train ) |
| 1450 | { |
| 1451 | // get object |
| 1452 | SVM_Object * svm = (SVM_Object *)OBJ_MEMBER_UINT( SELF, SVM_offset_data ); |
| 1453 | // get args |
| 1454 | Chuck_ArrayInt * x = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1455 | Chuck_ArrayInt * y = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1456 | // add |
| 1457 | svm->train( *x, *y ); |
| 1458 | } |
| 1459 | |
| 1460 | CK_DLL_MFUN( SVM_predict ) |
| 1461 | { |
| 1462 | // get object |
| 1463 | SVM_Object * svm = (SVM_Object *)OBJ_MEMBER_UINT( SELF, SVM_offset_data ); |
| 1464 | // get args |
| 1465 | Chuck_ArrayFloat * x = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1466 | Chuck_ArrayFloat * y = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1467 | // add |
| 1468 | RETURN->v_int = svm->predict( *x, *y ); |
| 1469 | } |
| 1470 | |
| 1471 | //----------------------------------------------------------------------------- |
| 1472 | // name: struct KNN_Object |
| 1473 | // desc: k-nearest neighbor implementation | added 1.4.2.0 (yikai) |
| 1474 | //----------------------------------------------------------------------------- |
| 1475 | struct KNN_Object |
| 1476 | { |
| 1477 | public: |
| 1478 | // constructor |
| 1479 | KNN_Object() |
| 1480 | { |
| 1481 | X = NULL; |
| 1482 | Y = NULL; |
| 1483 | weights = NULL; |
| 1484 | } |
| 1485 | |
| 1486 | // destructor |
| 1487 | ~KNN_Object() |
| 1488 | { |
| 1489 | clear(); |
| 1490 | } |
| 1491 | |
| 1492 | // clear |
| 1493 | void clear() |
| 1494 | { |
| 1495 | CK_SAFE_DELETE( X ); |
| 1496 | CK_SAFE_DELETE( Y ); |
| 1497 | CK_SAFE_DELETE( weights ); |
| 1498 | } |
| 1499 | |
| 1500 | // train |
| 1501 | t_CKINT train( Chuck_ArrayInt & x_ ) |
| 1502 | { |
| 1503 | clear(); |
| 1504 | // init |
| 1505 | t_CKINT n_sample = x_.size(); |
| 1506 | t_CKUINT v; |
| 1507 | x_.get( 0, &v ); |
| 1508 | Chuck_ArrayFloat * x_i = (Chuck_ArrayFloat *)v; |
| 1509 | t_CKINT x_dim = x_i->size(); |
| 1510 | X = new ChaiMatrixFast<t_CKFLOAT>( n_sample, x_dim ); |
| 1511 | weights = new ChaiVectorFast<t_CKFLOAT>( x_dim ); |
| 1512 | // copy |
| 1513 | for( t_CKINT i = 0; i < n_sample; i++ ) |
| 1514 | { |
| 1515 | x_.get( i, &v ); |
| 1516 | x_i = (Chuck_ArrayFloat *)v; |
| 1517 | for( t_CKINT j = 0; j < x_dim; j++ ) |
| 1518 | { |
| 1519 | x_i->get( j, &X->v( i, j ) ); |
| 1520 | } |
| 1521 | } |
| 1522 | // init weights |
| 1523 | for( t_CKINT i = 0; i < x_dim; i++ ) |
| 1524 | { |
| 1525 | weights->v( i ) = 1.0; |
| 1526 | } |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | // train |
| 1531 | void train( ChaiMatrixFast<t_CKFLOAT> & x_, t_CKINT size ) |
| 1532 | { |
| 1533 | clear(); |
| 1534 | // init |
| 1535 | X = new ChaiMatrixFast<t_CKFLOAT>( size, x_.yDim() ); |
| 1536 | weights = new ChaiVectorFast<t_CKFLOAT>( x_.yDim() ); |
| 1537 | // copy |
| 1538 | for( t_CKINT i = 0; i < size; i++ ) |
| 1539 | { |
| 1540 | for( t_CKINT j = 0; j < x_.yDim(); j++ ) |
| 1541 | { |
| 1542 | X->v( i, j ) = x_.v( i, j ); |
| 1543 | } |
| 1544 | } |
| 1545 | // init weights |
| 1546 | for( t_CKINT i = 0; i < x_.yDim(); i++ ) |
| 1547 | { |
| 1548 | weights->v( i ) = 1.0; |
| 1549 | } |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | // train |
| 1554 | t_CKINT train( Chuck_ArrayInt & x_, Chuck_ArrayInt & y_ ) |
| 1555 | { |
| 1556 | clear(); |
| 1557 | // init |
| 1558 | t_CKINT n_sample = x_.size(); |
| 1559 | t_CKUINT v; |
| 1560 | x_.get( 0, &v ); |
| 1561 | Chuck_ArrayFloat * x_i = (Chuck_ArrayFloat *)v; |
| 1562 | t_CKUINT x_dim = x_i->size(); |
| 1563 | X = new ChaiMatrixFast<t_CKFLOAT>( n_sample, x_dim ); |
| 1564 | Y = new ChaiVectorFast<t_CKUINT>( n_sample ); |
| 1565 | weights = new ChaiVectorFast<t_CKFLOAT>( x_dim ); |
| 1566 | // copy |
| 1567 | for( t_CKINT i = 0; i < n_sample; i++ ) |
| 1568 | { |
| 1569 | x_.get( i, &v ); |
| 1570 | x_i = (Chuck_ArrayFloat *)v; |
| 1571 | for( t_CKINT j = 0; j < x_dim; j++ ) |
| 1572 | { |
| 1573 | x_i->get( j, &X->v( i, j ) ); |
| 1574 | } |
| 1575 | y_.get( i, &Y->v( i ) ); |
| 1576 | } |
| 1577 | // init weights |
| 1578 | for( t_CKINT i = 0; i < x_dim; i++ ) |
| 1579 | { |
| 1580 | weights->v( i ) = 1.0; |
| 1581 | } |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | void getNearestNeighbors( const vector<t_CKFLOAT> & query, t_CKINT k, ChaiVectorFast<t_CKINT> & indices ) |
| 1586 | { |
| 1587 | // check query dimension against training dimensions |
| 1588 | if( query.size() < X->yDim() ) |
| 1589 | { |
| 1590 | // warning |
| 1591 | EM_error3( "k-NN query dimension %d less than training dimension %d; zero-padding...", |
| 1592 | query.size(), X->yDim() ); |
| 1593 | } |
| 1594 | |
| 1595 | // allocate |
| 1596 | ChaiVectorFast<t_CKFLOAT> top_distances( k ); |
| 1597 | ChaiVectorFast<t_CKINT> top_indices( k ); |
| 1598 | // initialize |
| 1599 | for( t_CKINT i = 0; i < k; i++ ) |
| 1600 | { |
| 1601 | top_distances.v( i ) = 1e10; |
| 1602 | top_indices.v( i ) = 0; |
| 1603 | } |
| 1604 | // compute distances |
| 1605 | t_CKFLOAT distance, diff; |
| 1606 | for( t_CKINT i = 0; i < X->xDim(); i++ ) |
| 1607 | { |
| 1608 | distance = 0.0; |
| 1609 | for( t_CKINT j = 0; j < X->yDim(); j++ ) |
| 1610 | { |
| 1611 | // extra check in case query less than training dimensions |
| 1612 | diff = ( j < query.size() ? query[j] : 0.0 ) - X->v( i, j ); |
| 1613 | distance += diff * diff * weights->v( j ); |
| 1614 | } |
| 1615 | // check |
| 1616 | for( t_CKINT j = 0; j < k; j++ ) |
| 1617 | { |
| 1618 | if( distance < top_distances.v( j ) ) |
| 1619 | { |
| 1620 | for( t_CKINT l = k - 1; l > j; l-- ) |
| 1621 | { |
| 1622 | top_distances.v( l ) = top_distances.v( l - 1 ); |
| 1623 | top_indices.v( l ) = top_indices.v( l - 1 ); |
| 1624 | } |
| 1625 | top_distances.v( j ) = distance; |
| 1626 | top_indices.v( j ) = i; |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | // copy |
| 1632 | for( t_CKINT i = 0; i < k; i++ ) |
| 1633 | { |
| 1634 | indices.v( i ) = top_indices.v( i ); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | // search; returns indices |
| 1639 | void search0( const vector<t_CKFLOAT> & query, t_CKINT k, Chuck_ArrayInt & indices_ ) |
| 1640 | { |
| 1641 | // init |
| 1642 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1643 | // search |
| 1644 | getNearestNeighbors( query, k, indices ); |
| 1645 | // resize |
| 1646 | indices_.set_size( k ); |
| 1647 | // copy |
| 1648 | for( t_CKINT i = 0; i < k; i++ ) |
| 1649 | { |
| 1650 | indices_.m_vector[i] = indices[i]; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | // search; returns labels |
| 1655 | void search1( const vector<t_CKFLOAT> & query, t_CKINT k, Chuck_ArrayInt & labels_ ) |
| 1656 | { |
| 1657 | // init |
| 1658 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1659 | // search |
| 1660 | getNearestNeighbors( query, k, indices ); |
| 1661 | // resize |
| 1662 | labels_.set_size( k ); |
| 1663 | // copy |
| 1664 | for( t_CKINT i = 0; i < k; i++ ) |
| 1665 | { |
| 1666 | labels_.m_vector[i] = Y->v( indices.v( i ) ); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | // search; returns labels and indices |
| 1671 | void search2( const vector<t_CKFLOAT> & query, t_CKINT k, Chuck_ArrayInt & labels_, Chuck_ArrayInt & indices_ ) |
| 1672 | { |
| 1673 | // init |
| 1674 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1675 | // search |
| 1676 | getNearestNeighbors( query, k, indices ); |
| 1677 | // resize |
| 1678 | labels_.set_size( k ); |
| 1679 | indices_.set_size( k ); |
| 1680 | for( t_CKINT i = 0; i < k; i++ ) |
| 1681 | { |
| 1682 | labels_.m_vector[i] = Y->v( indices.v( i ) ); |
| 1683 | indices_.m_vector[i] = indices[i]; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | // search; returns labels, indices, and feature vectors |
| 1688 | void search3( const vector<t_CKFLOAT> & query, |
| 1689 | t_CKINT k, |
| 1690 | Chuck_ArrayInt & labels_, |
| 1691 | Chuck_ArrayInt & indices_, |
| 1692 | Chuck_ArrayInt & features_ ) |
| 1693 | { |
| 1694 | // init |
| 1695 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1696 | // search |
| 1697 | getNearestNeighbors( query, k, indices ); |
| 1698 | // resize |
| 1699 | labels_.set_size( k ); |
| 1700 | indices_.set_size( k ); |
| 1701 | // check dimensions |
| 1702 | if( features_.size() < k ) |
| 1703 | { |
| 1704 | Chuck_ArrayFloat * x_i = features_.size() ? (Chuck_ArrayFloat *)features_.m_vector[0] : NULL; |
| 1705 | EM_error3( "KNN2: insufficient 'features' matrix provided: %dx%d (expecting %dx%d)", |
| 1706 | features_.size(), x_i != NULL ? x_i->m_vector.size() : 0, k, this->X->xDim() ); |
| 1707 | return; |
| 1708 | } |
| 1709 | // copy |
| 1710 | Chuck_ArrayFloat * x_i; |
| 1711 | for( t_CKINT i = 0; i < k; i++ ) |
| 1712 | { |
| 1713 | labels_.m_vector[i] = Y->v( indices.v( i ) ); |
| 1714 | indices_.m_vector[i] = indices[i]; |
| 1715 | x_i = (Chuck_ArrayFloat *)features_.m_vector[i]; |
| 1716 | for( t_CKINT j = 0; j < X->yDim(); j++ ) |
| 1717 | { |
| 1718 | x_i->m_vector[j] = X->v( indices.v( i ), j ); |
| 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | // search; returns labels, indices, and feature vectors |
| 1724 | void search3b( const vector<t_CKFLOAT> & query, |
| 1725 | t_CKINT k, |
| 1726 | Chuck_ArrayInt & indices_, |
| 1727 | Chuck_ArrayInt & features_ ) |
| 1728 | { |
| 1729 | // init |
| 1730 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1731 | // search |
| 1732 | getNearestNeighbors( query, k, indices ); |
| 1733 | // resize |
| 1734 | indices_.set_size( k ); |
| 1735 | // check dimensions |
| 1736 | if( features_.size() < k ) |
| 1737 | { |
| 1738 | Chuck_ArrayFloat * x_i = features_.size() ? (Chuck_ArrayFloat *)features_.m_vector[0] : NULL; |
| 1739 | EM_error3( "KNN: insufficient 'features' matrix provided: %dx%d (expecting %dx%d)", |
| 1740 | features_.size(), x_i != NULL ? x_i->m_vector.size() : 0, k, this->X->xDim() ); |
| 1741 | return; |
| 1742 | } |
| 1743 | // copy |
| 1744 | Chuck_ArrayFloat * x_i; |
| 1745 | for( t_CKINT i = 0; i < k; i++ ) |
| 1746 | { |
| 1747 | indices_.m_vector[i] = indices[i]; |
| 1748 | x_i = (Chuck_ArrayFloat *)features_.m_vector[i]; |
| 1749 | for( t_CKINT j = 0; j < X->yDim(); j++ ) |
| 1750 | { |
| 1751 | x_i->m_vector[j] = X->v( indices.v( i ), j ); |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | // predict |
| 1757 | t_CKBOOL predict( const vector<t_CKFLOAT> & query, t_CKINT k, Chuck_ArrayFloat & prob_ ) |
| 1758 | { |
| 1759 | // check k |
| 1760 | if( k <= 0 ) |
| 1761 | { |
| 1762 | EM_error3( "KNN: invalid 'k' value provided: %d", k ); |
| 1763 | return FALSE; |
| 1764 | } |
| 1765 | |
| 1766 | // TODO: check if model initialized |
| 1767 | // init |
| 1768 | ChaiVectorFast<t_CKINT> indices( k ); |
| 1769 | // search |
| 1770 | getNearestNeighbors( query, k, indices ); |
| 1771 | // copy |
| 1772 | prob_.set_size( Y->getMax() + 1 ); |
| 1773 | // zero out |
| 1774 | prob_.zero( 0, prob_.size() ); |
| 1775 | for( t_CKINT i = 0; i < k; i++ ) |
| 1776 | { |
| 1777 | prob_.m_vector[Y->v( indices.v( i ) )] += 1.0; |
| 1778 | } |
| 1779 | for( t_CKINT i = 0; i < prob_.size(); i++ ) |
| 1780 | { |
| 1781 | prob_.m_vector[i] /= k; |
| 1782 | } |
| 1783 | return TRUE; |
| 1784 | } |
| 1785 | |
| 1786 | // weigh |
| 1787 | void weigh( const vector<t_CKFLOAT> & weights_ ) |
| 1788 | { |
| 1789 | // sanity check |
| 1790 | if( X == NULL ) |
| 1791 | { |
| 1792 | EM_error3( "KNN: cannot weigh without training data" ); |
| 1793 | return; |
| 1794 | } |
| 1795 | if( weights_.size() != X->yDim() ) |
| 1796 | { |
| 1797 | EM_error3( "KNN: invalid 'weights' vector provided: %d (expecting %d)", |
| 1798 | weights_.size(), X->yDim() ); |
| 1799 | return; |
| 1800 | } |
| 1801 | // copy |
| 1802 | for( t_CKINT i = 0; i < weights_.size(); i++ ) |
| 1803 | { |
| 1804 | weights->v( i ) = weights_[i]; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | private: |
| 1809 | ChaiMatrixFast<t_CKFLOAT> * X; |
| 1810 | ChaiVectorFast<t_CKUINT> * Y; |
| 1811 | ChaiVectorFast<t_CKFLOAT> * weights; |
| 1812 | }; |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | //----------------------------------------------------------------------------- |
| 1818 | // KNN c++ hooks |
| 1819 | //----------------------------------------------------------------------------- |
| 1820 | CK_DLL_CTOR( KNN_ctor ) |
| 1821 | { |
| 1822 | KNN_Object * knn = new KNN_Object(); |
| 1823 | OBJ_MEMBER_UINT( SELF, KNN_offset_data ) = (t_CKUINT)knn; |
| 1824 | } |
| 1825 | |
| 1826 | CK_DLL_DTOR( KNN_dtor ) |
| 1827 | { |
| 1828 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN_offset_data ); |
| 1829 | CK_SAFE_DELETE( knn ); |
| 1830 | OBJ_MEMBER_UINT( SELF, KNN_offset_data ) = 0; |
| 1831 | } |
| 1832 | |
| 1833 | CK_DLL_MFUN( KNN_train ) |
| 1834 | { |
| 1835 | // get object |
| 1836 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN_offset_data ); |
| 1837 | // get args |
| 1838 | Chuck_ArrayInt * x = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1839 | |
| 1840 | // check for NULL |
| 1841 | if( x == NULL ) |
| 1842 | { |
| 1843 | EM_error3( "KNN.train(): NULL input encountered..." ); |
| 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | // train |
| 1848 | knn->train( *x ); |
| 1849 | } |
| 1850 | |
| 1851 | CK_DLL_MFUN( KNN_search ) |
| 1852 | { |
| 1853 | // get object |
| 1854 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN_offset_data ); |
| 1855 | // get args |
| 1856 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1857 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 1858 | Chuck_ArrayInt * indices = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1859 | |
| 1860 | // check for NULL |
| 1861 | if( query == NULL || indices == NULL ) |
| 1862 | { |
| 1863 | EM_error3( "KNN.search(): NULL input encountered..." ); |
| 1864 | return; |
| 1865 | } |
| 1866 | |
| 1867 | // search0 |
| 1868 | knn->search0( query->m_vector, k, *indices ); |
| 1869 | } |
| 1870 | |
| 1871 | CK_DLL_MFUN( KNN_search2 ) |
| 1872 | { |
| 1873 | // get object |
| 1874 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1875 | // get args |
| 1876 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1877 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 1878 | Chuck_ArrayInt * indices = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1879 | Chuck_ArrayInt * features = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1880 | |
| 1881 | // check for NULL |
| 1882 | if( query == NULL || indices == NULL || features == NULL ) |
| 1883 | { |
| 1884 | EM_error3( "KNN2.search(): NULL input encountered..." ); |
| 1885 | return; |
| 1886 | } |
| 1887 | |
| 1888 | // search3 |
| 1889 | knn->search3b( query->m_vector, k, *indices, *features ); |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | CK_DLL_MFUN( KNN_weigh ) |
| 1894 | { |
| 1895 | // get object |
| 1896 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN_offset_data ); |
| 1897 | // get args |
| 1898 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1899 | // weigh |
| 1900 | knn->weigh( weights->m_vector ); |
| 1901 | } |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | //----------------------------------------------------------------------------- |
| 1907 | // KNN2 c++ hooks |
| 1908 | //----------------------------------------------------------------------------- |
| 1909 | CK_DLL_CTOR( KNN2_ctor ) |
| 1910 | { |
| 1911 | KNN_Object * knn = new KNN_Object(); |
| 1912 | OBJ_MEMBER_UINT( SELF, KNN2_offset_data ) = (t_CKUINT)knn; |
| 1913 | } |
| 1914 | |
| 1915 | CK_DLL_DTOR( KNN2_dtor ) |
| 1916 | { |
| 1917 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1918 | CK_SAFE_DELETE( knn ); |
| 1919 | OBJ_MEMBER_UINT( SELF, KNN2_offset_data ) = 0; |
| 1920 | } |
| 1921 | |
| 1922 | CK_DLL_MFUN( KNN2_train ) |
| 1923 | { |
| 1924 | // get object |
| 1925 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1926 | // get args |
| 1927 | Chuck_ArrayInt * x = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1928 | Chuck_ArrayInt * y = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1929 | |
| 1930 | // check for NULL |
| 1931 | if( x == NULL || y == NULL ) |
| 1932 | { |
| 1933 | EM_error3( "KNN2.train(): NULL input encountered..." ); |
| 1934 | return; |
| 1935 | } |
| 1936 | |
| 1937 | // train |
| 1938 | knn->train( *x, *y ); |
| 1939 | } |
| 1940 | |
| 1941 | CK_DLL_MFUN( KNN2_predict ) |
| 1942 | { |
| 1943 | // get object |
| 1944 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1945 | // get args |
| 1946 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1947 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 1948 | Chuck_ArrayFloat * prob = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1949 | |
| 1950 | // check for NULL |
| 1951 | if( query == NULL || prob == NULL ) |
| 1952 | { |
| 1953 | EM_error3( "KNN2.predict(): NULL input encountered..." ); |
| 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | // predict |
| 1958 | RETURN->v_int = knn->predict( query->m_vector, k, *prob ); |
| 1959 | } |
| 1960 | |
| 1961 | CK_DLL_MFUN( KNN2_search ) |
| 1962 | { |
| 1963 | // get object |
| 1964 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1965 | // get args |
| 1966 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1967 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 1968 | Chuck_ArrayInt * labels = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1969 | |
| 1970 | // check for NULL |
| 1971 | if( query == NULL || labels == NULL ) |
| 1972 | { |
| 1973 | EM_error3( "KNN2.search(): NULL input encountered..." ); |
| 1974 | return; |
| 1975 | } |
| 1976 | |
| 1977 | // search1 |
| 1978 | knn->search1( query->m_vector, k, *labels ); |
| 1979 | } |
| 1980 | |
| 1981 | CK_DLL_MFUN( KNN2_search2 ) |
| 1982 | { |
| 1983 | // get object |
| 1984 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 1985 | // get args |
| 1986 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 1987 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 1988 | Chuck_ArrayInt * labels = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1989 | Chuck_ArrayInt * indices = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 1990 | |
| 1991 | // check for NULL |
| 1992 | if( query == NULL || labels == NULL || indices == NULL ) |
| 1993 | { |
| 1994 | EM_error3( "KNN2.search(): NULL input encountered..." ); |
| 1995 | return; |
| 1996 | } |
| 1997 | |
| 1998 | // search2 |
| 1999 | knn->search2( query->m_vector, k, *labels, *indices ); |
| 2000 | } |
| 2001 | |
| 2002 | CK_DLL_MFUN( KNN2_search3 ) |
| 2003 | { |
| 2004 | // get object |
| 2005 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 2006 | // get args |
| 2007 | Chuck_ArrayFloat * query = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 2008 | t_CKINT k = GET_NEXT_INT( ARGS ); |
| 2009 | Chuck_ArrayInt * labels = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2010 | Chuck_ArrayInt * indices = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2011 | Chuck_ArrayInt * features = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2012 | |
| 2013 | // check for NULL |
| 2014 | if( query == NULL || labels == NULL || indices == NULL || features == NULL ) |
| 2015 | { |
| 2016 | EM_error3( "KNN2.search(): NULL input encountered..." ); |
| 2017 | return; |
| 2018 | } |
| 2019 | |
| 2020 | // search3 |
| 2021 | knn->search3( query->m_vector, k, *labels, *indices, *features ); |
| 2022 | } |
| 2023 | |
| 2024 | CK_DLL_MFUN( KNN2_weigh ) |
| 2025 | { |
| 2026 | // get object |
| 2027 | KNN_Object * knn = (KNN_Object *)OBJ_MEMBER_UINT( SELF, KNN2_offset_data ); |
| 2028 | // get args |
| 2029 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 2030 | // weigh |
| 2031 | knn->weigh( weights->m_vector ); |
| 2032 | } |
| 2033 | |
| 2034 | //----------------------------------------------------------------------------- |
| 2035 | // name: struct HMM_Object |
| 2036 | // desc: hidden markov model implementation | added 1.4.2.0 (yikai) |
| 2037 | //----------------------------------------------------------------------------- |
| 2038 | struct HMM_Object |
| 2039 | { |
| 2040 | public: |
| 2041 | // constructor |
| 2042 | HMM_Object() : initial( NULL ), transition( NULL ), emission( NULL ) |
| 2043 | { } |
| 2044 | |
| 2045 | // destructor |
| 2046 | ~HMM_Object() |
| 2047 | { |
| 2048 | clear(); |
| 2049 | } |
| 2050 | |
| 2051 | // clear |
| 2052 | void clear() |
| 2053 | { |
| 2054 | CK_SAFE_DELETE( initial ); |
| 2055 | CK_SAFE_DELETE( transition ); |
| 2056 | CK_SAFE_DELETE( emission ); |
| 2057 | } |
| 2058 | |
| 2059 | // init |
| 2060 | t_CKINT init( Chuck_ArrayFloat & initial_, Chuck_ArrayInt & transition_, Chuck_ArrayInt & emission_ ) |
| 2061 | { |
| 2062 | clear(); |
| 2063 | initial = chuck2chai( initial_ ); |
| 2064 | transition = chuck2chai( transition_ ); |
| 2065 | emission = chuck2chai( emission_ ); |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | void filter( ChaiVectorFast<t_CKINT> & y, ChaiMatrixFast<t_CKFLOAT> & phi, ChaiVectorFast<t_CKFLOAT> & c ) |
| 2070 | { |
| 2071 | t_CKINT i, j, t, n = y.size(), N = emission->xDim(); // M = emission->yDim(); |
| 2072 | ChaiVectorFast<t_CKFLOAT> z( N ); |
| 2073 | c.v( 0 ) = 0; |
| 2074 | for( j = 0; j < N; j++ ) |
| 2075 | { |
| 2076 | z.v( j ) = initial->v( j ) * emission->v( j, y.v( 0 ) ); |
| 2077 | c.v( 0 ) += z.v( j ); |
| 2078 | } |
| 2079 | for( j = 0; j < N; j++ ) |
| 2080 | phi.v( 0, j ) = z.v( j ) / c.v( 0 ); |
| 2081 | for( t = 1; t < n; t++ ) |
| 2082 | { |
| 2083 | c.v( t ) = 0; |
| 2084 | for( j = 0; j < N; j++ ) |
| 2085 | { |
| 2086 | z.v( j ) = 0; |
| 2087 | for( i = 0; i < N; i++ ) |
| 2088 | z.v( j ) += phi.v( t - 1, i ) * transition->v( i, j ) * emission->v( j, y.v( t ) ); |
| 2089 | c.v( t ) += z.v( j ); |
| 2090 | } |
| 2091 | for( j = 0; j < N; j++ ) |
| 2092 | phi.v( t, j ) = z.v( j ) / c.v( t ); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | void smoother( ChaiVectorFast<t_CKINT> & y, ChaiVectorFast<t_CKFLOAT> & c, ChaiMatrixFast<t_CKFLOAT> & beta ) |
| 2097 | { |
| 2098 | t_CKINT i, j, t, n = y.size(), N = emission->xDim(); // M = emission->yDim(); |
| 2099 | for( j = 0; j < N; j++ ) |
| 2100 | beta.v( n - 1, j ) = 1; |
| 2101 | for( t = n - 2; t >= 0; t-- ) |
| 2102 | for( i = 0; i < N; i++ ) |
| 2103 | { |
| 2104 | beta.v( t, i ) = 0; |
| 2105 | for( j = 0; j < N; j++ ) |
| 2106 | beta.v( t, i ) += transition->v( i, j ) * emission->v( j, y.v( t + 1 ) ) * beta.v( t + 1, j ); |
| 2107 | beta.v( t, i ) /= c.v( t + 1 ); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | void randomTransitionKernel() |
| 2112 | { |
| 2113 | t_CKINT i, j, N = transition->xDim(); |
| 2114 | t_CKFLOAT s; |
| 2115 | for( i = 0; i < N; i++ ) |
| 2116 | { |
| 2117 | s = 0; |
| 2118 | for( j = 0; j < N; j++ ) |
| 2119 | { |
| 2120 | transition->v( i, j ) = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2121 | s += transition->v( i, j ); |
| 2122 | } |
| 2123 | for( j = 0; j < N; j++ ) |
| 2124 | { |
| 2125 | transition->v( i, j ) /= s; |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | void randomEmissionKernel() |
| 2131 | { |
| 2132 | t_CKINT i, j, N = emission->xDim(), M = emission->yDim(); |
| 2133 | t_CKFLOAT s; |
| 2134 | for( i = 0; i < N; i++ ) |
| 2135 | { |
| 2136 | s = 0; |
| 2137 | for( j = 0; j < M; j++ ) |
| 2138 | { |
| 2139 | emission->v( i, j ) = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2140 | s += emission->v( i, j ); |
| 2141 | } |
| 2142 | for( j = 0; j < M; j++ ) |
| 2143 | { |
| 2144 | emission->v( i, j ) /= s; |
| 2145 | } |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | // train |
| 2150 | t_CKINT train( t_CKINT n_state, t_CKINT n_emission, Chuck_ArrayInt & observations ) |
| 2151 | { |
| 2152 | clear(); |
| 2153 | // check |
| 2154 | for( t_CKINT i = 0; i < observations.size(); i++ ) { |
| 2155 | if( observations.m_vector[i] >= n_emission ) { |
| 2156 | EM_error3( "HMM::train() -- observation[%ld] '%ld' exceeds number of emissions '%d'...", i, observations.m_vector[i], n_emission ); |
| 2157 | return FALSE; |
| 2158 | } |
| 2159 | } |
| 2160 | // init |
| 2161 | initial = new ChaiVectorFast<t_CKFLOAT>( n_state ); |
| 2162 | transition = new ChaiMatrixFast<t_CKFLOAT>( n_state, n_state ); |
| 2163 | emission = new ChaiMatrixFast<t_CKFLOAT>( n_state, n_emission ); |
| 2164 | // train |
| 2165 | t_CKINT maxIt = 100; |
| 2166 | t_CKFLOAT tol = 1e-4; |
| 2167 | t_CKINT i, j, it, t, n = observations.size(), N = n_state, M = n_emission; |
| 2168 | t_CKFLOAT z, l = 0, change = tol + 1; |
| 2169 | initial->v( 0 ) = 1; |
| 2170 | for( i = 1; i < N; i++ ) |
| 2171 | initial->v( i ) = 0; |
| 2172 | randomTransitionKernel(); |
| 2173 | randomEmissionKernel(); |
| 2174 | ChaiMatrixFast<t_CKFLOAT> phi( n, N ); |
| 2175 | ChaiMatrixFast<t_CKFLOAT> beta( n, N ); |
| 2176 | ChaiVectorFast<t_CKFLOAT> c( n ); |
| 2177 | ChaiMatrixFast<t_CKFLOAT> A( N, M ); |
| 2178 | ChaiVectorFast<t_CKFLOAT> s( N ); |
| 2179 | ChaiMatrixFast<t_CKFLOAT> B( N, N ); |
| 2180 | ChaiVectorFast<t_CKFLOAT> s2( N ); |
| 2181 | ChaiVectorFast<t_CKINT> y( n ); |
| 2182 | for( i = 0; i < n; i++ ) |
| 2183 | { |
| 2184 | y.v( i ) = observations.m_vector[i]; |
| 2185 | } |
| 2186 | for( it = 0; ( change > tol ) && ( it < maxIt ); it++ ) |
| 2187 | { |
| 2188 | change = 0; |
| 2189 | filter( y, phi, c ); |
| 2190 | smoother( y, c, beta ); |
| 2191 | for( i = 0; i < N; i++ ) |
| 2192 | { |
| 2193 | s.v( i ) = 0; |
| 2194 | s2.v( i ) = 0; |
| 2195 | for( j = 0; j < M; j++ ) |
| 2196 | A.v( i, j ) = 0; |
| 2197 | for( j = 0; j < N; j++ ) |
| 2198 | B.v( i, j ) = 0; |
| 2199 | } |
| 2200 | for( t = 0; t < n; t++ ) |
| 2201 | for( i = 0; i < N; i++ ) |
| 2202 | { |
| 2203 | z = phi.v( t, i ) * beta.v( t, i ); |
| 2204 | A.v( i, y.v( t ) ) += z; |
| 2205 | s.v( i ) += z; |
| 2206 | } |
| 2207 | for( i = 0; i < N; i++ ) |
| 2208 | for( j = 0; j < M; j++ ) |
| 2209 | { |
| 2210 | change = ck_max( change, fabs( emission->v( i, j ) - A.v( i, j ) / s.v( i ) ) ); |
| 2211 | emission->v( i, j ) = A.v( i, j ) / s.v( i ); |
| 2212 | } |
| 2213 | for( t = 1; t < n; t++ ) |
| 2214 | for( i = 0; i < N; i++ ) |
| 2215 | for( j = 0; j < N; j++ ) |
| 2216 | { |
| 2217 | z = phi.v( t - 1, i ) * transition->v( i, j ) * emission->v( j, y.v( t ) ) * beta.v( t, j ) |
| 2218 | / c.v( t ); |
| 2219 | B.v( i, j ) += z; |
| 2220 | s2.v( i ) += z; |
| 2221 | } |
| 2222 | for( i = 0; i < N; i++ ) |
| 2223 | for( j = 0; j < N; j++ ) |
| 2224 | { |
| 2225 | change = ck_max( change, fabs( transition->v( i, j ) - B.v( i, j ) / s2.v( i ) ) ); |
| 2226 | transition->v( i, j ) = B.v( i, j ) / s2.v( i ); |
| 2227 | } |
| 2228 | |
| 2229 | } |
| 2230 | for( i = 0; i < n; i++ ) |
| 2231 | l += log( c.v( i ) ); |
| 2232 | return TRUE; |
| 2233 | } |
| 2234 | |
| 2235 | // generate |
| 2236 | t_CKINT generate( t_CKINT length, Chuck_ArrayInt & output_ ) |
| 2237 | { |
| 2238 | // check |
| 2239 | if( emission == NULL ) |
| 2240 | { |
| 2241 | EM_error3( "HMM::generate() -- generate() called before successful call to train()..." ); |
| 2242 | return FALSE; |
| 2243 | } |
| 2244 | |
| 2245 | // compute |
| 2246 | t_CKINT state = 0; |
| 2247 | t_CKFLOAT r = 0; |
| 2248 | for( t_CKINT i = 0; i < length; i++ ) |
| 2249 | { |
| 2250 | // state |
| 2251 | if( i == 0 ) |
| 2252 | { |
| 2253 | r = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2254 | for( t_CKINT j = 0; j < initial->size(); j++ ) |
| 2255 | { |
| 2256 | if( r < initial->v( j ) ) |
| 2257 | { |
| 2258 | state = j; |
| 2259 | break; |
| 2260 | } |
| 2261 | r -= initial->v( j ); |
| 2262 | } |
| 2263 | } |
| 2264 | else |
| 2265 | { |
| 2266 | r = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2267 | for( t_CKINT j = 0; j < transition->yDim(); j++ ) |
| 2268 | { |
| 2269 | if( r < transition->v( state, j ) ) |
| 2270 | { |
| 2271 | state = j; |
| 2272 | break; |
| 2273 | } |
| 2274 | r -= transition->v( state, j ); |
| 2275 | } |
| 2276 | } |
| 2277 | // observation |
| 2278 | r = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2279 | for( t_CKINT j = 0; j < emission->yDim(); j++ ) |
| 2280 | { |
| 2281 | if( r < emission->v( state, j ) ) |
| 2282 | { |
| 2283 | output_.set( i, j ); |
| 2284 | break; |
| 2285 | } |
| 2286 | r -= emission->v( state, j ); |
| 2287 | } |
| 2288 | } |
| 2289 | return TRUE; |
| 2290 | } |
| 2291 | |
| 2292 | private: |
| 2293 | ChaiVectorFast<t_CKFLOAT> * initial; |
| 2294 | ChaiMatrixFast<t_CKFLOAT> * transition; |
| 2295 | ChaiMatrixFast<t_CKFLOAT> * emission; |
| 2296 | }; |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | |
| 2301 | //----------------------------------------------------------------------------- |
| 2302 | // HMM c++ hooks |
| 2303 | //----------------------------------------------------------------------------- |
| 2304 | CK_DLL_CTOR( HMM_ctor ) |
| 2305 | { |
| 2306 | HMM_Object * hmm = new HMM_Object(); |
| 2307 | OBJ_MEMBER_UINT( SELF, HMM_offset_data ) = (t_CKUINT)hmm; |
| 2308 | } |
| 2309 | |
| 2310 | CK_DLL_DTOR( HMM_dtor ) |
| 2311 | { |
| 2312 | HMM_Object * hmm = (HMM_Object *)OBJ_MEMBER_UINT( SELF, HMM_offset_data ); |
| 2313 | CK_SAFE_DELETE( hmm ); |
| 2314 | OBJ_MEMBER_UINT( SELF, HMM_offset_data ) = 0; |
| 2315 | } |
| 2316 | |
| 2317 | CK_DLL_MFUN( HMM_init ) |
| 2318 | { |
| 2319 | // get object |
| 2320 | HMM_Object * hmm = (HMM_Object *)OBJ_MEMBER_UINT( SELF, HMM_offset_data ); |
| 2321 | // get args |
| 2322 | Chuck_ArrayFloat * initial = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 2323 | Chuck_ArrayInt * transition = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2324 | Chuck_ArrayInt * emission = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2325 | // init |
| 2326 | hmm->init( *initial, *transition, *emission ); |
| 2327 | } |
| 2328 | |
| 2329 | CK_DLL_MFUN( HMM_train ) |
| 2330 | { |
| 2331 | // get object |
| 2332 | HMM_Object * hmm = (HMM_Object *)OBJ_MEMBER_UINT( SELF, HMM_offset_data ); |
| 2333 | // get args |
| 2334 | t_CKINT n_state = GET_NEXT_INT( ARGS ); |
| 2335 | t_CKINT n_emission = GET_NEXT_INT( ARGS ); |
| 2336 | Chuck_ArrayInt * observations = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2337 | // train |
| 2338 | hmm->train( n_state, n_emission, *observations ); |
| 2339 | } |
| 2340 | |
| 2341 | CK_DLL_MFUN( HMM_generate ) |
| 2342 | { |
| 2343 | // get object |
| 2344 | HMM_Object * hmm = (HMM_Object *)OBJ_MEMBER_UINT( SELF, HMM_offset_data ); |
| 2345 | // get args |
| 2346 | t_CKINT length = GET_NEXT_INT( ARGS ); |
| 2347 | Chuck_ArrayInt * output = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2348 | // generate |
| 2349 | hmm->generate( length, *output ); |
| 2350 | } |
| 2351 | |
| 2352 | //----------------------------------------------------------------------------- |
| 2353 | // name: struct W2V_Dictionary |
| 2354 | // desc: struct for caching Word2Vec | added 1.5.0.0 (ge) |
| 2355 | //----------------------------------------------------------------------------- |
| 2356 | struct W2V_Dictionary |
| 2357 | { |
| 2358 | t_CKINT dictionarySize, vectorLength; |
| 2359 | std::map<std::string, t_CKUINT> * key_to_index; |
| 2360 | ChaiVectorFast<std::string> * index_to_key; |
| 2361 | ChaiMatrixFast<t_CKFLOAT> * word_vectors; |
| 2362 | struct kdtree * tree; |
| 2363 | // bounds for each dimension |
| 2364 | ChaiVectorFast<t_CKFLOAT> mins; |
| 2365 | ChaiVectorFast<t_CKFLOAT> maxs; |
| 2366 | |
| 2367 | W2V_Dictionary() |
| 2368 | : dictionarySize( 0 ), |
| 2369 | vectorLength( 0 ), |
| 2370 | key_to_index( NULL ), |
| 2371 | index_to_key( NULL ), |
| 2372 | word_vectors( NULL ), |
| 2373 | tree( NULL ) |
| 2374 | { } |
| 2375 | |
| 2376 | // clear |
| 2377 | void clear() |
| 2378 | { |
| 2379 | dictionarySize = 0; |
| 2380 | vectorLength = 0; |
| 2381 | CK_SAFE_DELETE( key_to_index ); |
| 2382 | CK_SAFE_DELETE( index_to_key ); |
| 2383 | CK_SAFE_DELETE( word_vectors ); |
| 2384 | kdtree_destroy( tree ); |
| 2385 | } |
| 2386 | }; |
| 2387 | |
| 2388 | //----------------------------------------------------------------------------- |
| 2389 | // name: struct Word2Vec_Object |
| 2390 | // desc: Word2Vec implementation | added 1.5.0.0 (yikai) |
| 2391 | //----------------------------------------------------------------------------- |
| 2392 | struct Word2Vec_Object |
| 2393 | { |
| 2394 | private: |
| 2395 | // static (shared) cache | added (ge) 1.5.0.0 |
| 2396 | static map<string, W2V_Dictionary *> o_cache; |
| 2397 | |
| 2398 | private: |
| 2399 | // pointer to dictionary |
| 2400 | W2V_Dictionary * dictionary; |
| 2401 | // line number for parsing error reporting |
| 2402 | t_CKINT m_lineNum; |
| 2403 | |
| 2404 | public: |
| 2405 | // constructor |
| 2406 | Word2Vec_Object() |
| 2407 | : dictionary( NULL ), m_lineNum( 0 ) |
| 2408 | { } |
| 2409 | |
| 2410 | // destructor |
| 2411 | ~Word2Vec_Object() |
| 2412 | { |
| 2413 | clear(); |
| 2414 | } |
| 2415 | |
| 2416 | // clear |
| 2417 | void clear() |
| 2418 | { |
| 2419 | // just zero the pointer (no delete since we are caching) |
| 2420 | dictionary = NULL; |
| 2421 | // zero out line number |
| 2422 | m_lineNum = 0; |
| 2423 | } |
| 2424 | |
| 2425 | // get dictionary size |
| 2426 | t_CKINT getDictionarySize() const |
| 2427 | { |
| 2428 | return dictionary != NULL ? dictionary->dictionarySize : 0; |
| 2429 | } |
| 2430 | |
| 2431 | // get dictionary dimensions |
| 2432 | t_CKINT getDictionaryDim() const |
| 2433 | { |
| 2434 | return dictionary != NULL ? dictionary->vectorLength : 0; |
| 2435 | } |
| 2436 | |
| 2437 | // get dim mins and maxs |
| 2438 | void getDimMinMaxs( Chuck_ArrayFloat * mins, Chuck_ArrayFloat * maxs ) |
| 2439 | { |
| 2440 | // get dim |
| 2441 | t_CKINT dims = getDictionaryDim(); |
| 2442 | // allocate |
| 2443 | if( dims == 0 ) return; |
| 2444 | |
| 2445 | // if not null |
| 2446 | if( mins != NULL ) |
| 2447 | { |
| 2448 | // set size |
| 2449 | mins->set_size( dims ); |
| 2450 | // copy minimums |
| 2451 | for( int i = 0; i < dims; i++ ) |
| 2452 | { mins->set( i, dictionary->mins.v( i ) ); } |
| 2453 | } |
| 2454 | |
| 2455 | // if not null |
| 2456 | if( maxs != NULL ) |
| 2457 | { |
| 2458 | // set size |
| 2459 | maxs->set_size( dims ); |
| 2460 | // copy minimums |
| 2461 | for( int i = 0; i < dims; i++ ) |
| 2462 | { maxs->set( i, dictionary->maxs.v( i ) ); } |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | // get dictionary dimensions |
| 2467 | t_CKBOOL getUseKDTree() const |
| 2468 | { |
| 2469 | return dictionary != NULL ? dictionary->tree != NULL : 0; |
| 2470 | } |
| 2471 | |
| 2472 | // read next non-empty or commented line |
| 2473 | t_CKBOOL nextline( std::ifstream & fin, string & line, t_CKBOOL commentIsHash ) |
| 2474 | { |
| 2475 | // skip over empty lines and commented lines (starts with #) |
| 2476 | do |
| 2477 | { |
| 2478 | // increment line number |
| 2479 | m_lineNum++; |
| 2480 | // get line |
| 2481 | if( !std::getline( fin, line ) ) |
| 2482 | { |
| 2483 | line = ""; |
| 2484 | return FALSE; |
| 2485 | } |
| 2486 | // ltrim leading white spaces |
| 2487 | line = ltrim( line ); |
| 2488 | } while( line == "" || ( commentIsHash && line[0] == '#' ) ); |
| 2489 | |
| 2490 | return TRUE; |
| 2491 | } |
| 2492 | |
| 2493 | // load |
| 2494 | t_CKINT load( Chuck_String & path, t_CKINT useKDTreeIfDimLEQtoThis ) |
| 2495 | { |
| 2496 | // clear |
| 2497 | clear(); |
| 2498 | |
| 2499 | // check cache |
| 2500 | if( o_cache.find( path.str() ) != o_cache.end() ) |
| 2501 | { |
| 2502 | // retrieve from cache |
| 2503 | dictionary = o_cache[path.str()]; |
| 2504 | // done |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
| 2508 | // open file |
| 2509 | std::ifstream fin( path.str() ); |
| 2510 | if( !fin.is_open() ) |
| 2511 | return false; |
| 2512 | |
| 2513 | // instantiate dictionary |
| 2514 | dictionary = new W2V_Dictionary(); |
| 2515 | // string to hold the line |
| 2516 | string line; |
| 2517 | |
| 2518 | // get next non-empty, non-commented # line |
| 2519 | if( !nextline( fin, line, TRUE ) ) |
| 2520 | { |
| 2521 | EM_error3( "Word2Vec: error parsing model on line: %d", m_lineNum ); |
| 2522 | return false; |
| 2523 | } |
| 2524 | |
| 2525 | // string stream |
| 2526 | istringstream strin( line ); |
| 2527 | // read header |
| 2528 | strin >> dictionary->dictionarySize >> dictionary->vectorLength; |
| 2529 | |
| 2530 | // allocate |
| 2531 | dictionary->key_to_index = new std::map<std::string, t_CKUINT>(); |
| 2532 | dictionary->index_to_key = new ChaiVectorFast<std::string>( dictionary->dictionarySize ); |
| 2533 | dictionary->mins.allocate( dictionary->vectorLength ); |
| 2534 | dictionary->maxs.allocate( dictionary->vectorLength ); |
| 2535 | for( int i = 0; i < dictionary->vectorLength; i++ ) |
| 2536 | { |
| 2537 | // initialize |
| 2538 | dictionary->mins.v( i ) = 1e10; |
| 2539 | dictionary->maxs.v( i ) = -1e10; |
| 2540 | } |
| 2541 | |
| 2542 | // check if using kdtree |
| 2543 | if( useKDTreeIfDimLEQtoThis < 0 || dictionary->vectorLength <= useKDTreeIfDimLEQtoThis ) |
| 2544 | { |
| 2545 | // read vectors and build kd-tree |
| 2546 | dictionary->tree = kdtree_init( dictionary->vectorLength ); |
| 2547 | std::string key; |
| 2548 | t_CKFLOAT value; |
| 2549 | ChaiVectorFast<t_CKFLOAT> vector( dictionary->vectorLength ); |
| 2550 | for( t_CKINT i = 0; i < dictionary->dictionarySize; i++ ) |
| 2551 | { |
| 2552 | // get next non-empty line |
| 2553 | if( !nextline( fin, line, FALSE ) ) |
| 2554 | { |
| 2555 | EM_error3( "Word2Vec: error parsing model on line: %d", m_lineNum ); |
| 2556 | return false; |
| 2557 | } |
| 2558 | // set into string stream |
| 2559 | strin.str( line ); |
| 2560 | strin.clear(); |
| 2561 | |
| 2562 | // get the word |
| 2563 | strin >> key; |
| 2564 | dictionary->index_to_key->v( i ) = key; |
| 2565 | ( *( dictionary->key_to_index ) )[key] = i; |
| 2566 | for( t_CKINT j = 0; j < dictionary->vectorLength; j++ ) |
| 2567 | { |
| 2568 | strin >> value; |
| 2569 | vector.v( j ) = value; |
| 2570 | |
| 2571 | // find min/max |
| 2572 | if( value < dictionary->mins.v( j ) ) dictionary->mins.v( j ) = value; |
| 2573 | else if( value > dictionary->maxs.v( j ) ) dictionary->maxs.v( j ) = value; |
| 2574 | } |
| 2575 | kdtree_insert( dictionary->tree, vector.m_vector ); |
| 2576 | } |
| 2577 | |
| 2578 | // rebuild kdtree |
| 2579 | kdtree_rebuild( dictionary->tree ); |
| 2580 | } |
| 2581 | else |
| 2582 | { |
| 2583 | // allocate word vectors |
| 2584 | dictionary->word_vectors = |
| 2585 | new ChaiMatrixFast<t_CKFLOAT>( dictionary->dictionarySize, dictionary->vectorLength ); |
| 2586 | |
| 2587 | // read vectors |
| 2588 | std::string key; |
| 2589 | t_CKFLOAT value; |
| 2590 | for( t_CKINT i = 0; i < dictionary->dictionarySize; i++ ) |
| 2591 | { |
| 2592 | // get next non-empty line |
| 2593 | if( !nextline( fin, line, FALSE ) ) |
| 2594 | { |
| 2595 | EM_error3( "Word2Vec: error parsing model on line: %d", m_lineNum ); |
| 2596 | return false; |
| 2597 | } |
| 2598 | // set into string stream |
| 2599 | strin.str( line ); |
| 2600 | strin.clear(); |
| 2601 | |
| 2602 | strin >> key; |
| 2603 | dictionary->index_to_key->v( i ) = key; |
| 2604 | ( *( dictionary->key_to_index ) )[key] = i; |
| 2605 | for( t_CKINT j = 0; j < dictionary->vectorLength; j++ ) |
| 2606 | { |
| 2607 | strin >> value; |
| 2608 | dictionary->word_vectors->v( i, j ) = value; |
| 2609 | |
| 2610 | // find min/max |
| 2611 | if( value < dictionary->mins.v( j ) ) dictionary->mins.v( j ) = value; |
| 2612 | else if( value > dictionary->maxs.v( j ) ) dictionary->maxs.v( j ) = value; |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // close file |
| 2618 | fin.close(); |
| 2619 | |
| 2620 | // add to cache |
| 2621 | o_cache[path.str()] = dictionary; |
| 2622 | |
| 2623 | return true; |
| 2624 | } |
| 2625 | |
| 2626 | // get nearest neighbors (brute force) |
| 2627 | void getNearestNeighborsBF( ChaiVectorFast<t_CKFLOAT> & vector, t_CKINT topn, ChaiVectorFast<t_CKINT> & indices ) |
| 2628 | { |
| 2629 | // allocate |
| 2630 | ChaiVectorFast<t_CKFLOAT> top_distances( topn ); |
| 2631 | ChaiVectorFast<t_CKINT> top_indices( topn ); |
| 2632 | // initialize |
| 2633 | for( t_CKINT i = 0; i < topn; i++ ) |
| 2634 | { |
| 2635 | top_distances.v( i ) = 1e10; |
| 2636 | top_indices.v( i ) = 0; |
| 2637 | } |
| 2638 | // compute distances |
| 2639 | t_CKFLOAT distance, diff; |
| 2640 | for( t_CKINT i = 0; i < dictionary->dictionarySize; i++ ) |
| 2641 | { |
| 2642 | distance = 0; |
| 2643 | for( t_CKINT j = 0; j < dictionary->vectorLength; j++ ) |
| 2644 | { |
| 2645 | diff = vector.v( j ) - dictionary->word_vectors->v( i, j ); |
| 2646 | distance += diff * diff; |
| 2647 | } |
| 2648 | for( t_CKINT j = 0; j < topn; j++ ) |
| 2649 | { |
| 2650 | if( distance < top_distances.v( j ) ) |
| 2651 | { |
| 2652 | for( t_CKINT k = topn - 1; k > j; k-- ) |
| 2653 | { |
| 2654 | top_distances.v( k ) = top_distances.v( k - 1 ); |
| 2655 | top_indices.v( k ) = top_indices.v( k - 1 ); |
| 2656 | } |
| 2657 | top_distances.v( j ) = distance; |
| 2658 | top_indices.v( j ) = i; |
| 2659 | break; |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | // copy |
| 2664 | for( t_CKINT i = 0; i < topn; i++ ) |
| 2665 | { |
| 2666 | indices.v( i ) = top_indices.v( i ); |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | // get nearest neighbors |
| 2671 | void getNearestNeighborsKDTree( ChaiVectorFast<t_CKFLOAT> & vector, |
| 2672 | t_CKINT topn, |
| 2673 | ChaiVectorFast<t_CKINT> & indices ) |
| 2674 | { |
| 2675 | kdtree_knn_search( dictionary->tree, vector.m_vector, topn ); |
| 2676 | struct knn_list * p = dictionary->tree->knn_list_head.next; |
| 2677 | t_CKINT i = 0; |
| 2678 | // TODO: check this -- ge: add index check to prevent out of bounds |
| 2679 | while( p != &dictionary->tree->knn_list_head && i < indices.length() ) |
| 2680 | { |
| 2681 | indices.v( i++ ) = p->node->coord_index; |
| 2682 | p = p->next; |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | // get nearest neighbors |
| 2687 | void getNearestNeighbors( ChaiVectorFast<t_CKFLOAT> & vector, t_CKINT topn, ChaiVectorFast<t_CKINT> & indices ) |
| 2688 | { |
| 2689 | // check |
| 2690 | if( dictionary == NULL ) |
| 2691 | { |
| 2692 | EM_error3( "Word2Vec: no model loaded..." ); |
| 2693 | return; |
| 2694 | } |
| 2695 | |
| 2696 | // check if using kdtree |
| 2697 | if( dictionary->tree != NULL ) return getNearestNeighborsKDTree( vector, topn, indices ); |
| 2698 | else return getNearestNeighborsBF( vector, topn, indices ); |
| 2699 | } |
| 2700 | |
| 2701 | // getMostSimilar |
| 2702 | t_CKBOOL getMostSimilarByWord( const string & word, t_CKINT topn, Chuck_ArrayInt & output_ ) |
| 2703 | { |
| 2704 | // error flag |
| 2705 | t_CKBOOL hasError = FALSE; |
| 2706 | // clear |
| 2707 | for( t_CKINT i = 0; i < output_.size(); i++ ) |
| 2708 | { ( (Chuck_String *)output_.m_vector[i] )->set( "" ); } |
| 2709 | |
| 2710 | // check |
| 2711 | if( dictionary == NULL ) |
| 2712 | { |
| 2713 | EM_error3( "Word2Vec: no model loaded..." ); |
| 2714 | return false; |
| 2715 | } |
| 2716 | |
| 2717 | // check |
| 2718 | if( output_.size() < topn ) |
| 2719 | { |
| 2720 | EM_error3( "Word2Vec.getSimilar() results array insufficient size %d (expecting %d)...", |
| 2721 | output_.size(), |
| 2722 | topn ); |
| 2723 | // reduce k and do our best |
| 2724 | topn = output_.size(); |
| 2725 | // but mark error to return false (since we won't fully fulfill our function) |
| 2726 | hasError = TRUE; |
| 2727 | } |
| 2728 | |
| 2729 | // see if word is there |
| 2730 | if( !contains( word ) ) |
| 2731 | { return false; } |
| 2732 | |
| 2733 | // get index |
| 2734 | t_CKINT index = ( *( dictionary->key_to_index ) )[word]; |
| 2735 | // get vector |
| 2736 | ChaiVectorFast<t_CKFLOAT> vector( dictionary->vectorLength ); |
| 2737 | |
| 2738 | // use tree? |
| 2739 | if( dictionary->tree != NULL ) |
| 2740 | { |
| 2741 | for( t_CKINT i = 0; i < dictionary->vectorLength; i++ ) |
| 2742 | vector.v( i ) = dictionary->tree->coord_table[index][i]; |
| 2743 | // get nearest neighbors |
| 2744 | ChaiVectorFast<t_CKINT> top_indices( topn ); |
| 2745 | getNearestNeighbors( vector, topn, top_indices ); |
| 2746 | // copy |
| 2747 | for( t_CKINT i = 0; i < topn; i++ ) |
| 2748 | { |
| 2749 | ( (Chuck_String *)output_.m_vector[i] )->set( dictionary->index_to_key->v( top_indices.v( i ) ) ); |
| 2750 | } |
| 2751 | } |
| 2752 | else |
| 2753 | { |
| 2754 | for( t_CKINT i = 0; i < dictionary->vectorLength; i++ ) |
| 2755 | vector.v( i ) = dictionary->word_vectors->v( index, i ); |
| 2756 | // get nearest neighbors |
| 2757 | ChaiVectorFast<t_CKINT> top_indices( topn ); |
| 2758 | getNearestNeighbors( vector, topn, top_indices ); |
| 2759 | // copy |
| 2760 | for( t_CKINT i = 0; i < topn; i++ ) |
| 2761 | { |
| 2762 | ( (Chuck_String *)output_.m_vector[i] )->set( dictionary->index_to_key->v( top_indices.v( i ) ) ); |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | return !hasError; |
| 2767 | } |
| 2768 | |
| 2769 | // getByVector |
| 2770 | t_CKBOOL getMostSimilarByVector( Chuck_ArrayFloat & vec_, t_CKINT topn, Chuck_ArrayInt & output_ ) |
| 2771 | { |
| 2772 | // error flag |
| 2773 | t_CKBOOL hasError = FALSE; |
| 2774 | // clear |
| 2775 | for( t_CKINT i = 0; i < output_.size(); i++ ) |
| 2776 | { ( (Chuck_String *)output_.m_vector[i] )->set( "" ); } |
| 2777 | |
| 2778 | // check |
| 2779 | if( dictionary == NULL ) |
| 2780 | { |
| 2781 | EM_error3( "Word2Vec: no model loaded..." ); |
| 2782 | return false; |
| 2783 | } |
| 2784 | |
| 2785 | // check |
| 2786 | if( output_.size() < topn ) |
| 2787 | { |
| 2788 | EM_error3( "Word2Vec.getSimilar() results array insufficient size %d (expecting %d)...", |
| 2789 | output_.size(), |
| 2790 | topn ); |
| 2791 | // reduce k and do our best |
| 2792 | topn = output_.size(); |
| 2793 | // but mark error to return false (since we won't fully fulfill our function) |
| 2794 | hasError = TRUE; |
| 2795 | } |
| 2796 | |
| 2797 | // get vector |
| 2798 | ChaiVectorFast<t_CKFLOAT> vector( dictionary->vectorLength ); |
| 2799 | for( t_CKINT i = 0; i < dictionary->vectorLength; i++ ) |
| 2800 | vector.v( i ) = vec_.m_vector[i]; |
| 2801 | // get nearest neighbors |
| 2802 | ChaiVectorFast<t_CKINT> top_indices( topn ); |
| 2803 | getNearestNeighbors( vector, topn, top_indices ); |
| 2804 | // copy |
| 2805 | for( t_CKINT i = 0; i < topn; i++ ) |
| 2806 | { |
| 2807 | ( (Chuck_String *)output_.m_vector[i] )->set( dictionary->index_to_key->v( top_indices.v( i ) ) ); |
| 2808 | } |
| 2809 | |
| 2810 | return !hasError; |
| 2811 | } |
| 2812 | |
| 2813 | // contains |
| 2814 | t_CKBOOL contains( const string & word ) |
| 2815 | { |
| 2816 | // see if word is there |
| 2817 | std::map<string, t_CKUINT> & key2index = *( dictionary->key_to_index ); |
| 2818 | return key2index.find( word ) != key2index.end(); |
| 2819 | } |
| 2820 | |
| 2821 | // getVector |
| 2822 | t_CKBOOL getVector( const string & word, Chuck_ArrayFloat & output_ ) |
| 2823 | { |
| 2824 | // check |
| 2825 | if( dictionary == NULL ) |
| 2826 | { |
| 2827 | EM_error3( "Word2Vec: no model loaded..." ); |
| 2828 | return false; |
| 2829 | } |
| 2830 | |
| 2831 | // ensure length |
| 2832 | if( output_.size() < dictionary->vectorLength ) |
| 2833 | { output_.set_size( dictionary->vectorLength ); } |
| 2834 | // zero out |
| 2835 | output_.zero( 0, output_.size() ); |
| 2836 | // see if word is there |
| 2837 | if( !contains( word ) ) |
| 2838 | { return false; } |
| 2839 | |
| 2840 | // get index |
| 2841 | std::map<string, t_CKUINT> & key2index = *( dictionary->key_to_index ); |
| 2842 | t_CKINT index = key2index[word]; |
| 2843 | |
| 2844 | // whether we are using |
| 2845 | if( dictionary->tree != NULL ) |
| 2846 | { |
| 2847 | // copy |
| 2848 | for( t_CKINT i = 0; i < dictionary->vectorLength; i++ ) |
| 2849 | { |
| 2850 | output_.m_vector[i] = dictionary->tree->coord_table[index][i]; |
| 2851 | } |
| 2852 | } |
| 2853 | else |
| 2854 | { |
| 2855 | // copy |
| 2856 | for( t_CKINT i = 0; i < dictionary->vectorLength; i++ ) |
| 2857 | { |
| 2858 | output_.m_vector[i] = dictionary->word_vectors->v( index, i ); |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | // done |
| 2863 | return true; |
| 2864 | } |
| 2865 | }; |
| 2866 | |
| 2867 | // static instantiation | added (ge) 1.5.0.0 |
| 2868 | map<string, W2V_Dictionary *> Word2Vec_Object::o_cache; |
| 2869 | |
| 2870 | |
| 2871 | |
| 2872 | |
| 2873 | //----------------------------------------------------------------------------- |
| 2874 | // Word2Vec c++ hooks |
| 2875 | //----------------------------------------------------------------------------- |
| 2876 | CK_DLL_CTOR( Word2Vec_ctor ) |
| 2877 | { |
| 2878 | Word2Vec_Object * word2vec = new Word2Vec_Object(); |
| 2879 | OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ) = (t_CKUINT)word2vec; |
| 2880 | } |
| 2881 | |
| 2882 | CK_DLL_DTOR( Word2Vec_dtor ) |
| 2883 | { |
| 2884 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2885 | CK_SAFE_DELETE( word2vec ); |
| 2886 | OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ) = 0; |
| 2887 | } |
| 2888 | |
| 2889 | CK_DLL_MFUN( Word2Vec_load ) |
| 2890 | { |
| 2891 | // get object |
| 2892 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2893 | // get args |
| 2894 | Chuck_String * path = GET_NEXT_STRING( ARGS ); |
| 2895 | // load |
| 2896 | RETURN->v_int = word2vec->load( *path, 8 ); |
| 2897 | } |
| 2898 | |
| 2899 | CK_DLL_MFUN( Word2Vec_load2 ) |
| 2900 | { |
| 2901 | // get object |
| 2902 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2903 | // get args |
| 2904 | Chuck_String * path = GET_NEXT_STRING( ARGS ); |
| 2905 | t_CKINT useKDTreeDim = GET_NEXT_INT( ARGS ); |
| 2906 | // load |
| 2907 | RETURN->v_int = word2vec->load( *path, useKDTreeDim ); |
| 2908 | } |
| 2909 | |
| 2910 | CK_DLL_MFUN( Word2Vec_getMostSimilarByWord ) |
| 2911 | { |
| 2912 | // get object |
| 2913 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2914 | // get args |
| 2915 | Chuck_String * word = GET_NEXT_STRING( ARGS ); |
| 2916 | t_CKINT topn = GET_NEXT_INT( ARGS ); |
| 2917 | Chuck_ArrayInt * output = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2918 | |
| 2919 | // check for NULL |
| 2920 | if( word == NULL || output == NULL ) |
| 2921 | { |
| 2922 | RETURN->v_int = FALSE; |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | // get by word |
| 2927 | RETURN->v_int = word2vec->getMostSimilarByWord( word->str(), topn, *output ); |
| 2928 | } |
| 2929 | |
| 2930 | CK_DLL_MFUN( Word2Vec_getMostSimilarByVector ) |
| 2931 | { |
| 2932 | // get object |
| 2933 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2934 | // get args |
| 2935 | Chuck_ArrayFloat * vec = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 2936 | t_CKINT topn = GET_NEXT_INT( ARGS ); |
| 2937 | Chuck_ArrayInt * output = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 2938 | |
| 2939 | // check for NULL |
| 2940 | if( vec == NULL || output == NULL ) |
| 2941 | { |
| 2942 | RETURN->v_int = FALSE; |
| 2943 | return; |
| 2944 | } |
| 2945 | |
| 2946 | // get by vector |
| 2947 | RETURN->v_int = word2vec->getMostSimilarByVector( *vec, topn, *output ); |
| 2948 | } |
| 2949 | |
| 2950 | CK_DLL_MFUN( Word2Vec_getVector ) |
| 2951 | { |
| 2952 | // get object |
| 2953 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2954 | // get args |
| 2955 | Chuck_String * word = GET_NEXT_STRING( ARGS ); |
| 2956 | Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 2957 | |
| 2958 | // check for NULL |
| 2959 | if( word == NULL || output == NULL ) |
| 2960 | { |
| 2961 | RETURN->v_int = FALSE; |
| 2962 | return; |
| 2963 | } |
| 2964 | |
| 2965 | // getVector |
| 2966 | RETURN->v_int = word2vec->getVector( word->str(), *output ); |
| 2967 | } |
| 2968 | |
| 2969 | CK_DLL_MFUN( Word2Vec_contains ) |
| 2970 | { |
| 2971 | // get object |
| 2972 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2973 | // get args |
| 2974 | Chuck_String * word = GET_NEXT_STRING( ARGS ); |
| 2975 | |
| 2976 | // check for NULL |
| 2977 | if( word == NULL ) |
| 2978 | { |
| 2979 | RETURN->v_int = FALSE; |
| 2980 | return; |
| 2981 | } |
| 2982 | |
| 2983 | // contains |
| 2984 | RETURN->v_int = word2vec->contains( word->str() ); |
| 2985 | } |
| 2986 | |
| 2987 | CK_DLL_MFUN( Word2Vec_getDictionarySize ) |
| 2988 | { |
| 2989 | // get object |
| 2990 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2991 | // get size |
| 2992 | RETURN->v_int = word2vec->getDictionarySize(); |
| 2993 | } |
| 2994 | |
| 2995 | CK_DLL_MFUN( Word2Vec_getDictionaryDim ) |
| 2996 | { |
| 2997 | // get object |
| 2998 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 2999 | // get dim |
| 3000 | RETURN->v_int = word2vec->getDictionaryDim(); |
| 3001 | } |
| 3002 | |
| 3003 | CK_DLL_MFUN( Word2Vec_getDimMinMax ) |
| 3004 | { |
| 3005 | // get object |
| 3006 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 3007 | // get chuck arrays |
| 3008 | Chuck_ArrayFloat * mins = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3009 | Chuck_ArrayFloat * maxs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3010 | |
| 3011 | // get it |
| 3012 | word2vec->getDimMinMaxs( mins, maxs ); |
| 3013 | } |
| 3014 | |
| 3015 | CK_DLL_MFUN( Word2Vec_getUseKDTree ) |
| 3016 | { |
| 3017 | // get object |
| 3018 | Word2Vec_Object * word2vec = (Word2Vec_Object *)OBJ_MEMBER_UINT( SELF, Word2Vec_offset_data ); |
| 3019 | // get dim |
| 3020 | RETURN->v_int = word2vec->getUseKDTree(); |
| 3021 | } |
| 3022 | |
| 3023 | //----------------------------------------------------------------------------- |
| 3024 | // name: struct PCA_Object |
| 3025 | // desc: Principal Component Analysis Implementation | added 1.5.0.0 (yikai) |
| 3026 | //----------------------------------------------------------------------------- |
| 3027 | struct PCA_Object |
| 3028 | { |
| 3029 | public: |
| 3030 | // constructor |
| 3031 | PCA_Object() |
| 3032 | { } |
| 3033 | |
| 3034 | // destructor |
| 3035 | ~PCA_Object() |
| 3036 | { } |
| 3037 | |
| 3038 | /* Reduce a real, symmetric matrix to a symmetric, tridiag. matrix. */ |
| 3039 | static void tred2( ChaiMatrixFast<t_CKFLOAT> & a, |
| 3040 | t_CKINT m, |
| 3041 | ChaiVectorFast<t_CKFLOAT> & d, |
| 3042 | ChaiVectorFast<t_CKFLOAT> & e ) |
| 3043 | /* Householder reductiom of matrix a to tridiagomal form. |
| 3044 | Algorithm: Martim et al., Num. Math. 11, 181-195, 1968. |
| 3045 | Ref: Smith et al., Matrix Eigemsystem Routimes -- EISPACK Guide |
| 3046 | Sprimger-Verlag, 1976, pp. 489-494. |
| 3047 | W H Press et al., Numerical Recipes im C, Cambridge U P, |
| 3048 | 1988, pp. 373-374. |
| 3049 | Source code adapted from F. Murtagh, Munich, 6 June 1989 |
| 3050 | http://astro.u-strasbg.fr/~fmurtagh/mda-sw/pca.c |
| 3051 | */ |
| 3052 | { |
| 3053 | t_CKINT l, k, j, i; |
| 3054 | t_CKFLOAT scale, hh, h, g, f; |
| 3055 | |
| 3056 | for( i = m - 1; i > 0; i-- ) |
| 3057 | { |
| 3058 | l = i - 1; |
| 3059 | h = scale = 0.0; |
| 3060 | if( l > 0 ) |
| 3061 | { |
| 3062 | for( k = 0; k <= l; k++ ) |
| 3063 | scale += fabs( a( i, k ) ); |
| 3064 | if( scale == 0.0 ) |
| 3065 | e[i] = a( i, l ); |
| 3066 | else |
| 3067 | { |
| 3068 | for( k = 0; k <= l; k++ ) |
| 3069 | { |
| 3070 | a( i, k ) /= scale; |
| 3071 | h += a( i, k ) * a( i, k ); |
| 3072 | } |
| 3073 | f = a( i, l ); |
| 3074 | g = f > 0 ? -sqrt( h ) : sqrt( h ); |
| 3075 | e[i] = scale * g; |
| 3076 | |
| 3077 | h -= f * g; |
| 3078 | a( i, l ) = f - g; |
| 3079 | f = 0.0; |
| 3080 | for( j = 0; j <= l; j++ ) |
| 3081 | { |
| 3082 | a( j, i ) = a( i, j ) / h; |
| 3083 | g = 0.0; |
| 3084 | for( k = 0; k <= j; k++ ) |
| 3085 | g += a( j, k ) * a( i, k ); |
| 3086 | for( k = j + 1; k <= l; k++ ) |
| 3087 | g += a( k, j ) * a( i, k ); |
| 3088 | e[j] = g / h; |
| 3089 | f += e[j] * a( i, j ); |
| 3090 | } |
| 3091 | hh = f / ( h + h ); |
| 3092 | for( j = 0; j <= l; j++ ) |
| 3093 | { |
| 3094 | f = a( i, j ); |
| 3095 | e[j] = g = e[j] - hh * f; |
| 3096 | for( k = 0; k <= j; k++ ) |
| 3097 | a( j, k ) -= ( f * e[k] + g * a( i, k ) ); |
| 3098 | } |
| 3099 | } |
| 3100 | } |
| 3101 | else |
| 3102 | e[i] = a( i, l ); |
| 3103 | d[i] = h; |
| 3104 | } |
| 3105 | d[0] = 0.0; |
| 3106 | e[0] = 0.0; |
| 3107 | for( i = 0; i < m; ++i ) |
| 3108 | { |
| 3109 | l = i - 1; |
| 3110 | if( d[i] ) |
| 3111 | { |
| 3112 | for( j = 0; j <= l; j++ ) |
| 3113 | { |
| 3114 | g = 0.0; |
| 3115 | for( k = 0; k <= l; k++ ) |
| 3116 | g += a( i, k ) * a( k, j ); |
| 3117 | for( k = 0; k <= l; k++ ) |
| 3118 | a( k, j ) -= g * a( k, i ); |
| 3119 | } |
| 3120 | } |
| 3121 | d[i] = a( i, i ); |
| 3122 | a( i, i ) = 1.0; |
| 3123 | |
| 3124 | for( j = 0; j <= l; j++ ) |
| 3125 | a( j, i ) = a( i, j ) = 0.0; |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | /* Tridiagonal QL algorithm -- Implicit */ |
| 3130 | static void tqli( ChaiVectorFast<t_CKFLOAT> & d, |
| 3131 | ChaiVectorFast<t_CKFLOAT> & e, |
| 3132 | t_CKINT m, |
| 3133 | ChaiMatrixFast<t_CKFLOAT> & z ) |
| 3134 | /* |
| 3135 | Source code adapted from F. Murtagh, Munich, 6 June 1989 |
| 3136 | http://astro.u-strasbg.fr/~fmurtagh/mda-sw/pca.c |
| 3137 | */ |
| 3138 | { |
| 3139 | t_CKINT n, l, i, k; |
| 3140 | t_CKFLOAT s, r, p, g, f, dd, c, b; |
| 3141 | |
| 3142 | for( i = 1; i < m; ++i ) |
| 3143 | e[i - 1] = e[i]; |
| 3144 | e[m - 1] = 0.0; |
| 3145 | for( l = 0; l < m; l++ ) |
| 3146 | { |
| 3147 | do |
| 3148 | { |
| 3149 | for( n = l; n < m - 1; n++ ) |
| 3150 | { |
| 3151 | dd = fabs( d[n] ) + fabs( d[n + 1] ); |
| 3152 | if( fabs( e[n] ) + dd == dd ) break; |
| 3153 | } |
| 3154 | if( n != l ) |
| 3155 | { |
| 3156 | g = ( d[l + 1] - d[l] ) / ( 2.0 * e[l] ); |
| 3157 | r = sqrt( ( g * g ) + 1.0 ); |
| 3158 | g = d[n] - d[l] + e[l] / ( g + ( ( g ) < 0 ? -fabs( r ) : fabs( r ) ) ); |
| 3159 | s = c = 1.0; |
| 3160 | p = 0.0; |
| 3161 | for( i = n - 1; i >= l; i-- ) |
| 3162 | { |
| 3163 | f = s * e[i]; |
| 3164 | b = c * e[i]; |
| 3165 | if( fabs( f ) >= fabs( g ) ) |
| 3166 | { |
| 3167 | c = g / f; |
| 3168 | r = sqrt( ( c * c ) + 1.0 ); |
| 3169 | e[i + 1] = f * r; |
| 3170 | c *= ( s = 1.0 / r ); |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | s = f / g; |
| 3175 | r = sqrt( ( s * s ) + 1.0 ); |
| 3176 | e[i + 1] = g * r; |
| 3177 | s *= ( c = 1.0 / r ); |
| 3178 | } |
| 3179 | g = d[i + 1] - p; |
| 3180 | r = ( d[i] - g ) * s + 2.0 * c * b; |
| 3181 | p = s * r; |
| 3182 | d[i + 1] = g + p; |
| 3183 | g = c * r - b; |
| 3184 | for( k = 0; k < m; k++ ) |
| 3185 | { |
| 3186 | f = z( k, i + 1 ); |
| 3187 | z( k, i + 1 ) = s * z( k, i ) + c * f; |
| 3188 | z( k, i ) = c * z( k, i ) - s * f; |
| 3189 | } |
| 3190 | } |
| 3191 | d[l] = d[l] - p; |
| 3192 | e[l] = g; |
| 3193 | e[n] = 0.0; |
| 3194 | } |
| 3195 | } while( n != l ); |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | // transform |
| 3200 | static void transform( Chuck_ArrayInt & input_, t_CKINT npc, Chuck_ArrayInt & output_ ) |
| 3201 | { |
| 3202 | ChaiMatrixFast<t_CKFLOAT> * input = chuck2chai( input_ ); |
| 3203 | ChaiMatrixFast<t_CKFLOAT> * output = chuck2chai( output_ ); |
| 3204 | |
| 3205 | t_CKINT t, o; |
| 3206 | t_CKINT o1, o2; |
| 3207 | t_CKINT n = input->xDim(), d = input->yDim(), dd = output->yDim(); |
| 3208 | |
| 3209 | // Check output dimension |
| 3210 | if( npc > dd ) |
| 3211 | { |
| 3212 | EM_error3( "PCA: number of principal components %d exceeds output dimension %d", npc, dd ); |
| 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | ChaiMatrixFast<t_CKFLOAT> temp_matrix( d, n ); |
| 3217 | for( t = 0; t < n; t++ ) |
| 3218 | { |
| 3219 | for( o = 0; o < d; o++ ) |
| 3220 | { |
| 3221 | temp_matrix( o, t ) = input->v( t, o ); |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | ChaiVectorFast<t_CKFLOAT> means( d ); |
| 3226 | ChaiVectorFast<t_CKFLOAT> stds( d ); |
| 3227 | for( o = 0; o < d; o++ ) |
| 3228 | { |
| 3229 | means[o] = 0; |
| 3230 | for( t = 0; t < n; t++ ) |
| 3231 | { |
| 3232 | means[o] += temp_matrix( o, t ); |
| 3233 | } |
| 3234 | means[o] /= n; |
| 3235 | stds[o] = 0; |
| 3236 | for( t = 0; t < n; t++ ) |
| 3237 | { |
| 3238 | stds[o] += pow( temp_matrix( o, t ) - means[o], 2 ); |
| 3239 | } |
| 3240 | stds[o] = sqrt( stds[o] / n ); |
| 3241 | } |
| 3242 | |
| 3243 | // Adjust data : ( X - means(X) ) / ( sqrt(n) * stds(X) ) |
| 3244 | for( o = 0; o < d; o++ ) |
| 3245 | for( t = 0; t < n; t++ ) |
| 3246 | temp_matrix( o, t ) = |
| 3247 | ( temp_matrix( o, t ) - means[o] ) / ( sqrt( (t_CKFLOAT)n ) * stds[o] ); |
| 3248 | |
| 3249 | ChaiMatrixFast<t_CKFLOAT> corr_matrix( d, d ); |
| 3250 | // Calculate the correlation matrix |
| 3251 | for( o1 = 0; o1 < d; o1++ ) |
| 3252 | { |
| 3253 | corr_matrix( o1, o1 ) = 1.0; |
| 3254 | for( o2 = o1; o2 < d; o2++ ) |
| 3255 | { |
| 3256 | corr_matrix( o1, o2 ) = 0.0; |
| 3257 | for( t = 0; t < n; t++ ) |
| 3258 | corr_matrix( o1, o2 ) += temp_matrix( o1, t ) * temp_matrix( o2, t ); |
| 3259 | corr_matrix( o2, o1 ) = corr_matrix( o1, o2 ); |
| 3260 | } |
| 3261 | } |
| 3262 | corr_matrix( d - 1, d - 1 ) = 1.0; |
| 3263 | |
| 3264 | ChaiVectorFast<t_CKFLOAT> evals( d ); |
| 3265 | ChaiVectorFast<t_CKFLOAT> interm( d ); |
| 3266 | |
| 3267 | // Triangular decomposition |
| 3268 | tred2( corr_matrix, d, evals, interm ); |
| 3269 | |
| 3270 | // Reduction of symmetric tridiagonal matrix |
| 3271 | tqli( evals, interm, d, corr_matrix ); |
| 3272 | |
| 3273 | // t_CKFLOAT percent_eig = 0.0; |
| 3274 | // t_CKFLOAT sum_eig = 0.0; |
| 3275 | // for( t_CKINT m = d - 2; m >= 0; m-- ) |
| 3276 | // sum_eig += evals[m]; |
| 3277 | // |
| 3278 | // for( t_CKINT m = d - 2; m >= 0; m-- ) |
| 3279 | // { |
| 3280 | // percent_eig += evals[m]; |
| 3281 | // std::cout << evals[m] / sum_eig << "\t"; |
| 3282 | // std::cout << percent_eig / sum_eig << std::endl; |
| 3283 | // } |
| 3284 | |
| 3285 | /* evals now contains the eigenvalues, |
| 3286 | corr_matrix now contains the associated eigenvectors. */ |
| 3287 | |
| 3288 | /* Project row data onto the top "npc_" principal components. */ |
| 3289 | Chuck_ArrayFloat * vi; |
| 3290 | for( t = 0; t < n; t++ ) |
| 3291 | { |
| 3292 | vi = (Chuck_ArrayFloat *)output_.m_vector[t]; |
| 3293 | for( o = 0; o < npc; o++ ) |
| 3294 | { |
| 3295 | vi->m_vector[o] = 0.0; |
| 3296 | for( o1 = 0; o1 < d; o1++ ) |
| 3297 | vi->m_vector[o] += input->v( t, o1 ) * corr_matrix( o1, d - 1 - o ); |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | // clean up |
| 3302 | CK_SAFE_DELETE( input ); |
| 3303 | } |
| 3304 | |
| 3305 | private: |
| 3306 | }; |
| 3307 | |
| 3308 | //----------------------------------------------------------------------------- |
| 3309 | // PCA c++ hooks |
| 3310 | //----------------------------------------------------------------------------- |
| 3311 | CK_DLL_CTOR( PCA_ctor ) |
| 3312 | { |
| 3313 | PCA_Object * pca = new PCA_Object(); |
| 3314 | OBJ_MEMBER_UINT( SELF, PCA_offset_data ) = (t_CKUINT)pca; |
| 3315 | } |
| 3316 | |
| 3317 | CK_DLL_DTOR( PCA_dtor ) |
| 3318 | { |
| 3319 | PCA_Object * pca = (PCA_Object *)OBJ_MEMBER_UINT( SELF, PCA_offset_data ); |
| 3320 | CK_SAFE_DELETE( pca ); |
| 3321 | OBJ_MEMBER_UINT( SELF, PCA_offset_data ) = 0; |
| 3322 | } |
| 3323 | |
| 3324 | CK_DLL_SFUN( PCA_reduce ) |
| 3325 | { |
| 3326 | // get args |
| 3327 | Chuck_ArrayInt * input = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3328 | t_CKINT n_components = GET_NEXT_INT( ARGS ); |
| 3329 | Chuck_ArrayInt * output = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3330 | // transform |
| 3331 | PCA_Object::transform( *input, n_components, *output ); |
| 3332 | } |
| 3333 | |
| 3334 | //----------------------------------------------------------------------------- |
| 3335 | // name: struct MLP_Object |
| 3336 | // desc: Multilayer Perceptron Implementation | added 1.5.0.0 (yikai) |
| 3337 | //----------------------------------------------------------------------------- |
| 3338 | struct MLP_Object |
| 3339 | { |
| 3340 | private: |
| 3341 | vector<t_CKINT> units_per_layer; |
| 3342 | vector<t_CKINT> activation_per_layer; |
| 3343 | vector<ChaiMatrixFast<t_CKFLOAT> *> weights; |
| 3344 | vector<ChaiVectorFast<t_CKFLOAT> *> biases; |
| 3345 | vector<ChaiVectorFast<t_CKFLOAT> *> activations; |
| 3346 | vector<ChaiVectorFast<t_CKFLOAT> *> gradients; |
| 3347 | public: |
| 3348 | // constructor |
| 3349 | MLP_Object() |
| 3350 | { } |
| 3351 | |
| 3352 | // destructor |
| 3353 | ~MLP_Object() |
| 3354 | { |
| 3355 | clear(); |
| 3356 | } |
| 3357 | |
| 3358 | // clear |
| 3359 | void clear() |
| 3360 | { |
| 3361 | for( t_CKINT i = 0; i < weights.size(); i++ ) |
| 3362 | CK_SAFE_DELETE( weights[i] ); |
| 3363 | for( t_CKINT i = 0; i < biases.size(); i++ ) |
| 3364 | CK_SAFE_DELETE( biases[i] ); |
| 3365 | for( t_CKINT i = 0; i < activations.size(); i++ ) |
| 3366 | CK_SAFE_DELETE( activations[i] ); |
| 3367 | for( t_CKINT i = 0; i < gradients.size(); i++ ) |
| 3368 | CK_SAFE_DELETE( gradients[i] ); |
| 3369 | units_per_layer.clear(); |
| 3370 | activation_per_layer.clear(); |
| 3371 | weights.clear(); |
| 3372 | biases.clear(); |
| 3373 | activations.clear(); |
| 3374 | gradients.clear(); |
| 3375 | } |
| 3376 | |
| 3377 | // init |
| 3378 | bool init( const vector<t_CKUINT> & units_per_layer_ ) |
| 3379 | { |
| 3380 | // clear |
| 3381 | clear(); |
| 3382 | |
| 3383 | // check |
| 3384 | if( units_per_layer_.size() == 0 ) |
| 3385 | { |
| 3386 | EM_error3( "MLP.init(): empty array 'unitsPerLayer'" ); |
| 3387 | return false; |
| 3388 | } |
| 3389 | |
| 3390 | // get units per layer |
| 3391 | for( t_CKINT i = 0; i < units_per_layer_.size(); i++ ) |
| 3392 | units_per_layer.push_back( units_per_layer_[i] ); |
| 3393 | // allocate |
| 3394 | for( t_CKINT i = 0; i < units_per_layer.size(); i++ ) |
| 3395 | { |
| 3396 | activations.push_back( new ChaiVectorFast<t_CKFLOAT>( units_per_layer[i] ) ); |
| 3397 | gradients.push_back( new ChaiVectorFast<t_CKFLOAT>( units_per_layer[i] ) ); |
| 3398 | } |
| 3399 | |
| 3400 | for( t_CKINT i = 0; i < units_per_layer.size() - 1; i++ ) |
| 3401 | { |
| 3402 | weights.push_back( new ChaiMatrixFast<t_CKFLOAT>( units_per_layer[i + 1], units_per_layer[i] ) ); |
| 3403 | biases.push_back( new ChaiVectorFast<t_CKFLOAT>( units_per_layer[i + 1] ) ); |
| 3404 | activation_per_layer.push_back( g_at_sigmoid ); // default to sigmoid... |
| 3405 | } |
| 3406 | // set output layer activation function to linear by default |
| 3407 | activation_per_layer.back() = g_at_linear; |
| 3408 | |
| 3409 | // random |
| 3410 | for( t_CKINT i = 0; i < weights.size(); i++ ) |
| 3411 | for( t_CKINT j = 0; j < weights[i]->xDim(); j++ ) |
| 3412 | for( t_CKINT k = 0; k < weights[i]->yDim(); k++ ) |
| 3413 | weights[i]->v( j, k ) = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 3414 | for( t_CKINT i = 0; i < biases.size(); i++ ) |
| 3415 | for( t_CKINT j = 0; j < biases[i]->size(); j++ ) |
| 3416 | biases[i]->v( j ) = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX; |
| 3417 | |
| 3418 | // done |
| 3419 | return true; |
| 3420 | } |
| 3421 | |
| 3422 | // init2 |
| 3423 | t_CKBOOL init( const vector<t_CKUINT> & units_per_layer_, const vector<t_CKUINT> & activation_per_layer_ ) |
| 3424 | { |
| 3425 | t_CKBOOL retval = init( units_per_layer_ ); |
| 3426 | if( retval ) |
| 3427 | { |
| 3428 | for( t_CKINT i = 0; i < activation_per_layer_.size(); i++ ) |
| 3429 | activation_per_layer.push_back( activation_per_layer_[i] ); |
| 3430 | } |
| 3431 | return retval; |
| 3432 | } |
| 3433 | |
| 3434 | // init3 |
| 3435 | t_CKBOOL init( const vector<t_CKUINT> & units_per_layer_, t_CKINT activation_function_ ) |
| 3436 | { |
| 3437 | t_CKBOOL retval = init( units_per_layer_ ); |
| 3438 | if( retval ) |
| 3439 | { |
| 3440 | for( t_CKINT i = 0; i < units_per_layer_.size(); i++ ) |
| 3441 | activation_per_layer.push_back( activation_function_ ); |
| 3442 | } |
| 3443 | return retval; |
| 3444 | } |
| 3445 | |
| 3446 | // forward |
| 3447 | void forward( ChaiVectorFast<t_CKFLOAT> & input ) |
| 3448 | { |
| 3449 | // sanity check |
| 3450 | if( units_per_layer.size() == 0 ) |
| 3451 | { |
| 3452 | EM_error3( "MLP.forward(): network not initialized" ); |
| 3453 | return; |
| 3454 | } |
| 3455 | |
| 3456 | // copy |
| 3457 | for( t_CKINT i = 0; i < input.size(); i++ ) |
| 3458 | activations[0]->v( i ) = input[i]; |
| 3459 | // forward |
| 3460 | t_CKFLOAT v, sum; |
| 3461 | for( t_CKINT i = 0; i < weights.size(); i++ ) |
| 3462 | { |
| 3463 | for( t_CKINT j = 0; j < weights[i]->xDim(); j++ ) |
| 3464 | { |
| 3465 | v = 0.0; |
| 3466 | for( t_CKINT k = 0; k < weights[i]->yDim(); k++ ) |
| 3467 | v += weights[i]->v( j, k ) * activations[i]->v( k ); |
| 3468 | v += biases[i]->v( j ); |
| 3469 | if( activation_per_layer[i] == g_at_sigmoid ) |
| 3470 | v = 1.0 / ( 1.0 + exp( -v ) ); |
| 3471 | else if( activation_per_layer[i] == g_at_tanh ) |
| 3472 | v = tanh( v ); |
| 3473 | else if( activation_per_layer[i] == g_at_relu ) |
| 3474 | v = v > 0.0 ? v : 0.0; |
| 3475 | else if( activation_per_layer[i] == g_at_softmax ) |
| 3476 | v = exp( v ); |
| 3477 | else if( activation_per_layer[i] == g_at_linear ) |
| 3478 | { /* v = v; */ } |
| 3479 | activations[i + 1]->v( j ) = v; |
| 3480 | } |
| 3481 | if( activation_per_layer[i] == g_at_softmax ) |
| 3482 | { |
| 3483 | sum = 0; |
| 3484 | for( t_CKINT j = 0; j < activations[i + 1]->size(); j++ ) |
| 3485 | sum += activations[i + 1]->v( j ); |
| 3486 | for( t_CKINT j = 0; j < activations[i + 1]->size(); j++ ) |
| 3487 | activations[i + 1]->v( j ) /= sum; |
| 3488 | } |
| 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | // backprop |
| 3493 | void backprop( ChaiVectorFast<t_CKFLOAT> & output, t_CKFLOAT lr ) |
| 3494 | { |
| 3495 | // sanity check |
| 3496 | if( units_per_layer.size() == 0 ) |
| 3497 | { |
| 3498 | EM_error3( "MLP.backprop(): network not initialized" ); |
| 3499 | return; |
| 3500 | } |
| 3501 | // error |
| 3502 | for( t_CKINT i = 0; i < output.size(); i++ ) |
| 3503 | gradients.back()->v( i ) = output[i] - activations.back()->v( i ); |
| 3504 | |
| 3505 | // backprop |
| 3506 | t_CKFLOAT v; |
| 3507 | for( t_CKINT i = weights.size() - 1; i >= 0; i-- ) |
| 3508 | { |
| 3509 | // prev layer |
| 3510 | for( t_CKINT j = 0; j < weights[i]->yDim(); j++ ) |
| 3511 | { |
| 3512 | v = 0.0; |
| 3513 | for( t_CKINT k = 0; k < weights[i]->xDim(); k++ ) |
| 3514 | v += weights[i]->v( k, j ) * gradients[i + 1]->v( k ); |
| 3515 | gradients[i]->v( j ) = v; |
| 3516 | } |
| 3517 | // gradient |
| 3518 | for( t_CKINT j = 0; j < weights[i]->xDim(); j++ ) |
| 3519 | { |
| 3520 | v = activations[i + 1]->v( j ); |
| 3521 | if( activation_per_layer[i] == g_at_sigmoid ) |
| 3522 | v = v * ( 1.0 - v ); |
| 3523 | else if( activation_per_layer[i] == g_at_tanh ) |
| 3524 | v = 1.0 - v * v; |
| 3525 | else if( activation_per_layer[i] == g_at_relu ) |
| 3526 | v = v > 0.0 ? 1.0 : 0.0; |
| 3527 | else if( activation_per_layer[i] == g_at_softmax ) |
| 3528 | v = v * ( 1.0 - v ); |
| 3529 | else if( activation_per_layer[i] == g_at_linear ) |
| 3530 | v = 1.0; |
| 3531 | v = v * gradients[i + 1]->v( j ) * lr; |
| 3532 | for( t_CKINT k = 0; k < weights[i]->yDim(); k++ ) |
| 3533 | weights[i]->v( j, k ) += v * activations[i]->v( k ); |
| 3534 | biases[i]->v( j ) += v; |
| 3535 | gradients[i + 1]->v( j ) = v; |
| 3536 | } |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | // train |
| 3541 | void train( ChaiMatrixFast<t_CKFLOAT> & X, |
| 3542 | ChaiMatrixFast<t_CKFLOAT> & Y, |
| 3543 | t_CKINT size, |
| 3544 | t_CKFLOAT lr, |
| 3545 | t_CKINT epochs ) |
| 3546 | { |
| 3547 | // sanity check |
| 3548 | if( units_per_layer.size() == 0 ) |
| 3549 | { |
| 3550 | EM_error3( "MLP.train(): network not initialized" ); |
| 3551 | return; |
| 3552 | } |
| 3553 | |
| 3554 | ChaiVectorFast<t_CKFLOAT> x( X.yDim() ); |
| 3555 | ChaiVectorFast<t_CKFLOAT> y( Y.yDim() ); |
| 3556 | ChaiVectorFast<t_CKINT> indices( size ); |
| 3557 | for( t_CKINT i = 0; i < size; i++ ) |
| 3558 | indices[i] = i; |
| 3559 | for( t_CKINT i = 0; i < epochs; i++ ) |
| 3560 | { |
| 3561 | indices.shuffle(); |
| 3562 | for( t_CKINT j = 0; j < size; j++ ) |
| 3563 | { |
| 3564 | for( t_CKINT k = 0; k < X.yDim(); k++ ) |
| 3565 | x[k] = X.v( indices[j], k ); |
| 3566 | forward( x ); |
| 3567 | for( t_CKINT k = 0; k < Y.yDim(); k++ ) |
| 3568 | y[k] = Y.v( indices[j], k ); |
| 3569 | backprop( y, lr ); |
| 3570 | } |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | // // train |
| 3575 | // void train( Chuck_ArrayInt & inputs_, Chuck_ArrayInt & outputs_ ) |
| 3576 | // { |
| 3577 | // ChaiMatrixFast<t_CKFLOAT> * X = chuck2chai( inputs_ ); |
| 3578 | // ChaiMatrixFast<t_CKFLOAT> * Y = chuck2chai( outputs_ ); |
| 3579 | // train( *X, *Y, X->xDim(), 1e-3, 100 ); |
| 3580 | // CK_SAFE_DELETE( X ); |
| 3581 | // CK_SAFE_DELETE( Y ); |
| 3582 | // } |
| 3583 | |
| 3584 | // train2 |
| 3585 | void train( Chuck_ArrayInt & inputs_, Chuck_ArrayInt & outputs_, t_CKFLOAT learning_rate, t_CKINT max_iterations ) |
| 3586 | { |
| 3587 | ChaiMatrixFast<t_CKFLOAT> * X = chuck2chai( inputs_ ); |
| 3588 | ChaiMatrixFast<t_CKFLOAT> * Y = chuck2chai( outputs_ ); |
| 3589 | train( *X, *Y, X->xDim(), learning_rate, max_iterations ); |
| 3590 | CK_SAFE_DELETE( X ); |
| 3591 | CK_SAFE_DELETE( Y ); |
| 3592 | } |
| 3593 | |
| 3594 | // predict |
| 3595 | t_CKBOOL predict( Chuck_ArrayFloat & input_, Chuck_ArrayFloat & output_ ) |
| 3596 | { |
| 3597 | // sanity check |
| 3598 | if( units_per_layer.size() == 0 ) |
| 3599 | { |
| 3600 | // error message |
| 3601 | EM_error3( "MLP.predict(): network not initialized" ); |
| 3602 | // zero out |
| 3603 | output_.zero(); |
| 3604 | // done |
| 3605 | return FALSE; |
| 3606 | } |
| 3607 | if( input_.size() != units_per_layer.front() ) |
| 3608 | { |
| 3609 | // error message |
| 3610 | EM_error3( "MLP.predict(): input size mismatch" ); |
| 3611 | // zero out |
| 3612 | output_.zero(); |
| 3613 | // done |
| 3614 | return FALSE; |
| 3615 | } |
| 3616 | |
| 3617 | ChaiVectorFast<t_CKFLOAT> * input = chuck2chai( input_ ); |
| 3618 | forward( *input ); |
| 3619 | output_.set_size( units_per_layer.back() ); |
| 3620 | for( t_CKINT i = 0; i < units_per_layer.back(); i++ ) |
| 3621 | output_.m_vector[i] = activations.back()->v( i ); |
| 3622 | CK_SAFE_DELETE( input ); |
| 3623 | |
| 3624 | return TRUE; |
| 3625 | } |
| 3626 | |
| 3627 | // predict2 |
| 3628 | void predict( ChaiVectorFast<t_CKFLOAT> & input_, ChaiVectorFast<t_CKFLOAT> & output_ ) |
| 3629 | { |
| 3630 | forward( input_ ); |
| 3631 | for( t_CKINT i = 0; i < units_per_layer.back(); i++ ) |
| 3632 | output_.v( i ) = activations.back()->v( i ); |
| 3633 | } |
| 3634 | |
| 3635 | // get_weights |
| 3636 | void get_weights( t_CKINT layer, Chuck_ArrayInt & weights_ ) |
| 3637 | { |
| 3638 | // sanity check |
| 3639 | if( units_per_layer.size() == 0 ) |
| 3640 | { |
| 3641 | // error |
| 3642 | EM_error3( "MLP.getWeights(): network not initialized" ); |
| 3643 | // courtesy clear the array |
| 3644 | Chuck_ArrayFloat * wi = NULL; |
| 3645 | for( t_CKINT i = 0; i < weights_.size(); i++ ) |
| 3646 | { |
| 3647 | wi = (Chuck_ArrayFloat *)weights_.m_vector[i]; |
| 3648 | for( t_CKINT j = 0; j < wi->size(); j++ ) |
| 3649 | wi->m_vector[j] = 0.0; |
| 3650 | } |
| 3651 | return; |
| 3652 | } |
| 3653 | |
| 3654 | Chuck_ArrayFloat * wi = NULL; |
| 3655 | for( t_CKINT i = 0; i < weights_.size(); i++ ) |
| 3656 | { |
| 3657 | wi = (Chuck_ArrayFloat *)weights_.m_vector[i]; |
| 3658 | for( t_CKINT j = 0; j < wi->size(); j++ ) |
| 3659 | wi->m_vector[j] = weights[layer]->v( j, i ); |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | // get_biases |
| 3664 | void get_biases( t_CKINT layer, Chuck_ArrayFloat & biases_ ) |
| 3665 | { |
| 3666 | // sanity check |
| 3667 | if( units_per_layer.size() == 0 ) |
| 3668 | { |
| 3669 | // error |
| 3670 | EM_error3( "MLP.getBiases(): network not initialized" ); |
| 3671 | // clear |
| 3672 | biases_.clear(); |
| 3673 | // done |
| 3674 | return; |
| 3675 | } |
| 3676 | |
| 3677 | biases_.set_size( biases[layer]->size() ); |
| 3678 | for( t_CKINT i = 0; i < biases_.size(); i++ ) |
| 3679 | biases_.m_vector[i] = biases[layer]->v( i ); |
| 3680 | } |
| 3681 | |
| 3682 | // get_gradients |
| 3683 | void get_gradients( t_CKINT layer, Chuck_ArrayFloat & gradients_ ) |
| 3684 | { |
| 3685 | // sanity check |
| 3686 | if( units_per_layer.size() == 0 ) |
| 3687 | { |
| 3688 | // error |
| 3689 | EM_error3( "MLP.getGradients(): network not initialized" ); |
| 3690 | // clear |
| 3691 | gradients_.clear(); |
| 3692 | // done |
| 3693 | return; |
| 3694 | } |
| 3695 | |
| 3696 | gradients_.set_size( gradients[layer]->size() ); |
| 3697 | for( t_CKINT i = 0; i < gradients_.size(); i++ ) |
| 3698 | gradients_.m_vector[i] = gradients[layer]->v( i ); |
| 3699 | } |
| 3700 | |
| 3701 | // get_activations |
| 3702 | void get_activations( t_CKINT layer, Chuck_ArrayFloat & activations_ ) |
| 3703 | { |
| 3704 | // sanity check |
| 3705 | if( units_per_layer.size() == 0 ) |
| 3706 | { |
| 3707 | // error |
| 3708 | EM_error3( "MLP.getActivations(): network not initialized" ); |
| 3709 | // clear |
| 3710 | activations_.clear(); |
| 3711 | // done |
| 3712 | return; |
| 3713 | } |
| 3714 | |
| 3715 | activations_.set_size( activations[layer]->size() ); |
| 3716 | for( t_CKINT i = 0; i < activations_.size(); i++ ) |
| 3717 | activations_.m_vector[i] = activations[layer]->v( i ); |
| 3718 | } |
| 3719 | |
| 3720 | // forward |
| 3721 | void forward( Chuck_ArrayFloat & input_ ) |
| 3722 | { |
| 3723 | ChaiVectorFast<t_CKFLOAT> * input = chuck2chai( input_ ); |
| 3724 | forward( *input ); |
| 3725 | CK_SAFE_DELETE( input ); |
| 3726 | } |
| 3727 | |
| 3728 | // backprop |
| 3729 | void backprop( Chuck_ArrayFloat & output_, t_CKFLOAT learning_rate ) |
| 3730 | { |
| 3731 | ChaiVectorFast<t_CKFLOAT> * output = chuck2chai( output_ ); |
| 3732 | backprop( *output, learning_rate ); |
| 3733 | CK_SAFE_DELETE( output ); |
| 3734 | } |
| 3735 | |
| 3736 | // save |
| 3737 | t_CKBOOL save( ofstream & fout ) |
| 3738 | { |
| 3739 | fout << "# layers" << endl; |
| 3740 | // fout << units_per_layer.size() << endl; |
| 3741 | for( t_CKINT i = 0; i < units_per_layer.size(); i++ ) |
| 3742 | fout << units_per_layer[i] << " "; |
| 3743 | fout << endl; |
| 3744 | |
| 3745 | fout << "# activation functions (0=Linear, 1=Sigmoid, 2=ReLU, 3=Tanh, 4=Softmax)" << endl; |
| 3746 | for( t_CKINT i = 0; i < activation_per_layer.size(); i++ ) |
| 3747 | fout << activation_per_layer[i] << " "; |
| 3748 | fout << endl; |
| 3749 | |
| 3750 | fout << "# weights" << endl; |
| 3751 | for( t_CKINT i = 0; i < weights.size(); i++ ) |
| 3752 | { |
| 3753 | for( t_CKINT j = 0; j < weights[i]->xDim(); j++ ) |
| 3754 | { |
| 3755 | for( t_CKINT k = 0; k < weights[i]->yDim(); k++ ) |
| 3756 | fout << weights[i]->v( j, k ) << " "; |
| 3757 | fout << endl; |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | fout << "# biases" << endl; |
| 3762 | for( t_CKINT i = 0; i < biases.size(); i++ ) |
| 3763 | { |
| 3764 | for( t_CKINT j = 0; j < biases[i]->size(); j++ ) |
| 3765 | fout << biases[i]->v( j ) << " "; |
| 3766 | fout << endl; |
| 3767 | } |
| 3768 | return TRUE; |
| 3769 | } |
| 3770 | |
| 3771 | // save |
| 3772 | t_CKBOOL save( const string & filename ) |
| 3773 | { |
| 3774 | // open file |
| 3775 | ofstream fout( filename.c_str() ); |
| 3776 | // check |
| 3777 | if( !fout.good() ) |
| 3778 | { |
| 3779 | EM_error3( "MLP.save(): cannot open file for output:\n |- %s", filename.c_str() ); |
| 3780 | return FALSE; |
| 3781 | } |
| 3782 | |
| 3783 | return save( fout ); |
| 3784 | } |
| 3785 | |
| 3786 | // load |
| 3787 | t_CKBOOL load( ifstream & fin ) |
| 3788 | { |
| 3789 | string line; |
| 3790 | t_CKINT num; |
| 3791 | |
| 3792 | vector<t_CKUINT> units_per_layer_; |
| 3793 | nextline( fin, line, TRUE ); // get nodes per layer |
| 3794 | istringstream strin( line ); |
| 3795 | |
| 3796 | // tokenize |
| 3797 | while( strin >> num ) |
| 3798 | { |
| 3799 | units_per_layer_.push_back( num ); |
| 3800 | } |
| 3801 | t_CKINT num_layers = units_per_layer_.size(); |
| 3802 | |
| 3803 | // initialize |
| 3804 | if( !init( units_per_layer_ ) ) |
| 3805 | { |
| 3806 | EM_error3( "MLP.load(): cannot initialize model from file" ); |
| 3807 | return FALSE; |
| 3808 | } |
| 3809 | |
| 3810 | strin.clear(); |
| 3811 | nextline( fin, line, TRUE ); |
| 3812 | strin.str( line ); |
| 3813 | for( t_CKINT i = 0; i < num_layers - 1; i++ ) |
| 3814 | strin >> activation_per_layer[i]; |
| 3815 | |
| 3816 | weights.resize( num_layers - 1 ); |
| 3817 | for( t_CKINT i = 0; i < num_layers - 1; i++ ) |
| 3818 | { |
| 3819 | weights[i] = new ChaiMatrixFast<t_CKFLOAT>( units_per_layer[i + 1], units_per_layer[i] ); |
| 3820 | for( t_CKINT j = 0; j < units_per_layer[i + 1]; j++ ) |
| 3821 | { |
| 3822 | strin.clear(); |
| 3823 | nextline( fin, line, TRUE ); |
| 3824 | strin.str( line ); |
| 3825 | for( t_CKINT k = 0; k < units_per_layer[i]; k++ ) |
| 3826 | strin >> weights[i]->v( j, k ); |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | biases.resize( num_layers - 1 ); |
| 3831 | for( t_CKINT i = 0; i < num_layers - 1; i++ ) |
| 3832 | { |
| 3833 | biases[i] = new ChaiVectorFast<t_CKFLOAT>( units_per_layer[i + 1] ); |
| 3834 | strin.clear(); |
| 3835 | nextline( fin, line, TRUE ); |
| 3836 | strin.str( line ); |
| 3837 | for( t_CKINT j = 0; j < units_per_layer[i + 1]; j++ ) |
| 3838 | strin >> biases[i]->v( j ); |
| 3839 | } |
| 3840 | return TRUE; |
| 3841 | } |
| 3842 | |
| 3843 | // load |
| 3844 | t_CKBOOL load( const string & filename ) |
| 3845 | { |
| 3846 | // fin |
| 3847 | ifstream fin( filename.c_str() ); |
| 3848 | // check |
| 3849 | if( !fin.good() ) |
| 3850 | { |
| 3851 | EM_error3( "MLP.load(): cannot open file:\n |- %s", filename.c_str() ); |
| 3852 | return FALSE; |
| 3853 | } |
| 3854 | return load( fin ); |
| 3855 | } |
| 3856 | }; |
| 3857 | |
| 3858 | |
| 3859 | //----------------------------------------------------------------------------- |
| 3860 | // MLP c++ hooks |
| 3861 | //----------------------------------------------------------------------------- |
| 3862 | CK_DLL_CTOR( MLP_ctor ) |
| 3863 | { |
| 3864 | MLP_Object * mlp = new MLP_Object(); |
| 3865 | OBJ_MEMBER_UINT( SELF, MLP_offset_data ) = (t_CKUINT)mlp; |
| 3866 | } |
| 3867 | |
| 3868 | CK_DLL_DTOR( MLP_dtor ) |
| 3869 | { |
| 3870 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3871 | CK_SAFE_DELETE( mlp ); |
| 3872 | OBJ_MEMBER_UINT( SELF, MLP_offset_data ) = 0; |
| 3873 | } |
| 3874 | |
| 3875 | CK_DLL_MFUN( MLP_init ) |
| 3876 | { |
| 3877 | // get object |
| 3878 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3879 | // get args |
| 3880 | Chuck_ArrayInt * units_per_layer = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3881 | // init |
| 3882 | mlp->init( units_per_layer->m_vector ); |
| 3883 | } |
| 3884 | |
| 3885 | CK_DLL_MFUN( MLP_init2 ) |
| 3886 | { |
| 3887 | // get object |
| 3888 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3889 | // get args |
| 3890 | Chuck_ArrayInt * units_per_layer = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3891 | Chuck_ArrayInt * activation_per_layer = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3892 | // init |
| 3893 | mlp->init( units_per_layer->m_vector, activation_per_layer->m_vector ); |
| 3894 | } |
| 3895 | |
| 3896 | CK_DLL_MFUN( MLP_init3 ) |
| 3897 | { |
| 3898 | // get object |
| 3899 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3900 | // get args |
| 3901 | Chuck_ArrayInt * units_per_layer = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3902 | t_CKINT activation_function = GET_NEXT_INT( ARGS ); |
| 3903 | // init |
| 3904 | mlp->init( units_per_layer->m_vector, activation_function ); |
| 3905 | } |
| 3906 | |
| 3907 | CK_DLL_MFUN( MLP_train ) |
| 3908 | { |
| 3909 | // get object |
| 3910 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3911 | // get args |
| 3912 | Chuck_ArrayInt * inputs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3913 | Chuck_ArrayInt * outputs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3914 | // train, with defaults |
| 3915 | mlp->train( *inputs, *outputs, .01, 100 ); |
| 3916 | } |
| 3917 | |
| 3918 | CK_DLL_MFUN( MLP_train2 ) |
| 3919 | { |
| 3920 | // get object |
| 3921 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3922 | // get args |
| 3923 | Chuck_ArrayInt * inputs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3924 | Chuck_ArrayInt * outputs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3925 | t_CKFLOAT learning_rate = GET_NEXT_FLOAT( ARGS ); |
| 3926 | t_CKINT max_iterations = GET_NEXT_INT( ARGS ); |
| 3927 | // train2 |
| 3928 | mlp->train( *inputs, *outputs, learning_rate, max_iterations ); |
| 3929 | } |
| 3930 | |
| 3931 | CK_DLL_MFUN( MLP_predict ) |
| 3932 | { |
| 3933 | // get object |
| 3934 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3935 | // get args |
| 3936 | Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3937 | Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3938 | // predict |
| 3939 | RETURN->v_int = mlp->predict( *input, *output ); |
| 3940 | } |
| 3941 | |
| 3942 | CK_DLL_MFUN( MLP_get_weights ) |
| 3943 | { |
| 3944 | // get object |
| 3945 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3946 | // get args |
| 3947 | t_CKINT layer = GET_NEXT_INT( ARGS ); |
| 3948 | Chuck_ArrayInt * weights = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 3949 | // get_weights |
| 3950 | mlp->get_weights( layer, *weights ); |
| 3951 | } |
| 3952 | |
| 3953 | CK_DLL_MFUN( MLP_get_biases ) |
| 3954 | { |
| 3955 | // get object |
| 3956 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3957 | // get args |
| 3958 | t_CKINT layer = GET_NEXT_INT( ARGS ); |
| 3959 | Chuck_ArrayFloat * biases = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3960 | // get_biases |
| 3961 | mlp->get_biases( layer, *biases ); |
| 3962 | } |
| 3963 | |
| 3964 | CK_DLL_MFUN( MLP_get_gradients ) |
| 3965 | { |
| 3966 | // get object |
| 3967 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3968 | // get args |
| 3969 | t_CKINT layer = GET_NEXT_INT( ARGS ); |
| 3970 | Chuck_ArrayFloat * gradients = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3971 | // get_gradients |
| 3972 | mlp->get_gradients( layer, *gradients ); |
| 3973 | } |
| 3974 | |
| 3975 | CK_DLL_MFUN( MLP_get_activations ) |
| 3976 | { |
| 3977 | // get object |
| 3978 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3979 | // get args |
| 3980 | t_CKINT layer = GET_NEXT_INT( ARGS ); |
| 3981 | Chuck_ArrayFloat * activations = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3982 | // get_activations |
| 3983 | mlp->get_activations( layer, *activations ); |
| 3984 | } |
| 3985 | |
| 3986 | CK_DLL_MFUN( MLP_forward ) |
| 3987 | { |
| 3988 | // get object |
| 3989 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 3990 | // get args |
| 3991 | Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 3992 | // forward |
| 3993 | mlp->forward( *input ); |
| 3994 | } |
| 3995 | |
| 3996 | CK_DLL_MFUN( MLP_backprop ) |
| 3997 | { |
| 3998 | // get object |
| 3999 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 4000 | // get args |
| 4001 | Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 4002 | t_CKFLOAT learning_rate = GET_NEXT_FLOAT( ARGS ); |
| 4003 | // backprop |
| 4004 | mlp->backprop( *output, learning_rate ); |
| 4005 | } |
| 4006 | |
| 4007 | CK_DLL_MFUN( MLP_save ) |
| 4008 | { |
| 4009 | // get object |
| 4010 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 4011 | // get args |
| 4012 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 4013 | // save |
| 4014 | RETURN->v_int = mlp->save( filename->str() ); |
| 4015 | } |
| 4016 | |
| 4017 | CK_DLL_MFUN( MLP_load ) |
| 4018 | { |
| 4019 | // get object |
| 4020 | MLP_Object * mlp = (MLP_Object *)OBJ_MEMBER_UINT( SELF, MLP_offset_data ); |
| 4021 | // get args |
| 4022 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 4023 | // load |
| 4024 | RETURN->v_int = mlp->load( filename->str() ); |
| 4025 | } |
| 4026 | |
| 4027 | CK_DLL_SFUN( MLP_shuffle ) |
| 4028 | { |
| 4029 | // get args |
| 4030 | Chuck_ArrayInt * X = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 4031 | Chuck_ArrayInt * Y = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 4032 | // shuffle |
| 4033 | shuffle( *X, *Y ); |
| 4034 | } |
| 4035 | |
| 4036 | //----------------------------------------------------------------------------- |
| 4037 | // name: struct Wekinator_Model |
| 4038 | // desc: Wekinator model implementation | added 1.5.0.0 (yikai) |
| 4039 | //----------------------------------------------------------------------------- |
| 4040 | struct Wekinator_Model |
| 4041 | { |
| 4042 | public: |
| 4043 | // data |
| 4044 | vector<t_CKINT> example_ids; |
| 4045 | |
| 4046 | // model |
| 4047 | MLP_Object * mlp; |
| 4048 | KNN_Object * knn; |
| 4049 | SVM_Object * svm; |
| 4050 | |
| 4051 | // -- properties -- |
| 4052 | t_CKINT task_type; // AI.Regression, AI.Classification |
| 4053 | t_CKINT model_type; // AI.Regression: AI.MLP, AI.LR; AI.Classification: AI.KNN, AI.SVM, AI.DT |
| 4054 | // AI.Classification |
| 4055 | t_CKINT classification_classes; |
| 4056 | // AI.Regression |
| 4057 | t_CKFLOAT regression_min; |
| 4058 | t_CKFLOAT regression_max; |
| 4059 | t_CKBOOL regression_limit; |
| 4060 | // AI.MLP |
| 4061 | t_CKINT mlp_hidden_layers; |
| 4062 | t_CKINT mlp_nodes_per_hidden_layer; |
| 4063 | t_CKFLOAT mlp_learning_rate; |
| 4064 | t_CKINT mlp_epochs; |
| 4065 | // AI.KNN |
| 4066 | t_CKINT knn_k; |
| 4067 | // system |
| 4068 | t_CKBOOL record_status; |
| 4069 | t_CKBOOL run_status; |
| 4070 | t_CKBOOL train_status; |
| 4071 | vector<t_CKINT> connected_inputs; |
| 4072 | |
| 4073 | public: |
| 4074 | Wekinator_Model() |
| 4075 | { |
| 4076 | // model |
| 4077 | mlp = NULL; |
| 4078 | knn = NULL; |
| 4079 | svm = NULL; |
| 4080 | // -- properties -- |
| 4081 | task_type = g_tt_regression; |
| 4082 | model_type = g_mt_mlp; |
| 4083 | // AI.Classification |
| 4084 | classification_classes = 0; |
| 4085 | // AI.Regression |
| 4086 | regression_min = 0.0; |
| 4087 | regression_max = 1.0; |
| 4088 | regression_limit = FALSE; |
| 4089 | // AI.MLP |
| 4090 | mlp_hidden_layers = 1; |
| 4091 | mlp_nodes_per_hidden_layer = 0; |
| 4092 | mlp_learning_rate = 0.01; |
| 4093 | mlp_epochs = 100; |
| 4094 | // AI.KNN |
| 4095 | knn_k = 1; |
| 4096 | // system |
| 4097 | record_status = TRUE; |
| 4098 | run_status = TRUE; |
| 4099 | train_status = FALSE; |
| 4100 | } |
| 4101 | |
| 4102 | Wekinator_Model( const Wekinator_Model & other ) |
| 4103 | { |
| 4104 | // model |
| 4105 | mlp = NULL; |
| 4106 | knn = NULL; |
| 4107 | svm = NULL; |
| 4108 | // -- properties -- |
| 4109 | task_type = other.task_type; |
| 4110 | model_type = other.model_type; |
| 4111 | // AI.Classification |
| 4112 | classification_classes = other.classification_classes; |
| 4113 | // AI.Regression |
| 4114 | regression_min = other.regression_min; |
| 4115 | regression_max = other.regression_max; |
| 4116 | regression_limit = other.regression_limit; |
| 4117 | // AI.MLP |
| 4118 | mlp_hidden_layers = other.mlp_hidden_layers; |
| 4119 | mlp_nodes_per_hidden_layer = other.mlp_nodes_per_hidden_layer; |
| 4120 | mlp_learning_rate = other.mlp_learning_rate; |
| 4121 | mlp_epochs = other.mlp_epochs; |
| 4122 | // AI.KNN |
| 4123 | knn_k = other.knn_k; |
| 4124 | // system |
| 4125 | record_status = other.record_status; |
| 4126 | run_status = other.run_status; |
| 4127 | train_status = FALSE; |
| 4128 | connected_inputs = other.connected_inputs; |
| 4129 | } |
| 4130 | |
| 4131 | ~Wekinator_Model() |
| 4132 | { |
| 4133 | clear(); |
| 4134 | } |
| 4135 | |
| 4136 | void clear() |
| 4137 | { |
| 4138 | // data |
| 4139 | example_ids.clear(); |
| 4140 | // model |
| 4141 | CK_SAFE_DELETE( mlp ); |
| 4142 | CK_SAFE_DELETE( knn ); |
| 4143 | CK_SAFE_DELETE( svm ); |
| 4144 | // system |
| 4145 | connected_inputs.clear(); |
| 4146 | } |
| 4147 | |
| 4148 | void reset_connected_inputs( t_CKINT num_inputs ) |
| 4149 | { |
| 4150 | connected_inputs.resize( num_inputs ); |
| 4151 | for( t_CKINT i = 0; i < num_inputs; i++ ) |
| 4152 | connected_inputs[i] = i; |
| 4153 | } |
| 4154 | |
| 4155 | void clear_obs( t_CKINT lo, t_CKINT hi ) |
| 4156 | { |
| 4157 | // sanity check |
| 4158 | if( lo > hi ) |
| 4159 | { |
| 4160 | EM_error3( "Wekinator.clear_obs: invalid range: %d, %d", lo, hi ); |
| 4161 | } |
| 4162 | // erase |
| 4163 | t_CKINT start = 0, end = 0; |
| 4164 | for( t_CKINT i = 0; i < example_ids.size(); i++ ) |
| 4165 | { |
| 4166 | if( example_ids[i] < lo ) |
| 4167 | ++start; |
| 4168 | if( example_ids[i] <= hi ) |
| 4169 | ++end; |
| 4170 | } |
| 4171 | if( start < end ) |
| 4172 | example_ids.erase( example_ids.begin() + start, example_ids.begin() + end ); |
| 4173 | } |
| 4174 | |
| 4175 | // save |
| 4176 | void save( ofstream & fout ) |
| 4177 | { |
| 4178 | fout << "# obs_ids" << endl; |
| 4179 | fout << example_ids.size() << endl; |
| 4180 | for( t_CKINT i = 0; i < example_ids.size(); i++ ) |
| 4181 | fout << example_ids[i] << " "; |
| 4182 | fout << endl; |
| 4183 | fout << "# connected_inputs" << endl; |
| 4184 | fout << connected_inputs.size() << endl; |
| 4185 | for( t_CKINT i = 0; i < connected_inputs.size(); i++ ) |
| 4186 | fout << connected_inputs[i] << " "; |
| 4187 | fout << endl; |
| 4188 | |
| 4189 | fout << "# task_type" << endl; |
| 4190 | fout << task_type << endl; |
| 4191 | fout << "# model_type" << endl; |
| 4192 | fout << model_type << endl; |
| 4193 | fout << "# classification_classes" << endl; |
| 4194 | fout << classification_classes << endl; |
| 4195 | fout << "# regression_min" << endl; |
| 4196 | fout << regression_min << endl; |
| 4197 | fout << "# regression_max" << endl; |
| 4198 | fout << regression_max << endl; |
| 4199 | fout << "# regression_limit" << endl; |
| 4200 | fout << regression_limit << endl; |
| 4201 | fout << "# mlp_hidden_layers" << endl; |
| 4202 | fout << mlp_hidden_layers << endl; |
| 4203 | fout << "# mlp_nodes_per_hidden_layer" << endl; |
| 4204 | fout << mlp_nodes_per_hidden_layer << endl; |
| 4205 | fout << "# mlp_learning_rate" << endl; |
| 4206 | fout << mlp_learning_rate << endl; |
| 4207 | fout << "# mlp_epochs" << endl; |
| 4208 | fout << mlp_epochs << endl; |
| 4209 | fout << "# knn_k" << endl; |
| 4210 | fout << knn_k << endl; |
| 4211 | fout << "# record_status" << endl; |
| 4212 | fout << record_status << endl; |
| 4213 | fout << "# run_status" << endl; |
| 4214 | fout << run_status << endl; |
| 4215 | fout << "# train_status" << endl; |
| 4216 | fout << train_status << endl; |
| 4217 | |
| 4218 | if( train_status ) |
| 4219 | { |
| 4220 | if( model_type == g_mt_mlp ) |
| 4221 | { |
| 4222 | fout << "# mlp" << endl; |
| 4223 | mlp->save( fout ); |
| 4224 | } |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | // load |
| 4229 | void load( ifstream & fin ) |
| 4230 | { |
| 4231 | // clear |
| 4232 | clear(); |
| 4233 | |
| 4234 | t_CKINT num; |
| 4235 | string line; |
| 4236 | istringstream strin; |
| 4237 | |
| 4238 | // obs_ids |
| 4239 | nextline( fin, line, TRUE ); |
| 4240 | strin.clear(); |
| 4241 | strin.str( line ); |
| 4242 | strin >> num; |
| 4243 | if (num > 0) |
| 4244 | { |
| 4245 | nextline( fin, line, TRUE ); |
| 4246 | strin.clear(); |
| 4247 | strin.str( line ); |
| 4248 | example_ids.resize( num ); |
| 4249 | for( t_CKINT i = 0; i < num; i++ ) |
| 4250 | strin >> example_ids[i]; |
| 4251 | } |
| 4252 | |
| 4253 | // connected_inputs |
| 4254 | nextline( fin, line, TRUE ); |
| 4255 | strin.clear(); |
| 4256 | strin.str( line ); |
| 4257 | strin >> num; |
| 4258 | if (num > 0) |
| 4259 | { |
| 4260 | nextline( fin, line, TRUE ); |
| 4261 | strin.clear(); |
| 4262 | strin.str( line ); |
| 4263 | connected_inputs.resize( num ); |
| 4264 | for( t_CKINT i = 0; i < num; i++ ) |
| 4265 | strin >> connected_inputs[i]; |
| 4266 | } |
| 4267 | |
| 4268 | // task_type |
| 4269 | nextline( fin, line, TRUE ); |
| 4270 | strin.clear(); |
| 4271 | strin.str( line ); |
| 4272 | strin >> task_type; |
| 4273 | |
| 4274 | // model_type |
| 4275 | nextline( fin, line, TRUE ); |
| 4276 | strin.clear(); |
| 4277 | strin.str( line ); |
| 4278 | strin >> model_type; |
| 4279 | |
| 4280 | // classification_classes |
| 4281 | nextline( fin, line, TRUE ); |
| 4282 | strin.clear(); |
| 4283 | strin.str( line ); |
| 4284 | strin >> classification_classes; |
| 4285 | |
| 4286 | // regression_min |
| 4287 | nextline( fin, line, TRUE ); |
| 4288 | strin.clear(); |
| 4289 | strin.str( line ); |
| 4290 | strin >> regression_min; |
| 4291 | |
| 4292 | // regression_max |
| 4293 | nextline( fin, line, TRUE ); |
| 4294 | strin.clear(); |
| 4295 | strin.str( line ); |
| 4296 | strin >> regression_max; |
| 4297 | |
| 4298 | // regression_limit |
| 4299 | nextline( fin, line, TRUE ); |
| 4300 | strin.clear(); |
| 4301 | strin.str( line ); |
| 4302 | strin >> regression_limit; |
| 4303 | |
| 4304 | // mlp_hidden_layers |
| 4305 | nextline( fin, line, TRUE ); |
| 4306 | strin.clear(); |
| 4307 | strin.str( line ); |
| 4308 | strin >> mlp_hidden_layers; |
| 4309 | |
| 4310 | // mlp_nodes_per_hidden_layer |
| 4311 | nextline( fin, line, TRUE ); |
| 4312 | strin.clear(); |
| 4313 | strin.str( line ); |
| 4314 | strin >> mlp_nodes_per_hidden_layer; |
| 4315 | |
| 4316 | // mlp_learning_rate |
| 4317 | nextline( fin, line, TRUE ); |
| 4318 | strin.clear(); |
| 4319 | strin.str( line ); |
| 4320 | strin >> mlp_learning_rate; |
| 4321 | |
| 4322 | // mlp_epochs |
| 4323 | nextline( fin, line, TRUE ); |
| 4324 | strin.clear(); |
| 4325 | strin.str( line ); |
| 4326 | strin >> mlp_epochs; |
| 4327 | |
| 4328 | // knn_k |
| 4329 | nextline( fin, line, TRUE ); |
| 4330 | strin.clear(); |
| 4331 | strin.str( line ); |
| 4332 | strin >> knn_k; |
| 4333 | |
| 4334 | // record_status |
| 4335 | nextline( fin, line, TRUE ); |
| 4336 | strin.clear(); |
| 4337 | strin.str( line ); |
| 4338 | strin >> record_status; |
| 4339 | |
| 4340 | // run_status |
| 4341 | nextline( fin, line, TRUE ); |
| 4342 | strin.clear(); |
| 4343 | strin.str( line ); |
| 4344 | strin >> run_status; |
| 4345 | |
| 4346 | // train_status |
| 4347 | nextline( fin, line, TRUE ); |
| 4348 | strin.clear(); |
| 4349 | strin.str( line ); |
| 4350 | strin >> train_status; |
| 4351 | |
| 4352 | if( train_status ) |
| 4353 | { |
| 4354 | if( model_type == g_mt_mlp ) |
| 4355 | { |
| 4356 | mlp = new MLP_Object(); |
| 4357 | mlp->load( fin ); |
| 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | }; |
| 4362 | |
| 4363 | //----------------------------------------------------------------------------- |
| 4364 | // name: struct Wekinator_Object |
| 4365 | // desc: Wekinator implementation | 1.5.0.0 (yikai) added |
| 4366 | // adapted from Rebecca Fiebrink's Wekinator java/Weka implementation |
| 4367 | //----------------------------------------------------------------------------- |
| 4368 | struct Wekinator_Object |
| 4369 | { |
| 4370 | private: |
| 4371 | // input |
| 4372 | t_CKINT num_inputs; |
| 4373 | vector<t_CKFLOAT> inputs; |
| 4374 | vector<string> inputs_names; |
| 4375 | // output |
| 4376 | t_CKINT num_outputs; |
| 4377 | vector<t_CKFLOAT> outputs; |
| 4378 | vector<string> outputs_names; |
| 4379 | // model |
| 4380 | Wekinator_Model dummy_model; |
| 4381 | vector<Wekinator_Model> models; |
| 4382 | // data |
| 4383 | vector<vector<t_CKFLOAT> > examples; |
| 4384 | t_CKINT recording_round; |
| 4385 | |
| 4386 | //------------------------------------------------------------------------- |
| 4387 | // why train a separate model for each output channel? |
| 4388 | /* let's ask chatGPT (a stochastic parrot that hallucinates -M.S.) |
| 4389 | Ge: in working with multi-layer perceptrons, what is the difference |
| 4390 | between having all output dimensions in one network, versus having a |
| 4391 | separate network trained on each dimension of the output? |
| 4392 | chatGPT: In multi-layer perceptrons (MLPs), the number of output dimensions |
| 4393 | represents the number of distinct classes or values that the model can predict. |
| 4394 | Having all output dimensions in one network means that the MLP outputs |
| 4395 | vector with multiple dimensions, each representing a distinct class |
| 4396 | or value that the model can predict. This approach is often used in |
| 4397 | multi-class classification problems or in problems where the output |
| 4398 | is a vector of continuous values (e.g., regression problems). The |
| 4399 | advantage of this approach is that the model can learn complex relationships |
| 4400 | between input features and output classes/values, and it can capture |
| 4401 | dependencies between different output dimensions. |
| 4402 | On the other hand, having a separate network trained on each dimension of |
| 4403 | the output means that multiple MLPs are trained, each one responsible |
| 4404 | for predicting a single dimension of the output. This approach is often |
| 4405 | used in problems where the output dimensions are independent of each |
| 4406 | other (e.g., multi-target regression problems). The advantage of this |
| 4407 | approach is that it can be easier to train and optimize each individual |
| 4408 | MLP, and it can also be more computationally efficient than training a |
| 4409 | single MLP with multiple output dimensions. |
| 4410 | In general, whether to use a single MLP with multiple output dimensions or |
| 4411 | multiple MLPs depends on the specific problem and the nature of the |
| 4412 | output variables. It's worth noting that some architectures, such as the |
| 4413 | encoder-decoder architecture, combine both approaches by using a single |
| 4414 | etwork to encode input features and separate networks to decode the |
| 4415 | output into multiple dimensions. */ |
| 4416 | //------------------------------------------------------------------------- |
| 4417 | |
| 4418 | public: |
| 4419 | // constructor |
| 4420 | Wekinator_Object() |
| 4421 | { |
| 4422 | num_inputs = 0; |
| 4423 | num_outputs = 0; |
| 4424 | recording_round = 0; |
| 4425 | } |
| 4426 | |
| 4427 | // destructor |
| 4428 | ~Wekinator_Object() |
| 4429 | { |
| 4430 | inputs.clear(); |
| 4431 | inputs_names.clear(); |
| 4432 | outputs.clear(); |
| 4433 | outputs_names.clear(); |
| 4434 | examples.clear(); |
| 4435 | models.clear(); |
| 4436 | } |
| 4437 | |
| 4438 | // clear_all_obs |
| 4439 | void clear_all_obs() |
| 4440 | { |
| 4441 | examples.clear(); |
| 4442 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4443 | models[i].example_ids.clear(); |
| 4444 | recording_round = 0; |
| 4445 | } |
| 4446 | |
| 4447 | // set_num_inputs |
| 4448 | void set_num_inputs( t_CKINT num_inputs_ ) |
| 4449 | { |
| 4450 | if( num_inputs_ < 0 ) |
| 4451 | { |
| 4452 | EM_error3( "Wekinator.set_num_inputs: number of inputs must be positive." ); |
| 4453 | return; |
| 4454 | } |
| 4455 | if( num_inputs_ == num_inputs ) |
| 4456 | return; |
| 4457 | |
| 4458 | num_inputs = num_inputs_; |
| 4459 | |
| 4460 | inputs.resize( num_inputs ); |
| 4461 | |
| 4462 | inputs_names.resize( num_inputs ); |
| 4463 | for( t_CKINT i = 0; i < num_inputs; i++ ) |
| 4464 | inputs_names[i] = "input-" + to_string( i ); |
| 4465 | |
| 4466 | dummy_model.reset_connected_inputs( num_inputs ); |
| 4467 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4468 | models[i].reset_connected_inputs( num_inputs ); |
| 4469 | |
| 4470 | clear_all_obs(); |
| 4471 | } |
| 4472 | |
| 4473 | // set_num_outputs |
| 4474 | void set_num_outputs( t_CKINT num_outputs_ ) |
| 4475 | { |
| 4476 | if( num_outputs_ < 0 ) |
| 4477 | { |
| 4478 | EM_error3( "Wekinator.set_num_outputs: number of outputs must be positive." ); |
| 4479 | return; |
| 4480 | } |
| 4481 | if( num_outputs_ == num_outputs ) |
| 4482 | return; |
| 4483 | |
| 4484 | num_outputs = num_outputs_; |
| 4485 | |
| 4486 | outputs.resize( num_outputs ); |
| 4487 | |
| 4488 | outputs_names.resize( num_outputs ); |
| 4489 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 4490 | outputs_names[i] = "output-" + to_string( i ); |
| 4491 | |
| 4492 | models.clear(); |
| 4493 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 4494 | models.push_back( dummy_model ); |
| 4495 | |
| 4496 | clear_all_obs(); |
| 4497 | } |
| 4498 | |
| 4499 | // set_inputs |
| 4500 | void set_inputs( Chuck_ArrayFloat & inputs_ ) |
| 4501 | { |
| 4502 | if( num_inputs == 0 ) |
| 4503 | set_num_inputs( inputs_.size() ); |
| 4504 | else if( inputs_.size() != num_inputs ) |
| 4505 | { |
| 4506 | EM_error3( "Wekinator.set_inputs: number of inputs does not match." ); |
| 4507 | return; |
| 4508 | } |
| 4509 | inputs = inputs_.m_vector; |
| 4510 | } |
| 4511 | |
| 4512 | // set_outputs |
| 4513 | void set_outputs( Chuck_ArrayFloat & outputs_ ) |
| 4514 | { |
| 4515 | if( num_outputs == 0 ) |
| 4516 | set_num_outputs( outputs_.size() ); |
| 4517 | else if( outputs_.size() != num_outputs ) |
| 4518 | { |
| 4519 | EM_error3( "Wekinator.set_outputs: number of outputs does not match." ); |
| 4520 | return; |
| 4521 | } |
| 4522 | outputs = outputs_.m_vector; |
| 4523 | } |
| 4524 | |
| 4525 | // train |
| 4526 | void train() |
| 4527 | { |
| 4528 | ChaiMatrixFast<t_CKFLOAT> X; |
| 4529 | ChaiMatrixFast<t_CKFLOAT> Y; |
| 4530 | t_CKINT n, d, r; |
| 4531 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 4532 | { |
| 4533 | // prepare data |
| 4534 | n = models[i].example_ids.size(); |
| 4535 | d = models[i].connected_inputs.size(); |
| 4536 | X.allocate( n, d ); |
| 4537 | Y.allocate( n, 1 ); |
| 4538 | for( t_CKINT j = 0; j < n; j++ ) |
| 4539 | { |
| 4540 | r = models[i].example_ids[j]; |
| 4541 | for( t_CKINT k = 0; k < d; k++ ) |
| 4542 | X( j, k ) = examples[r][3 + models[i].connected_inputs[k]]; |
| 4543 | Y( j, 0 ) = examples[r][3 + num_inputs + i]; |
| 4544 | } |
| 4545 | // prepare model |
| 4546 | if( models[i].task_type == g_tt_regression ) |
| 4547 | { |
| 4548 | if( models[i].model_type == g_mt_mlp ) |
| 4549 | { |
| 4550 | CK_SAFE_DELETE( models[i].mlp ); |
| 4551 | models[i].mlp = new MLP_Object(); |
| 4552 | // init |
| 4553 | vector<t_CKUINT> units_per_layer; |
| 4554 | units_per_layer.push_back( d ); |
| 4555 | t_CKINT nodes_per_hidden_layer = |
| 4556 | models[i].mlp_nodes_per_hidden_layer == 0 ? d : models[i].mlp_nodes_per_hidden_layer; |
| 4557 | for( t_CKINT j = 0; j < models[i].mlp_hidden_layers; j++ ) |
| 4558 | units_per_layer.push_back( nodes_per_hidden_layer ); |
| 4559 | units_per_layer.push_back( 1 ); |
| 4560 | models[i].mlp->init( units_per_layer ); |
| 4561 | // train |
| 4562 | models[i].mlp->train( X, Y, n, models[i].mlp_learning_rate, models[i].mlp_epochs ); |
| 4563 | } |
| 4564 | else |
| 4565 | { |
| 4566 | EM_error3( "Wekinator.train: unknown regression model_type." ); |
| 4567 | return; |
| 4568 | } |
| 4569 | } |
| 4570 | else if( models[i].task_type == g_tt_classification ) |
| 4571 | { |
| 4572 | EM_error3( "Wekinator.train: classification not implemented yet." ); |
| 4573 | return; |
| 4574 | } |
| 4575 | // set status |
| 4576 | models[i].train_status = TRUE; |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | // set_property |
| 4581 | void set_property( t_CKINT property_type, Chuck_String & property_name, t_CKINT property_value ) |
| 4582 | { |
| 4583 | string key = tolower( property_name.str() ); |
| 4584 | |
| 4585 | if( property_type == g_tt_classification ) |
| 4586 | { |
| 4587 | if( key == "classes" ) |
| 4588 | { |
| 4589 | dummy_model.classification_classes = property_value; |
| 4590 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4591 | models[i].classification_classes = property_value; |
| 4592 | } |
| 4593 | else |
| 4594 | { |
| 4595 | EM_error3( "Wekinator.set_property: unknown classification property." ); |
| 4596 | return; |
| 4597 | } |
| 4598 | } |
| 4599 | else if( property_type == g_tt_regression ) |
| 4600 | { |
| 4601 | if( key == "limit" ) |
| 4602 | { |
| 4603 | if( property_value != 0 && property_value != 1 ) |
| 4604 | { |
| 4605 | EM_error3( "Wekinator.set_property: regression limit must be true or false." ); |
| 4606 | return; |
| 4607 | } |
| 4608 | dummy_model.regression_limit = property_value; |
| 4609 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4610 | models[i].regression_limit = property_value; |
| 4611 | } |
| 4612 | else if( key == "min" ) |
| 4613 | { |
| 4614 | dummy_model.regression_min = property_value; |
| 4615 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4616 | models[i].regression_min = property_value; |
| 4617 | } |
| 4618 | else if( key == "max" ) |
| 4619 | { |
| 4620 | dummy_model.regression_max = property_value; |
| 4621 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4622 | models[i].regression_max = property_value; |
| 4623 | } |
| 4624 | else |
| 4625 | { |
| 4626 | EM_error3( "Wekinator.set_property: unknown regression property." ); |
| 4627 | return; |
| 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | if( property_type == g_mt_knn ) |
| 4632 | { |
| 4633 | if( key == "k" ) |
| 4634 | { |
| 4635 | dummy_model.knn_k = property_value; |
| 4636 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4637 | models[i].knn_k = property_value; |
| 4638 | } |
| 4639 | else |
| 4640 | { |
| 4641 | EM_error3( "Wekinator.set_property: unknown kNN property." ); |
| 4642 | return; |
| 4643 | } |
| 4644 | } |
| 4645 | else if( property_type == g_mt_mlp ) |
| 4646 | { |
| 4647 | if( key == "hiddenlayers" ) |
| 4648 | { |
| 4649 | dummy_model.mlp_hidden_layers = property_value; |
| 4650 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4651 | models[i].mlp_hidden_layers = property_value; |
| 4652 | } |
| 4653 | else if( key == "nodesperhiddenlayer" ) |
| 4654 | { |
| 4655 | dummy_model.mlp_nodes_per_hidden_layer = property_value; |
| 4656 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4657 | models[i].mlp_nodes_per_hidden_layer = property_value; |
| 4658 | } |
| 4659 | else if( key == "epochs" ) |
| 4660 | { |
| 4661 | dummy_model.mlp_epochs = property_value; |
| 4662 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4663 | models[i].mlp_epochs = property_value; |
| 4664 | } |
| 4665 | else |
| 4666 | { |
| 4667 | EM_error3( "Wekinator.set_property: unknown MLP property." ); |
| 4668 | return; |
| 4669 | } |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | // set_property |
| 4674 | void set_property( t_CKINT property_type, Chuck_String & property_name, t_CKFLOAT property_value ) |
| 4675 | { |
| 4676 | string key = tolower( property_name.str() ); |
| 4677 | |
| 4678 | if( property_type == g_tt_regression ) |
| 4679 | { |
| 4680 | if( key == "min" ) |
| 4681 | { |
| 4682 | dummy_model.regression_min = property_value; |
| 4683 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4684 | models[i].regression_min = property_value; |
| 4685 | } |
| 4686 | else if( key == "max" ) |
| 4687 | { |
| 4688 | dummy_model.regression_max = property_value; |
| 4689 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4690 | models[i].regression_max = property_value; |
| 4691 | } |
| 4692 | else |
| 4693 | { |
| 4694 | EM_error3( "Wekinator.set_property: unknown regression property." ); |
| 4695 | return; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | if( property_type == g_mt_mlp ) |
| 4700 | { |
| 4701 | if( key == "learningrate" ) |
| 4702 | { |
| 4703 | dummy_model.mlp_learning_rate = property_value; |
| 4704 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 4705 | models[i].mlp_learning_rate = property_value; |
| 4706 | } |
| 4707 | else |
| 4708 | { |
| 4709 | EM_error3( "Wekinator.set_property: unknown MLP property." ); |
| 4710 | return; |
| 4711 | } |
| 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | // get_property_int |
| 4716 | t_CKINT get_property_int( t_CKINT property_type, Chuck_String & property_name ) |
| 4717 | { |
| 4718 | string key = tolower( property_name.str() ); |
| 4719 | |
| 4720 | if( property_type == g_tt_classification ) |
| 4721 | { |
| 4722 | if( key == "classes" ) |
| 4723 | return dummy_model.classification_classes; |
| 4724 | else |
| 4725 | { |
| 4726 | EM_error3( "Wekinator.get_property: unknown classification property." ); |
| 4727 | return 0; |
| 4728 | } |
| 4729 | } |
| 4730 | else if( property_type == g_tt_regression ) |
| 4731 | { |
| 4732 | if( key == "limit" ) |
| 4733 | return dummy_model.regression_limit; |
| 4734 | else |
| 4735 | { |
| 4736 | EM_error3( "Wekinator.get_property: unknown regression property." ); |
| 4737 | return 0; |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | if( property_type == g_mt_knn ) |
| 4742 | { |
| 4743 | if( key == "k" ) |
| 4744 | return dummy_model.knn_k; |
| 4745 | else |
| 4746 | { |
| 4747 | EM_error3( "Wekinator.get_property: unknown kNN property." ); |
| 4748 | return 0; |
| 4749 | } |
| 4750 | } |
| 4751 | else if( property_type == g_mt_mlp ) |
| 4752 | { |
| 4753 | if( key == "hiddenlayers" ) |
| 4754 | return dummy_model.mlp_hidden_layers; |
| 4755 | else if( key == "nodesperhiddenlayer" ) |
| 4756 | return dummy_model.mlp_nodes_per_hidden_layer == 0 ? num_inputs |
| 4757 | : dummy_model.mlp_nodes_per_hidden_layer; |
| 4758 | else if( key == "epochs" ) |
| 4759 | return dummy_model.mlp_epochs; |
| 4760 | else |
| 4761 | { |
| 4762 | EM_error3( "Wekinator.get_property: unknown MLP property." ); |
| 4763 | return 0; |
| 4764 | } |
| 4765 | } |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | // get_property_float |
| 4770 | t_CKFLOAT get_property_float( t_CKINT property_type, Chuck_String & property_name ) |
| 4771 | { |
| 4772 | string key = tolower( property_name.str() ); |
| 4773 | |
| 4774 | if( property_type == g_tt_regression ) |
| 4775 | { |
| 4776 | if( key == "min" ) |
| 4777 | return dummy_model.regression_min; |
| 4778 | else if( key == "max" ) |
| 4779 | return dummy_model.regression_max; |
| 4780 | else |
| 4781 | { |
| 4782 | EM_error3( "Wekinator.get_property: unknown regression property." ); |
| 4783 | return 0; |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | if( property_type == g_mt_mlp ) |
| 4788 | { |
| 4789 | if( key == "learningrate" ) |
| 4790 | return dummy_model.mlp_learning_rate; |
| 4791 | else |
| 4792 | { |
| 4793 | EM_error3( "Wekinator.get_property: unknown MLP property." ); |
| 4794 | return 0; |
| 4795 | } |
| 4796 | } |
| 4797 | |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
| 4801 | // set_output_property |
| 4802 | void set_output_property( t_CKINT output_index, Chuck_String & property_name, t_CKINT property_value ) |
| 4803 | { |
| 4804 | // sanity check |
| 4805 | if( output_index < 0 || output_index >= num_outputs ) |
| 4806 | { |
| 4807 | EM_error3( "Wekinator.set_output_property: output index out of range." ); |
| 4808 | return; |
| 4809 | } |
| 4810 | |
| 4811 | string key = tolower( property_name.str() ); |
| 4812 | |
| 4813 | if( key == "tasktype" ) |
| 4814 | { |
| 4815 | if( property_value == g_tt_classification || property_value == g_tt_regression ) |
| 4816 | { |
| 4817 | models[output_index].task_type = property_value; |
| 4818 | } |
| 4819 | else |
| 4820 | { |
| 4821 | EM_error3( "Wekinator.set_output_property: unknown output task_type." ); |
| 4822 | return; |
| 4823 | } |
| 4824 | } |
| 4825 | else if( key == "modeltype" ) |
| 4826 | { |
| 4827 | if( models[output_index].task_type == g_tt_classification ) |
| 4828 | { |
| 4829 | if( property_value == g_mt_knn ) |
| 4830 | { |
| 4831 | models[output_index].model_type = property_value; |
| 4832 | } |
| 4833 | else |
| 4834 | { |
| 4835 | EM_error3( "Wekinator.set_output_property: unknown classification model_type." ); |
| 4836 | return; |
| 4837 | } |
| 4838 | } |
| 4839 | else if( models[output_index].task_type == g_tt_regression ) |
| 4840 | { |
| 4841 | if( property_value == g_mt_mlp ) |
| 4842 | { |
| 4843 | models[output_index].model_type = property_value; |
| 4844 | } |
| 4845 | else |
| 4846 | { |
| 4847 | EM_error3( "Wekinator.set_output_property: unknown regression model_type." ); |
| 4848 | return; |
| 4849 | } |
| 4850 | } |
| 4851 | } |
| 4852 | else |
| 4853 | { |
| 4854 | EM_error3( "Wekinator.set_output_property: unknown output property." ); |
| 4855 | return; |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | // set_output_property |
| 4860 | void set_output_property( t_CKINT output_index, |
| 4861 | t_CKINT property_type, |
| 4862 | Chuck_String & property_name, |
| 4863 | t_CKINT property_value ) |
| 4864 | { |
| 4865 | // sanity check |
| 4866 | if( output_index < 0 || output_index >= num_outputs ) |
| 4867 | { |
| 4868 | EM_error3( "Wekinator.set_output_property: output index out of range." ); |
| 4869 | return; |
| 4870 | } |
| 4871 | |
| 4872 | string key = tolower( property_name.str() ); |
| 4873 | |
| 4874 | if( property_type == g_tt_classification ) |
| 4875 | { |
| 4876 | if( key == "classes" ) |
| 4877 | { |
| 4878 | models[output_index].classification_classes = property_value; |
| 4879 | } |
| 4880 | else |
| 4881 | { |
| 4882 | EM_error3( "Wekinator.set_output_property: unknown classification property." ); |
| 4883 | return; |
| 4884 | } |
| 4885 | } |
| 4886 | else if( property_type == g_tt_regression ) |
| 4887 | { |
| 4888 | if( key == "limit" ) |
| 4889 | { |
| 4890 | if( property_value != 0 && property_value != 1 ) |
| 4891 | { |
| 4892 | EM_error3( "Wekinator.set_output_property: regression limit must be true or false." ); |
| 4893 | return; |
| 4894 | } |
| 4895 | models[output_index].regression_limit = property_value; |
| 4896 | } |
| 4897 | else if( key == "min" ) |
| 4898 | { |
| 4899 | models[output_index].regression_min = property_value; |
| 4900 | } |
| 4901 | else if( key == "max" ) |
| 4902 | { |
| 4903 | models[output_index].regression_max = property_value; |
| 4904 | } |
| 4905 | else |
| 4906 | { |
| 4907 | EM_error3( "Wekinator.set_output_property: unknown regression property." ); |
| 4908 | return; |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | if( property_type == g_mt_knn ) |
| 4913 | { |
| 4914 | if( key == "k" ) |
| 4915 | { |
| 4916 | models[output_index].knn_k = property_value; |
| 4917 | } |
| 4918 | else |
| 4919 | { |
| 4920 | EM_error3( "Wekinator.set_output_property: unknown kNN property." ); |
| 4921 | return; |
| 4922 | } |
| 4923 | } |
| 4924 | else if( property_type == g_mt_mlp ) |
| 4925 | { |
| 4926 | if( key == "hiddenlayers" ) |
| 4927 | { |
| 4928 | models[output_index].mlp_hidden_layers = property_value; |
| 4929 | } |
| 4930 | else if( key == "nodesperhiddenlayer" ) |
| 4931 | { |
| 4932 | models[output_index].mlp_nodes_per_hidden_layer = property_value; |
| 4933 | } |
| 4934 | else if( key == "epochs" ) |
| 4935 | { |
| 4936 | models[output_index].mlp_epochs = property_value; |
| 4937 | } |
| 4938 | else |
| 4939 | { |
| 4940 | EM_error3( "Wekinator.set_output_property: unknown MLP property." ); |
| 4941 | return; |
| 4942 | } |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | // set_output_property |
| 4947 | void set_output_property( t_CKINT output_index, |
| 4948 | t_CKINT property_type, |
| 4949 | Chuck_String & property_name, |
| 4950 | t_CKFLOAT property_value ) |
| 4951 | { |
| 4952 | // sanity check |
| 4953 | if( output_index < 0 || output_index >= num_outputs ) |
| 4954 | { |
| 4955 | EM_error3( "Wekinator.set_output_property: output index out of range." ); |
| 4956 | return; |
| 4957 | } |
| 4958 | |
| 4959 | string key = tolower( property_name.str() ); |
| 4960 | |
| 4961 | if( property_type == g_mt_mlp ) |
| 4962 | { |
| 4963 | if( key == "learningrate" ) |
| 4964 | { |
| 4965 | models[output_index].mlp_learning_rate = property_value; |
| 4966 | } |
| 4967 | else |
| 4968 | { |
| 4969 | EM_error3( "Wekinator.set_output_property: unknown MLP property." ); |
| 4970 | return; |
| 4971 | } |
| 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | // get_output_property_int |
| 4976 | t_CKINT get_output_property_int( t_CKINT output_index, Chuck_String & property_name ) |
| 4977 | { |
| 4978 | // sanity check |
| 4979 | if( output_index < 0 || output_index >= num_outputs ) |
| 4980 | { |
| 4981 | EM_error3( "Wekinator.get_output_property: output index out of range." ); |
| 4982 | return 0; |
| 4983 | } |
| 4984 | |
| 4985 | string key = tolower( property_name.str() ); |
| 4986 | |
| 4987 | if( key == "tasktype" ) |
| 4988 | { |
| 4989 | return models[output_index].task_type; |
| 4990 | } |
| 4991 | else if( key == "modeltype" ) |
| 4992 | { |
| 4993 | return models[output_index].model_type; |
| 4994 | } |
| 4995 | else |
| 4996 | { |
| 4997 | EM_error3( "Wekinator.get_output_property: unknown output property." ); |
| 4998 | return 0; |
| 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | // get_output_property_float |
| 5003 | t_CKFLOAT get_output_property_float( t_CKINT output_index, Chuck_String & property_name ) |
| 5004 | { |
| 5005 | EM_error3( "Wekinator.get_output_property: unknown output property." ); |
| 5006 | return 0; |
| 5007 | } |
| 5008 | |
| 5009 | // get_output_property_int1 |
| 5010 | t_CKINT get_output_property_int( t_CKINT output_index, t_CKINT property_type, Chuck_String & property_name ) |
| 5011 | { |
| 5012 | // sanity check |
| 5013 | if( output_index < 0 || output_index >= num_outputs ) |
| 5014 | { |
| 5015 | EM_error3( "Wekinator.get_output_property: output index out of range." ); |
| 5016 | return 0; |
| 5017 | } |
| 5018 | |
| 5019 | string key = tolower( property_name.str() ); |
| 5020 | |
| 5021 | if( property_type == g_tt_classification ) |
| 5022 | { |
| 5023 | if( key == "classes" ) |
| 5024 | { |
| 5025 | return models[output_index].classification_classes; |
| 5026 | } |
| 5027 | else |
| 5028 | { |
| 5029 | EM_error3( "Wekinator.get_output_property: unknown classification property." ); |
| 5030 | return 0; |
| 5031 | } |
| 5032 | } |
| 5033 | else if( property_type == g_tt_regression ) |
| 5034 | { |
| 5035 | if( key == "limit" ) |
| 5036 | { |
| 5037 | return models[output_index].regression_limit; |
| 5038 | } |
| 5039 | else |
| 5040 | { |
| 5041 | EM_error3( "Wekinator.get_output_property: unknown regression property." ); |
| 5042 | return 0; |
| 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | if( property_type == g_mt_knn ) |
| 5047 | { |
| 5048 | if( key == "k" ) |
| 5049 | { |
| 5050 | return models[output_index].knn_k; |
| 5051 | } |
| 5052 | else |
| 5053 | { |
| 5054 | EM_error3( "Wekinator.get_output_property: unknown kNN property." ); |
| 5055 | return 0; |
| 5056 | } |
| 5057 | } |
| 5058 | else if( property_type == g_mt_mlp ) |
| 5059 | { |
| 5060 | if( key == "hiddenlayers" ) |
| 5061 | { |
| 5062 | return models[output_index].mlp_hidden_layers; |
| 5063 | } |
| 5064 | else if( key == "nodesperhiddenlayer" ) |
| 5065 | { |
| 5066 | return models[output_index].mlp_nodes_per_hidden_layer == 0 ? num_inputs |
| 5067 | : models[output_index].mlp_nodes_per_hidden_layer; |
| 5068 | } |
| 5069 | else if( key == "epochs" ) |
| 5070 | { |
| 5071 | return models[output_index].mlp_epochs; |
| 5072 | } |
| 5073 | else |
| 5074 | { |
| 5075 | EM_error3( "Wekinator.get_output_property: unknown MLP property." ); |
| 5076 | return 0; |
| 5077 | } |
| 5078 | } |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | // get_output_property_float1 |
| 5083 | t_CKFLOAT get_output_property_float( t_CKINT output_index, t_CKINT property_type, Chuck_String & property_name ) |
| 5084 | { |
| 5085 | // sanity check |
| 5086 | if( output_index < 0 || output_index >= num_outputs ) |
| 5087 | { |
| 5088 | EM_error3( "Wekinator.get_output_property: output index out of range." ); |
| 5089 | return 0; |
| 5090 | } |
| 5091 | |
| 5092 | string key = tolower( property_name.str() ); |
| 5093 | |
| 5094 | if( property_type == g_tt_regression ) |
| 5095 | { |
| 5096 | if( key == "min" ) |
| 5097 | { |
| 5098 | return models[output_index].regression_min; |
| 5099 | } |
| 5100 | else if( key == "max" ) |
| 5101 | { |
| 5102 | return models[output_index].regression_max; |
| 5103 | } |
| 5104 | else |
| 5105 | { |
| 5106 | EM_error3( "Wekinator.get_output_property: unknown regression property." ); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | if( property_type == g_mt_mlp ) |
| 5112 | { |
| 5113 | if( key == "learningrate" ) |
| 5114 | { |
| 5115 | return models[output_index].mlp_learning_rate; |
| 5116 | } |
| 5117 | else |
| 5118 | { |
| 5119 | EM_error3( "Wekinator.get_output_property: unknown MLP property." ); |
| 5120 | return 0; |
| 5121 | } |
| 5122 | } |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
| 5126 | // set_output_property3 |
| 5127 | void set_output_property( t_CKINT output_index, Chuck_String & property_name, Chuck_ArrayInt & property_value_ ) |
| 5128 | { |
| 5129 | // sanity check |
| 5130 | if( output_index < 0 || output_index >= num_outputs ) |
| 5131 | { |
| 5132 | EM_error3( "Wekinator.set_output_property: output index out of range." ); |
| 5133 | return; |
| 5134 | } |
| 5135 | // set |
| 5136 | string key = tolower( property_name.str() ); |
| 5137 | t_CKINT value; |
| 5138 | |
| 5139 | if( key == "connectedinputs" ) |
| 5140 | { |
| 5141 | models[output_index].connected_inputs.clear(); |
| 5142 | for( t_CKINT i = 0; i < property_value_.size(); i++ ) |
| 5143 | { |
| 5144 | value = property_value_.m_vector[i]; |
| 5145 | // sanity check |
| 5146 | if( value < 0 || value >= num_inputs ) |
| 5147 | { |
| 5148 | EM_error3( "Wekinator.set_output_property: input index out of range." ); |
| 5149 | return; |
| 5150 | } |
| 5151 | models[output_index].connected_inputs.push_back( value ); |
| 5152 | } |
| 5153 | } |
| 5154 | else |
| 5155 | { |
| 5156 | EM_error3( "Wekinator.set_output_property: unknown output property." ); |
| 5157 | return; |
| 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | // get_output_property |
| 5162 | void get_output_property( t_CKINT output_index, Chuck_String & property_name, Chuck_ArrayInt & property_value_ ) |
| 5163 | { |
| 5164 | // sanity check |
| 5165 | if( output_index < 0 || output_index >= num_outputs ) |
| 5166 | { |
| 5167 | EM_error3( "Wekinator.get_output_property: output index out of range." ); |
| 5168 | return; |
| 5169 | } |
| 5170 | // get |
| 5171 | string key = tolower( property_name.str() ); |
| 5172 | |
| 5173 | if( key == "connectedinputs" ) |
| 5174 | { |
| 5175 | property_value_.set_size( models[output_index].connected_inputs.size() ); |
| 5176 | for( t_CKINT i = 0; i < models[output_index].connected_inputs.size(); i++ ) |
| 5177 | { |
| 5178 | property_value_.m_vector[i] = models[output_index].connected_inputs[i]; |
| 5179 | } |
| 5180 | } |
| 5181 | else |
| 5182 | { |
| 5183 | EM_error3( "Wekinator.get_output_property: unknown output property." ); |
| 5184 | return; |
| 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | // get_num_inputs |
| 5189 | t_CKINT get_num_inputs() |
| 5190 | { |
| 5191 | return num_inputs; |
| 5192 | } |
| 5193 | |
| 5194 | // get_num_outputs |
| 5195 | t_CKINT get_num_outputs() |
| 5196 | { |
| 5197 | return num_outputs; |
| 5198 | } |
| 5199 | |
| 5200 | // push back inputs and outputs |
| 5201 | void push_back_example( const vector<t_CKFLOAT> & inputs_, const vector<t_CKFLOAT> & outputs_ ) |
| 5202 | { |
| 5203 | vector<t_CKFLOAT> example( 3 + num_inputs + num_outputs ); |
| 5204 | example[0] = examples.size(); |
| 5205 | example[1] = 0; |
| 5206 | example[2] = recording_round; |
| 5207 | for( t_CKINT i = 0; i < num_inputs; i++ ) |
| 5208 | example[3 + i] = inputs_[i]; |
| 5209 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5210 | example[3 + num_inputs + i] = outputs_[i]; |
| 5211 | |
| 5212 | examples.push_back( example ); |
| 5213 | } |
| 5214 | |
| 5215 | // add |
| 5216 | void add() |
| 5217 | { |
| 5218 | push_back_example( inputs, outputs ); |
| 5219 | if( dummy_model.record_status ) |
| 5220 | dummy_model.example_ids.push_back( examples.size() - 1 ); |
| 5221 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5222 | if( models[i].record_status ) |
| 5223 | models[i].example_ids.push_back( examples.size() - 1 ); |
| 5224 | } |
| 5225 | |
| 5226 | // predict |
| 5227 | void predict( Chuck_ArrayFloat & inputs_, Chuck_ArrayFloat & outputs_ ) |
| 5228 | { |
| 5229 | // sanity check |
| 5230 | if( inputs_.size() != num_inputs ) |
| 5231 | { |
| 5232 | EM_error3( "Wekinator.predict: input array size mismatch." ); |
| 5233 | return; |
| 5234 | } |
| 5235 | outputs_.set_size( num_outputs ); |
| 5236 | outputs_.zero(); |
| 5237 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5238 | { |
| 5239 | if( !models[i].train_status ) |
| 5240 | { |
| 5241 | EM_error3( "Wekinator.run: model not trained." ); |
| 5242 | return; |
| 5243 | } |
| 5244 | if( !models[i].run_status ) |
| 5245 | continue; |
| 5246 | // prepare data |
| 5247 | ChaiVectorFast<t_CKFLOAT> x( models[i].connected_inputs.size() ); |
| 5248 | ChaiVectorFast<t_CKFLOAT> y( 1 ); |
| 5249 | for( t_CKINT j = 0; j < x.size(); j++ ) |
| 5250 | x[j] = inputs_.m_vector[models[i].connected_inputs[j]]; |
| 5251 | // predict |
| 5252 | if( models[i].task_type == g_tt_regression ) |
| 5253 | { |
| 5254 | if( models[i].model_type == g_mt_mlp ) |
| 5255 | { |
| 5256 | models[i].mlp->predict( x, y ); |
| 5257 | outputs_.m_vector[i] = y[0]; |
| 5258 | } |
| 5259 | else |
| 5260 | { |
| 5261 | EM_error3( "Wekinator.predict: unknown regression model_type." ); |
| 5262 | return; |
| 5263 | } |
| 5264 | if( models[i].regression_limit ) |
| 5265 | { |
| 5266 | if( outputs_.m_vector[i] < models[i].regression_min ) |
| 5267 | outputs_.m_vector[i] = models[i].regression_min; |
| 5268 | else if( outputs_.m_vector[i] > models[i].regression_max ) |
| 5269 | outputs_.m_vector[i] = models[i].regression_max; |
| 5270 | } |
| 5271 | } |
| 5272 | else if( models[i].task_type == g_tt_classification ) |
| 5273 | { |
| 5274 | EM_error3( "Wekinator.predict: classification not implemented." ); |
| 5275 | return; |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | // set_task_type |
| 5281 | void set_task_type( t_CKINT task_type ) |
| 5282 | { |
| 5283 | if( task_type != g_tt_regression && task_type != g_tt_classification ) |
| 5284 | { |
| 5285 | EM_error3( "Wekinator.set_task_type: unknown task_type." ); |
| 5286 | return; |
| 5287 | } |
| 5288 | dummy_model.task_type = task_type; |
| 5289 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 5290 | models[i].task_type = task_type; |
| 5291 | } |
| 5292 | |
| 5293 | // get_task_type |
| 5294 | t_CKINT get_task_type() |
| 5295 | { |
| 5296 | return dummy_model.task_type; |
| 5297 | } |
| 5298 | |
| 5299 | // get_task_type_name |
| 5300 | void get_task_type_name( Chuck_String & name ) |
| 5301 | { |
| 5302 | if( dummy_model.task_type == g_tt_regression ) |
| 5303 | name.set( "AI.Regression" ); |
| 5304 | else if( dummy_model.task_type == g_tt_classification ) |
| 5305 | name.set( "AI.Classification" ); |
| 5306 | } |
| 5307 | |
| 5308 | // set_model_type |
| 5309 | void set_model_type( t_CKINT model_type ) |
| 5310 | { |
| 5311 | if( dummy_model.task_type == g_tt_classification ) |
| 5312 | { |
| 5313 | if( model_type != g_mt_knn ) |
| 5314 | { |
| 5315 | EM_error3( "Wekinator.set_model_type: unknown model_type for classification. Available: AI.KNN." ); |
| 5316 | return; |
| 5317 | } |
| 5318 | dummy_model.model_type = model_type; |
| 5319 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 5320 | models[i].model_type = model_type; |
| 5321 | } |
| 5322 | else if( dummy_model.task_type == g_tt_regression ) |
| 5323 | { |
| 5324 | if( model_type != g_mt_mlp ) |
| 5325 | { |
| 5326 | EM_error3( "Wekinator.set_model_type: unknown model_type for regression. Available: AI.MLP" ); |
| 5327 | return; |
| 5328 | } |
| 5329 | dummy_model.model_type = model_type; |
| 5330 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 5331 | models[i].model_type = model_type; |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | // get_model_type |
| 5336 | t_CKINT get_model_type() |
| 5337 | { |
| 5338 | return dummy_model.model_type; |
| 5339 | } |
| 5340 | |
| 5341 | // get_model_type_name |
| 5342 | void get_model_type_name( Chuck_String & name ) |
| 5343 | { |
| 5344 | if( dummy_model.model_type == g_mt_mlp ) |
| 5345 | name.set( "AI.MLP" ); |
| 5346 | else if( dummy_model.model_type == g_mt_svm ) |
| 5347 | name.set( "AI.SVM" ); |
| 5348 | else if( dummy_model.model_type == g_mt_knn ) |
| 5349 | name.set( "AI.KNN" ); |
| 5350 | } |
| 5351 | |
| 5352 | // randomize_outputs |
| 5353 | void randomize_outputs() |
| 5354 | { |
| 5355 | t_CKFLOAT random_min, random_max; |
| 5356 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5357 | { |
| 5358 | random_min = models[i].regression_min; |
| 5359 | random_max = models[i].regression_max; |
| 5360 | outputs[i] = ck_random() / (t_CKFLOAT)CK_RANDOM_MAX * ( random_max - random_min ) + random_min; |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | // get_num_obs |
| 5365 | t_CKINT get_num_obs() |
| 5366 | { |
| 5367 | return dummy_model.example_ids.size(); |
| 5368 | } |
| 5369 | |
| 5370 | // get_num_obs1 |
| 5371 | t_CKINT get_num_obs1( t_CKINT output_index ) |
| 5372 | { |
| 5373 | // sanity check |
| 5374 | if( output_index < 0 || output_index >= num_outputs ) |
| 5375 | { |
| 5376 | EM_error3( "Wekinator.get_num_obs: output_index out of range." ); |
| 5377 | return 0; |
| 5378 | } |
| 5379 | return models[output_index].example_ids.size(); |
| 5380 | } |
| 5381 | |
| 5382 | // get_obs |
| 5383 | void get_obs( Chuck_ArrayInt & obs_ ) |
| 5384 | { |
| 5385 | // sanity check |
| 5386 | if( obs_.size() < dummy_model.example_ids.size() ) |
| 5387 | { |
| 5388 | EM_error3( "Wekinator.get_obs: input array too small." ); |
| 5389 | return; |
| 5390 | } |
| 5391 | Chuck_ArrayFloat * row; |
| 5392 | t_CKINT dim = 3 + num_inputs + num_outputs; |
| 5393 | // copy |
| 5394 | for( t_CKINT i = 0; i < dummy_model.example_ids.size(); i++ ) |
| 5395 | { |
| 5396 | row = (Chuck_ArrayFloat *)obs_.m_vector[i]; |
| 5397 | row->set_size( dim ); |
| 5398 | row->m_vector = examples[dummy_model.example_ids[i]]; |
| 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | // get_obs1 |
| 5403 | void get_obs1( t_CKINT output_index, Chuck_ArrayInt & obs_ ) |
| 5404 | { |
| 5405 | // sanity check |
| 5406 | if( output_index < 0 || output_index >= num_outputs ) |
| 5407 | { |
| 5408 | EM_error3( "Wekinator.get_obs1: output_index out of range." ); |
| 5409 | return; |
| 5410 | } |
| 5411 | if( obs_.size() < models[output_index].example_ids.size() ) |
| 5412 | { |
| 5413 | EM_error3( "Wekinator.get_obs1: input array too small." ); |
| 5414 | return; |
| 5415 | } |
| 5416 | Chuck_ArrayFloat * row; |
| 5417 | t_CKINT dim = 3 + num_inputs + num_outputs; |
| 5418 | // copy |
| 5419 | for( t_CKINT i = 0; i < models[output_index].example_ids.size(); i++ ) |
| 5420 | { |
| 5421 | row = (Chuck_ArrayFloat *)obs_.m_vector[i]; |
| 5422 | row->set_size( dim ); |
| 5423 | row->m_vector = examples[models[output_index].example_ids[i]]; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | // clear_obs |
| 5428 | void clear_obs( t_CKINT lo, t_CKINT hi ) |
| 5429 | { |
| 5430 | dummy_model.clear_obs( lo, hi ); |
| 5431 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5432 | models[i].clear_obs( lo, hi ); |
| 5433 | } |
| 5434 | |
| 5435 | // clear_obs1 |
| 5436 | void clear_obs1( t_CKINT output_index, t_CKINT lo, t_CKINT hi ) |
| 5437 | { |
| 5438 | // sanity check |
| 5439 | if( output_index < 0 || output_index >= num_outputs ) |
| 5440 | { |
| 5441 | EM_error3( "Wekinator.clear_obs1: output_index out of range." ); |
| 5442 | return; |
| 5443 | } |
| 5444 | models[output_index].clear_obs( lo, hi ); |
| 5445 | } |
| 5446 | |
| 5447 | // save |
| 5448 | t_CKBOOL save( Chuck_String & filename ) |
| 5449 | { |
| 5450 | ofstream fout( filename.str().c_str() ); |
| 5451 | // check |
| 5452 | if( !fout.good() ) |
| 5453 | { |
| 5454 | EM_error3( "Wekinator.save: could not open file '%s' for writing.", filename.str().c_str() ); |
| 5455 | return FALSE; |
| 5456 | } |
| 5457 | |
| 5458 | // data |
| 5459 | fout << "## data" << endl; |
| 5460 | fout << "# num_inputs" << endl; |
| 5461 | fout << num_inputs << endl; |
| 5462 | fout << "# num_outputs" << endl; |
| 5463 | fout << num_outputs << endl; |
| 5464 | fout << "# num_obs" << endl; |
| 5465 | t_CKINT num_obs = dummy_model.example_ids.size(), dim = 3 + num_inputs + num_outputs; |
| 5466 | fout << num_obs << endl; |
| 5467 | fout << "# obs" << endl; |
| 5468 | for( t_CKINT i = 0; i < num_obs; i++ ) |
| 5469 | { |
| 5470 | for( t_CKINT j = 0; j < dim; j++ ) |
| 5471 | fout << examples[dummy_model.example_ids[i]][j] << " "; |
| 5472 | fout << endl; |
| 5473 | } |
| 5474 | // model |
| 5475 | fout << "## models" << endl; |
| 5476 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5477 | { |
| 5478 | fout << "# output-" << i << endl; |
| 5479 | models[i].save( fout ); |
| 5480 | } |
| 5481 | return TRUE; |
| 5482 | } |
| 5483 | |
| 5484 | // load |
| 5485 | t_CKBOOL load( Chuck_String & filename ) |
| 5486 | { |
| 5487 | ifstream fin( filename.str().c_str() ); |
| 5488 | // check |
| 5489 | if( !fin.good() ) |
| 5490 | { |
| 5491 | EM_error3( "Wekinator.load: could not open file '%s' for reading.", filename.str().c_str() ); |
| 5492 | return FALSE; |
| 5493 | } |
| 5494 | |
| 5495 | t_CKINT num; |
| 5496 | string line; |
| 5497 | istringstream strin; |
| 5498 | |
| 5499 | // num_inputs |
| 5500 | nextline( fin, line, TRUE ); |
| 5501 | strin.clear(); |
| 5502 | strin.str( line ); |
| 5503 | strin >> num; |
| 5504 | set_num_inputs( num ); |
| 5505 | // num_outputs |
| 5506 | nextline( fin, line, TRUE ); |
| 5507 | strin.clear(); |
| 5508 | strin.str( line ); |
| 5509 | strin >> num; |
| 5510 | set_num_outputs( num ); |
| 5511 | |
| 5512 | t_CKINT dim = 3 + num_inputs + num_outputs, id = 0, target; |
| 5513 | vector<t_CKFLOAT> example( dim ); |
| 5514 | // num_obs |
| 5515 | nextline( fin, line, TRUE ); |
| 5516 | strin.clear(); |
| 5517 | strin.str( line ); |
| 5518 | strin >> num; |
| 5519 | // obs |
| 5520 | for( t_CKINT i = 0; i < num; i++ ) |
| 5521 | { |
| 5522 | nextline( fin, line, TRUE ); |
| 5523 | strin.clear(); |
| 5524 | strin.str( line ); |
| 5525 | for( t_CKINT j = 0; j < dim; j++ ) |
| 5526 | strin >> example[j]; |
| 5527 | target = (t_CKINT)( example[0] + 0.5 ); |
| 5528 | while( id <= target ) |
| 5529 | { |
| 5530 | examples.push_back( example ); |
| 5531 | id++; |
| 5532 | } |
| 5533 | dummy_model.example_ids.push_back( target ); |
| 5534 | } |
| 5535 | |
| 5536 | // models |
| 5537 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5538 | { |
| 5539 | models[i].load( fin ); |
| 5540 | } |
| 5541 | return TRUE; |
| 5542 | } |
| 5543 | |
| 5544 | // save_obs_as_arff |
| 5545 | void save_obs_as_arff( const string & filename, const vector<t_CKINT> example_ids_ ) |
| 5546 | { |
| 5547 | ofstream fout( filename.c_str() ); |
| 5548 | // check |
| 5549 | if( !fout.good() ) |
| 5550 | { |
| 5551 | EM_error3( "Wekinator.export_obs: could not open file '%s' for writing.", filename.c_str() ); |
| 5552 | return; |
| 5553 | } |
| 5554 | |
| 5555 | string header = "@relation dataset" |
| 5556 | "\n" |
| 5557 | "@attribute ID numeric" |
| 5558 | "@attribute Time date 'yyyy/MM/dd HH:mm:ss:SSS'" |
| 5559 | "@attribute 'Training round' numeric"; |
| 5560 | fout << header << endl; |
| 5561 | |
| 5562 | for( t_CKINT i = 0; i < num_inputs; i++ ) |
| 5563 | { |
| 5564 | fout << "@attribute inputs-" << i + 1 << " numeric" << endl; |
| 5565 | } |
| 5566 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5567 | { |
| 5568 | fout << "@attribute outputs-" << i + 1 << " numeric" << endl; |
| 5569 | } |
| 5570 | |
| 5571 | t_CKINT index, dim = num_inputs + num_outputs; |
| 5572 | fout << "\n@data" << endl; |
| 5573 | for( t_CKINT i = 0; i < example_ids_.size(); i++ ) |
| 5574 | { |
| 5575 | index = example_ids_[i]; |
| 5576 | fout << index << ", 2023/01/01 00:00:00:000, " << (t_CKINT)( examples[index][2] + 0.5 ); |
| 5577 | for( t_CKINT j = 0; j < dim; j++ ) |
| 5578 | { |
| 5579 | fout << ", " << examples[index][3 + j]; |
| 5580 | } |
| 5581 | fout << endl; |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | // export_obs |
| 5586 | void export_obs( Chuck_String & filename ) |
| 5587 | { |
| 5588 | save_obs_as_arff( filename.str(), dummy_model.example_ids ); |
| 5589 | } |
| 5590 | |
| 5591 | // export_obs1 |
| 5592 | void export_obs1( t_CKINT output_index, Chuck_String & filename ) |
| 5593 | { |
| 5594 | // sanity check |
| 5595 | if( output_index < 0 || output_index >= num_outputs ) |
| 5596 | { |
| 5597 | EM_error3( "Wekinator.export_obs: output_index out of range." ); |
| 5598 | return; |
| 5599 | } |
| 5600 | // export |
| 5601 | save_obs_as_arff( filename.str(), models[output_index].example_ids ); |
| 5602 | } |
| 5603 | |
| 5604 | // import_obs |
| 5605 | void import_obs( Chuck_String & filename ) |
| 5606 | { |
| 5607 | // init |
| 5608 | t_CKINT x_size = 0, y_size = 0; |
| 5609 | ifstream fin( filename.str().c_str() ); |
| 5610 | string line; |
| 5611 | while( getline( fin, line ) ) |
| 5612 | { |
| 5613 | if( line.find( "@attribute" ) != string::npos ) |
| 5614 | { |
| 5615 | if( line.find( "inputs" ) != string::npos ) |
| 5616 | { |
| 5617 | x_size++; |
| 5618 | } |
| 5619 | else if( line.find( "outputs" ) != string::npos ) |
| 5620 | { |
| 5621 | y_size++; |
| 5622 | } |
| 5623 | } |
| 5624 | else if( line.find( "@data" ) != string::npos ) |
| 5625 | { |
| 5626 | break; |
| 5627 | } |
| 5628 | } |
| 5629 | set_num_inputs( x_size ); |
| 5630 | set_num_outputs( y_size ); |
| 5631 | |
| 5632 | // load |
| 5633 | stringstream ss; |
| 5634 | string token; |
| 5635 | t_CKINT index, size = 3 + x_size + y_size; |
| 5636 | vector<t_CKFLOAT> example( size ); |
| 5637 | while( getline( fin, line ) ) |
| 5638 | { |
| 5639 | ss.clear(); |
| 5640 | ss.str( line ); |
| 5641 | index = 0; |
| 5642 | while( getline( ss, token, ',' ) ) |
| 5643 | { |
| 5644 | if( index == size ) |
| 5645 | break; |
| 5646 | else if( index == 1 ) |
| 5647 | example[1] = 0; |
| 5648 | else |
| 5649 | { |
| 5650 | example[index] = atof( token.c_str() ); |
| 5651 | } |
| 5652 | ++index; |
| 5653 | } |
| 5654 | examples.push_back( example ); |
| 5655 | dummy_model.example_ids.push_back( examples.size() - 1 ); |
| 5656 | for( t_CKINT i = 0; i < models.size(); i++ ) |
| 5657 | models[i].example_ids.push_back( examples.size() - 1 ); |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | // clear_all_obs1 |
| 5662 | void clear_all_obs1( t_CKINT output_index ) |
| 5663 | { |
| 5664 | // sanity check |
| 5665 | if( output_index < 0 || output_index >= num_outputs ) |
| 5666 | { |
| 5667 | EM_error3( "Wekinator.clear_all_obs1: output_index out of range." ); |
| 5668 | return; |
| 5669 | } |
| 5670 | // clear |
| 5671 | models[output_index].example_ids.clear(); |
| 5672 | } |
| 5673 | |
| 5674 | // set_all_record_status |
| 5675 | void set_all_record_status( t_CKINT status ) |
| 5676 | { |
| 5677 | if( status != 0 && status != 1 ) |
| 5678 | { |
| 5679 | EM_error3( "Wekinator.set_all_record_status: status must be true or false." ); |
| 5680 | return; |
| 5681 | } |
| 5682 | dummy_model.record_status = status; |
| 5683 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5684 | models[i].record_status = status; |
| 5685 | } |
| 5686 | |
| 5687 | // set_output_record_status |
| 5688 | void set_output_record_status( t_CKINT output_index, t_CKINT status ) |
| 5689 | { |
| 5690 | // sanity check |
| 5691 | if( output_index < 0 || output_index >= num_outputs ) |
| 5692 | { |
| 5693 | EM_error3( "Wekinator.set_output_record_status: output_index out of range." ); |
| 5694 | return; |
| 5695 | } |
| 5696 | if( status != 0 && status != 1 ) |
| 5697 | { |
| 5698 | EM_error3( "Wekinator.set_output_record_status: status must be true or false." ); |
| 5699 | return; |
| 5700 | } |
| 5701 | // set |
| 5702 | models[output_index].record_status = status; |
| 5703 | } |
| 5704 | |
| 5705 | // get_output_record_status |
| 5706 | t_CKINT get_output_record_status( t_CKINT output_index ) |
| 5707 | { |
| 5708 | // sanity check |
| 5709 | if( output_index < 0 || output_index >= num_outputs ) |
| 5710 | { |
| 5711 | EM_error3( "Wekinator.get_output_record_status: output_index out of range." ); |
| 5712 | return 0; |
| 5713 | } |
| 5714 | // get |
| 5715 | return models[output_index].record_status; |
| 5716 | } |
| 5717 | |
| 5718 | // set_all_run_status |
| 5719 | void set_all_run_status( t_CKINT status ) |
| 5720 | { |
| 5721 | if( status != 0 && status != 1 ) |
| 5722 | { |
| 5723 | EM_error3( "Wekinator.set_all_run_status: status must be true or false." ); |
| 5724 | return; |
| 5725 | } |
| 5726 | dummy_model.run_status = status; |
| 5727 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5728 | models[i].run_status = status; |
| 5729 | } |
| 5730 | |
| 5731 | // set_output_run_status |
| 5732 | void set_output_run_status( t_CKINT output_index, t_CKINT status ) |
| 5733 | { |
| 5734 | // sanity check |
| 5735 | if( output_index < 0 || output_index >= num_outputs ) |
| 5736 | { |
| 5737 | EM_error3( "Wekinator.set_output_run_status: output_index out of range." ); |
| 5738 | return; |
| 5739 | } |
| 5740 | if( status != 0 && status != 1 ) |
| 5741 | { |
| 5742 | EM_error3( "Wekinator.set_output_run_status: status must be true or false." ); |
| 5743 | return; |
| 5744 | } |
| 5745 | // set |
| 5746 | models[output_index].run_status = status; |
| 5747 | } |
| 5748 | |
| 5749 | // get_output_run_status |
| 5750 | t_CKINT get_output_run_status( t_CKINT output_index ) |
| 5751 | { |
| 5752 | // sanity check |
| 5753 | if( output_index < 0 || output_index >= num_outputs ) |
| 5754 | { |
| 5755 | EM_error3( "Wekinator.get_output_run_status: output_index out of range." ); |
| 5756 | return 0; |
| 5757 | } |
| 5758 | // get |
| 5759 | return models[output_index].run_status; |
| 5760 | } |
| 5761 | |
| 5762 | // add1 |
| 5763 | void add1( Chuck_ArrayFloat & inputs_, Chuck_ArrayFloat & outputs_ ) |
| 5764 | { |
| 5765 | // sanity check |
| 5766 | if( inputs_.size() != num_inputs ) |
| 5767 | { |
| 5768 | EM_error3( "Wekinator.add1: inputs array size does not match number of inputs." ); |
| 5769 | return; |
| 5770 | } |
| 5771 | if( outputs_.size() != num_outputs ) |
| 5772 | { |
| 5773 | EM_error3( "Wekinator.add1: outputs array size does not match number of outputs." ); |
| 5774 | return; |
| 5775 | } |
| 5776 | push_back_example( inputs_.m_vector, outputs_.m_vector ); |
| 5777 | if( dummy_model.record_status ) |
| 5778 | dummy_model.example_ids.push_back( examples.size() - 1 ); |
| 5779 | for( t_CKINT i = 0; i < num_outputs; i++ ) |
| 5780 | if( models[i].record_status ) |
| 5781 | models[i].example_ids.push_back( examples.size() - 1 ); |
| 5782 | } |
| 5783 | |
| 5784 | // add2 |
| 5785 | void add2( t_CKINT output_index, Chuck_ArrayFloat & inputs_, Chuck_ArrayFloat & outputs_ ) |
| 5786 | { |
| 5787 | // sanity check |
| 5788 | if( output_index < 0 || output_index >= num_outputs ) |
| 5789 | { |
| 5790 | EM_error3( "Wekinator.add2: output_index out of range." ); |
| 5791 | return; |
| 5792 | } |
| 5793 | if( inputs_.size() != num_inputs ) |
| 5794 | { |
| 5795 | EM_error3( "Wekinator.add2: inputs array size does not match number of inputs." ); |
| 5796 | return; |
| 5797 | } |
| 5798 | if( outputs_.size() != num_outputs ) |
| 5799 | { |
| 5800 | EM_error3( "Wekinator.add2: outputs array size does not match number of outputs." ); |
| 5801 | return; |
| 5802 | } |
| 5803 | push_back_example( inputs_.m_vector, outputs_.m_vector ); |
| 5804 | dummy_model.example_ids.push_back( examples.size() - 1 ); |
| 5805 | models[output_index].example_ids.push_back( examples.size() - 1 ); |
| 5806 | } |
| 5807 | |
| 5808 | // next_round |
| 5809 | void next_round() |
| 5810 | { |
| 5811 | ++recording_round; |
| 5812 | } |
| 5813 | |
| 5814 | // delete_last_round |
| 5815 | void delete_last_round() |
| 5816 | { |
| 5817 | t_CKINT start = 0, end = examples.size(); |
| 5818 | for( t_CKINT i = 0; i < end; i++ ) |
| 5819 | { |
| 5820 | if( recording_round == (t_CKINT)( examples[i][2] + 0.5 ) ) |
| 5821 | { |
| 5822 | start = i; |
| 5823 | break; |
| 5824 | } |
| 5825 | } |
| 5826 | clear_obs( start, end ); |
| 5827 | } |
| 5828 | |
| 5829 | // clear |
| 5830 | void clear() |
| 5831 | { |
| 5832 | clear_all_obs(); |
| 5833 | set_num_inputs( 0 ); |
| 5834 | set_num_outputs( 0 ); |
| 5835 | recording_round = 0; |
| 5836 | } |
| 5837 | |
| 5838 | // get_round |
| 5839 | t_CKINT get_round() |
| 5840 | { |
| 5841 | return recording_round; |
| 5842 | } |
| 5843 | |
| 5844 | // get_all_record_status |
| 5845 | t_CKINT get_all_record_status() |
| 5846 | { |
| 5847 | return dummy_model.record_status; |
| 5848 | } |
| 5849 | |
| 5850 | // get_all_run_status |
| 5851 | t_CKINT get_all_run_status() |
| 5852 | { |
| 5853 | return dummy_model.run_status; |
| 5854 | } |
| 5855 | }; |
| 5856 | |
| 5857 | //----------------------------------------------------------------------------- |
| 5858 | // Wekinator c++ hooks |
| 5859 | //----------------------------------------------------------------------------- |
| 5860 | CK_DLL_CTOR( Wekinator_ctor ) |
| 5861 | { |
| 5862 | Wekinator_Object * wekinator = new Wekinator_Object(); |
| 5863 | OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ) = (t_CKUINT)wekinator; |
| 5864 | } |
| 5865 | |
| 5866 | CK_DLL_DTOR( Wekinator_dtor ) |
| 5867 | { |
| 5868 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5869 | CK_SAFE_DELETE( wekinator ); |
| 5870 | OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ) = 0; |
| 5871 | } |
| 5872 | |
| 5873 | CK_DLL_MFUN( Wekinator_set_num_inputs ) |
| 5874 | { |
| 5875 | // get object |
| 5876 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5877 | // get args |
| 5878 | t_CKINT num_inputs = GET_NEXT_INT( ARGS ); |
| 5879 | // set_num_inputs |
| 5880 | wekinator->set_num_inputs( num_inputs ); |
| 5881 | // pass through |
| 5882 | RETURN->v_int = num_inputs; |
| 5883 | } |
| 5884 | |
| 5885 | CK_DLL_MFUN( Wekinator_get_num_inputs ) |
| 5886 | { |
| 5887 | // get object |
| 5888 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5889 | // get_num_inputs |
| 5890 | RETURN->v_int = wekinator->get_num_inputs(); |
| 5891 | } |
| 5892 | |
| 5893 | CK_DLL_MFUN( Wekinator_set_num_outputs ) |
| 5894 | { |
| 5895 | // get object |
| 5896 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5897 | // get args |
| 5898 | t_CKINT num_outputs = GET_NEXT_INT( ARGS ); |
| 5899 | // set_num_outputs |
| 5900 | wekinator->set_num_outputs( num_outputs ); |
| 5901 | // pass throught |
| 5902 | RETURN->v_int = num_outputs; |
| 5903 | } |
| 5904 | |
| 5905 | CK_DLL_MFUN( Wekinator_get_num_outputs ) |
| 5906 | { |
| 5907 | // get object |
| 5908 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5909 | // get_num_outputs |
| 5910 | RETURN->v_int = wekinator->get_num_outputs(); |
| 5911 | } |
| 5912 | |
| 5913 | CK_DLL_MFUN( Wekinator_set_task_type ) |
| 5914 | { |
| 5915 | // get object |
| 5916 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5917 | // get args |
| 5918 | t_CKINT task_type = GET_NEXT_INT( ARGS ); |
| 5919 | // set_task_type |
| 5920 | wekinator->set_task_type( task_type ); |
| 5921 | } |
| 5922 | |
| 5923 | CK_DLL_MFUN( Wekinator_get_task_type ) |
| 5924 | { |
| 5925 | // get object |
| 5926 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5927 | // get_task_type |
| 5928 | RETURN->v_int = wekinator->get_task_type(); |
| 5929 | } |
| 5930 | |
| 5931 | CK_DLL_MFUN( Wekinator_get_task_type_name ) |
| 5932 | { |
| 5933 | // get object |
| 5934 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5935 | // make a new chuck string |
| 5936 | Chuck_String * name = (Chuck_String *)instantiate_and_initialize_object( VM->env()->ckt_string, SHRED ); |
| 5937 | // get_task_type_name |
| 5938 | wekinator->get_task_type_name( *name ); |
| 5939 | // return |
| 5940 | RETURN->v_object = name; |
| 5941 | } |
| 5942 | |
| 5943 | CK_DLL_MFUN( Wekinator_set_property ) |
| 5944 | { |
| 5945 | // get object |
| 5946 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5947 | // get args |
| 5948 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 5949 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 5950 | t_CKINT property_value = GET_NEXT_INT( ARGS ); |
| 5951 | // set_property |
| 5952 | wekinator->set_property( property_type, *property_name, property_value ); |
| 5953 | } |
| 5954 | |
| 5955 | CK_DLL_MFUN( Wekinator_set_property1 ) |
| 5956 | { |
| 5957 | // get object |
| 5958 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5959 | // get args |
| 5960 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 5961 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 5962 | t_CKFLOAT property_value = GET_NEXT_FLOAT( ARGS ); |
| 5963 | // set_property1 |
| 5964 | wekinator->set_property( property_type, *property_name, property_value ); |
| 5965 | } |
| 5966 | |
| 5967 | CK_DLL_MFUN( Wekinator_get_property_int ) |
| 5968 | { |
| 5969 | // get object |
| 5970 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5971 | // get args |
| 5972 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 5973 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 5974 | // get_property_int |
| 5975 | RETURN->v_int = wekinator->get_property_int( property_type, *property_name ); |
| 5976 | } |
| 5977 | |
| 5978 | CK_DLL_MFUN( Wekinator_get_property_float ) |
| 5979 | { |
| 5980 | // get object |
| 5981 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5982 | // get args |
| 5983 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 5984 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 5985 | // get_property_float |
| 5986 | RETURN->v_float = wekinator->get_property_float( property_type, *property_name ); |
| 5987 | } |
| 5988 | |
| 5989 | CK_DLL_MFUN( Wekinator_set_model_type ) |
| 5990 | { |
| 5991 | // get object |
| 5992 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 5993 | // get args |
| 5994 | t_CKINT model_type = GET_NEXT_INT( ARGS ); |
| 5995 | // set_model_type |
| 5996 | wekinator->set_model_type( model_type ); |
| 5997 | } |
| 5998 | |
| 5999 | CK_DLL_MFUN( Wekinator_get_model_type ) |
| 6000 | { |
| 6001 | // get object |
| 6002 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6003 | // get_model_type |
| 6004 | RETURN->v_int = wekinator->get_model_type(); |
| 6005 | } |
| 6006 | |
| 6007 | CK_DLL_MFUN( Wekinator_get_model_type_name ) |
| 6008 | { |
| 6009 | // get object |
| 6010 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6011 | // make a chuck string |
| 6012 | Chuck_String * name = (Chuck_String *)instantiate_and_initialize_object( VM->env()->ckt_string, SHRED ); |
| 6013 | // get_model_type_name |
| 6014 | wekinator->get_model_type_name( *name ); |
| 6015 | // return it |
| 6016 | RETURN->v_object = name; |
| 6017 | } |
| 6018 | |
| 6019 | CK_DLL_MFUN( Wekinator_set_output_property ) |
| 6020 | { |
| 6021 | // get object |
| 6022 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6023 | // get args |
| 6024 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6025 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6026 | t_CKINT property_value = GET_NEXT_INT( ARGS ); |
| 6027 | // set_output_property |
| 6028 | wekinator->set_output_property( output_index, *property_name, property_value ); |
| 6029 | } |
| 6030 | |
| 6031 | CK_DLL_MFUN( Wekinator_get_output_property_int ) |
| 6032 | { |
| 6033 | // get object |
| 6034 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6035 | // get args |
| 6036 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6037 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6038 | // get_output_property_int |
| 6039 | RETURN->v_int = wekinator->get_output_property_int( output_index, *property_name ); |
| 6040 | } |
| 6041 | |
| 6042 | CK_DLL_MFUN( Wekinator_set_output_property1 ) |
| 6043 | { |
| 6044 | // get object |
| 6045 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6046 | // get args |
| 6047 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6048 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 6049 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6050 | t_CKINT property_value = GET_NEXT_INT( ARGS ); |
| 6051 | // set_output_property1 |
| 6052 | wekinator->set_output_property( output_index, property_type, *property_name, property_value ); |
| 6053 | } |
| 6054 | |
| 6055 | CK_DLL_MFUN( Wekinator_set_output_property2 ) |
| 6056 | { |
| 6057 | // get object |
| 6058 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6059 | // get args |
| 6060 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6061 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 6062 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6063 | t_CKFLOAT property_value = GET_NEXT_FLOAT( ARGS ); |
| 6064 | // set_output_property2 |
| 6065 | wekinator->set_output_property( output_index, property_type, *property_name, property_value ); |
| 6066 | } |
| 6067 | |
| 6068 | CK_DLL_MFUN( Wekinator_get_output_property_int1 ) |
| 6069 | { |
| 6070 | // get object |
| 6071 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6072 | // get args |
| 6073 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6074 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 6075 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6076 | // get_output_property_int1 |
| 6077 | RETURN->v_int = wekinator->get_output_property_int( output_index, property_type, *property_name ); |
| 6078 | } |
| 6079 | |
| 6080 | CK_DLL_MFUN( Wekinator_get_output_property_float ) |
| 6081 | { |
| 6082 | // get object |
| 6083 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6084 | // get args |
| 6085 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6086 | t_CKINT property_type = GET_NEXT_INT( ARGS ); |
| 6087 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6088 | // get_output_property_float |
| 6089 | RETURN->v_float = wekinator->get_output_property_float( output_index, property_type, *property_name ); |
| 6090 | } |
| 6091 | |
| 6092 | CK_DLL_MFUN( Wekinator_set_output_property3 ) |
| 6093 | { |
| 6094 | // get object |
| 6095 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6096 | // get args |
| 6097 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6098 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6099 | Chuck_ArrayInt * property_value = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 6100 | // set_output_property3 |
| 6101 | wekinator->set_output_property( output_index, *property_name, *property_value ); |
| 6102 | } |
| 6103 | |
| 6104 | CK_DLL_MFUN( Wekinator_get_output_property ) |
| 6105 | { |
| 6106 | // get object |
| 6107 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6108 | // get args |
| 6109 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6110 | Chuck_String * property_name = GET_NEXT_STRING( ARGS ); |
| 6111 | Chuck_ArrayInt * property_value = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 6112 | // get_output_property |
| 6113 | wekinator->get_output_property( output_index, *property_name, *property_value ); |
| 6114 | } |
| 6115 | |
| 6116 | CK_DLL_MFUN( Wekinator_set_inputs ) |
| 6117 | { |
| 6118 | // get object |
| 6119 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6120 | // get args |
| 6121 | Chuck_ArrayFloat * inputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6122 | // set_inputs |
| 6123 | wekinator->set_inputs( *inputs ); |
| 6124 | } |
| 6125 | |
| 6126 | CK_DLL_MFUN( Wekinator_set_outputs ) |
| 6127 | { |
| 6128 | // get object |
| 6129 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6130 | // get args |
| 6131 | Chuck_ArrayFloat * outputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6132 | // set_outputs |
| 6133 | wekinator->set_outputs( *outputs ); |
| 6134 | } |
| 6135 | |
| 6136 | CK_DLL_MFUN( Wekinator_randomize_outputs ) |
| 6137 | { |
| 6138 | // get object |
| 6139 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6140 | // randomize_outputs |
| 6141 | wekinator->randomize_outputs(); |
| 6142 | } |
| 6143 | |
| 6144 | CK_DLL_MFUN( Wekinator_get_num_obs ) |
| 6145 | { |
| 6146 | // get object |
| 6147 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6148 | // get_num_obs |
| 6149 | RETURN->v_int = wekinator->get_num_obs(); |
| 6150 | } |
| 6151 | |
| 6152 | CK_DLL_MFUN( Wekinator_get_num_obs1 ) |
| 6153 | { |
| 6154 | // get object |
| 6155 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6156 | // get args |
| 6157 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6158 | // get_num_obs1 |
| 6159 | RETURN->v_int = wekinator->get_num_obs1( output_index ); |
| 6160 | } |
| 6161 | |
| 6162 | CK_DLL_MFUN( Wekinator_get_obs ) |
| 6163 | { |
| 6164 | // get object |
| 6165 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6166 | // get args |
| 6167 | Chuck_ArrayInt * obs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 6168 | // get_obs |
| 6169 | wekinator->get_obs( *obs ); |
| 6170 | } |
| 6171 | |
| 6172 | CK_DLL_MFUN( Wekinator_get_obs1 ) |
| 6173 | { |
| 6174 | // get object |
| 6175 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6176 | // get args |
| 6177 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6178 | Chuck_ArrayInt * obs = (Chuck_ArrayInt *)GET_NEXT_OBJECT( ARGS ); |
| 6179 | // get_obs1 |
| 6180 | wekinator->get_obs1( output_index, *obs ); |
| 6181 | } |
| 6182 | |
| 6183 | CK_DLL_MFUN( Wekinator_clear_obs ) |
| 6184 | { |
| 6185 | // get object |
| 6186 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6187 | // get args |
| 6188 | t_CKINT lo = GET_NEXT_INT( ARGS ); |
| 6189 | t_CKINT hi = GET_NEXT_INT( ARGS ); |
| 6190 | // clear_obs |
| 6191 | wekinator->clear_obs( lo, hi ); |
| 6192 | } |
| 6193 | |
| 6194 | CK_DLL_MFUN( Wekinator_clear_obs1 ) |
| 6195 | { |
| 6196 | // get object |
| 6197 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6198 | // get args |
| 6199 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6200 | t_CKINT lo = GET_NEXT_INT( ARGS ); |
| 6201 | t_CKINT hi = GET_NEXT_INT( ARGS ); |
| 6202 | // clear_obs1 |
| 6203 | wekinator->clear_obs1( output_index, lo, hi ); |
| 6204 | } |
| 6205 | |
| 6206 | CK_DLL_MFUN( Wekinator_save ) |
| 6207 | { |
| 6208 | // get object |
| 6209 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6210 | // get args |
| 6211 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 6212 | // save |
| 6213 | wekinator->save( *filename ); |
| 6214 | } |
| 6215 | |
| 6216 | CK_DLL_MFUN( Wekinator_load ) |
| 6217 | { |
| 6218 | // get object |
| 6219 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6220 | // get args |
| 6221 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 6222 | // load |
| 6223 | wekinator->load( *filename ); |
| 6224 | } |
| 6225 | |
| 6226 | CK_DLL_MFUN( Wekinator_export_obs ) |
| 6227 | { |
| 6228 | // get object |
| 6229 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6230 | // get args |
| 6231 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 6232 | // export_obs |
| 6233 | wekinator->export_obs( *filename ); |
| 6234 | } |
| 6235 | |
| 6236 | CK_DLL_MFUN( Wekinator_export_obs1 ) |
| 6237 | { |
| 6238 | // get object |
| 6239 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6240 | // get args |
| 6241 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6242 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 6243 | // export_obs1 |
| 6244 | wekinator->export_obs1( output_index, *filename ); |
| 6245 | } |
| 6246 | |
| 6247 | CK_DLL_MFUN( Wekinator_import_obs ) |
| 6248 | { |
| 6249 | // get object |
| 6250 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6251 | // get args |
| 6252 | Chuck_String * filename = GET_NEXT_STRING( ARGS ); |
| 6253 | // import_obs |
| 6254 | wekinator->import_obs( *filename ); |
| 6255 | } |
| 6256 | |
| 6257 | CK_DLL_MFUN( Wekinator_clear_all_obs ) |
| 6258 | { |
| 6259 | // get object |
| 6260 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6261 | // clear_all_obs |
| 6262 | wekinator->clear_all_obs(); |
| 6263 | } |
| 6264 | |
| 6265 | CK_DLL_MFUN( Wekinator_clear_all_obs1 ) |
| 6266 | { |
| 6267 | // get object |
| 6268 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6269 | // get args |
| 6270 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6271 | // clear_all_obs1 |
| 6272 | wekinator->clear_all_obs1( output_index ); |
| 6273 | } |
| 6274 | |
| 6275 | CK_DLL_MFUN( Wekinator_set_all_record_status ) |
| 6276 | { |
| 6277 | // get object |
| 6278 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6279 | // get args |
| 6280 | t_CKINT status = GET_NEXT_INT( ARGS ); |
| 6281 | // set_all_record_status |
| 6282 | wekinator->set_all_record_status( status ); |
| 6283 | } |
| 6284 | |
| 6285 | CK_DLL_MFUN( Wekinator_set_output_record_status ) |
| 6286 | { |
| 6287 | // get object |
| 6288 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6289 | // get args |
| 6290 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6291 | t_CKINT status = GET_NEXT_INT( ARGS ); |
| 6292 | // set_output_record_status |
| 6293 | wekinator->set_output_record_status( output_index, status ); |
| 6294 | } |
| 6295 | |
| 6296 | CK_DLL_MFUN( Wekinator_get_output_record_status ) |
| 6297 | { |
| 6298 | // get object |
| 6299 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6300 | // get args |
| 6301 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6302 | // get_output_record_status |
| 6303 | RETURN->v_int = wekinator->get_output_record_status( output_index ); |
| 6304 | } |
| 6305 | |
| 6306 | CK_DLL_MFUN( Wekinator_set_all_run_status ) |
| 6307 | { |
| 6308 | // get object |
| 6309 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6310 | // get args |
| 6311 | t_CKINT status = GET_NEXT_INT( ARGS ); |
| 6312 | // set_all_run_status |
| 6313 | wekinator->set_all_run_status( status ); |
| 6314 | } |
| 6315 | |
| 6316 | CK_DLL_MFUN( Wekinator_set_output_run_status ) |
| 6317 | { |
| 6318 | // get object |
| 6319 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6320 | // get args |
| 6321 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6322 | t_CKINT status = GET_NEXT_INT( ARGS ); |
| 6323 | // set_output_run_status |
| 6324 | wekinator->set_output_run_status( output_index, status ); |
| 6325 | } |
| 6326 | |
| 6327 | CK_DLL_MFUN( Wekinator_get_output_run_status ) |
| 6328 | { |
| 6329 | // get object |
| 6330 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6331 | // get args |
| 6332 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6333 | // get_output_run_status |
| 6334 | RETURN->v_int = wekinator->get_output_run_status( output_index ); |
| 6335 | } |
| 6336 | |
| 6337 | CK_DLL_MFUN( Wekinator_add ) |
| 6338 | { |
| 6339 | // get object |
| 6340 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6341 | // add |
| 6342 | wekinator->add(); |
| 6343 | } |
| 6344 | |
| 6345 | CK_DLL_MFUN( Wekinator_add1 ) |
| 6346 | { |
| 6347 | // get object |
| 6348 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6349 | // get args |
| 6350 | Chuck_ArrayFloat * inputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6351 | Chuck_ArrayFloat * outputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6352 | // add1 |
| 6353 | wekinator->add1( *inputs, *outputs ); |
| 6354 | } |
| 6355 | |
| 6356 | CK_DLL_MFUN( Wekinator_add2 ) |
| 6357 | { |
| 6358 | // get object |
| 6359 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6360 | // get args |
| 6361 | t_CKINT output_index = GET_NEXT_INT( ARGS ); |
| 6362 | Chuck_ArrayFloat * inputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6363 | Chuck_ArrayFloat * outputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6364 | // add2 |
| 6365 | wekinator->add2( output_index, *inputs, *outputs ); |
| 6366 | } |
| 6367 | |
| 6368 | CK_DLL_MFUN( Wekinator_next_round ) |
| 6369 | { |
| 6370 | // get object |
| 6371 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6372 | // next_round |
| 6373 | wekinator->next_round(); |
| 6374 | } |
| 6375 | |
| 6376 | CK_DLL_MFUN( Wekinator_train ) |
| 6377 | { |
| 6378 | // get object |
| 6379 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6380 | // train |
| 6381 | wekinator->train(); |
| 6382 | } |
| 6383 | |
| 6384 | CK_DLL_MFUN( Wekinator_predict ) |
| 6385 | { |
| 6386 | // get object |
| 6387 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6388 | // get args |
| 6389 | Chuck_ArrayFloat * inputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6390 | Chuck_ArrayFloat * outputs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS ); |
| 6391 | // predict |
| 6392 | wekinator->predict( *inputs, *outputs ); |
| 6393 | } |
| 6394 | |
| 6395 | CK_DLL_MFUN( Wekinator_delete_last_round ) |
| 6396 | { |
| 6397 | // get object |
| 6398 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6399 | // delete_last_round |
| 6400 | wekinator->delete_last_round(); |
| 6401 | } |
| 6402 | |
| 6403 | CK_DLL_MFUN( Wekinator_clear ) |
| 6404 | { |
| 6405 | // get object |
| 6406 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6407 | // clear |
| 6408 | wekinator->clear(); |
| 6409 | } |
| 6410 | |
| 6411 | CK_DLL_MFUN( Wekinator_get_round ) |
| 6412 | { |
| 6413 | // get object |
| 6414 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6415 | // get_round |
| 6416 | RETURN->v_int = wekinator->get_round(); |
| 6417 | } |
| 6418 | |
| 6419 | CK_DLL_MFUN( Wekinator_get_all_record_status ) |
| 6420 | { |
| 6421 | // get object |
| 6422 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6423 | // get_all_record_status |
| 6424 | RETURN->v_int = wekinator->get_all_record_status(); |
| 6425 | } |
| 6426 | |
| 6427 | CK_DLL_MFUN( Wekinator_get_all_run_status ) |
| 6428 | { |
| 6429 | // get object |
| 6430 | Wekinator_Object * wekinator = (Wekinator_Object *)OBJ_MEMBER_UINT( SELF, Wekinator_offset_data ); |
| 6431 | // get_all_run_status |
| 6432 | RETURN->v_int = wekinator->get_all_run_status(); |
| 6433 | } |
| 6434 | |
| 6435 | //----------------------------------------------------------------------------- |
| 6436 | // KDTree implementation |
| 6437 | //----------------------------------------------------------------------------- |
| 6438 | static inline t_CKINT is_leaf( struct kdnode * node ) |
| 6439 | { |
| 6440 | return node->left == node->right; |
| 6441 | } |
| 6442 | |
| 6443 | static inline void swap( long * a, long * b ) |
| 6444 | { |
| 6445 | long tmp = *a; |
| 6446 | *a = *b; |
| 6447 | *b = tmp; |
| 6448 | } |
| 6449 | |
| 6450 | static inline double square( double d ) |
| 6451 | { |
| 6452 | return d * d; |
| 6453 | } |
| 6454 | |
| 6455 | static inline double distance( double * c1, double * c2, t_CKINT dim ) |
| 6456 | { |
| 6457 | double distance = 0; |
| 6458 | while( dim-- > 0 ) |
| 6459 | { |
| 6460 | distance += square( *c1++ - *c2++ ); |
| 6461 | } |
| 6462 | return distance; |
| 6463 | } |
| 6464 | |
| 6465 | static inline double knn_max( struct kdtree * tree ) |
| 6466 | { |
| 6467 | return tree->knn_list_head.prev->distance; |
| 6468 | } |
| 6469 | |
| 6470 | static inline double D( struct kdtree * tree, long index, t_CKINT r ) |
| 6471 | { |
| 6472 | return tree->coord_table[index][r]; |
| 6473 | } |
| 6474 | |
| 6475 | static inline t_CKINT kdnode_passed( struct kdtree * tree, struct kdnode * node ) |
| 6476 | { |
| 6477 | return node != NULL ? tree->coord_passed[node->coord_index] : 1; |
| 6478 | } |
| 6479 | |
| 6480 | static inline t_CKINT knn_search_on( struct kdtree * tree, t_CKINT k, double value, double target ) |
| 6481 | { |
| 6482 | return tree->knn_num < k || square( target - value ) < knn_max( tree ); |
| 6483 | } |
| 6484 | |
| 6485 | static inline void coord_index_reset( struct kdtree * tree ) |
| 6486 | { |
| 6487 | long i; |
| 6488 | for( i = 0; i < tree->capacity; i++ ) |
| 6489 | { |
| 6490 | tree->coord_indexes[i] = i; |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | static inline void coord_table_reset( struct kdtree * tree ) |
| 6495 | { |
| 6496 | long i; |
| 6497 | for( i = 0; i < tree->capacity; i++ ) |
| 6498 | { |
| 6499 | tree->coord_table[i] = tree->coords + i * tree->dim; |
| 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | static inline void coord_deleted_reset( struct kdtree * tree ) |
| 6504 | { |
| 6505 | memset( tree->coord_deleted, 0, tree->capacity ); |
| 6506 | } |
| 6507 | |
| 6508 | static inline void coord_passed_reset( struct kdtree * tree ) |
| 6509 | { |
| 6510 | memset( tree->coord_passed, 0, tree->capacity ); |
| 6511 | } |
| 6512 | |
| 6513 | static void coord_dump_all( struct kdtree * tree ) |
| 6514 | { |
| 6515 | long i, j; |
| 6516 | for( i = 0; i < tree->count; i++ ) |
| 6517 | { |
| 6518 | long index = tree->coord_indexes[i]; |
| 6519 | double * coord = tree->coord_table[index]; |
| 6520 | printf( "(" ); |
| 6521 | for( j = 0; j < tree->dim; j++ ) |
| 6522 | { |
| 6523 | if( j != tree->dim - 1 ) |
| 6524 | { |
| 6525 | printf( "%.2f,", coord[j] ); |
| 6526 | } |
| 6527 | else |
| 6528 | { |
| 6529 | printf( "%.2f)\n", coord[j] ); |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | static void coord_dump_by_indexes( struct kdtree * tree, long low, long high, t_CKINT r ) |
| 6536 | { |
| 6537 | long i; |
| 6538 | printf( "r=%d:", (int)r ); |
| 6539 | for( i = 0; i <= high; i++ ) |
| 6540 | { |
| 6541 | if( i < low ) |
| 6542 | { |
| 6543 | printf( "%8s", " " ); |
| 6544 | } |
| 6545 | else |
| 6546 | { |
| 6547 | long index = tree->coord_indexes[i]; |
| 6548 | printf( "%8.2f", tree->coord_table[index][r] ); |
| 6549 | } |
| 6550 | } |
| 6551 | printf( "\n" ); |
| 6552 | } |
| 6553 | |
| 6554 | static void quicksort( struct kdtree * tree, long lo, long hi, t_CKINT r ) |
| 6555 | { |
| 6556 | long i, j, pivot, * indexes; |
| 6557 | // double * p; |
| 6558 | |
| 6559 | if( lo >= hi ) |
| 6560 | { |
| 6561 | return; |
| 6562 | } |
| 6563 | |
| 6564 | i = lo; |
| 6565 | j = hi; |
| 6566 | indexes = tree->coord_indexes; |
| 6567 | pivot = indexes[lo]; |
| 6568 | |
| 6569 | while( i < j ) |
| 6570 | { |
| 6571 | while( i < j && D( tree, indexes[j], r ) >= D( tree, pivot, r ) ) j--; |
| 6572 | /* Loop invariant: nums[j] > pivot or i == j */ |
| 6573 | indexes[i] = indexes[j]; |
| 6574 | while( i < j && D( tree, indexes[i], r ) <= D( tree, pivot, r ) ) i++; |
| 6575 | /* Loop invariant: nums[i] < pivot or i == j */ |
| 6576 | indexes[j] = indexes[i]; |
| 6577 | } |
| 6578 | /* Loop invariant: i == j */ |
| 6579 | indexes[i] = pivot; |
| 6580 | |
| 6581 | quicksort( tree, lo, i - 1, r ); |
| 6582 | quicksort( tree, i + 1, hi, r ); |
| 6583 | } |
| 6584 | |
| 6585 | static struct kdnode * kdnode_alloc( double * coord, long index, t_CKINT r ) |
| 6586 | { |
| 6587 | struct kdnode * node = (struct kdnode *)malloc( sizeof( *node ) ); |
| 6588 | if( node != NULL ) |
| 6589 | { |
| 6590 | memset( node, 0, sizeof( *node ) ); |
| 6591 | node->coord = coord; |
| 6592 | node->coord_index = index; |
| 6593 | node->r = r; |
| 6594 | } |
| 6595 | return node; |
| 6596 | } |
| 6597 | |
| 6598 | static void kdnode_free( struct kdnode * node ) |
| 6599 | { |
| 6600 | free( node ); |
| 6601 | } |
| 6602 | |
| 6603 | static t_CKINT coord_cmp( double * c1, double * c2, t_CKINT dim ) |
| 6604 | { |
| 6605 | t_CKINT i; |
| 6606 | double ret = 0; |
| 6607 | for( i = 0; i < dim; i++ ) |
| 6608 | { |
| 6609 | ret = *c1++ - *c2++; |
| 6610 | if( fabs( ret ) >= DBL_EPSILON ) |
| 6611 | { |
| 6612 | return ret > 0 ? 1 : -1; |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | if( fabs( ret ) < DBL_EPSILON ) |
| 6617 | { |
| 6618 | return 0; |
| 6619 | } |
| 6620 | else |
| 6621 | { |
| 6622 | return ret > 0 ? 1 : -1; |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | static void knn_list_add( struct kdtree * tree, struct kdnode * node, double distance ) |
| 6627 | { |
| 6628 | if( node == NULL ) return; |
| 6629 | |
| 6630 | struct knn_list * head = &tree->knn_list_head; |
| 6631 | struct knn_list * p = head->prev; |
| 6632 | if( tree->knn_num == 1 ) |
| 6633 | { |
| 6634 | if( p->distance > distance ) |
| 6635 | { |
| 6636 | p = p->prev; |
| 6637 | } |
| 6638 | } |
| 6639 | else |
| 6640 | { |
| 6641 | while( p != head && p->distance > distance ) |
| 6642 | { |
| 6643 | p = p->prev; |
| 6644 | } |
| 6645 | } |
| 6646 | |
| 6647 | if( p == head || coord_cmp( p->node->coord, node->coord, tree->dim ) ) |
| 6648 | { |
| 6649 | struct knn_list * log = (struct knn_list *)malloc( sizeof( *log ) ); |
| 6650 | if( log != NULL ) |
| 6651 | { |
| 6652 | log->node = node; |
| 6653 | log->distance = distance; |
| 6654 | log->prev = p; |
| 6655 | log->next = p->next; |
| 6656 | p->next->prev = log; |
| 6657 | p->next = log; |
| 6658 | tree->knn_num++; |
| 6659 | } |
| 6660 | } |
| 6661 | } |
| 6662 | |
| 6663 | static void knn_list_adjust( struct kdtree * tree, struct kdnode * node, double distance ) |
| 6664 | { |
| 6665 | if( node == NULL ) return; |
| 6666 | |
| 6667 | struct knn_list * head = &tree->knn_list_head; |
| 6668 | struct knn_list * p = head->prev; |
| 6669 | if( tree->knn_num == 1 ) |
| 6670 | { |
| 6671 | if( p->distance > distance ) |
| 6672 | { |
| 6673 | p = p->prev; |
| 6674 | } |
| 6675 | } |
| 6676 | else |
| 6677 | { |
| 6678 | while( p != head && p->distance > distance ) |
| 6679 | { |
| 6680 | p = p->prev; |
| 6681 | } |
| 6682 | } |
| 6683 | |
| 6684 | if( p == head || coord_cmp( p->node->coord, node->coord, tree->dim ) ) |
| 6685 | { |
| 6686 | struct knn_list * log = head->prev; |
| 6687 | /* Replace the original max one */ |
| 6688 | log->node = node; |
| 6689 | log->distance = distance; |
| 6690 | /* Remove from the max position */ |
| 6691 | head->prev = log->prev; |
| 6692 | log->prev->next = head; |
| 6693 | /* insert as a new one */ |
| 6694 | log->prev = p; |
| 6695 | log->next = p->next; |
| 6696 | p->next->prev = log; |
| 6697 | p->next = log; |
| 6698 | } |
| 6699 | } |
| 6700 | |
| 6701 | static void knn_list_clear( struct kdtree * tree ) |
| 6702 | { |
| 6703 | struct knn_list * head = &tree->knn_list_head; |
| 6704 | struct knn_list * p = head->next; |
| 6705 | while( p != head ) |
| 6706 | { |
| 6707 | struct knn_list * prev = p; |
| 6708 | p = p->next; |
| 6709 | free( prev ); |
| 6710 | } |
| 6711 | tree->knn_num = 0; |
| 6712 | } |
| 6713 | |
| 6714 | static void resize( struct kdtree * tree ) |
| 6715 | { |
| 6716 | tree->capacity *= 2; |
| 6717 | tree->coords = (double *)realloc( tree->coords, tree->dim * sizeof( double ) * tree->capacity ); |
| 6718 | tree->coord_table = (double **)realloc( tree->coord_table, sizeof( double * ) * tree->capacity ); |
| 6719 | tree->coord_indexes = (long *)realloc( tree->coord_indexes, sizeof( long ) * tree->capacity ); |
| 6720 | tree->coord_deleted = (unsigned char *)realloc( tree->coord_deleted, sizeof( char ) * tree->capacity ); |
| 6721 | tree->coord_passed = (unsigned char *)realloc( tree->coord_passed, sizeof( char ) * tree->capacity ); |
| 6722 | coord_table_reset( tree ); |
| 6723 | coord_index_reset( tree ); |
| 6724 | coord_deleted_reset( tree ); |
| 6725 | coord_passed_reset( tree ); |
| 6726 | } |
| 6727 | |
| 6728 | static void kdnode_dump( struct kdnode * node, t_CKINT dim ) |
| 6729 | { |
| 6730 | t_CKINT i; |
| 6731 | if( node->coord != NULL ) |
| 6732 | { |
| 6733 | printf( "(" ); |
| 6734 | for( i = 0; i < dim; i++ ) |
| 6735 | { |
| 6736 | if( i != dim - 1 ) |
| 6737 | { |
| 6738 | printf( "%.2f,", node->coord[i] ); |
| 6739 | } |
| 6740 | else |
| 6741 | { |
| 6742 | printf( "%.2f)\n", node->coord[i] ); |
| 6743 | } |
| 6744 | } |
| 6745 | } |
| 6746 | else |
| 6747 | { |
| 6748 | printf( "(none)\n" ); |
| 6749 | } |
| 6750 | } |
| 6751 | |
| 6752 | void kdtree_insert( struct kdtree * tree, double * coord ) |
| 6753 | { |
| 6754 | if( tree->count + 1 > tree->capacity ) |
| 6755 | { |
| 6756 | resize( tree ); |
| 6757 | } |
| 6758 | memcpy( tree->coord_table[tree->count++], coord, tree->dim * sizeof( double ) ); |
| 6759 | } |
| 6760 | |
| 6761 | static void knn_pickup( struct kdtree * tree, struct kdnode * node, double * target, t_CKINT k ) |
| 6762 | { |
| 6763 | double dist = distance( node->coord, target, tree->dim ); |
| 6764 | if( tree->knn_num < k ) |
| 6765 | { |
| 6766 | knn_list_add( tree, node, dist ); |
| 6767 | } |
| 6768 | else |
| 6769 | { |
| 6770 | if( dist < knn_max( tree ) ) |
| 6771 | { |
| 6772 | knn_list_adjust( tree, node, dist ); |
| 6773 | } |
| 6774 | else if( fabs( dist - knn_max( tree ) ) < DBL_EPSILON ) |
| 6775 | { |
| 6776 | knn_list_add( tree, node, dist ); |
| 6777 | } |
| 6778 | } |
| 6779 | } |
| 6780 | |
| 6781 | static void kdtree_search_recursive( struct kdtree * tree, |
| 6782 | struct kdnode * node, |
| 6783 | double * target, |
| 6784 | t_CKINT k, |
| 6785 | t_CKINT * pickup ) |
| 6786 | { |
| 6787 | if( node == NULL || kdnode_passed( tree, node ) ) |
| 6788 | { |
| 6789 | return; |
| 6790 | } |
| 6791 | |
| 6792 | t_CKINT r = node->r; |
| 6793 | if( !knn_search_on( tree, k, node->coord[r], target[r] ) ) |
| 6794 | { |
| 6795 | return; |
| 6796 | } |
| 6797 | |
| 6798 | if( is_leaf( node ) ) |
| 6799 | { |
| 6800 | *pickup = 1; |
| 6801 | } |
| 6802 | else |
| 6803 | { |
| 6804 | if( target[r] <= node->coord[r] ) |
| 6805 | { |
| 6806 | kdtree_search_recursive( tree, node->left, target, k, pickup ); |
| 6807 | if( *pickup || node->left == NULL ) |
| 6808 | { |
| 6809 | kdtree_search_recursive( tree, node->right, target, k, pickup ); |
| 6810 | } |
| 6811 | } |
| 6812 | else |
| 6813 | { |
| 6814 | kdtree_search_recursive( tree, node->right, target, k, pickup ); |
| 6815 | if( *pickup || node->right == NULL ) |
| 6816 | { |
| 6817 | kdtree_search_recursive( tree, node->left, target, k, pickup ); |
| 6818 | } |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | /* back track and pick up */ |
| 6823 | if( *pickup ) |
| 6824 | { |
| 6825 | tree->coord_passed[node->coord_index] = 1; |
| 6826 | knn_pickup( tree, node, target, k ); |
| 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | void kdtree_knn_search( struct kdtree * tree, double * target, t_CKINT k ) |
| 6831 | { |
| 6832 | if( k > 0 ) |
| 6833 | { |
| 6834 | t_CKINT pickup = 0; |
| 6835 | // HACK: reset for each new search |
| 6836 | coord_passed_reset( tree ); |
| 6837 | struct knn_list * p = tree->knn_list_head.next; |
| 6838 | while( p != &tree->knn_list_head ) |
| 6839 | { |
| 6840 | p->distance = 1e10; |
| 6841 | p = p->next; |
| 6842 | } |
| 6843 | // END HACK |
| 6844 | kdtree_search_recursive( tree, tree->root, target, k, &pickup ); |
| 6845 | } |
| 6846 | } |
| 6847 | |
| 6848 | //void kdtree_knn_search( struct kdtree * tree, double * target, t_CKINT k ) |
| 6849 | //{ |
| 6850 | // if( k > 0 ) |
| 6851 | // { |
| 6852 | // t_CKINT pickup = 0; |
| 6853 | // kdtree_search_recursive( tree, tree->root, target, k, &pickup ); |
| 6854 | // } |
| 6855 | //} |
| 6856 | |
| 6857 | void kdtree_delete( struct kdtree * tree, double * coord ) |
| 6858 | { |
| 6859 | t_CKINT r = 0; |
| 6860 | struct kdnode * node = tree->root; |
| 6861 | struct kdnode * parent = node; |
| 6862 | |
| 6863 | while( node != NULL ) |
| 6864 | { |
| 6865 | if( node->coord == NULL ) |
| 6866 | { |
| 6867 | if( parent->right->coord == NULL ) |
| 6868 | { |
| 6869 | break; |
| 6870 | } |
| 6871 | else |
| 6872 | { |
| 6873 | node = parent->right; |
| 6874 | continue; |
| 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | if( coord[r] < node->coord[r] ) |
| 6879 | { |
| 6880 | parent = node; |
| 6881 | node = node->left; |
| 6882 | } |
| 6883 | else if( coord[r] > node->coord[r] ) |
| 6884 | { |
| 6885 | parent = node; |
| 6886 | node = node->right; |
| 6887 | } |
| 6888 | else |
| 6889 | { |
| 6890 | t_CKINT ret = coord_cmp( coord, node->coord, tree->dim ); |
| 6891 | if( ret < 0 ) |
| 6892 | { |
| 6893 | parent = node; |
| 6894 | node = node->left; |
| 6895 | } |
| 6896 | else if( ret > 0 ) |
| 6897 | { |
| 6898 | parent = node; |
| 6899 | node = node->right; |
| 6900 | } |
| 6901 | else |
| 6902 | { |
| 6903 | node->coord = NULL; |
| 6904 | break; |
| 6905 | } |
| 6906 | } |
| 6907 | r = ( r + 1 ) % tree->dim; |
| 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | static void kdnode_build( struct kdtree * tree, struct kdnode ** nptr, t_CKINT r, long low, long high ) |
| 6912 | { |
| 6913 | if( low == high ) |
| 6914 | { |
| 6915 | long index = tree->coord_indexes[low]; |
| 6916 | *nptr = kdnode_alloc( tree->coord_table[index], index, r ); |
| 6917 | } |
| 6918 | else if( low < high ) |
| 6919 | { |
| 6920 | /* Sort and fetch the median to build a balanced BST */ |
| 6921 | quicksort( tree, low, high, r ); |
| 6922 | long median = low + ( high - low ) / 2; |
| 6923 | long median_index = tree->coord_indexes[median]; |
| 6924 | struct kdnode * node = *nptr = kdnode_alloc( tree->coord_table[median_index], median_index, r ); |
| 6925 | r = ( r + 1 ) % tree->dim; |
| 6926 | kdnode_build( tree, &node->left, r, low, median - 1 ); |
| 6927 | kdnode_build( tree, &node->right, r, median + 1, high ); |
| 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | static void kdtree_build( struct kdtree * tree ) |
| 6932 | { |
| 6933 | kdnode_build( tree, &tree->root, 0, 0, (long)( tree->count - 1 ) ); |
| 6934 | } |
| 6935 | |
| 6936 | void kdtree_rebuild( struct kdtree * tree ) |
| 6937 | { |
| 6938 | long i, j; |
| 6939 | size_t size_of_coord = tree->dim * sizeof( double ); |
| 6940 | for( i = 0, j = 0; j < tree->count; i++, j++ ) |
| 6941 | { |
| 6942 | while( j < tree->count && tree->coord_deleted[j] ) |
| 6943 | { |
| 6944 | j++; |
| 6945 | } |
| 6946 | if( i != j && j < tree->count ) |
| 6947 | { |
| 6948 | memcpy( tree->coord_table[i], tree->coord_table[j], size_of_coord ); |
| 6949 | tree->coord_deleted[i] = 0; |
| 6950 | } |
| 6951 | } |
| 6952 | tree->count = i; |
| 6953 | coord_index_reset( tree ); |
| 6954 | kdtree_build( tree ); |
| 6955 | } |
| 6956 | |
| 6957 | struct kdtree * kdtree_init( t_CKINT dim ) |
| 6958 | { |
| 6959 | struct kdtree * tree = (struct kdtree *)malloc( sizeof( *tree ) ); |
| 6960 | if( tree != NULL ) |
| 6961 | { |
| 6962 | tree->root = NULL; |
| 6963 | tree->dim = dim; |
| 6964 | tree->count = 0; |
| 6965 | tree->capacity = 65536; |
| 6966 | tree->knn_list_head.next = &tree->knn_list_head; |
| 6967 | tree->knn_list_head.prev = &tree->knn_list_head; |
| 6968 | tree->knn_list_head.node = NULL; |
| 6969 | tree->knn_list_head.distance = 0; |
| 6970 | tree->knn_num = 0; |
| 6971 | tree->coords = (double *)malloc( dim * sizeof( double ) * tree->capacity ); |
| 6972 | tree->coord_table = (double **)malloc( sizeof( double * ) * tree->capacity ); |
| 6973 | tree->coord_indexes = (long *)malloc( sizeof( long ) * tree->capacity ); |
| 6974 | tree->coord_deleted = (unsigned char *)malloc( sizeof( char ) * tree->capacity ); |
| 6975 | tree->coord_passed = (unsigned char *)malloc( sizeof( char ) * tree->capacity ); |
| 6976 | coord_index_reset( tree ); |
| 6977 | coord_table_reset( tree ); |
| 6978 | coord_deleted_reset( tree ); |
| 6979 | coord_passed_reset( tree ); |
| 6980 | } |
| 6981 | return tree; |
| 6982 | } |
| 6983 | |
| 6984 | static void kdnode_destroy( struct kdnode * node ) |
| 6985 | { |
| 6986 | if( node == NULL ) return; |
| 6987 | kdnode_destroy( node->left ); |
| 6988 | kdnode_destroy( node->right ); |
| 6989 | kdnode_free( node ); |
| 6990 | } |
| 6991 | |
| 6992 | void kdtree_destroy( struct kdtree * tree ) |
| 6993 | { |
| 6994 | kdnode_destroy( tree->root ); |
| 6995 | knn_list_clear( tree ); |
| 6996 | free( tree->coords ); |
| 6997 | free( tree->coord_table ); |
| 6998 | free( tree->coord_indexes ); |
| 6999 | free( tree->coord_deleted ); |
| 7000 | free( tree->coord_passed ); |
| 7001 | free( tree ); |
| 7002 | } |