Java Getting Started

To use Comet’s Java SDK programming interface, you will first need a Comet API key.

Once you have your key, you will need to either clone Comet’s Java SDK, or download the latest release.

Experiment

The core concept of Comet.ml's Java API is Experiment interface with 2 implementations:

OnlineExperimentImpl is a specific run of a script that generated a result such as training a model on a single set of hyperparameters. OnlineExperimentImpl will allow you to log script output (stdout/stderr), parameters, metrics, and other items on any Java-based program.

While OnlineExperimentImpl is for new experiments, ApiExperiment is for existing ones. ApiExperiment will fetch or update data on previously created experiment.

CometApiImpl stands for common project queries. CometApi can fetch all user workspaces, find experiments by project id, etc.

Java Config

Before you can compile a Java program to use Comet, you will need to set your API key, project and workspace names in your defaults.conf file.

The defaults.conf should look similar to:

comet {
   url = "https://www.comet.ml"
   maxAuthRetries = 4
   apiKey = "XXXX"
   project = "java-sdk"
   workspace = "my-team"
}

This file should be saved in your comet-examples/src/main/resources/defaults.conf file.

Java Example

Please see the complete Comet Java SDK in our Github repository.

Here is an end-to-end example showing the Java SDK in action. This file is saved in comet-examples/src/main/java/com/comet/examples/OnlineExperimentExample.java:

package ml.comet.examples;

import ml.comet.experiment.OnlineExperiment;
import ml.comet.experiment.OnlineExperimentImpl;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;


public class OnlineExperimentExample {

    public static void main(String[] args) throws IOException {
        //this will take configs from /comet-java-sdk/comet-examples/src/main/resources/defaults.conf
        //be sure you have set up apiKey, project, workspace in defaults.conf before you start!

        OnlineExperiment experiment = new OnlineExperimentImpl();
        experiment.setInterceptStdout();

        //you can use a builder or just inject params
        //OnlineExperimentImpl.builder();

        experiment.setExperimentName("Java-SDK15");
        experiment.nextStep();

        //metric can be a number, string , or double
        experiment.logMetric("strMetric", "123");
        experiment.logMetric("numMetric", 123, 123);
        experiment.logMetric("doubleMetric", 123.5d);

        generateCharts(experiment);

        experiment.setStep(1234);

        experiment.logHtml(generateCustomHtmlReport(), false);

        experiment.logParameter("batch_size", "500");
        experiment.logParameter("learning_rate", 12);

        experiment.uploadAsset(getFile("chart.png"), "amazing chart.png", false);
        experiment.uploadAsset(getFile("model.hd5"), false);

        experiment.logOther("Parameter", 4);

        System.out.println("Epoch 1/20");
        System.out.println("- loss: 0.7858 - acc: 0.7759 - val_loss: 0.3416 - val_acc: 0.9026");

        experiment.logGraph(loadGraph("graph.json"));

        //will close connection, if not called connection will close on jvm exit
        experiment.end();
    }

    public static String askUserForInputOn(String message) {
        System.out.println(message);
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        scan.close();
        return s;
    }

    private static File getFile(String imageName) {
        File file = new File(OnlineExperimentExample.class.getClassLoader().getResource(imageName).getFile());
        return file;
    }

    private static void generateCharts(OnlineExperiment experiment){
        long currentStep = experiment.getStep();

        for (int i = 1; i < 15; i++) {
            experiment.logMetric("numMetric", 123 + i, currentStep + i);
        }

        for (int i = 1; i < 15; i++) {
            experiment.logMetric("strMetric", "123" + i, currentStep + i);
        }

        for (int i = 1; i < 15; i++) {
            experiment.logMetric("doubleMetric", 123.12d + i, currentStep + i);
        }
    }

    private static String generateCustomHtmlReport() throws IOException {
        File file = new File(OnlineExperimentExample.class.getClassLoader().getResource("report.html").getFile());
        return FileUtils.readFileToString(file, "UTF-8");
    }

    private static String loadGraph(String fileName) throws IOException {
        File file = new File(OnlineExperimentExample.class.getClassLoader().getResource(fileName).getFile());
        return FileUtils.readFileToString(file, "UTF-8");
    }

}

For more information, please see: