org.deeplearning4j.ui.api.UIServer Java Examples

The following examples show how to use org.deeplearning4j.ui.api.UIServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUICompGraph() {

    StatsStorage ss = new InMemoryStatsStorage();

    UIServer uiServer = UIServer.getInstance();
    uiServer.attach(ss);

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in")
                    .addLayer("L0", new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(4).build(),
                                    "in")
                    .addLayer("L1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                                    .activation(Activation.SOFTMAX).nIn(4).nOut(3).build(), "L0")
                    .setOutputs("L1").build();

    ComputationGraph net = new ComputationGraph(conf);
    net.init();

    net.setListeners(new StatsListener(ss), new ScoreIterationListener(1));

    DataSetIterator iter = new IrisDataSetIterator(150, 150);

    for (int i = 0; i < 100; i++) {
        net.fit(iter);
    }
}
 
Example #2
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test (expected = DL4JException.class)
public void testUIStartPortAlreadyBound() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    //Create HttpServer that binds the same port
    int port = VertxUIServer.DEFAULT_UI_PORT;
    Vertx vertx = Vertx.vertx();
    vertx.createHttpServer()
            .requestHandler(event -> {})
            .listen(port, result -> latch.countDown());
    latch.await();

    try {
        //DL4JException signals that the port cannot be bound, UI server cannot start
        UIServer.getInstance();
    } finally {
        vertx.close();
    }
}
 
Example #3
Source File: VertxUIServer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public void main(String[] args){
    CLIParams d = new CLIParams();
    new JCommander(d).parse(args);
    instancePort = d.getCliPort();
    UIServer.getInstance(d.isCliMultiSession(), null);
    if(d.isCliEnableRemote()){
        try {
            File tempStatsFile = DL4JFileUtils.createTempFile("dl4j", "UIstats");
            tempStatsFile.delete();
            tempStatsFile.deleteOnExit();
            enableRemoteListener(new FileStatsStorage(tempStatsFile), true);
        } catch(Exception e) {
            log.error("Failed to create temporary file for stats storage",e);
            System.exit(1);
        }
    }
}
 
Example #4
Source File: TestRemoteReceiver.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void startRemoteUI() throws Exception {

    UIServer s = UIServer.getInstance();
    s.enableRemoteListener();

    Thread.sleep(1000000);
}
 
Example #5
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testUIMultipleSessions() throws Exception {

    for (int session = 0; session < 3; session++) {

        StatsStorage ss = new InMemoryStatsStorage();

        UIServer uiServer = UIServer.getInstance();
        uiServer.attach(ss);

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).list()
                .layer(0, new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(4).build())
                .layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                        .activation(Activation.SOFTMAX).nIn(4).nOut(3).build())
                .build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();
        net.setListeners(new StatsListener(ss, 1), new ScoreIterationListener(1));

        DataSetIterator iter = new IrisDataSetIterator(150, 150);

        for (int i = 0; i < 20; i++) {
            net.fit(iter);
            Thread.sleep(100);
        }
    }
}
 
Example #6
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testUI_VAE() throws Exception {
    //Variational autoencoder - for unsupervised layerwise pretraining

    StatsStorage ss = new InMemoryStatsStorage();

    UIServer uiServer = UIServer.getInstance();
    uiServer.attach(ss);

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .updater(new Sgd(1e-5))
                    .list().layer(0,
                                    new VariationalAutoencoder.Builder().nIn(4).nOut(3).encoderLayerSizes(10, 11)
                                                    .decoderLayerSizes(12, 13).weightInit(WeightInit.XAVIER)
                                                    .pzxActivationFunction(Activation.IDENTITY)
                                                    .reconstructionDistribution(
                                                                    new GaussianReconstructionDistribution())
                                                    .activation(Activation.LEAKYRELU).build())
                    .layer(1, new VariationalAutoencoder.Builder().nIn(3).nOut(3).encoderLayerSizes(7)
                                    .decoderLayerSizes(8).weightInit(WeightInit.XAVIER)
                                    .pzxActivationFunction(Activation.IDENTITY)
                                    .reconstructionDistribution(new GaussianReconstructionDistribution())
                                    .activation(Activation.LEAKYRELU).build())
                    .layer(2, new OutputLayer.Builder().nIn(3).nOut(3).build())
                    .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    net.setListeners(new StatsListener(ss), new ScoreIterationListener(1));

    DataSetIterator iter = new IrisDataSetIterator(150, 150);

    for (int i = 0; i < 50; i++) {
        net.fit(iter);
        Thread.sleep(100);
    }

}
 
Example #7
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testUIAttachDetach() throws Exception {
    StatsStorage ss = new InMemoryStatsStorage();

    UIServer uiServer = UIServer.getInstance();
    uiServer.attach(ss);
    assertFalse(uiServer.getStatsStorageInstances().isEmpty());
    uiServer.detach(ss);
    assertTrue(uiServer.getStatsStorageInstances().isEmpty());
}
 
Example #8
Source File: TestVertxUIMultiSession.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test (expected = DL4JException.class)
public void testUIServerGetInstanceMultipleCalls1() {
    UIServer uiServer = UIServer.getInstance();
    assertFalse(uiServer.isMultiSession());
    UIServer.getInstance(true, null);

}
 
Example #9
Source File: ConvolutionalIterationListener.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public ConvolutionalIterationListener(StatsStorageRouter ssr, int iterations, boolean openBrowser, String sessionID,
                String workerID) {
    this.ssr = ssr;
    if (sessionID == null) {
        //TODO handle syncing session IDs across different listeners in the same model...
        this.sessionID = UUID.randomUUID().toString();
    } else {
        this.sessionID = sessionID;
    }
    if (workerID == null) {
        this.workerID = UIDProvider.getJVMUID() + "_" + Thread.currentThread().getId();
    } else {
        this.workerID = workerID;
    }

    String subPath = "activations";

    this.freq = iterations;
    this.openBrowser = openBrowser;
    path = "http://localhost:" + UIServer.getInstance().getPort() + "/" + subPath;

    if (openBrowser && ssr instanceof StatsStorage) {
        UIServer.getInstance().attach((StatsStorage) ssr);
    }

    System.out.println("ConvolutionTrainingListener path: " + path);
}
 
Example #10
Source File: ManualTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 300000)
public void testTsne() throws Exception {
    DataTypeUtil.setDTypeForContext(DataType.DOUBLE);
    Nd4j.getRandom().setSeed(123);
    BarnesHutTsne b = new BarnesHutTsne.Builder().stopLyingIteration(10).setMaxIter(10).theta(0.5).learningRate(500)
                    .useAdaGrad(true).build();

    File f = Resources.asFile("/deeplearning4j-core/mnist2500_X.txt");
    INDArray data = Nd4j.readNumpy(f.getAbsolutePath(), "   ").get(NDArrayIndex.interval(0, 100),
                    NDArrayIndex.interval(0, 784));



    ClassPathResource labels = new ClassPathResource("mnist2500_labels.txt");
    List<String> labelsList = IOUtils.readLines(labels.getInputStream()).subList(0, 100);
    b.fit(data);
    File save = new File(System.getProperty("java.io.tmpdir"), "labels-" + UUID.randomUUID().toString());
    System.out.println("Saved to " + save.getAbsolutePath());
    save.deleteOnExit();
    b.saveAsFile(labelsList, save.getAbsolutePath());

    INDArray output = b.getData();
    System.out.println("Coordinates");

    UIServer server = UIServer.getInstance();
    Thread.sleep(10000000000L);
}
 
Example #11
Source File: Main.java    From twse-captcha-solver-dl4j with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  long startTime = System.currentTimeMillis();
  logger.info("start up time: " + startTime);

  File modelDir = new File(modelDirPath);

  // create dir
  boolean hasDir = modelDir.exists() || modelDir.mkdirs();
  logger.info(modelPath);

  // create model
  ComputationGraph model = createModel();
  // monitor the model score
  UIServer uiServer = UIServer.getInstance();
  StatsStorage statsStorage = new InMemoryStatsStorage();
  uiServer.attach(statsStorage);

  model.setListeners(new ScoreIterationListener(36), new StatsListener(statsStorage));

  // construct the iterator
  MultiDataSetIterator trainMulIterator = new CaptchaSetIterator(batchSize, "train");
  MultiDataSetIterator testMulIterator = new CaptchaSetIterator(batchSize, "test");
  MultiDataSetIterator validateMulIterator = new CaptchaSetIterator(batchSize, "validate");
  // fit
  for (int i = 0; i < epochs; i++) {
    System.out.println("Epoch=====================" + i);
    model.fit(trainMulIterator);
  }
  ModelSerializer.writeModel(model, modelPath, true);
  long endTime = System.currentTimeMillis();
  System.out.println("=============run time=====================" + (endTime - startTime));

  System.out.println("=====eval model=====test==================");
  modelPredict(model, testMulIterator);

  System.out.println("=====eval model=====validate==================");
  modelPredict(model, validateMulIterator);
}
 
Example #12
Source File: TrainUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public static UIServer getUIServer() {
	if (uiServer == null) {
		uiServer = UIServer.getInstance();
	}
	
	return uiServer;
}
 
Example #13
Source File: TestParallelEarlyStoppingUI.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore //To be run manually
public void testParallelStatsListenerCompatibility() throws Exception {
    UIServer uiServer = UIServer.getInstance();

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .updater(new Sgd()).weightInit(WeightInit.XAVIER).list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new OutputLayer.Builder().nIn(3).nOut(3)
                                    .lossFunction(LossFunctions.LossFunction.MCXENT).build())
                    .build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);

    // it's important that the UI can report results from parallel training
    // there's potential for StatsListener to fail if certain properties aren't set in the model
    StatsStorage statsStorage = new InMemoryStatsStorage();
    net.setListeners(new StatsListener(statsStorage));
    uiServer.attach(statsStorage);

    DataSetIterator irisIter = new IrisDataSetIterator(50, 500);
    EarlyStoppingModelSaver<MultiLayerNetwork> saver = new InMemoryModelSaver<>();
    EarlyStoppingConfiguration<MultiLayerNetwork> esConf =
                    new EarlyStoppingConfiguration.Builder<MultiLayerNetwork>()
                                    .epochTerminationConditions(new MaxEpochsTerminationCondition(500))
                                    .scoreCalculator(new DataSetLossCalculator(irisIter, true))
                                    .evaluateEveryNEpochs(2).modelSaver(saver).build();

    IEarlyStoppingTrainer<MultiLayerNetwork> trainer =
                    new EarlyStoppingParallelTrainer<>(esConf, net, irisIter, null, 3, 6, 2);

    EarlyStoppingResult<MultiLayerNetwork> result = trainer.fit();
    System.out.println(result);

    assertEquals(EarlyStoppingResult.TerminationReason.EpochTerminationCondition, result.getTerminationReason());
}
 
Example #14
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    UIServer.stopInstance();
}
 
Example #15
Source File: TestRemoteReceiver.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testRemoteBasic() throws Exception {

    List<Persistable> updates = new ArrayList<>();
    List<Persistable> staticInfo = new ArrayList<>();
    List<StorageMetaData> metaData = new ArrayList<>();
    CollectionStatsStorageRouter collectionRouter = new CollectionStatsStorageRouter(metaData, staticInfo, updates);


    UIServer s = UIServer.getInstance();
    Thread.sleep(1000);
    s.enableRemoteListener(collectionRouter, false);


    try(RemoteUIStatsStorageRouter remoteRouter = new RemoteUIStatsStorageRouter("http://localhost:9000")) {    //Use closeable to ensure async thread is shut down

        SbeStatsReport update1 = new SbeStatsReport();
        update1.setMemoryUsePresent(true);
        update1.setDeviceCurrentBytes(new long[]{1, 2});
        update1.setDeviceMaxBytes(new long[]{100, 200});
        update1.reportIterationCount(10);
        update1.reportIDs("sid", "tid", "wid", 123456);
        update1.reportPerformance(10, 20, 30, 40, 50);

        SbeStatsReport update2 = new SbeStatsReport();
        update2.setMemoryUsePresent(true);
        update2.setDeviceCurrentBytes(new long[]{3, 4});
        update2.setDeviceMaxBytes(new long[]{300, 400});
        update2.reportIterationCount(20);
        update2.reportIDs("sid2", "tid2", "wid2", 123456);
        update2.reportPerformance(11, 21, 31, 40, 50);

        StorageMetaData smd1 = new SbeStorageMetaData(123, "sid", "typeid", "wid", "initTypeClass", "updaterTypeClass");
        StorageMetaData smd2 =
                new SbeStorageMetaData(456, "sid2", "typeid2", "wid2", "initTypeClass2", "updaterTypeClass2");

        SbeStatsInitializationReport init1 = new SbeStatsInitializationReport();
        init1.reportIDs("sid", "wid", "tid", 3145253452L);
        init1.reportHardwareInfo(1, 2, 3, 4, null, null, "2344253");
        init1.setHwDeviceTotalMemory(new long[]{1,2});
        init1.setHwDeviceDescription(new String[]{"d1", "d2"});
        init1.setHasHardwareInfo(true);

        remoteRouter.putUpdate(update1);
        Thread.sleep(100);
        remoteRouter.putStorageMetaData(smd1);
        Thread.sleep(100);
        remoteRouter.putStaticInfo(init1);
        Thread.sleep(100);
        remoteRouter.putUpdate(update2);
        Thread.sleep(100);
        remoteRouter.putStorageMetaData(smd2);


        Thread.sleep(2000);

        assertEquals(2, metaData.size());
        assertEquals(2, updates.size());
        assertEquals(1, staticInfo.size());

        assertEquals(Arrays.asList(update1, update2), updates);
        assertEquals(Arrays.asList(smd1, smd2), metaData);
        assertEquals(Collections.singletonList(init1), staticInfo);
    }
}
 
Example #16
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoAttach() throws Exception {

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in")
            .addLayer("L0", new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(4).build(),
                    "in")
            .addLayer("L1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                    .activation(Activation.SOFTMAX).nIn(4).nOut(3).build(), "L0")
            .setOutputs("L1").build();

    ComputationGraph net = new ComputationGraph(conf);
    net.init();

    StatsStorage ss1 = new InMemoryStatsStorage();

    net.setListeners(new StatsListener(ss1, 1, "ss1"));

    DataSetIterator iter = new IrisDataSetIterator(150, 150);

    for (int i = 0; i < 5; i++) {
        net.fit(iter);
    }

    StatsStorage ss2 = new InMemoryStatsStorage();
    net.setListeners(new StatsListener(ss2, 1, "ss2"));

    for (int i = 0; i < 4; i++) {
        net.fit(iter);
    }

    UIServer ui = UIServer.getInstance(true, null);
    try {
        ((VertxUIServer) ui).autoAttachStatsStorageBySessionId(new Function<String, StatsStorage>() {
            @Override
            public StatsStorage apply(String s) {
                if ("ss1".equals(s)) {
                    return ss1;
                } else if ("ss2".equals(s)) {
                    return ss2;
                }
                return null;
            }
        });

        String json1 = IOUtils.toString(new URL("http://localhost:9000/train/ss1/overview/data"),
                StandardCharsets.UTF_8);

        String json2 = IOUtils.toString(new URL("http://localhost:9000/train/ss2/overview/data"),
                StandardCharsets.UTF_8);

        assertNotEquals(json1, json2);

        Map<String, Object> m1 = JsonMappers.getMapper().readValue(json1, Map.class);
        Map<String, Object> m2 = JsonMappers.getMapper().readValue(json2, Map.class);

        List<Object> s1 = (List<Object>) m1.get("scores");
        List<Object> s2 = (List<Object>) m2.get("scores");
        assertEquals(5, s1.size());
        assertEquals(4, s2.size());
    } finally {
        ui.stop();
    }
}
 
Example #17
Source File: TestBasic.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testBasicMnistDataSource() throws InterruptedException {
    ParameterSpace<Double> learningRateHyperparam = new ContinuousParameterSpace(0.0001, 0.1);
    ParameterSpace<Integer> layerSizeHyperparam = new IntegerParameterSpace(16, 256);

    MultiLayerSpace hyperparameterSpace = new MultiLayerSpace.Builder()
            .weightInit(WeightInit.XAVIER)
            .l2(0.0001)
            .updater(new SgdSpace(learningRateHyperparam))
            .addLayer(new DenseLayerSpace.Builder()
                    .nIn(784)
                    .activation(Activation.LEAKYRELU)
                    .nOut(layerSizeHyperparam)
                    .build())
            .addLayer(new OutputLayerSpace.Builder()
                    .nOut(10)
                    .activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT)
                    .build())
            .build();
    CandidateGenerator candidateGenerator = new RandomSearchGenerator(hyperparameterSpace, null);
    ScoreFunction scoreFunction = new EvaluationScoreFunction(Evaluation.Metric.ACCURACY);
    TerminationCondition[] terminationConditions = {
            new MaxTimeCondition(5, TimeUnit.MINUTES),
            new MaxCandidatesCondition(2)};

    String modelSavePath = new File(System.getProperty("java.io.tmpdir"), "ArbiterUiTestBasicMnist\\").getAbsolutePath();

    File f = new File(modelSavePath);
    if (f.exists())
        f.delete();
    f.mkdir();
    if (!f.exists())
        throw new RuntimeException();
    Class<? extends DataSource> ds = MnistDataSource.class;
    Properties dsp = new Properties();
    dsp.setProperty("minibatch", "8");
    OptimizationConfiguration configuration = new OptimizationConfiguration.Builder()
            .candidateGenerator(candidateGenerator).dataSource(ds, dsp)
            .modelSaver(new FileModelSaver(modelSavePath))
            .scoreFunction(scoreFunction)
            .terminationConditions(terminationConditions)
            .build();

    IOptimizationRunner runner = new LocalOptimizationRunner(configuration, new MultiLayerNetworkTaskCreator());

    StatsStorage ss = new InMemoryStatsStorage();
    StatusListener sl = new ArbiterStatusListener(ss);
    runner.addListeners(sl);

    UIServer.getInstance().attach(ss);

    runner.execute();
    Thread.sleep(90000);
}
 
Example #18
Source File: TestBasic.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testBasicMnistCompGraph() throws Exception {

    ComputationGraphSpace cgs = new ComputationGraphSpace.Builder()
            .updater(new SgdSpace(new ContinuousParameterSpace(0.0001, 0.2)))
            .l2(new ContinuousParameterSpace(0.0001, 0.05))
            .addInputs("in")
            .addLayer("0",
                    new ConvolutionLayerSpace.Builder().nIn(1)
                            .nOut(new IntegerParameterSpace(5, 30))
                            .kernelSize(new DiscreteParameterSpace<>(new int[]{3, 3},
                                    new int[]{4, 4}, new int[]{5, 5}))
                            .stride(new DiscreteParameterSpace<>(new int[]{1, 1},
                                    new int[]{2, 2}))
                            .activation(new DiscreteParameterSpace<>(Activation.RELU,
                                    Activation.SOFTPLUS, Activation.LEAKYRELU))
                            .build(), "in")
            .addLayer("1", new DenseLayerSpace.Builder().nOut(new IntegerParameterSpace(32, 128))
                    .activation(new DiscreteParameterSpace<>(Activation.RELU, Activation.TANH))
                    .build(), "0")
            .addLayer("out", new OutputLayerSpace.Builder().nOut(10).activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build(), "1")
            .setOutputs("out")
            .setInputTypes(InputType.convolutionalFlat(28, 28, 1))
            .build();

    //Define configuration:
    CandidateGenerator candidateGenerator = new RandomSearchGenerator(cgs);
    DataProvider dataProvider = new MnistDataSetProvider();


    String modelSavePath = new File(System.getProperty("java.io.tmpdir"), "ArbiterUiTestBasicMnistCG\\").getAbsolutePath();

    File f = new File(modelSavePath);
    if (f.exists())
        f.delete();
    f.mkdir();
    if (!f.exists())
        throw new RuntimeException();

    OptimizationConfiguration configuration =
            new OptimizationConfiguration.Builder()
                    .candidateGenerator(candidateGenerator).dataProvider(dataProvider)
                    .modelSaver(new FileModelSaver(modelSavePath))
                    .scoreFunction(new TestSetLossScoreFunction(true))
                    .terminationConditions(new MaxTimeCondition(120, TimeUnit.MINUTES),
                            new MaxCandidatesCondition(100))
                    .build();

    IOptimizationRunner runner =
            new LocalOptimizationRunner(configuration, new ComputationGraphTaskCreator());

    StatsStorage ss = new InMemoryStatsStorage();
    StatusListener sl = new ArbiterStatusListener(ss);
    runner.addListeners(sl);

    UIServer.getInstance().attach(ss);

    runner.execute();
    Thread.sleep(100000);
}
 
Example #19
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testUI() throws Exception {
    VertxUIServer uiServer = (VertxUIServer) UIServer.getInstance();
    assertEquals(9000, uiServer.getPort());
    uiServer.stop();
}
 
Example #20
Source File: CustomerRetentionPredictionExample.java    From Java-Deep-Learning-Cookbook with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

       final int labelIndex=11;
       final int batchSize=8;
       final int numClasses=2;
       final INDArray weightsArray = Nd4j.create(new double[]{0.57, 0.75});

       final RecordReader recordReader = generateReader(new ClassPathResource("Churn_Modelling.csv").getFile());
       final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(recordReader,batchSize)
                                                                .classification(labelIndex,numClasses)
                                                                .build();
       final DataNormalization dataNormalization = new NormalizerStandardize();
       dataNormalization.fit(dataSetIterator);
       dataSetIterator.setPreProcessor(dataNormalization);
       final DataSetIteratorSplitter dataSetIteratorSplitter = new DataSetIteratorSplitter(dataSetIterator,1250,0.8);

       log.info("Building Model------------------->>>>>>>>>");

        final MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
                                                                    .weightInit(WeightInit.RELU_UNIFORM)
                                                                    .updater(new Adam(0.015D))
                                                                    .list()
                                                                    .layer(new DenseLayer.Builder().nIn(11).nOut(6).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new DenseLayer.Builder().nIn(6).nOut(6).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new DenseLayer.Builder().nIn(6).nOut(4).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new OutputLayer.Builder(new LossMCXENT(weightsArray)).nIn(4).nOut(2).activation(Activation.SOFTMAX).build())
                                                                    .build();

        final UIServer uiServer = UIServer.getInstance();
        final StatsStorage statsStorage = new InMemoryStatsStorage();

        final MultiLayerNetwork multiLayerNetwork = new MultiLayerNetwork(configuration);
        multiLayerNetwork.init();
        multiLayerNetwork.setListeners(new ScoreIterationListener(100),
                                       new StatsListener(statsStorage));
        uiServer.attach(statsStorage);
        multiLayerNetwork.fit(dataSetIteratorSplitter.getTrainIterator(),100);

        final Evaluation evaluation =  multiLayerNetwork.evaluate(dataSetIteratorSplitter.getTestIterator(),Arrays.asList("0","1"));
        System.out.println(evaluation.stats());

        final File file = new File("model.zip");
        ModelSerializer.writeModel(multiLayerNetwork,file,true);
        ModelSerializer.addNormalizerToModel(file,dataNormalization);


    }
 
Example #21
Source File: TestVertxUIManual.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public void setUIServer(UIServer uIServer) {
    this.uIServer = uIServer;
}
 
Example #22
Source File: TestVertxUIMultiSession.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test (expected = DL4JException.class)
public void testUIServerGetInstanceMultipleCalls2() {
    UIServer uiServer = UIServer.getInstance(true, null);
    assertTrue(uiServer.isMultiSession());
    UIServer.getInstance(false, null);
}
 
Example #23
Source File: TestVertxUIMultiSession.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testUIAutoAttach() throws Exception {
    HashMap<String, StatsStorage> statsStorageForSession = new HashMap<>();

    Function<String, StatsStorage> statsStorageProvider = statsStorageForSession::get;
    UIServer uIServer = UIServer.getInstance(true, statsStorageProvider);

    for (int session = 0; session < 3; session++) {
        int layerSize = session + 4;

        InMemoryStatsStorage ss = new InMemoryStatsStorage();
        String sessionId = Integer.toString(session);
        statsStorageForSession.put(sessionId, ss);
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).list()
                .layer(0, new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(layerSize).build())
                .layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                        .activation(Activation.SOFTMAX).nIn(layerSize).nOut(3).build())
                .build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        StatsListener statsListener = new StatsListener(ss, 1);
        statsListener.setSessionID(sessionId);
        net.setListeners(statsListener, new ScoreIterationListener(1));
        uIServer.attach(ss);

        DataSetIterator iter = new IrisDataSetIterator(150, 150);

        for (int i = 0; i < 20; i++) {
            net.fit(iter);
        }

        assertTrue(uIServer.isAttached(statsStorageForSession.get(sessionId)));
        uIServer.detach(ss);
        assertFalse(uIServer.isAttached(statsStorageForSession.get(sessionId)));

        /*
         * Visiting /train/:sessionId to auto-attach StatsStorage
         */
        String sessionUrl = trainingSessionUrl(uIServer.getAddress(), sessionId);
        HttpURLConnection conn = (HttpURLConnection) new URL(sessionUrl).openConnection();
        conn.connect();

        assertEquals(HttpResponseStatus.OK.code(), conn.getResponseCode());
        assertTrue(uIServer.isAttached(statsStorageForSession.get(sessionId)));
    }
}
 
Example #24
Source File: TestVertxUIMultiSession.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    UIServer.stopInstance();
}
 
Example #25
Source File: MLPMnistUIExample.java    From dl4j-tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    //number of rows and columns in the input pictures
    final int numRows = 28;
    final int numColumns = 28;
    int outputNum = 10; // number of output classes
    int batchSize = 128; // batch size for each epoch
    int rngSeed = 123; // random number seed for reproducibility
    int numEpochs = 15; // number of epochs to perform
    int listenerFrequency = 1;


    //Get the DataSetIterators:
    DataSetIterator mnistTrain = new MnistDataSetIterator(batchSize, true, rngSeed);
    DataSetIterator mnistTest = new MnistDataSetIterator(batchSize, false, rngSeed);


    log.info("Build model....");
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(rngSeed) //include a random seed for reproducibility
            // use stochastic gradient descent as an optimization algorithm
            .updater(new Nesterovs(0.006, 0.9))
            .l2(1e-4)
            .list()
            .layer(0, new DenseLayer.Builder() //create the first, input layer with xavier initialization
                    // batchSize, features
                    .nIn(numRows * numColumns)
                    .nOut(1000)
                    .activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER)
                    .build())
            .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) //create hidden layer
                    .nIn(1000)
                    .nOut(outputNum)
                    .activation(Activation.SOFTMAX)
                    .weightInit(WeightInit.XAVIER)
                    .build())
            .pretrain(false).backprop(true) //use backpropagation to adjust weights
            .build();

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    //Initialize the user interface backend
    // 获取一个UI实例
    UIServer uiServer = UIServer.getInstance();

    //Configure where the network information (gradients, activations, score vs. time etc) is to be stored
    //Then add the StatsListener to collect this information from the network, as it trains
    // 训练的存储位置
    StatsStorage statsStorage = new InMemoryStatsStorage();             //Alternative: new FileStatsStorage(File) - see UIStorageExample

    //Attach the StatsStorage instance to the UI: this allows the contents of the StatsStorage to be visualized
    uiServer.attach(statsStorage);
    model.init();
    //print the score with every 1 iteration
    model.setListeners(new StatsListener(statsStorage, listenerFrequency)
            ,new ScoreIterationListener(1)
    );

    log.info("Train model....");
    for( int i=0; i<numEpochs; i++ ){
        model.fit(mnistTrain);
    }


    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(outputNum); //create an evaluation object with 10 possible classes
    while(mnistTest.hasNext()){
        DataSet next = mnistTest.next();
        INDArray output = model.output(next.getFeatures(), false); //get the networks prediction
        eval.eval(next.getLabels(), output); //check the prediction against the true class
    }

    log.info(eval.stats());
    log.info("****************Example finished********************");
}
 
Example #26
Source File: HyperParameterTuningArbiterUiExample.java    From Java-Deep-Learning-Cookbook with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        ParameterSpace<Double> learningRateParam = new ContinuousParameterSpace(0.0001,0.01);
        ParameterSpace<Integer> layerSizeParam = new IntegerParameterSpace(5,11);
        MultiLayerSpace hyperParamaterSpace = new MultiLayerSpace.Builder()
                .updater(new AdamSpace(learningRateParam))
                //  .weightInit(WeightInit.DISTRIBUTION).dist(new LogNormalDistribution())
                .addLayer(new DenseLayerSpace.Builder()
                        .activation(Activation.RELU)
                        .nIn(11)
                        .nOut(layerSizeParam)
                        .build())
                .addLayer(new DenseLayerSpace.Builder()
                        .activation(Activation.RELU)
                        .nIn(layerSizeParam)
                        .nOut(layerSizeParam)
                        .build())
                .addLayer(new OutputLayerSpace.Builder()
                        .activation(Activation.SIGMOID)
                        .lossFunction(LossFunctions.LossFunction.XENT)
                        .nOut(1)
                        .build())
                .build();

        Map<String,Object> dataParams = new HashMap<>();
        dataParams.put("batchSize",new Integer(10));

        Map<String,Object> commands = new HashMap<>();
        commands.put(DataSetIteratorFactoryProvider.FACTORY_KEY, HyperParameterTuningArbiterUiExample.ExampleDataSource.class.getCanonicalName());

        CandidateGenerator candidateGenerator = new RandomSearchGenerator(hyperParamaterSpace,dataParams);

        Properties dataSourceProperties = new Properties();
        dataSourceProperties.setProperty("minibatchSize", "64");

        ResultSaver modelSaver = new FileModelSaver("resources/");
        ScoreFunction scoreFunction = new EvaluationScoreFunction(org.deeplearning4j.eval.Evaluation.Metric.ACCURACY);


        TerminationCondition[] conditions = {
                new MaxTimeCondition(120, TimeUnit.MINUTES),
                new MaxCandidatesCondition(30)

        };

        OptimizationConfiguration optimizationConfiguration = new OptimizationConfiguration.Builder()
                .candidateGenerator(candidateGenerator)
                .dataSource(HyperParameterTuningArbiterUiExample.ExampleDataSource.class,dataSourceProperties)
                .modelSaver(modelSaver)
                .scoreFunction(scoreFunction)
                .terminationConditions(conditions)
                .build();

        IOptimizationRunner runner = new LocalOptimizationRunner(optimizationConfiguration,new MultiLayerNetworkTaskCreator());
        //Uncomment this if you want to store the model.
        StatsStorage ss = new FileStatsStorage(new File("HyperParamOptimizationStats.dl4j"));
        runner.addListeners(new ArbiterStatusListener(ss));
        UIServer.getInstance().attach(ss);
        //runner.addListeners(new LoggingStatusListener()); //new ArbiterStatusListener(ss)
        runner.execute();

        //Print the best hyper params

        double bestScore = runner.bestScore();
        int bestCandidateIndex = runner.bestScoreCandidateIndex();
        int numberOfConfigsEvaluated = runner.numCandidatesCompleted();

        String s = "Best score: " + bestScore + "\n" +
                "Index of model with best score: " + bestCandidateIndex + "\n" +
                "Number of configurations evaluated: " + numberOfConfigsEvaluated + "\n";

        System.out.println(s);

    }
 
Example #27
Source File: CustomerRetentionPredictionExample.java    From Java-Deep-Learning-Cookbook with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

       final int labelIndex=11;
       final int batchSize=8;
       final int numClasses=2;
       final INDArray weightsArray = Nd4j.create(new double[]{0.57, 0.75});

       final RecordReader recordReader = generateReader(new ClassPathResource("Churn_Modelling.csv").getFile());
       final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(recordReader,batchSize)
                                                                .classification(labelIndex,numClasses)
                                                                .build();
       final DataNormalization dataNormalization = new NormalizerStandardize();
       dataNormalization.fit(dataSetIterator);
       dataSetIterator.setPreProcessor(dataNormalization);
       final DataSetIteratorSplitter dataSetIteratorSplitter = new DataSetIteratorSplitter(dataSetIterator,1250,0.8);

       log.info("Building Model------------------->>>>>>>>>");

        final MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
                                                                    .weightInit(WeightInit.RELU_UNIFORM)
                                                                    .updater(new Adam(0.015D))
                                                                    .list()
                                                                    .layer(new DenseLayer.Builder().nIn(11).nOut(6).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new DenseLayer.Builder().nIn(6).nOut(6).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new DenseLayer.Builder().nIn(6).nOut(4).activation(Activation.RELU).dropOut(0.9).build())
                                                                    .layer(new OutputLayer.Builder(new LossMCXENT(weightsArray)).nIn(4).nOut(2).activation(Activation.SOFTMAX).build())
                                                                    .build();

        final UIServer uiServer = UIServer.getInstance();
        final StatsStorage statsStorage = new InMemoryStatsStorage();

        final MultiLayerNetwork multiLayerNetwork = new MultiLayerNetwork(configuration);
        multiLayerNetwork.init();
        multiLayerNetwork.setListeners(new ScoreIterationListener(100),
                                       new StatsListener(statsStorage));
        uiServer.attach(statsStorage);
        multiLayerNetwork.fit(dataSetIteratorSplitter.getTrainIterator(),100);

        final Evaluation evaluation =  multiLayerNetwork.evaluate(dataSetIteratorSplitter.getTestIterator(),Arrays.asList("0","1"));
        System.out.println(evaluation.stats());

        final File file = new File("model.zip");
        ModelSerializer.writeModel(multiLayerNetwork,file,true);
        ModelSerializer.addNormalizerToModel(file,dataNormalization);


    }
 
Example #28
Source File: HyperParameterTuningArbiterUiExample.java    From Java-Deep-Learning-Cookbook with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        ParameterSpace<Double> learningRateParam = new ContinuousParameterSpace(0.0001,0.01);
        ParameterSpace<Integer> layerSizeParam = new IntegerParameterSpace(5,11);
        MultiLayerSpace hyperParamaterSpace = new MultiLayerSpace.Builder()
                .updater(new AdamSpace(learningRateParam))
                //  .weightInit(WeightInit.DISTRIBUTION).dist(new LogNormalDistribution())
                .addLayer(new DenseLayerSpace.Builder()
                        .activation(Activation.RELU)
                        .nIn(11)
                        .nOut(layerSizeParam)
                        .build())
                .addLayer(new DenseLayerSpace.Builder()
                        .activation(Activation.RELU)
                        .nIn(layerSizeParam)
                        .nOut(layerSizeParam)
                        .build())
                .addLayer(new OutputLayerSpace.Builder()
                        .activation(Activation.SIGMOID)
                        .lossFunction(LossFunctions.LossFunction.XENT)
                        .nOut(1)
                        .build())
                .build();

        Map<String,Object> dataParams = new HashMap<>();
        dataParams.put("batchSize",new Integer(10));

        Map<String,Object> commands = new HashMap<>();
        commands.put(DataSetIteratorFactoryProvider.FACTORY_KEY, HyperParameterTuningArbiterUiExample.ExampleDataSource.class.getCanonicalName());

        CandidateGenerator candidateGenerator = new RandomSearchGenerator(hyperParamaterSpace,dataParams);

        Properties dataSourceProperties = new Properties();
        dataSourceProperties.setProperty("minibatchSize", "64");

        ResultSaver modelSaver = new FileModelSaver("resources/");
        ScoreFunction scoreFunction = new EvaluationScoreFunction(org.deeplearning4j.eval.Evaluation.Metric.ACCURACY);


        TerminationCondition[] conditions = {
                new MaxTimeCondition(120, TimeUnit.MINUTES),
                new MaxCandidatesCondition(30)

        };

        OptimizationConfiguration optimizationConfiguration = new OptimizationConfiguration.Builder()
                .candidateGenerator(candidateGenerator)
                .dataSource(HyperParameterTuningArbiterUiExample.ExampleDataSource.class,dataSourceProperties)
                .modelSaver(modelSaver)
                .scoreFunction(scoreFunction)
                .terminationConditions(conditions)
                .build();

        IOptimizationRunner runner = new LocalOptimizationRunner(optimizationConfiguration,new MultiLayerNetworkTaskCreator());
        //Uncomment this if you want to store the model.
        StatsStorage ss = new FileStatsStorage(new File("HyperParamOptimizationStats.dl4j"));
        runner.addListeners(new ArbiterStatusListener(ss));
        UIServer.getInstance().attach(ss);
        //runner.addListeners(new LoggingStatusListener()); //new ArbiterStatusListener(ss)
        runner.execute();

        //Print the best hyper params

        double bestScore = runner.bestScore();
        int bestCandidateIndex = runner.bestScoreCandidateIndex();
        int numberOfConfigsEvaluated = runner.numCandidatesCompleted();

        String s = "Best score: " + bestScore + "\n" +
                "Index of model with best score: " + bestCandidateIndex + "\n" +
                "Number of configurations evaluated: " + numberOfConfigsEvaluated + "\n";

        System.out.println(s);

    }
 
Example #29
Source File: UITest.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
@Test
public void testPosting() throws Exception {

    //        File inputFile = Resources.asFile("big/raw_sentences.txt");
    File inputFile = new ClassPathResource("/basic/word2vec_advance.txt").getFile();
    SentenceIterator iter = UimaSentenceIterator.createWithPath(inputFile.getAbsolutePath());
    // Split on white spaces in the line to get words
    TokenizerFactory t = new DefaultTokenizerFactory();
    t.setTokenPreProcessor(new CommonPreprocessor());

    Word2Vec vec = new Word2Vec.Builder().minWordFrequency(1).epochs(1).layerSize(20)
                    .stopWords(new ArrayList<String>()).useAdaGrad(false).negativeSample(5).seed(42).windowSize(5)
                    .iterate(iter).tokenizerFactory(t).build();

    vec.fit();

    File tempFile = File.createTempFile("temp", "w2v");
    tempFile.deleteOnExit();

    WordVectorSerializer.writeWordVectors(vec, tempFile);

    WordVectors vectors = WordVectorSerializer.loadTxtVectors(tempFile);

    UIServer.getInstance(); //Initialize

    UiConnectionInfo uiConnectionInfo =
                    new UiConnectionInfo.Builder().setAddress("localhost").setPort(9000).build();

    BarnesHutTsne tsne = new BarnesHutTsne.Builder().normalize(false).setFinalMomentum(0.8f).numDimension(2)
                    .setMaxIter(10).build();

    vectors.lookupTable().plotVocab(tsne, vectors.lookupTable().getVocabCache().numWords(), uiConnectionInfo);


    Thread.sleep(100000);
}