Search in sources :

Example 1 with Distribution

use of hex.Distribution in project h2o-3 by h2oai.

the class Neurons method init.

/**
   * Initialization of the parameters and connectivity of a Neuron layer
   * @param neurons Array of all neuron layers, to establish feed-forward connectivity
   * @param index Which layer am I?
   * @param p User-given parameters (Job parental object hierarchy is not used)
   * @param minfo Model information (weights/biases and their momenta)
   * @param training Whether training is done or just testing (no need for dropout)
   */
public final void init(Neurons[] neurons, int index, DeepLearningParameters p, final DeepLearningModelInfo minfo, boolean training) {
    _index = index - 1;
    params = (DeepLearningParameters) p.clone();
    params._hidden_dropout_ratios = minfo.get_params()._hidden_dropout_ratios;
    params._rate *= Math.pow(params._rate_decay, index - 1);
    params._distribution = minfo.get_params()._distribution;
    _dist = new Distribution(params);
    _a = new Storage.DenseVector[params._mini_batch_size];
    for (int mb = 0; mb < _a.length; ++mb) _a[mb] = new Storage.DenseVector(units);
    if (!(this instanceof Input)) {
        _e = new Storage.DenseVector[params._mini_batch_size];
        for (int mb = 0; mb < _e.length; ++mb) _e[mb] = new Storage.DenseVector(units);
    } else if (params._autoencoder && params._input_dropout_ratio > 0) {
        _origa = new Storage.DenseVector[params._mini_batch_size];
        for (int mb = 0; mb < _origa.length; ++mb) _origa[mb] = new Storage.DenseVector(units);
    }
    if (training && (this instanceof MaxoutDropout || this instanceof TanhDropout || this instanceof RectifierDropout || this instanceof ExpRectifierDropout || this instanceof Input)) {
        _dropout = this instanceof Input ? //input dropout
        (params._input_dropout_ratio == 0 ? null : new Dropout(units, params._input_dropout_ratio)) : //hidden dropout
        new Dropout(units, params._hidden_dropout_ratios[_index]);
    }
    if (!(this instanceof Input)) {
        //incoming neurons
        _previous = neurons[_index];
        _minfo = minfo;
        //incoming weights
        _w = minfo.get_weights(_index);
        //bias for this layer (starting at hidden layer)
        _b = minfo.get_biases(_index);
        if (params._autoencoder && params._sparsity_beta > 0 && _index < params._hidden.length) {
            _avg_a = minfo.get_avg_activations(_index);
        }
        if (minfo.has_momenta()) {
            //incoming weights
            _wm = minfo.get_weights_momenta(_index);
            //bias for this layer (starting at hidden layer)
            _bm = minfo.get_biases_momenta(_index);
        }
        if (minfo.adaDelta()) {
            _ada_dx_g = minfo.get_ada_dx_g(_index);
            _bias_ada_dx_g = minfo.get_biases_ada_dx_g(_index);
        }
        _shortcut = (params._fast_mode || (// not doing fast mode, but also don't have anything else to update (neither momentum nor ADADELTA history), and no L1/L2
        !params._adaptive_rate && !_minfo.has_momenta() && params._l1 == 0.0 && params._l2 == 0.0));
    }
    sanityCheck(training);
}
Also used : Distribution(hex.Distribution)

Example 2 with Distribution

use of hex.Distribution in project h2o-3 by h2oai.

the class GBMModel method score0.

/** Bulk scoring API for one row.  Chunks are all compatible with the model,
   *  and expect the last Chunks are for the final distribution and prediction.
   *  Default method is to just load the data into the tmp array, then call
   *  subclass scoring logic. */
@Override
protected double[] score0(double[] data, double[] preds, double weight, double offset, int ntrees) {
    // These are f_k(x) in Algorithm 10.4
    super.score0(data, preds, weight, offset, ntrees);
    if (_parms._distribution == DistributionFamily.bernoulli || _parms._distribution == DistributionFamily.modified_huber) {
        //Note: class 1 probability stored in preds[1] (since we have only one tree)
        double f = preds[1] + _output._init_f + offset;
        preds[2] = new Distribution(_parms).linkInv(f);
        preds[1] = 1.0 - preds[2];
    } else if (_parms._distribution == DistributionFamily.multinomial) {
        // Kept the initial prediction for binomial
        if (_output.nclasses() == 2) {
            //1-tree optimization for binomial
            //offset is not yet allowed, but added here to be future-proof
            preds[1] += _output._init_f + offset;
            preds[2] = -preds[1];
        }
        hex.genmodel.GenModel.GBM_rescale(preds);
    } else {
        //Regression
        double f = preds[0] + _output._init_f + offset;
        preds[0] = new Distribution(_parms).linkInv(f);
    }
    return preds;
}
Also used : Distribution(hex.Distribution)

Example 3 with Distribution

use of hex.Distribution 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 4 with Distribution

use of hex.Distribution in project h2o-3 by h2oai.

the class GBMModel method toJavaUnifyPreds.

// Note: POJO scoring code doesn't support per-row offsets (the scoring API would need to be changed to pass in offsets)
@Override
protected void toJavaUnifyPreds(SBPrintStream body) {
    // the loss function.
    if (_parms._distribution == DistributionFamily.bernoulli || _parms._distribution == DistributionFamily.modified_huber) {
        body.ip("preds[2] = preds[1] + ").p(_output._init_f).p(";").nl();
        body.ip("preds[2] = " + new Distribution(_parms).linkInvString("preds[2]") + ";").nl();
        body.ip("preds[1] = 1.0-preds[2];").nl();
        if (_parms._balance_classes)
            body.ip("hex.genmodel.GenModel.correctProbabilities(preds, PRIOR_CLASS_DISTRIB, MODEL_CLASS_DISTRIB);").nl();
        body.ip("preds[0] = hex.genmodel.GenModel.getPrediction(preds, PRIOR_CLASS_DISTRIB, data, " + defaultThreshold() + ");").nl();
        return;
    }
    if (_output.nclasses() == 1) {
        // Regression
        body.ip("preds[0] += ").p(_output._init_f).p(";").nl();
        body.ip("preds[0] = " + new Distribution(_parms).linkInvString("preds[0]") + ";").nl();
        return;
    }
    if (_output.nclasses() == 2) {
        // Kept the initial prediction for binomial
        body.ip("preds[1] += ").p(_output._init_f).p(";").nl();
        body.ip("preds[2] = - preds[1];").nl();
    }
    body.ip("hex.genmodel.GenModel.GBM_rescale(preds);").nl();
    if (_parms._balance_classes)
        body.ip("hex.genmodel.GenModel.correctProbabilities(preds, PRIOR_CLASS_DISTRIB, MODEL_CLASS_DISTRIB);").nl();
    body.ip("preds[0] = hex.genmodel.GenModel.getPrediction(preds, PRIOR_CLASS_DISTRIB, data, " + defaultThreshold() + ");").nl();
}
Also used : Distribution(hex.Distribution)

Aggregations

Distribution (hex.Distribution)4 DeepLearningParameters (hex.deeplearning.DeepLearningModel.DeepLearningParameters)1 DistributionFamily (hex.genmodel.utils.DistributionFamily)1 Random (java.util.Random)1 Test (org.junit.Test)1 PrettyPrint (water.util.PrettyPrint)1