Vowpal Wabbit execute without writing to disk

2019-09-17 00:39发布

问题:

I wrote a java code to execute Vowpal Wabbit in the following way:

 System.out.println("Executing command " + command);
        final Runtime r = Runtime.getRuntime();
        final Process p = r.exec(command);
        System.out.println("waiting for the process");
        try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
            String line;

            while ((line = b.readLine()) != null) {
                final T lineResult = textParser.parseLine(line);
                parserResultCombiner.addToCombiner(lineResult);
            }
        }
        p.waitFor();
        System.out.println("done");
}

where the command is

vw -d input.txt --loss_function=logistic -f model.vw

The disadvantage of this is that it requires writing to disk. After some searching, I learned that vowpal wabbit supports reading data from standard input example in R

I could not find any example to accomplish this in Java 1.8. Could anyone share one with me?

回答1:

You need to start vw in daemon mode. This starts a process that listens on the port specified.

$ vw -i model.vw  -t --daemon --quiet --port 26542

Once the daemon has started, you can send samples to predict using socket calls

$ echo " abc-example| a b c" | netcat localhost 26542
0.000000 abc-example

$ echo " xyz-example| x y z" | netcat localhost 26542
1.000000 xyz-example

source: https://github.com/JohnLangford/vowpal_wabbit/wiki/daemon-example

Recently they pushed a java version of the code that interacts with vw using jni https://github.com/JohnLangford/vowpal_wabbit/tree/master/java