Search in sources :

Example 36 with DeepLearningParameters

use of hex.deeplearning.DeepLearningModel.DeepLearningParameters in project h2o-3 by h2oai.

the class DeepLearningTask method postGlobal.

/**
   * After all reduces are done, the driver node calls this method to clean up
   * This is only needed if we're not inside a DeepLearningTask2 (which will do the reduction between replicated data workers).
   * So if replication is disabled, and every node works on partial data, then we have work to do here (model averaging).
   */
@Override
protected void postGlobal() {
    DeepLearningParameters dlp = _localmodel.get_params();
    if (H2O.CLOUD.size() > 1 && !dlp._replicate_training_data) {
        long now = System.currentTimeMillis();
        if (_chunk_node_count < H2O.CLOUD.size() && (now - _lastWarn > 5000) && _warnCount < 3) {
            //        Log.info("Synchronizing across " + _chunk_node_count + " H2O node(s).");
            Log.warn(H2O.CLOUD.size() - _chunk_node_count + " node(s) (out of " + H2O.CLOUD.size() + ") are not contributing to model updates. Consider setting replicate_training_data to true or using a larger training dataset (or fewer H2O nodes).");
            _lastWarn = now;
            _warnCount++;
        }
    }
    // Check that we're not inside a DeepLearningTask2
    assert ((!dlp._replicate_training_data || H2O.CLOUD.size() == 1) == !_run_local);
    if (!_run_local) {
        //move local sample counts to global ones
        _localmodel.add_processed_global(_localmodel.get_processed_local());
        _localmodel.set_processed_local(0l);
        // model averaging
        if (_chunk_node_count > 1)
            _localmodel.div(_chunk_node_count);
        if (_localmodel.get_params()._elastic_averaging)
            _sharedmodel = DeepLearningModelInfo.timeAverage(_localmodel);
    } else {
        //Get ready for reduction in DeepLearningTask2
        //Just swap the local and global models
        _sharedmodel = _localmodel;
    }
    if (_sharedmodel == null)
        _sharedmodel = _localmodel;
    _localmodel = null;
}
Also used : DeepLearningParameters(hex.deeplearning.DeepLearningModel.DeepLearningParameters)

Example 37 with DeepLearningParameters

use of hex.deeplearning.DeepLearningModel.DeepLearningParameters in project h2o-3 by h2oai.

the class DeepLearningGradientCheck method checkDistributionGradients.

@Test
public void checkDistributionGradients() {
    Random rng = new Random(0xDECAF);
    for (DistributionFamily dist : new DistributionFamily[] { DistributionFamily.AUTO, DistributionFamily.gaussian, DistributionFamily.laplace, DistributionFamily.quantile, DistributionFamily.huber, DistributionFamily.gamma, DistributionFamily.poisson, DistributionFamily.tweedie, DistributionFamily.bernoulli }) {
        DeepLearningParameters p = new DeepLearningParameters();
        p._distribution = dist;
        int N = 1000;
        double eps = 1. / (10. * N);
        for (double y : new double[] { 0, 1 }) {
            // scan the range -2..2 in function approximation space (link space)
            for (int i = -5 * N; i < 5 * N; ++i) {
                p._huber_alpha = rng.nextDouble() + 0.1;
                p._tweedie_power = 1.01 + rng.nextDouble() * 0.9;
                p._quantile_alpha = 0.05 + rng.nextDouble() * 0.9;
                Distribution d = new Distribution(p);
                // avoid issues at 0
                double f = (i + 0.5) / N;
                //f in link space (model space)
                double grad = -2 * d.negHalfGradient(y, f);
                double w = rng.nextDouble() * 10;
                //deviance in real space
                double approxgrad = (d.deviance(w, y, d.linkInv(f + eps)) - d.deviance(w, y, d.linkInv(f - eps))) / (2 * eps * w);
                assert (Math.abs(grad - approxgrad) <= 1e-4);
            }
        }
    }
}
Also used : Random(java.util.Random) DistributionFamily(hex.genmodel.utils.DistributionFamily) Distribution(hex.Distribution) DeepLearningParameters(hex.deeplearning.DeepLearningModel.DeepLearningParameters) PrettyPrint(water.util.PrettyPrint) Test(org.junit.Test)

Example 38 with DeepLearningParameters

use of hex.deeplearning.DeepLearningModel.DeepLearningParameters in project h2o-3 by h2oai.

the class DeepLearningIrisTest method runFraction.

void runFraction(float fraction) {
    long seed0 = 0xDECAF;
    int num_runs = 0;
    Frame frame = null;
    try {
        frame = parse_test_file(Key.make("iris.hex"), PATH);
        for (int repeat = 0; repeat < 5; ++repeat) {
            // Testing different things
            // Note: Microsoft reference implementation is only for Tanh + MSE.
            // Note: Rectifier and MCE are implemented by H2O.ai (trivial).
            // Note: Initial weight distributions are copied, but what is tested is the stability behavior.
            Activation[] activations = { Activation.Tanh, Activation.Rectifier };
            Loss[] losses = { Loss.Quadratic, Loss.CrossEntropy };
            InitialWeightDistribution[] dists = { InitialWeightDistribution.Normal, InitialWeightDistribution.Uniform, InitialWeightDistribution.UniformAdaptive };
            final long seed = seed0 + repeat;
            Random rng = new Random(seed);
            double[] initial_weight_scales = { 1e-4 + rng.nextDouble() };
            double[] holdout_ratios = { 0.1 + rng.nextDouble() * 0.8 };
            double[] momenta = { rng.nextDouble() * 0.99 };
            int[] hiddens = { 1, 2 + rng.nextInt(50) };
            int[] epochs = { 1, 2 + rng.nextInt(50) };
            double[] rates = { 0.01, 1e-5 + rng.nextDouble() * .1 };
            for (Activation activation : activations) {
                for (Loss loss : losses) {
                    for (InitialWeightDistribution dist : dists) {
                        for (double scale : initial_weight_scales) {
                            for (double holdout_ratio : holdout_ratios) {
                                for (double momentum : momenta) {
                                    for (int hidden : hiddens) {
                                        for (int epoch : epochs) {
                                            for (double rate : rates) {
                                                DeepLearningModel mymodel = null;
                                                Frame trainPredict = null;
                                                Frame testPredict = null;
                                                try {
                                                    num_runs++;
                                                    if (fraction < rng.nextFloat())
                                                        continue;
                                                    Log.info("");
                                                    Log.info("STARTING.");
                                                    Log.info("Running with " + activation.name() + " activation function and " + loss.name() + " loss function.");
                                                    Log.info("Initialization with " + dist.name() + " distribution and " + scale + " scale, holdout ratio " + holdout_ratio);
                                                    Log.info("Using " + hidden + " hidden layer neurons and momentum: " + momentum);
                                                    Log.info("Using seed " + seed);
                                                    Random rand;
                                                    int trial = 0;
                                                    do {
                                                        Log.info("Trial #" + ++trial);
                                                        if (_train != null)
                                                            _train.delete();
                                                        if (_test != null)
                                                            _test.delete();
                                                        rand = RandomUtils.getRNG(seed);
                                                        double[][] rows = new double[(int) frame.numRows()][frame.numCols()];
                                                        String[] names = new String[frame.numCols()];
                                                        for (int c = 0; c < frame.numCols(); c++) {
                                                            names[c] = "ColumnName" + c;
                                                            for (int r = 0; r < frame.numRows(); r++) rows[r][c] = frame.vecs()[c].at(r);
                                                        }
                                                        for (int i = rows.length - 1; i >= 0; i--) {
                                                            int shuffle = rand.nextInt(i + 1);
                                                            double[] row = rows[shuffle];
                                                            rows[shuffle] = rows[i];
                                                            rows[i] = row;
                                                        }
                                                        int limit = (int) (frame.numRows() * holdout_ratio);
                                                        _train = ArrayUtils.frame(names, water.util.ArrayUtils.subarray(rows, 0, limit));
                                                        _test = ArrayUtils.frame(names, water.util.ArrayUtils.subarray(rows, limit, (int) frame.numRows() - limit));
                                                        // Must have all output classes in training
                                                        // data (since that's what the reference
                                                        // implementation has hardcoded).  But count
                                                        // of classes is not known unless we visit
                                                        // all the response data - force that now.
                                                        String respname = _train.lastVecName();
                                                        Vec resp = _train.lastVec().toCategoricalVec();
                                                        _train.remove(respname).remove();
                                                        _train.add(respname, resp);
                                                        DKV.put(_train);
                                                        Vec vresp = _test.lastVec().toCategoricalVec();
                                                        _test.remove(respname).remove();
                                                        _test.add(respname, vresp);
                                                        DKV.put(_test);
                                                    } while (_train.lastVec().cardinality() < 3);
                                                    // use the same seed for the reference implementation
                                                    DeepLearningMLPReference ref = new DeepLearningMLPReference();
                                                    ref.init(activation, RandomUtils.getRNG(seed), holdout_ratio, hidden);
                                                    DeepLearningParameters p = new DeepLearningParameters();
                                                    p._train = _train._key;
                                                    p._response_column = _train.lastVecName();
                                                    assert _train.lastVec().isCategorical();
                                                    p._ignored_columns = null;
                                                    p._seed = seed;
                                                    p._hidden = new int[] { hidden };
                                                    p._adaptive_rate = false;
                                                    p._rho = 0;
                                                    p._epsilon = 0;
                                                    //adapt to (1-m) correction that's done inside (only for constant momentum!)
                                                    p._rate = rate / (1 - momentum);
                                                    p._activation = activation;
                                                    p._max_w2 = Float.POSITIVE_INFINITY;
                                                    p._input_dropout_ratio = 0;
                                                    //do not change - not implemented in reference
                                                    p._rate_annealing = 0;
                                                    p._l1 = 0;
                                                    p._loss = loss;
                                                    p._l2 = 0;
                                                    //reference only supports constant momentum
                                                    p._momentum_stable = momentum;
                                                    //do not change - not implemented in reference
                                                    p._momentum_start = p._momentum_stable;
                                                    //do not change - not implemented in reference
                                                    p._momentum_ramp = 0;
                                                    p._initial_weight_distribution = dist;
                                                    p._initial_weight_scale = scale;
                                                    p._valid = null;
                                                    p._quiet_mode = true;
                                                    //to be the same as reference
                                                    p._fast_mode = false;
                                                    //                            p._fast_mode = true; //to be the same as old NeuralNet code
                                                    //to be the same as reference
                                                    p._nesterov_accelerated_gradient = false;
                                                    //                            p._nesterov_accelerated_gradient = true; //to be the same as old NeuralNet code
                                                    //sync once per period
                                                    p._train_samples_per_iteration = 0;
                                                    p._ignore_const_cols = false;
                                                    p._shuffle_training_data = false;
                                                    //don't stop early -> need to compare against reference, which doesn't stop either
                                                    p._classification_stop = -1;
                                                    //keep just 1 chunk for reproducibility
                                                    p._force_load_balance = false;
                                                    p._overwrite_with_best_model = false;
                                                    p._replicate_training_data = false;
                                                    p._mini_batch_size = 1;
                                                    p._single_node_mode = true;
                                                    p._epochs = 0;
                                                    p._elastic_averaging = false;
                                                    mymodel = new DeepLearning(p).trainModel().get();
                                                    p._epochs = epoch;
                                                    Neurons[] neurons = DeepLearningTask.makeNeuronsForTraining(mymodel.model_info());
                                                    // use the same random weights for the reference implementation
                                                    Neurons l = neurons[1];
                                                    for (int o = 0; o < l._a[0].size(); o++) {
                                                        for (int i = 0; i < l._previous._a[0].size(); i++) {
                                                            //                                System.out.println("initial weight[" + o + "]=" + l._w[o * l._previous._a.length + i]);
                                                            ref._nn.ihWeights[i][o] = l._w.get(o, i);
                                                        }
                                                        ref._nn.hBiases[o] = l._b.get(o);
                                                    //                              System.out.println("initial bias[" + o + "]=" + l._b[o]);
                                                    }
                                                    l = neurons[2];
                                                    for (int o = 0; o < l._a[0].size(); o++) {
                                                        for (int i = 0; i < l._previous._a[0].size(); i++) {
                                                            //                                System.out.println("initial weight[" + o + "]=" + l._w[o * l._previous._a.length + i]);
                                                            ref._nn.hoWeights[i][o] = l._w.get(o, i);
                                                        }
                                                        ref._nn.oBiases[o] = l._b.get(o);
                                                    //                              System.out.println("initial bias[" + o + "]=" + l._b[o]);
                                                    }
                                                    // Train the Reference
                                                    ref.train((int) p._epochs, rate, p._momentum_stable, loss, seed);
                                                    // Train H2O
                                                    mymodel.delete();
                                                    DeepLearning dl = new DeepLearning(p);
                                                    mymodel = dl.trainModel().get();
                                                    Assert.assertTrue(mymodel.model_info().get_processed_total() == epoch * dl.train().numRows());
                                                    /**
                             * Tolerances (should ideally be super tight -> expect the same double/float precision math inside both algos)
                             */
                                                    final double abseps = 1e-6;
                                                    final double releps = 1e-6;
                                                    /**
                             * Compare weights and biases in hidden layer
                             */
                                                    //link the weights to the neurons, for easy access
                                                    neurons = DeepLearningTask.makeNeuronsForTesting(mymodel.model_info());
                                                    l = neurons[1];
                                                    for (int o = 0; o < l._a[0].size(); o++) {
                                                        for (int i = 0; i < l._previous._a[0].size(); i++) {
                                                            double a = ref._nn.ihWeights[i][o];
                                                            double b = l._w.get(o, i);
                                                            compareVal(a, b, abseps, releps);
                                                        //                                System.out.println("weight[" + o + "]=" + b);
                                                        }
                                                        double ba = ref._nn.hBiases[o];
                                                        double bb = l._b.get(o);
                                                        compareVal(ba, bb, abseps, releps);
                                                    }
                                                    Log.info("Weights and biases for hidden layer: PASS");
                                                    /**
                             * Compare weights and biases for output layer
                             */
                                                    l = neurons[2];
                                                    for (int o = 0; o < l._a[0].size(); o++) {
                                                        for (int i = 0; i < l._previous._a[0].size(); i++) {
                                                            double a = ref._nn.hoWeights[i][o];
                                                            double b = l._w.get(o, i);
                                                            compareVal(a, b, abseps, releps);
                                                        }
                                                        double ba = ref._nn.oBiases[o];
                                                        double bb = l._b.get(o);
                                                        compareVal(ba, bb, abseps, releps);
                                                    }
                                                    Log.info("Weights and biases for output layer: PASS");
                                                    /**
                             * Compare predictions
                             * Note: Reference and H2O each do their internal data normalization,
                             * so we must use their "own" test data, which is assumed to be created correctly.
                             */
                                                    // H2O predictions
                                                    //[0] is label, [1]...[4] are the probabilities
                                                    Frame fpreds = mymodel.score(_test);
                                                    try {
                                                        for (int i = 0; i < _test.numRows(); ++i) {
                                                            // Reference predictions
                                                            double[] xValues = new double[neurons[0]._a[0].size()];
                                                            System.arraycopy(ref._testData[i], 0, xValues, 0, xValues.length);
                                                            double[] ref_preds = ref._nn.ComputeOutputs(xValues);
                                                            // find the label
                                                            // do the same as H2O here (compare float values and break ties based on row number)
                                                            double[] preds = new double[ref_preds.length + 1];
                                                            for (int j = 0; j < ref_preds.length; ++j) preds[j + 1] = ref_preds[j];
                                                            preds[0] = GenModel.getPrediction(preds, null, xValues, 0.5);
                                                            // compare predicted label
                                                            Assert.assertTrue(preds[0] == (int) fpreds.vecs()[0].at(i));
                                                        //                                // compare predicted probabilities
                                                        //                                for (int j=0; j<ref_preds.length; ++j) {
                                                        //                                  compareVal((float)(ref_preds[j]), fpreds.vecs()[1+j].at(i), abseps, releps);
                                                        //                                }
                                                        }
                                                    } finally {
                                                        if (fpreds != null)
                                                            fpreds.delete();
                                                    }
                                                    Log.info("Predicted values: PASS");
                                                    /**
                             * Compare (self-reported) scoring
                             */
                                                    final double trainErr = ref._nn.Accuracy(ref._trainData);
                                                    final double testErr = ref._nn.Accuracy(ref._testData);
                                                    trainPredict = mymodel.score(_train);
                                                    testPredict = mymodel.score(_test);
                                                    hex.ModelMetrics mmtrain = hex.ModelMetrics.getFromDKV(mymodel, _train);
                                                    hex.ModelMetrics mmtest = hex.ModelMetrics.getFromDKV(mymodel, _test);
                                                    final double myTrainErr = mmtrain.cm().err();
                                                    final double myTestErr = mmtest.cm().err();
                                                    Log.info("H2O  training error : " + myTrainErr * 100 + "%, test error: " + myTestErr * 100 + "%");
                                                    Log.info("REF  training error : " + trainErr * 100 + "%, test error: " + testErr * 100 + "%");
                                                    compareVal(trainErr, myTrainErr, abseps, releps);
                                                    compareVal(testErr, myTestErr, abseps, releps);
                                                    Log.info("Scoring: PASS");
                                                    // get the actual best error on training data
                                                    float best_err = Float.MAX_VALUE;
                                                    for (ScoringInfo e : mymodel.scoring_history()) {
                                                        DeepLearningScoringInfo err = (DeepLearningScoringInfo) e;
                                                        //multi-class classification
                                                        best_err = Math.min(best_err, (float) (Double.isNaN(err.scored_train._classError) ? best_err : err.scored_train._classError));
                                                    }
                                                    Log.info("Actual best error : " + best_err * 100 + "%.");
                                                    // this is enabled by default
                                                    if (p._overwrite_with_best_model) {
                                                        Frame bestPredict = null;
                                                        try {
                                                            bestPredict = mymodel.score(_train);
                                                            hex.ModelMetrics mmbest = hex.ModelMetrics.getFromDKV(mymodel, _train);
                                                            final double bestErr = mmbest.cm().err();
                                                            Log.info("Best_model's error : " + bestErr * 100 + "%.");
                                                            compareVal(bestErr, best_err, abseps, releps);
                                                        } finally {
                                                            if (bestPredict != null)
                                                                bestPredict.delete();
                                                        }
                                                    }
                                                    Log.info("Parameters combination " + num_runs + ": PASS");
                                                } finally {
                                                    // cleanup
                                                    if (mymodel != null) {
                                                        mymodel.delete();
                                                    }
                                                    if (_train != null)
                                                        _train.delete();
                                                    if (_test != null)
                                                        _test.delete();
                                                    if (trainPredict != null)
                                                        trainPredict.delete();
                                                    if (testPredict != null)
                                                        testPredict.delete();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    } finally {
        if (frame != null)
            frame.delete();
    }
}
Also used : Frame(water.fvec.Frame) DeepLearningParameters(hex.deeplearning.DeepLearningModel.DeepLearningParameters) Activation(hex.deeplearning.DeepLearningModel.DeepLearningParameters.Activation) ScoringInfo(hex.ScoringInfo) Random(java.util.Random) Loss(hex.deeplearning.DeepLearningModel.DeepLearningParameters.Loss) InitialWeightDistribution(hex.deeplearning.DeepLearningModel.DeepLearningParameters.InitialWeightDistribution) Vec(water.fvec.Vec)

Example 39 with DeepLearningParameters

use of hex.deeplearning.DeepLearningModel.DeepLearningParameters in project h2o-3 by h2oai.

the class DeepLearningAutoEncoderCategoricalTest method run.

@Test
public void run() {
    long seed = 0xDECAF;
    NFSFileVec nfs = TestUtil.makeNfsFileVec(PATH);
    Frame train = ParseDataset.parse(Key.make("train.hex"), nfs._key);
    DeepLearningParameters p = new DeepLearningParameters();
    p._train = train._key;
    p._autoencoder = true;
    p._response_column = train.names()[train.names().length - 1];
    p._seed = seed;
    p._hidden = new int[] { 10, 5, 3 };
    p._adaptive_rate = true;
    //    String[] n = train.names();
    //    p._ignored_columns = new String[]{n[0],n[1],n[2],n[3],n[6],n[7],n[8],n[10]}; //Optional: ignore all categoricals
    //    p._ignored_columns = new String[]{train.names()[4], train.names()[5], train.names()[9]}; //Optional: ignore all numericals
    p._l1 = 1e-4;
    p._activation = DeepLearningParameters.Activation.Tanh;
    p._max_w2 = 10;
    p._train_samples_per_iteration = -1;
    p._loss = DeepLearningParameters.Loss.Huber;
    p._epochs = 0.2;
    p._force_load_balance = true;
    p._score_training_samples = 0;
    p._score_validation_samples = 0;
    p._reproducible = true;
    DeepLearning dl = new DeepLearning(p);
    DeepLearningModel mymodel = dl.trainModel().get();
    // Verification of results
    StringBuilder sb = new StringBuilder();
    sb.append("Verifying results.\n");
    sb.append("Reported mean reconstruction error: " + mymodel.mse() + "\n");
    // Training data
    // Reconstruct data using the same helper functions and verify that self-reported MSE agrees
    final Frame rec = mymodel.scoreAutoEncoder(train, Key.make(), true);
    sb.append("Reconstruction error per feature: " + rec.toString() + "\n");
    rec.remove();
    final Frame l2 = mymodel.scoreAutoEncoder(train, Key.make(), false);
    final Vec l2vec = l2.anyVec();
    sb.append("Actual   mean reconstruction error: " + l2vec.mean() + "\n");
    // print stats and potential outliers
    double quantile = 1 - 5. / train.numRows();
    sb.append("The following training points are reconstructed with an error above the " + quantile * 100 + "-th percentile - potential \"outliers\" in testing data.\n");
    double thresh = mymodel.calcOutlierThreshold(l2vec, quantile);
    for (long i = 0; i < l2vec.length(); i++) {
        if (l2vec.at(i) > thresh) {
            sb.append(String.format("row %d : l2vec error = %5f\n", i, l2vec.at(i)));
        }
    }
    Log.info(sb.toString());
    Assert.assertEquals(l2vec.mean(), mymodel.mse(), 1e-8 * mymodel.mse());
    // Create reconstruction
    Log.info("Creating full reconstruction.");
    final Frame recon_train = mymodel.score(train);
    Assert.assertTrue(mymodel.testJavaScoring(train, recon_train, 1e-5));
    Frame df1 = mymodel.scoreDeepFeatures(train, 0);
    Assert.assertTrue(df1.numCols() == 10);
    Assert.assertTrue(df1.numRows() == train.numRows());
    df1.delete();
    Frame df2 = mymodel.scoreDeepFeatures(train, 1);
    Assert.assertTrue(df2.numCols() == 5);
    Assert.assertTrue(df2.numRows() == train.numRows());
    df2.delete();
    Frame df3 = mymodel.scoreDeepFeatures(train, 2);
    Assert.assertTrue(df3.numCols() == 3);
    Assert.assertTrue(df3.numRows() == train.numRows());
    df3.delete();
    // cleanup
    recon_train.delete();
    train.delete();
    mymodel.delete();
    l2.delete();
}
Also used : Frame(water.fvec.Frame) Vec(water.fvec.Vec) NFSFileVec(water.fvec.NFSFileVec) NFSFileVec(water.fvec.NFSFileVec) DeepLearningParameters(hex.deeplearning.DeepLearningModel.DeepLearningParameters) Test(org.junit.Test)

Example 40 with DeepLearningParameters

use of hex.deeplearning.DeepLearningModel.DeepLearningParameters in project h2o-3 by h2oai.

the class DeepLearningAutoEncoderTest method run.

@Test
public void run() {
    long seed = 0xDECAF;
    Frame train = null, test = null;
    try {
        NFSFileVec nfs = TestUtil.makeNfsFileVec(PATH);
        train = ParseDataset.parse(Key.make("train.hex"), nfs._key);
        NFSFileVec nfs2 = TestUtil.makeNfsFileVec(PATH2);
        test = ParseDataset.parse(Key.make("test.hex"), nfs2._key);
        for (float sparsity_beta : new float[] { 0, 0.1f }) {
            DeepLearningParameters p = new DeepLearningParameters();
            p._train = train._key;
            p._valid = test._key;
            p._autoencoder = true;
            p._response_column = train.names()[train.names().length - 1];
            p._seed = seed;
            p._hidden = new int[] { 37, 12 };
            p._adaptive_rate = true;
            p._train_samples_per_iteration = -1;
            p._sparsity_beta = sparsity_beta;
            p._average_activation = -0.7;
            p._l1 = 1e-4;
            p._activation = DeepLearningParameters.Activation.TanhWithDropout;
            p._loss = DeepLearningParameters.Loss.Absolute;
            p._epochs = 13.3;
            p._force_load_balance = true;
            p._elastic_averaging = false;
            DeepLearning dl = new DeepLearning(p);
            DeepLearningModel mymodel = dl.trainModel().get();
            Frame l2_frame_train = null, l2_frame_test = null;
            // Verification of results
            StringBuilder sb = new StringBuilder();
            try {
                sb.append("Verifying results.\n");
                // Training data
                // Reconstruct data using the same helper functions and verify that self-reported MSE agrees
                double quantile = 0.95;
                l2_frame_test = mymodel.scoreAutoEncoder(test, Key.make(), true);
                sb.append("Reconstruction error per feature (test): ").append(l2_frame_test.toString()).append("\n");
                l2_frame_test.remove();
                l2_frame_test = mymodel.scoreAutoEncoder(test, Key.make(), false);
                Vec l2_test = l2_frame_test.anyVec();
                sb.append("Mean reconstruction error (test): ").append(l2_test.mean()).append("\n");
                Assert.assertEquals(l2_test.mean(), mymodel.mse(), 1e-7);
                Assert.assertTrue("too big a reconstruction error: " + l2_test.mean(), l2_test.mean() < 2.0);
                l2_test.remove();
                // manually compute L2
                //this creates real values in original space
                Frame reconstr = mymodel.score(train);
                Assert.assertTrue(mymodel.testJavaScoring(train, reconstr, 1e-6));
                l2_frame_train = mymodel.scoreAutoEncoder(train, Key.make(), false);
                final Vec l2_train = l2_frame_train.anyVec();
                double mean_l2 = 0;
                for (int r = 0; r < reconstr.numRows(); ++r) {
                    double my_l2 = 0;
                    for (int c = 0; c < reconstr.numCols(); ++c) {
                        //undo normalization here
                        my_l2 += Math.pow((reconstr.vec(c).at(r) - train.vec(c).at(r)) * mymodel.model_info().data_info()._normMul[c], 2);
                    }
                    my_l2 /= reconstr.numCols();
                    mean_l2 += my_l2;
                }
                mean_l2 /= reconstr.numRows();
                reconstr.delete();
                sb.append("Mean reconstruction error (train): ").append(l2_train.mean()).append("\n");
                Assert.assertEquals(mymodel._output.errors.scored_train._mse, mean_l2, 1e-7);
                // print stats and potential outliers
                sb.append("The following training points are reconstructed with an error above the ").append(quantile * 100).append("-th percentile - check for \"goodness\" of training data.\n");
                double thresh_train = mymodel.calcOutlierThreshold(l2_train, quantile);
                for (long i = 0; i < l2_train.length(); i++) {
                    if (l2_train.at(i) > thresh_train) {
                        sb.append(String.format("row %d : l2_train error = %5f\n", i, l2_train.at(i)));
                    }
                }
                // Test data
                // Reconstruct data using the same helper functions and verify that self-reported MSE agrees
                l2_frame_test.remove();
                l2_frame_test = mymodel.scoreAutoEncoder(test, Key.make(), false);
                l2_test = l2_frame_test.anyVec();
                double mult = 10;
                double thresh_test = mult * thresh_train;
                sb.append("\nFinding outliers.\n");
                sb.append("Mean reconstruction error (test): ").append(l2_test.mean()).append("\n");
                Frame df1 = mymodel.scoreDeepFeatures(test, 0);
                Assert.assertTrue(df1.numCols() == 37);
                Assert.assertTrue(df1.numRows() == test.numRows());
                df1.delete();
                Frame df2 = mymodel.scoreDeepFeatures(test, 1);
                Assert.assertTrue(df2.numCols() == 12);
                Assert.assertTrue(df2.numRows() == test.numRows());
                df2.delete();
                // print stats and potential outliers
                sb.append("The following test points are reconstructed with an error greater than ").append(mult).append(" times the mean reconstruction error of the training data:\n");
                HashSet<Long> outliers = new HashSet<>();
                for (long i = 0; i < l2_test.length(); i++) {
                    if (l2_test.at(i) > thresh_test) {
                        outliers.add(i);
                        sb.append(String.format("row %d : l2 error = %5f\n", i, l2_test.at(i)));
                    }
                }
                // check that the all outliers are found (and nothing else)
                Assert.assertTrue(outliers.contains(new Long(20)));
                Assert.assertTrue(outliers.contains(new Long(21)));
                Assert.assertTrue(outliers.contains(new Long(22)));
                Assert.assertTrue(outliers.size() == 3);
            } finally {
                Log.info(sb);
                // cleanup
                if (mymodel != null)
                    mymodel.delete();
                if (l2_frame_train != null)
                    l2_frame_train.delete();
                if (l2_frame_test != null)
                    l2_frame_test.delete();
            }
        }
    } finally {
        if (train != null)
            train.delete();
        if (test != null)
            test.delete();
    }
}
Also used : DeepLearningParameters(hex.deeplearning.DeepLearningModel.DeepLearningParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DeepLearningParameters (hex.deeplearning.DeepLearningModel.DeepLearningParameters)58 Frame (water.fvec.Frame)54 Test (org.junit.Test)52 NFSFileVec (water.fvec.NFSFileVec)26 Vec (water.fvec.Vec)22 hex (hex)5 Ignore (org.junit.Ignore)5 DistributionFamily (hex.genmodel.utils.DistributionFamily)4 Random (java.util.Random)4 H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)3 DataInfo (hex.DataInfo)2 File (java.io.File)2 Key (water.Key)2 H2OModelBuilderIllegalArgumentException (water.exceptions.H2OModelBuilderIllegalArgumentException)2 PrettyPrint (water.util.PrettyPrint)2 ConfusionMatrix (hex.ConfusionMatrix)1 Distribution (hex.Distribution)1 FrameSplitter (hex.FrameSplitter)1 FrameTask (hex.FrameTask)1 ModelMetricsBinomial (hex.ModelMetricsBinomial)1