gpt4 book ai didi

java - liblinear(在 java 中)简单的例子不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 03:41:20 24 4
gpt4 key购买 nike

我正在尝试操作 liblinear 库 (java),并且我正在使用一个 super 简单的示例,其中包含找到的模板 here .案例是判断一个形状是正方形还是长方形。

这是我的代码:

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)

我做错了什么?

最佳答案

我猜 NullPointerException 的原因是您将未初始化的特征数组传递给预测函数 - 首先尝试初始化 instance:

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

关于java - liblinear(在 java 中)简单的例子不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com