package samples;

import hex.nn.NN;
import water.Job;
import water.Key;
import water.TestUtil;
import water.api.FrameSplit;
import water.fvec.Frame;
import water.util.Log;

/**
 * Runs a neural network on the MNIST dataset.
 */
public class NeuralNetMnist2 extends Job {
  public static void main(String[] args) throws Exception {
    Class job = NeuralNetMnist2.class;
    samples.launchers.CloudLocal.launch(job, 1);
//    samples.launchers.CloudProcess.launch(job, 4);
    //samples.launchers.CloudConnect.launch(job, "localhost:54321");
//    samples.launchers.CloudRemote.launchIPs(job, "192.168.1.171", "192.168.1.172", "192.168.1.173", "192.168.1.174", "192.168.1.175");
//    samples.launchers.CloudRemote.launchIPs(job, "192.168.1.161", "192.168.1.162", "192.168.1.163", "192.168.1.164");
//    samples.launchers.CloudRemote.launchIPs(job, "192.168.1.161", "192.168.1.162");
//    samples.launchers.CloudRemote.launchIPs(job, "192.168.1.161");
//    samples.launchers.CloudRemote.launchEC2(job, 4);
  }

  @Override protected Status exec() {
    Log.info("Parsing data.");
    long seed = 0xC0FFEE;
    double fraction = 1.0;
    Frame trainf = TestUtil.parseFromH2OFolder("smalldata/mnist/train.csv");
    Frame testf = TestUtil.parseFromH2OFolder("smalldata/mnist/test.csv");
    if (fraction < 1) {
      System.out.println("Sampling " + fraction*100 + "% of data with random seed: " + seed + ".");
      FrameSplit split = new FrameSplit();
      final double[] ratios = {fraction, 1-fraction};
      trainf = split.splitFrame(trainf, ratios, seed)[0];
//      testf = split.splitFrame(testf, ratios, seed)[0];
    }
    Log.info("Done.");

    NN p = new NN();
    // Hinton parameters -> should lead to ~1 % test error after ~ 10M training points
    p.seed = seed;
    p.hidden = new int[]{1024,1024,2048};
//    p.hidden = new int[]{128,128,256};
    p.rate = 0.01;
    p.activation = NN.Activation.RectifierWithDropout;
    p.loss = NN.Loss.CrossEntropy;
    p.input_dropout_ratio = 0.2;
    p.max_w2 = 15;
    p.epochs = 200;
    p.rate_annealing = 1e-6;
    p.l1 = 1e-5;
    p.l2 = 0;
    p.momentum_stable = 0.99;
    p.momentum_start = 0.5;
    p.momentum_ramp = 1800000;
    p.initial_weight_distribution = NN.InitialWeightDistribution.UniformAdaptive;
//    p.initial_weight_scale = 0.01
    p.classification = true;
    p.diagnostics = true;
    p.expert_mode = true;
    p.score_training = 1000;
    p.score_validation = 10000;
    p.validation = testf;
    p.source = trainf;
    p.response = trainf.lastVec();
    p.ignored_cols = null;
    p.destination_key = Key.make("mnist.model");
    return p.exec();
  }
}

@@ -92,37 +92,8 @@
 il;%0A
-import water.api.FrameSplit;%0A
 impo
@@ -334,16 +334,18 @@
 .class;%0A
+//
     samp
@@ -846,26 +846,24 @@
 68.1.162%22);%0A
-//
     samples.
@@ -1099,16 +1099,18 @@
  = 1.0;%0A
+//
     Fram
@@ -1116,24 +1116,39 @@
 me trainf = 
+NN.sampleFrame(
 TestUtil.par
@@ -1195,211 +1195,168 @@
 sv%22)
-;%0A    Frame testf = TestUtil.parseFromH2OFolder(%22smalldata/mnist/test.csv%22);%0A    if (fraction %3C 1) %7B%0A      System.out.println(%22Sampling %22 + fraction*100 + %22%25 of data with random seed: %22 + seed + %22.%22
+, (long)(60000*fraction), seed);%0A    Frame trainf = NN.sampleFrame(TestUtil.parseFromH2OFolder(%22smalldata/mnist/train10x.csv%22), (long)(600000*fraction), seed
 );%0A
-  
     
@@ -1364,215 +1364,121 @@
 rame
-Split split = new FrameSplit();%0A      final double%5B%5D ratios = %7Bfraction, 1-fraction%7D;%0A      trainf = split.splitFrame(trainf, ratios, seed)%5B0%5D;%0A//      testf = split.splitFrame(testf, 
+ testf = NN.sampleFrame(TestUtil.parseFromH2OFolder(%22smalldata/mnist/test.csv%22), (long)(10000*f
 ra
+c
 tio
-s
+n)
 , seed
-)%5B0%5D;%0A    %7D
++1);
 %0A   
@@ -1588,28 +1588,34 @@
 ter 
-~ 10M training point
+a few dozen million sample
 s%0A  
@@ -2211,19 +2211,20 @@
 stics = 
-tru
+fals
 e;%0A    p
@@ -2264,23 +2264,32 @@
 training
+_samples
  = 1000
+0
 ;%0A    p.
@@ -2300,24 +2300,32 @@
 e_validation
+_samples
  = 10000;%0A  
@@ -2433,16 +2433,71 @@
 = null;%0A
+    p.sync_samples = 50000;%0A    p.score_interval = 30;%0A
     p.de

Update Mnist test case for latest changes in NN.