Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bordeaux.services; |
| 18 | |
| 19 | import android.bordeaux.learning.MulticlassPA; |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame^] | 20 | import android.os.IBinder; |
| 21 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.ArrayList; |
| 24 | |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame^] | 25 | public class Learning_MulticlassPA extends ILearning_MulticlassPA.Stub |
| 26 | implements IBordeauxLearner { |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 27 | private MulticlassPA mMulticlassPA_learner; |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame^] | 28 | private ModelChangeCallback modelChangeCallback = null; |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 29 | |
| 30 | class IntFloatArray { |
| 31 | int[] indexArray; |
| 32 | float[] floatArray; |
| 33 | }; |
| 34 | |
| 35 | private IntFloatArray splitIntFloatArray(List<IntFloat> sample) { |
| 36 | IntFloatArray splited = new IntFloatArray(); |
| 37 | ArrayList<IntFloat> s = (ArrayList<IntFloat>)sample; |
| 38 | splited.indexArray = new int[s.size()]; |
| 39 | splited.floatArray = new float[s.size()]; |
| 40 | for (int i = 0; i < s.size(); i++) { |
| 41 | splited.indexArray[i] = s.get(i).index; |
| 42 | splited.floatArray[i] = s.get(i).value; |
| 43 | } |
| 44 | return splited; |
| 45 | } |
| 46 | |
| 47 | public Learning_MulticlassPA() { |
| 48 | mMulticlassPA_learner = new MulticlassPA(2, 2, 0.001f); |
| 49 | } |
| 50 | |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame^] | 51 | // Beginning of the IBordeauxLearner Interface implementation |
| 52 | public byte [] getModel() { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | public boolean setModel(final byte [] modelData) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | public IBinder getBinder() { |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | public void setModelChangeCallback(ModelChangeCallback callback) { |
| 65 | modelChangeCallback = callback; |
| 66 | } |
| 67 | // End of IBordeauxLearner Interface implemenation |
| 68 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 69 | // This implementation, combines training and prediction in one step. |
| 70 | // The return value is the prediction value for the supplied sample. It |
| 71 | // also update the model with the current sample. |
| 72 | public void TrainOneSample(List<IntFloat> sample, int target) { |
| 73 | IntFloatArray splited = splitIntFloatArray(sample); |
| 74 | mMulticlassPA_learner.sparseTrainOneExample(splited.indexArray, |
| 75 | splited.floatArray, |
| 76 | target); |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame^] | 77 | if (modelChangeCallback != null) { |
| 78 | modelChangeCallback.modelChanged(this); |
| 79 | } |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public int Classify(List<IntFloat> sample) { |
| 83 | IntFloatArray splited = splitIntFloatArray(sample); |
| 84 | int prediction = mMulticlassPA_learner.sparseGetClass(splited.indexArray, |
| 85 | splited.floatArray); |
| 86 | return prediction; |
| 87 | } |
| 88 | |
| 89 | } |