Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use exp(F) as likelihood function.

  • Loading branch information...
commit 7efd7c40fb51d194289a68df1cbf47df8011ce12 1 parent 7ba7da3
@skywaLKer518 authored
Showing with 97 additions and 10 deletions.
  1. +25 −3 src/Vector4.java
  2. +71 −7 src/iSVM.java
  3. +1 −0  src/main.java
View
28 src/Vector4.java
@@ -7,6 +7,10 @@
class Vector4 extends Data{
private double v1[],v2[],v3[],v4[];
private int lable[];
+
+ // test TODO
+ private double F = 0;
+ private double num = 0;
Vector4(){
v1 = new double[Environment.dataSetSize];
v2 = new double[Environment.dataSetSize];
@@ -61,12 +65,24 @@ public double disFunc(double[] eta,int i){ // in train
}
public double disFunc(double[] eta,int i,int y){ // in test
+ double f = 0;
if (y == 0){// then f(y,x) = (x[1],x[2],x[3],x[4],0,0,0,0)
- return (eta[0] * v1[i] + eta[1] * v2[i] + eta[2] * v3[i] + eta[3] * v4[i]);
+ f =(eta[0] * v1[i] + eta[1] * v2[i] + eta[2] * v3[i] + eta[3] * v4[i]);
+// printV(i);
+// System.out.println("F = "+f);
+ F += f;
+ num ++;
+ return f;
}
else{ // then f(y,x) = (0,0,0,0,x[1],x[2],x[3],x[4])
- return (eta[4] * v1[i] + eta[5] * v2[i] + eta[6] * v3[i] + eta[7] * v4[i]);
+ f = (eta[4] * v1[i] + eta[5] * v2[i] + eta[6] * v3[i] + eta[7] * v4[i]);
+// printV(i);
+// System.out.println("F = "+f);
+ F += f;
+ num ++;
+ return f;
}
+
}
public int lableTest(int predic,int i){
@@ -79,6 +95,12 @@ public int getLable(int i){
public void printV(int i){
System.out.println("the "+i+"th : "+v1[i]+" "+v2[i]+" "+v3[i]+" "+v4[i]+" Lable: "+lable[i]);
}
-
+ public void printF(){
+ System.out.println();System.out.println();
+ System.out.println("F = "+F);
+ System.out.println("num= "+num);
+ System.out.println("average: "+F * 1.0 / num);
+ System.out.println();System.out.println();
+ }
}
View
78 src/iSVM.java
@@ -10,7 +10,7 @@
private static final double alphaDP = 1.0;
private final int etaLength1 = 8; // for data setting 1
private final int R = 5; // repeating times in Algorithm 5
- private final int Times = 5000; // TODO
+ private final int Times = 100; // TODO
private int z[];
private double eta[][];
private int prediction[];
@@ -52,7 +52,7 @@
public void go(Vector4 v4, int setSize, int trainSize) {
init();
train(v4,setSize,trainSize);
- test(v4,setSize,trainSize);
+ test2(v4,setSize,trainSize);
}
private void train(Vector4 v4, int setSize, int trainSize) {
@@ -66,6 +66,17 @@ private void train(Vector4 v4, int setSize, int trainSize) {
change ++;
}
}
+
+ // without learning, totally random
+ /*
+ for (int i = 0; i < 4; i++){
+ drawEta(i);
+ }
+ number[1] = 33;
+ number[2] = 33;
+ number[3] = 34;
+ cateIndexMax = 3;
+ */
}
private void init() {
@@ -86,17 +97,49 @@ private void init() {
/*
* test ( from 100 to 10000 - 1 )
+ * regard p(z = i) = number[i] / trainSize;
*/
private void test(Vector4 v4, int setSize, int trainSize) {
double disF0,disF1;score = 0;
- for (int i = 100; i < Environment.dataSetSize; i++){
+ for (int i = trainSize; i < Environment.dataSetSize; i++){
+ disF0 = 0;
+ disF1 = 0;
+ for (int j = 0; j <= cateIndexMax; j++){
+ if (number[j] <= 0)
+ continue;
+ disF0 += v4.disFunc(eta[j],i,0) * number[j] / (1.0 * trainSize);
+ disF1 += v4.disFunc(eta[j],i,1) * number[j] / (1.0 * trainSize);
+ }
+ if (disF0 > disF1) prediction[i] = 0;
+ else prediction[i] = 1;
+
+ //test TODO
+ if (prediction[i] == 0) predic0++;
+ else predic1++;
+ if (v4.getLable(i) == 0) data0++;
+ else data1++;
+
+ score += v4.lableTest(prediction[i], i);
+
+ }
+ }
+
+ /*
+ * test ( from 100 to 10000 - 1 )
+ * regard p(z = i) = 1 / cateNumber;
+ */
+ private void test2(Vector4 v4, int setSize, int trainSize) {
+ double disF0,disF1;score = 0;
+ for (int i = trainSize; i < Environment.dataSetSize; i++){
disF0 = 0;
disF1 = 0;
for (int j = 0; j <= cateIndexMax; j++){
if (number[j] <= 0)
continue;
- disF0 += v4.disFunc(eta[j],i,0) * number[j] / (1.0 * cateNumber);
- disF1 += v4.disFunc(eta[j],i,1) * number[j] / (1.0 * cateNumber);
+ disF0 += v4.disFunc(eta[j],i,0) * 1 / (1.0 * cateNumber);
+ disF1 += v4.disFunc(eta[j],i,1) * 1 / (1.0 * cateNumber);
+// System.out.println("F =: (0)"+v4.disFunc(eta[j],i,0) * 1 / (1.0 * cateNumber));
+// System.out.println("F =: (1)"+v4.disFunc(eta[j],i,0) * 1 / (1.0 * cateNumber));
}
if (disF0 > disF1) prediction[i] = 0;
else prediction[i] = 1;
@@ -107,7 +150,27 @@ private void test(Vector4 v4, int setSize, int trainSize) {
if (v4.getLable(i) == 0) data0++;
else data1++;
+ int t = score; // test
score += v4.lableTest(prediction[i], i);
+// if (t == score){ // wrong
+// System.out.print("--------------\nWrong\n :\n");
+// v4.printV(i);
+// System.out.println(cateNumber+" component");
+// for (int j = 0; j <= cateIndexMax; j++){
+// if (number[j] <= 0)
+// continue;
+// System.out.println("com "+j+" :"+v4.disFunc(eta[j],i,prediction[i]));
+// }
+// }
+// else{
+// System.out.print("--------------\nCorrect\n :\n");
+// v4.printV(i);
+// for (int j = 0; j <= cateIndexMax; j++){
+// if (number[j] <= 0)
+// continue;
+// System.out.println("com "+j+" :"+v4.disFunc(eta[j],i,prediction[i]));
+// }
+// }
}
}
@@ -120,7 +183,7 @@ public void evaluation() {
/*
- * here I use F(x,y,eta,z) (in paper iSVM, discriminative function) as the likelihood function.(problem //TODO)
+ * here I use exp_F(x,y,eta,z) (in paper iSVM, discriminative function) as the likelihood function.(problem //TODO)
*/
private void step1(Vector4 v4, int setSize, int trainSize) {
int c;
@@ -141,7 +204,8 @@ private void step1(Vector4 v4, int setSize, int trainSize) {
if (m2 >= m1)
accept = 1;
else
- accept = m2/m1;
+// accept = m2/m1;
+ accept = Math.exp(m2 - m1);
r = Math.random();
if (r <= accept){
updateCate(k,c);
View
1  src/main.java
@@ -21,6 +21,7 @@ public static void main(String args[]) throws IOException{
lk.evaluation();
}
lk.report();
+ v4.printF();
System.out.println(lk.changeTimes);
// for (int i = 5000; i < 5030; i ++){
Please sign in to comment.
Something went wrong with that request. Please try again.