blob: 438398d0fa623d0aba7278d5f1869af23bf70361 [file] [log] [blame]
Wei Hua6b4eebc2012-03-09 10:24:16 -08001/*
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
17package android.bordeaux.services;
18
19import android.bordeaux.learning.MulticlassPA;
Wei Hua1dd8ef52012-03-30 15:15:12 -070020import android.os.IBinder;
21
Wei Hua6b4eebc2012-03-09 10:24:16 -080022import java.util.List;
23import java.util.ArrayList;
24
Wei Hua1dd8ef52012-03-30 15:15:12 -070025public class Learning_MulticlassPA extends ILearning_MulticlassPA.Stub
26 implements IBordeauxLearner {
Wei Hua6b4eebc2012-03-09 10:24:16 -080027 private MulticlassPA mMulticlassPA_learner;
Wei Hua1dd8ef52012-03-30 15:15:12 -070028 private ModelChangeCallback modelChangeCallback = null;
Wei Hua6b4eebc2012-03-09 10:24:16 -080029
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 Hua1dd8ef52012-03-30 15:15:12 -070051 // 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 Hua6b4eebc2012-03-09 10:24:16 -080069 // 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 Hua1dd8ef52012-03-30 15:15:12 -070077 if (modelChangeCallback != null) {
78 modelChangeCallback.modelChanged(this);
79 }
Wei Hua6b4eebc2012-03-09 10:24:16 -080080 }
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}