2012-11-29 48 views
4

我試圖操作liblinear庫(java),並且我使用了一個超級簡單的例子,發現模板爲here。 這種情況的例子是確定一個形狀是正方形還是矩形。liblinear(在java中)簡單的例子不起作用

這裏是我的代碼:

import java.io.File; 
import java.io.IOException; 

import de.bwaldvogel.liblinear.Feature; 
import de.bwaldvogel.liblinear.FeatureNode; 
import de.bwaldvogel.liblinear.Linear; 
import de.bwaldvogel.liblinear.Model; 
import de.bwaldvogel.liblinear.Parameter; 
import de.bwaldvogel.liblinear.Problem; 
import de.bwaldvogel.liblinear.SolverType; 

public class Main { 

    static int NUM_OF_TS_EXAMPLES = 8; 

    // 1 = square, -1 = non-square 
    static double[] GROUPS_ARRAY = {1, 1, 1, 1, -1, -1, -1, -1}; 

    // squares 
    static FeatureNode[] shape1 = {new FeatureNode(1, 2), new FeatureNode(2, 2)}; 
    static FeatureNode[] shape2 = {new FeatureNode(1, 4), new FeatureNode(2, 4)}; 
    static FeatureNode[] shape3 = {new FeatureNode(1, 9), new FeatureNode(2, 9)}; 
    static FeatureNode[] shape4 = {new FeatureNode(1, 10), new FeatureNode(2, 10)}; 

    // not squares 
    static FeatureNode[] shape5 = {new FeatureNode(1, 5), new FeatureNode(2, 6)}; 
    static FeatureNode[] shape6 = {new FeatureNode(1, 3), new FeatureNode(2, 4)}; 
    static FeatureNode[] shape7 = {new FeatureNode(1, 6), new FeatureNode(2, 9)}; 
    static FeatureNode[] shape8 = {new FeatureNode(1, 4), new FeatureNode(2, 2)}; 

    // unknown squares 
    static FeatureNode[] unkown1 = {new FeatureNode(1, 32), new FeatureNode(2, 32)}; 
    static FeatureNode[] unkown2 = {new FeatureNode(1, 4), new FeatureNode(2, 2)}; 
    static FeatureNode[] unkown3 = {new FeatureNode(1, 4), new FeatureNode(2, 2)}; 
    static FeatureNode[][] trainingSetWithUnknown = { 
     shape1, 
     shape2, 
     shape3, 
     shape4, 
     shape5, 
     shape6, 
     shape7, 
     shape8 
    }; 
    public static void main(String[] args) throws IOException { 

     Problem problem = new Problem(); 

     // number of training examples 
     problem.l = NUM_OF_TS_EXAMPLES; 

     // number of features 
     problem.n = NUM_OF_TS_EXAMPLES + 1; 

     // problem.x = ... // feature nodes 
     problem.x = trainingSetWithUnknown; 

     // problem.y = ... // target values 
     problem.y = GROUPS_ARRAY; 

     SolverType solver = SolverType.L2R_LR; // -s 0 
     double C = 1.0; // cost of constraints violation 
     double eps = 0.01; // stopping criteria 

     Parameter parameter = new Parameter(solver, C, eps); 
     Model model = Linear.train(problem, parameter); 
     File modelFile = new File("model"); 
     model.save(modelFile); 
     // load model or use it directly 
     model = Model.load(modelFile); 

     Feature[] instance = new FeatureNode[5]; 
     double prediction = Linear.predict(model, instance); 
    } 
} 

運行後它,我得到這樣的結果在我的控制檯:

iter 1 act 1.969e-02 pre 1.966e-02 delta 4.283e-03 f 5.545e+00 |g| 9.192e+00 CG 1 

Exception in thread "main" java.lang.NullPointerException 
at de.bwaldvogel.liblinear.Linear.predictValues(Linear.java:370) 
at de.bwaldvogel.liblinear.Linear.predict(Linear.java:316) 
at Main.main(Main.java:73) 

我在做什麼錯?

+0

其從http://grepcode.com/file/repo1.maven.org/maven2/de.bwaldvogel/liblinear/未能上線370 1.91/DE/bwaldvogel/liblinear/Linear.java?AV = F嘗試將特徵節點更改爲8並嘗試。我不確定它指的是什麼。 –

+0

同樣在這裏......我不知道什麼是'Feature [] instance = new FeatureNode [5];'指的是。無論如何,更改數組的大小並沒有幫助,因爲它的所有成員都是NULL –

+0

我更新了http://liblinear.bwaldvogel.de上的示例。 –

回答

1

我猜測,NullPointerException的原因是要傳遞的功能未初始化數組預測功能 - 嘗試初始化instance第一:

Feature[] instance = { new FeatureNode(1, 4), new FeatureNode(2, 2) }; 
+0

但我已經在這裏做: 'problem.x = trainingSetWithUnknown;' –

+1

@rebertstrack 把'Feature [] instance = unkown1;''修復了問題! 我現在得到的結果如下: 'iter 1 act 3.378e-01 pre 3.335e-01 delta 4.751e-01 f 5.545e + 00 | g | 4.031e + 00 CG 2 iter 2 act 2.331e-04 pre 2.327e-04 delta 4.751e-01 f 5.207e + 00 | g | 1.383e-01 CG 2' 謝謝大家! –

+0

感謝您的建議。我更新了http://liblinear.bwaldvogel.de上的示例。 –

0

除此之外空指針問題的例子只產生怪異的預測,主要錯號碼的功能,因爲,這裏是我的版本:

public class LibLinearTest { 

// 1 = quadratic, -1 = non-quadratic 
static double[] GROUPS_ARRAY = {1, 1, 1, 1, -1, -1, -1, -1}; 

// quadratic 
static FeatureNode[] tp1 = {new FeatureNode(1, 2), new FeatureNode(2, 4)}; 
static FeatureNode[] tp2 = {new FeatureNode(1, 4), new FeatureNode(2, 8)}; 
static FeatureNode[] tp3 = {new FeatureNode(1, 9), new FeatureNode(2, 81)}; 
static FeatureNode[] tp4 = {new FeatureNode(1, 10), new FeatureNode(2, 100)}; 

// not quadratic 
static FeatureNode[] tp5 = {new FeatureNode(1, 5), new FeatureNode(2, 6)}; 
static FeatureNode[] tp6 = {new FeatureNode(1, 3), new FeatureNode(2, 4)}; 
static FeatureNode[] tp7 = {new FeatureNode(1, 6), new FeatureNode(2, 9)}; 
static FeatureNode[] tp8 = {new FeatureNode(1, 4), new FeatureNode(2, 2)}; 

// unknown 
static FeatureNode[] up1 = {new FeatureNode(1, 32), new FeatureNode(2, 32)}; 
static FeatureNode[] up2 = {new FeatureNode(1, 5), new FeatureNode(2, 25)}; 
static FeatureNode[] up3 = {new FeatureNode(1, 4), new FeatureNode(2, 2)}; 

static FeatureNode[][] trainingSetWithUnknown = { 
    tp1, tp2, tp3, tp4, tp5, tp6, tp7, tp8 
}; 


public static void main(String[] args) throws IOException { 

    Problem problem = new Problem(); 
    problem.l = trainingSetWithUnknown.length; 
    problem.n = 2; 
    problem.x = trainingSetWithUnknown; 
    problem.y = GROUPS_ARRAY; 

    SolverType solver = SolverType.L2R_LR; // -s 0 
    double C = 1.0; // cost of constraints violation 
    double eps = 0.001; // stopping criteria 

    Parameter parameter = new Parameter(solver, C, eps); 
    Model model = Linear.train(problem, parameter); 

    Feature[] instance = tp1; 
    double prediction = Linear.predict(model, instance); 

    System.out.println("prediction : " + prediction);  
} 
}