16b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua/* 26b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * Copyright (C) 2011 The Android Open Source Project 36b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * 46b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * Licensed under the Apache License, Version 2.0 (the "License"); 56b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * you may not use this file except in compliance with the License. 66b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * You may obtain a copy of the License at 76b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * 86b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * http://www.apache.org/licenses/LICENSE-2.0 96b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * 106b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * Unless required by applicable law or agreed to in writing, software 116b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * distributed under the License is distributed on an "AS IS" BASIS, 126b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * See the License for the specific language governing permissions and 146b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua * limitations under the License. 156b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua */ 166b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 176b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua#include "jni/jni_multiclass_pa.h" 186b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua#include "native/multiclass_pa.h" 196b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 206b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua#include <vector> 216b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 226b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huausing learningfw::MulticlassPA; 236b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huausing std::vector; 246b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huausing std::pair; 256b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 266b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huavoid CreateIndexValuePairs(const int* indices, const float* values, 276b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua const int length, vector<pair<int, float> >* pairs) { 286b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua pairs->clear(); 296b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 306b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua for (int i = 0; i < length; ++i) { 316b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua pair<int, float> new_pair(indices[i], values[i]); 326b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua pairs->push_back(new_pair); 336b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua } 346b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua} 356b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 3613223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikasjlong Java_android_bordeaux_learning_MulticlassPA_initNativeClassifier(JNIEnv* /* env */, 3713223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikas jobject /* thiz */, 386b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jint num_classes, 396b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jint num_dims, 406b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jfloat aggressiveness) { 416b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua MulticlassPA* classifier = new MulticlassPA(num_classes, 426b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua num_dims, 436b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua aggressiveness); 4455f6e8b32805d662fe05e2e3a8ea6dd9d303a324Marcus Oakland return ((jlong) classifier); 456b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua} 466b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 476b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 4813223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikasjboolean Java_android_bordeaux_learning_MulticlassPA_deleteNativeClassifier(JNIEnv* /* env */, 4913223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikas jobject /* thiz */, 5055f6e8b32805d662fe05e2e3a8ea6dd9d303a324Marcus Oakland jlong paPtr) { 516b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua MulticlassPA* classifier = (MulticlassPA*) paPtr; 526b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua delete classifier; 536b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua return JNI_TRUE; 546b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua} 556b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 566b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huajboolean Java_android_bordeaux_learning_MulticlassPA_nativeSparseTrainOneExample(JNIEnv* env, 5713223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikas jobject /* thiz */, 586b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jintArray index_array, 596b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jfloatArray value_array, 606b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jint target, 6155f6e8b32805d662fe05e2e3a8ea6dd9d303a324Marcus Oakland jlong paPtr) { 626b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua MulticlassPA* classifier = (MulticlassPA*) paPtr; 636b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 646b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua if (classifier && index_array && value_array) { 656b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 666b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jfloat* values = env->GetFloatArrayElements(value_array, NULL); 676b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jint* indices = env->GetIntArrayElements(index_array, NULL); 686b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua const int value_len = env->GetArrayLength(value_array); 696b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua const int index_len = env->GetArrayLength(index_array); 706b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 716b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua if (values && indices && value_len == index_len) { 726b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua vector<pair<int, float> > inputs; 736b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 746b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua CreateIndexValuePairs(indices, values, value_len, &inputs); 756b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua classifier->SparseTrainOneExample(inputs, target); 766b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT); 776b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT); 786b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 796b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua return JNI_TRUE; 806b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua } 816b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT); 826b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT); 836b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua } 846b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 856b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua return JNI_FALSE; 866b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua} 876b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 886b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 896b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Huajint Java_android_bordeaux_learning_MulticlassPA_nativeSparseGetClass(JNIEnv* env, 9013223cca22c388bf9c4941fca3c92c5acfe73650Aurimas Liutikas jobject /* thiz */, 916b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jintArray index_array, 926b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jfloatArray value_array, 9355f6e8b32805d662fe05e2e3a8ea6dd9d303a324Marcus Oakland jlong paPtr) { 946b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 956b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua MulticlassPA* classifier = (MulticlassPA*) paPtr; 966b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 976b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua if (classifier && index_array && value_array) { 986b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 996b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jfloat* values = env->GetFloatArrayElements(value_array, NULL); 1006b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua jint* indices = env->GetIntArrayElements(index_array, NULL); 1016b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua const int value_len = env->GetArrayLength(value_array); 1026b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua const int index_len = env->GetArrayLength(index_array); 1036b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 1046b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua if (values && indices && value_len == index_len) { 1056b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua vector<pair<int, float> > inputs; 1066b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua CreateIndexValuePairs(indices, values, value_len, &inputs); 1076b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT); 1086b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT); 1096b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua return classifier->SparseGetClass(inputs); 1106b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua } 1116b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseIntArrayElements(index_array, indices, JNI_ABORT); 1126b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua env->ReleaseFloatArrayElements(value_array, values, JNI_ABORT); 1136b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua } 1146b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua 1156b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua return -1; 1166b4eebc73439cbc3ddfb547444a341d1f9be7996Wei Hua} 117