Example usage for java.lang Double MAX_VALUE

List of usage examples for java.lang Double MAX_VALUE

Introduction

In this page you can find the example usage for java.lang Double MAX_VALUE.

Prototype

double MAX_VALUE

To view the source code for java.lang Double MAX_VALUE.

Click Source Link

Document

A constant holding the largest positive finite value of type double , (2-2-52)·21023.

Usage

From source file:main.java.edu.mit.compbio.qrf.QGBM_spark.java

public void doMain(String[] args) throws Exception {

    CmdLineParser parser = new CmdLineParser(this);
    //parser.setUsageWidth(80);
    try {// ww w .  j  a v a  2s . co m
        if (help || args.length < 2)
            throw new CmdLineException(USAGE);
        parser.parseArgument(args);

    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        // print the list of available options
        parser.printUsage(System.err);
        System.err.println();
        return;
    }

    //read input bed file, for each row,
    String modelFile = arguments.get(0);
    String inputFile = arguments.get(1);
    initiate();

    SparkConf sparkConf = new SparkConf().setAppName("QGBM_spark");
    JavaSparkContext sc = new JavaSparkContext(sparkConf);

    JavaRDD<LabeledPoint> inputData = sc.textFile(inputFile).map(new Function<String, LabeledPoint>() {

        @Override
        public LabeledPoint call(String line) throws Exception {
            String[] tmp = line.split(sep);
            double[] ds = new double[featureCols.size()];
            for (int i = 0; i < featureCols.size(); i++) {
                ds[i] = Double.parseDouble(tmp[featureCols.get(i) - 1]);
            }
            return new LabeledPoint(Double.parseDouble(tmp[labelCol - 1]), Vectors.dense(ds));
        }

    });

    // Prepare training documents, which are labeled.

    if (train) {
        JavaRDD<LabeledPoint>[] splits = inputData.randomSplit(folds, seed);

        BoostingStrategy boostingStrategy = BoostingStrategy.defaultParams("Regression");

        Map<Integer, Integer> categoricalFeaturesInfo = new HashMap<Integer, Integer>();
        boostingStrategy.setNumIterations(numTrees);
        boostingStrategy.setLearningRate(learningRate);
        boostingStrategy.treeStrategy().setMaxDepth(maxDepth);
        boostingStrategy.treeStrategy().setMaxBins(maxBins);
        boostingStrategy.treeStrategy().setMaxMemoryInMB(maxMemoryInMB);

        //  Empty categoricalFeaturesInfo indicates all features are continuous.
        boostingStrategy.treeStrategy().setCategoricalFeaturesInfo(categoricalFeaturesInfo);

        GradientBoostedTreesModel bestModel = null;
        double bestR2 = Double.NEGATIVE_INFINITY;
        double bestMse = Double.MAX_VALUE;
        double mseSum = 0.0;
        RegressionMetrics rmBest = null;
        for (int i = 0; i < kFold; i++) {
            JavaRDD<LabeledPoint> testData = splits[i];
            JavaRDD<LabeledPoint> trainingData = null;
            for (int j = 0; j < kFold; j++) {
                if (j == i)
                    continue;
                if (trainingData != null) {
                    trainingData.union(splits[j]);
                } else {
                    trainingData = splits[j];
                }

            }
            GradientBoostedTrees gbt = new GradientBoostedTrees(boostingStrategy);
            final GradientBoostedTreesModel model = gbt.runWithValidation(trainingData, testData);

            // Evaluate model on test instances and compute test error
            RDD<Tuple2<Object, Object>> predictionAndLabel = testData
                    .map(new Function<LabeledPoint, Tuple2<Object, Object>>() {
                        @Override
                        public Tuple2<Object, Object> call(LabeledPoint p) {
                            return new Tuple2<Object, Object>(model.predict(p.features()), p.label());
                        }
                    }).rdd();

            RegressionMetrics rm = new RegressionMetrics(predictionAndLabel);
            double r2 = rm.r2();
            mseSum += rm.meanSquaredError();
            if (r2 > bestR2) {
                bestModel = model;
                rmBest = rm;
                bestR2 = r2;
                bestMse = rm.meanSquaredError();
            }
        }

        log.info("After cross validation,  best model's MSE is: " + bestMse + "\tMean MSE is: " + mseSum / kFold
                + "\tVariance explained: " + rmBest.explainedVariance() + "\tR2: " + rmBest.r2());
        bestModel.save(sc.sc(), modelFile);

    } else {
        if (outputFile == null)
            throw new IllegalArgumentException(
                    "Need to provide output file name in -outputFile for Non training mode !!");
        //load trained model
        log.info("Loading model ...");
        final GradientBoostedTreesModel model = GradientBoostedTreesModel.load(sc.sc(), modelFile);

        inputData.map(new Function<LabeledPoint, String>() {

            @Override
            public String call(LabeledPoint p) {
                double predict = model.predict(p.features());
                String tmp = null;
                for (double s : p.features().toArray()) {
                    if (tmp == null) {
                        tmp = String.valueOf(s);
                    } else {
                        tmp = tmp + "\t" + String.valueOf(s);
                    }
                }

                return tmp + "\t" + p.label() + "\t" + predict;
            }
        }).saveAsTextFile(outputFile + ".tmp");

        log.info("Merging files ...");
        File[] listOfFiles = new File(outputFile + ".tmp").listFiles();

        OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile, true));
        for (File f : listOfFiles) {
            if (f.isFile() && f.getName().startsWith("part-")) {
                InputStream input = new BufferedInputStream(new FileInputStream(f));
                IOUtils.copy(input, output);
                IOUtils.closeQuietly(input);
            }

        }
        IOUtils.closeQuietly(output);
        FileUtils.deleteDirectory(new File(outputFile + ".tmp"));

        // Evaluate model on test instances and compute test error
        RDD<Tuple2<Object, Object>> predictionAndLabel = inputData
                .map(new Function<LabeledPoint, Tuple2<Object, Object>>() {
                    @Override
                    public Tuple2<Object, Object> call(LabeledPoint p) {
                        return new Tuple2<Object, Object>(model.predict(p.features()), p.label());
                    }
                }).rdd();

        RegressionMetrics rm = new RegressionMetrics(predictionAndLabel);
        log.info("For the test dataset, MSE is: " + rm.meanSquaredError() + "\tVariance explained: "
                + rm.explainedVariance() + "\tR2: " + rm.r2());

    }

    finish();
}

From source file:edu.umn.cs.sthadoop.operations.STRangeQuery.java

public static void rangeQueryOperation(OperationsParams parameters) throws Exception {
    final OperationsParams params = parameters;

    final Path[] paths = params.getPaths();
    if (paths.length <= 1 && !params.checkInput()) {
        printUsage();//from   w  ww  .  j a v  a  2  s .c  o  m
        System.exit(1);
    }
    if (paths.length >= 2 && !params.checkInputOutput()) {
        printUsage();
        System.exit(1);
    }
    if (params.get("rect") == null) {
        String x1 = "-" + Double.toString(Double.MAX_VALUE);
        String y1 = "-" + Double.toString(Double.MAX_VALUE);
        String x2 = Double.toString(Double.MAX_VALUE);
        String y2 = Double.toString(Double.MAX_VALUE);
        System.out.println(x1 + "," + y1 + "," + x2 + "," + y2);
        params.set("rect", x1 + "," + y1 + "," + x2 + "," + y2);
        //         System.err.println("You must provide a query range");
        //         printUsage();
        //         System.exit(1);
    }

    if (params.get("interval") == null) {
        System.err.println("Temporal range missing");
        printUsage();
        System.exit(1);
    }

    TextSerializable inObj = params.getShape("shape");
    if (!(inObj instanceof STPoint) && !(inObj instanceof STRectangle)) {
        LOG.error("Shape is not instance of STPoint or STRectangle");
        printUsage();
        System.exit(1);
    }

    // Get spatio-temporal slices.
    List<Path> STPaths = getIndexedSlices(params);
    final Path outPath = params.getOutputPath();
    final Rectangle[] queryRanges = params.getShapes("rect", new Rectangle());

    // All running jobs
    final Vector<Long> resultsCounts = new Vector<Long>();
    Vector<Job> jobs = new Vector<Job>();
    Vector<Thread> threads = new Vector<Thread>();

    long t1 = System.currentTimeMillis();
    for (Path stPath : STPaths) {
        final Path inPath = stPath;
        for (int i = 0; i < queryRanges.length; i++) {
            final OperationsParams queryParams = new OperationsParams(params);
            OperationsParams.setShape(queryParams, "rect", queryRanges[i]);
            if (OperationsParams.isLocal(new JobConf(queryParams), inPath)) {
                // Run in local mode
                final Rectangle queryRange = queryRanges[i];
                final Shape shape = queryParams.getShape("shape");
                final Path output = outPath == null ? null
                        : (queryRanges.length == 1 ? outPath : new Path(outPath, String.format("%05d", i)));
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        FSDataOutputStream outFile = null;
                        final byte[] newLine = System.getProperty("line.separator", "\n").getBytes();
                        try {
                            ResultCollector<Shape> collector = null;
                            if (output != null) {
                                FileSystem outFS = output.getFileSystem(queryParams);
                                final FSDataOutputStream foutFile = outFile = outFS.create(output);
                                collector = new ResultCollector<Shape>() {
                                    final Text tempText = new Text2();

                                    @Override
                                    public synchronized void collect(Shape r) {
                                        try {
                                            tempText.clear();
                                            r.toText(tempText);
                                            foutFile.write(tempText.getBytes(), 0, tempText.getLength());
                                            foutFile.write(newLine);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                };
                            } else {
                                outFile = null;
                            }
                            long resultCount = rangeQueryLocal(inPath, queryRange, shape, queryParams,
                                    collector);
                            resultsCounts.add(resultCount);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (outFile != null)
                                    outFile.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                thread.start();
                threads.add(thread);
            } else {
                // Run in MapReduce mode
                Path outTempPath = outPath == null ? null
                        : new Path(outPath, String.format("%05d", i) + "-" + inPath.getName());
                queryParams.setBoolean("background", true);
                Job job = rangeQueryMapReduce(inPath, outTempPath, queryParams);
                jobs.add(job);
            }
        }
    }

    while (!jobs.isEmpty()) {
        Job firstJob = jobs.firstElement();
        firstJob.waitForCompletion(false);
        if (!firstJob.isSuccessful()) {
            System.err.println("Error running job " + firstJob);
            System.err.println("Killing all remaining jobs");
            for (int j = 1; j < jobs.size(); j++)
                jobs.get(j).killJob();
            System.exit(1);
        }
        Counters counters = firstJob.getCounters();
        Counter outputRecordCounter = counters.findCounter(Task.Counter.MAP_OUTPUT_RECORDS);
        resultsCounts.add(outputRecordCounter.getValue());
        jobs.remove(0);
    }
    while (!threads.isEmpty()) {
        try {
            Thread thread = threads.firstElement();
            thread.join();
            threads.remove(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    long t2 = System.currentTimeMillis();
    System.out.println("QueryPlan:");
    for (Path stPath : STPaths) {
        System.out.println(stPath.getName());
    }
    System.out.println("Time for " + queryRanges.length + " jobs is " + (t2 - t1) + " millis");
    System.out.println("Results counts: " + resultsCounts);
}

From source file:edu.oregonstate.eecs.mcplan.domains.voyager.Voyager.java

public static Pair<Unit, Unit> minimaxMatchup(final int[] a, final int[] d) {
    double max_p = -Double.MAX_VALUE;
    int max_i = -1;
    int min_j = -1;
    for (int i = 0; i < a.length; ++i) {
        if (a[i] == 0) {
            continue;
        }/*from   w w  w .j a  va 2  s .  com*/
        double min_p = Double.MAX_VALUE;
        for (int j = 0; j < d.length; ++j) {
            if (d[j] == 0) {
                continue;
            }
            if (min_p > Unit.attack_matchups[i][j]) {
                min_p = Unit.attack_matchups[i][j];
                min_j = j;
            }
        }
        if (min_p > max_p) {
            max_p = min_p;
            max_i = i;
        }
    }
    assert (max_i >= 0);
    assert (min_j >= 0);
    return Pair.makePair(Unit.values()[max_i], Unit.values()[min_j]);
}

From source file:edu.fullerton.viewerplugin.PluginSupport.java

public static void getRangeLimits(TimeSeriesCollection mtds, Double rng[]) {
    Double minx, miny, maxx, maxy;

    minx = miny = Double.MAX_VALUE;

    maxx = maxy = -Double.MAX_VALUE;
    for (Iterator it = mtds.getSeries().iterator(); it.hasNext();) {
        TimeSeries ds = (TimeSeries) it.next();
        for (int item = 1; item < ds.getItemCount() - 1; item++) {
            TimeSeriesDataItem dataItem = ds.getDataItem(item);
            RegularTimePeriod period = dataItem.getPeriod();

            double y = dataItem.getValue().doubleValue();
            double x = period.getFirstMillisecond();

            minx = Math.min(minx, x);
            miny = Math.min(miny, y);
            maxx = Math.max(maxx, x);
            maxy = Math.max(maxy, y);
        }/*from w  w  w .j av  a2  s.c  om*/
    }
    rng[0] = minx;
    rng[1] = miny;
    rng[2] = maxx;
    rng[3] = maxy;
}

From source file:ch.epfl.lsir.xin.algorithm.core.SocialReg.java

/**
 * This function learns a matrix factorization model using Stochastic Gradient Descent 
 * *//*from   w  w w .j  ava2 s.  c  o m*/
public void buildSGD() {
    double preError = Double.MAX_VALUE;
    for (int i = 0; i < this.Iterations; i++) {
        System.out.println("Iteration: " + i);
        ArrayList<MatrixEntry2D> entries = this.ratingMatrix.getValidEntries();
        double error = 0; //overall error of this iteration
        System.out.println("Ratings: " + entries.size());
        while (entries.size() > 0) {
            //find a random entry
            int r = new Random().nextInt(entries.size());
            MatrixEntry2D entry = entries.get(r);
            double prediction = predict(entry.getRowIndex(), entry.getColumnIndex());
            //            System.out.println(prediction);
            if (prediction > this.maxRating)
                prediction = this.maxRating;
            if (prediction < this.minRating)
                prediction = this.minRating;
            double difference = entry.getValue() - prediction;
            for (int l = 0; l < this.latentFactors; l++) {
                //user latent update is a modified version of the original paper
                double tempU = -1;
                if (this.config.getString("MODE").equals("AVERAGE")) {
                    ArrayList<Integer> friendIDs = this.socialMatrix.getRatedItemIndex(entry.getRowIndex());
                    double simSum = 0;
                    double friendSum = 0;
                    for (int j = 0; j < friendIDs.size(); j++) {
                        double sim = this.socialMatrix.getRatingMatrix().get(entry.getRowIndex())
                                .get(friendIDs.get(j));
                        if (sim == 0)
                            continue;
                        simSum = simSum + sim;
                        friendSum = friendSum + sim * this.userMatrix.get(friendIDs.get(j), l);
                    }
                    if (simSum != 0)
                        friendSum = friendSum / simSum;
                    else
                        friendSum = 0;
                    //                  System.out.println(friendSum);
                    tempU = this.userMatrix.get(entry.getRowIndex(), l)
                            + this.learningRate * (difference * this.itemMatrix.get(entry.getColumnIndex(), l)
                                    - this.regUser * this.userMatrix.get(entry.getRowIndex(), l)
                                    - this.socialReg
                                            * (this.userMatrix.get(entry.getRowIndex(), l) - friendSum));
                } else if (this.config.getString("MODE").equals("INDIVIDUAL")) {
                    ArrayList<Integer> friendIDs = this.socialMatrix.getRatedItemIndex(entry.getRowIndex());
                    double friendSum = 0;
                    for (int j = 0; j < friendIDs.size(); j++) {
                        friendSum += this.socialMatrix.getRatingMatrix().get(entry.getRowIndex())
                                .get(friendIDs.get(j))
                                * (this.userMatrix.get(entry.getRowIndex(), l)
                                        - this.userMatrix.get(friendIDs.get(j), l));
                    }
                    tempU = this.userMatrix.get(entry.getRowIndex(), l)
                            + this.learningRate * (difference * this.itemMatrix.get(entry.getColumnIndex(), l)
                                    - this.regUser * this.userMatrix.get(entry.getRowIndex(), l)
                                    - this.socialReg * friendSum);
                } else {
                    logger.println("MODE is not set correctly.");
                }
                double tempI = this.itemMatrix.get(entry.getColumnIndex(), l)
                        + this.learningRate * (2 * difference * this.userMatrix.get(entry.getRowIndex(), l)
                                - this.regItem * this.itemMatrix.get(entry.getColumnIndex(), l));
                this.userMatrix.set(entry.getRowIndex(), l, tempU);
                this.itemMatrix.set(entry.getColumnIndex(), l, tempI);
            }

            entries.remove(r);
        }

        //error
        entries = this.ratingMatrix.getValidEntries();
        for (int k = 0; k < entries.size(); k++) {
            MatrixEntry2D entry = entries.get(k);
            double prediction = predict(entry.getRowIndex(), entry.getColumnIndex());
            if (prediction > this.maxRating)
                prediction = this.maxRating;
            if (prediction < this.minRating)
                prediction = this.minRating;
            error = error + Math.abs(entry.getValue() - prediction);
            //            for( int j = 0 ; j < this.latentFactors ; j++ )
            //            {
            //               error = error + this.regUser/2 * Math.pow(this.userMatrix.get(entry.getRowIndex(), j), 2) + 
            //                     this.regItem/2 * Math.pow(this.itemMatrix.get(entry.getColumnIndex(), j), 2);
            //            }      
        }
        this.logger.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Iteration " + i
                + " : Error ~ " + error);
        this.logger.flush();
        //check for convergence
        if (Math.abs(error - preError) <= this.convergence && error <= preError) {
            logger.println("The algorithm convergences.");
            this.logger.flush();
            break;
        }
        // learning rate update strategy 
        updateLearningRate(error, preError);

        preError = error;
        logger.flush();
    }
}

From source file:com.xtructure.xneat.network.impl.NeuralNetworkImpl.java

@Override
public double singleStep() {
    LOGGER.trace("begin %s.singleStep()", getClass().getSimpleName());
    double maxSignalDelta = -Double.MAX_VALUE;
    // pass neuron signals through connections
    for (Connection connection : connections) {
        int sourceIndex = connection.getSourceNeuronIndex();
        double weight = connection.getWeight();
        connection.setSignal(neurons.get(sourceIndex).getSignal() * weight);
    }/* w w w  . j  a v  a 2 s  .c o  m*/
    // accumulate connection signals for their target neurons
    for (Connection connection : connections) {
        int targetIndex = connection.getTargetNeuronIndex();
        neurons.get(targetIndex)
                .setInputSignal(neurons.get(targetIndex).getInputSignal() + connection.getSignal());
    }
    // pass accumulated input signals through neurons
    // NOTE: assumes recurrent links to inputs aren't allowed
    for (Neuron neuron : neurons.subList(outputStart, hiddenEnd)) {
        double signalDelta = neuron.calculateSignal();
        maxSignalDelta = Math.max(signalDelta, maxSignalDelta);
    }
    LOGGER.trace("will return: %s", maxSignalDelta);
    LOGGER.trace("end %s.singleStep()", getClass().getSimpleName());
    return maxSignalDelta;
}

From source file:uk.ac.leeds.ccg.andyt.projects.moses.process.RegressionReport.java

public static JFreeChart getYEqualsXLineChart(double[][] data, String xAxisLabel_String,
        String yAxisLabel_String) {
    JFreeChart a_XYLineChart = null;//from   w w w.  jav  a2  s . c  o m
    String title = null;
    boolean legend = false;
    boolean tooltips = false;
    boolean urls = false;
    double[][] lineChartData = new double[2][2];
    lineChartData[0][0] = Double.MAX_VALUE;// min_CASObservedData;
    lineChartData[0][1] = Double.MIN_VALUE;// max_CASObservedData;
    lineChartData[1][0] = Double.MAX_VALUE;// min_SARExpectedData;
    lineChartData[1][1] = Double.MIN_VALUE;// max_SARExpectedData;
    for (int j = 0; j < data[0].length; j++) {
        lineChartData[0][0] = Math.min(lineChartData[0][0], data[0][j]);
        lineChartData[0][1] = Math.max(lineChartData[0][1], data[0][j]);
        lineChartData[1][0] = Math.min(lineChartData[1][0], data[1][j]);
        lineChartData[1][1] = Math.max(lineChartData[1][1], data[1][j]);
    }
    lineChartData[1][0] = lineChartData[0][0];
    lineChartData[0][1] = Math.min(lineChartData[1][1], lineChartData[0][1]);
    lineChartData[1][1] = lineChartData[0][1];
    System.out.println("min lineChartData[0][0] " + lineChartData[0][0]);
    System.out.println("max lineChartData[0][1] " + lineChartData[0][1]);
    System.out.println("min lineChartData[1][0] " + lineChartData[1][0]);
    System.out.println("max lineChartData[1][1] " + lineChartData[1][1]);
    DefaultXYDataset a_DefaultXYDataset = new DefaultXYDataset();
    a_DefaultXYDataset.addSeries("y = x", lineChartData);
    a_XYLineChart = ChartFactory.createXYLineChart(title, xAxisLabel_String, yAxisLabel_String,
            a_DefaultXYDataset, PlotOrientation.HORIZONTAL, legend, tooltips, urls);
    return a_XYLineChart;
}

From source file:classif.fuzzycmeans.EUCFUZZYCMEANSSymbolicSequence.java

private void delcluster(int k) {
    centroidsPerCluster[k] = null;//w w  w.j  ava 2 s  .  c  o m
    sigmasPerCluster[k] = Double.NaN;
    Sequence[] newcenter = new Sequence[nbClusters - 1];
    double[] newsigma = new double[nbClusters - 1];
    nbClusters = nbClusters - 1;
    int flag = 0;
    for (int i = 0; i < centroidsPerCluster.length; i++) {
        if (centroidsPerCluster[i] != null) {
            newcenter[i - flag] = centroidsPerCluster[i];
            newsigma[i - flag] = sigmasPerCluster[i];
        } else
            flag++;
    }
    centroidsPerCluster = newcenter;
    sigmasPerCluster = newsigma;
    ArrayList<Sequence>[] affectation = new ArrayList[nbClusters];
    int[] clusterMap = new int[data.size()];
    for (int i = 0; i < affectation.length; i++) {
        affectation[i] = new ArrayList<Sequence>();
    }

    for (int j = 0; j < data.size(); j++) {

        double minDist = Double.MAX_VALUE;
        // for each cluster k
        for (int i = 0; i < centroidsPerCluster.length; i++) {
            // distance between cluster k and data point j
            if (centroidsPerCluster[i] != null) {
                double currentDist = centroidsPerCluster[i].distanceEuc(data.get(j));
                if (currentDist < minDist) {
                    clusterMap[j] = i;
                    minDist = currentDist;
                }
            }
        }

        // affect data point j to cluster affected to j
        affectation[clusterMap[j]].add(data.get(j));
    }
    for (int i = 0; i < nbClusters; i++) {
        nck[i] = affectation[i].size();
    }
}

From source file:com.github.tteofili.looseen.yay.SGM.java

/**
 * perform weights learning from the training examples using (configurable) mini batch gradient descent algorithm
 *
 * @param samples the training examples//w  ww  .j  av  a 2 s  . c  o  m
 * @return the final cost with the updated weights
 * @throws Exception if BGD fails to converge or any numerical error happens
 */
private double learnWeights(Sample... samples) throws Exception {

    int iterations = 0;

    double cost = Double.MAX_VALUE;

    int j = 0;

    // momentum
    RealMatrix vb = MatrixUtils.createRealMatrix(biases[0].getRowDimension(), biases[0].getColumnDimension());
    RealMatrix vb2 = MatrixUtils.createRealMatrix(biases[1].getRowDimension(), biases[1].getColumnDimension());
    RealMatrix vw = MatrixUtils.createRealMatrix(weights[0].getRowDimension(), weights[0].getColumnDimension());
    RealMatrix vw2 = MatrixUtils.createRealMatrix(weights[1].getRowDimension(),
            weights[1].getColumnDimension());

    long start = System.currentTimeMillis();
    int c = 1;
    RealMatrix x = MatrixUtils.createRealMatrix(configuration.batchSize, samples[0].getInputs().length);
    RealMatrix y = MatrixUtils.createRealMatrix(configuration.batchSize, samples[0].getOutputs().length);
    while (true) {

        int i = 0;
        for (int k = j * configuration.batchSize; k < j * configuration.batchSize
                + configuration.batchSize; k++) {
            Sample sample = samples[k % samples.length];
            x.setRow(i, sample.getInputs());
            y.setRow(i, sample.getOutputs());
            i++;
        }
        j++;

        long time = (System.currentTimeMillis() - start) / 1000;
        if (iterations % (1 + (configuration.maxIterations / 100)) == 0 && time > 60 * c) {
            c += 1;
            //                System.out.println("cost: " + cost + ", accuracy: " + evaluate(this) + " after " + iterations + " iterations in " + (time / 60) + " minutes (" + ((double) iterations / time) + " ips)");
        }

        RealMatrix w0t = weights[0].transpose();
        RealMatrix w1t = weights[1].transpose();

        RealMatrix hidden = rectifierFunction.applyMatrix(x.multiply(w0t));
        hidden.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return value + biases[0].getEntry(0, column);
            }

            @Override
            public double end() {
                return 0;
            }
        });
        RealMatrix scores = hidden.multiply(w1t);
        scores.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return value + biases[1].getEntry(0, column);
            }

            @Override
            public double end() {
                return 0;
            }
        });

        RealMatrix probs = scores.copy();
        int len = scores.getColumnDimension() - 1;
        for (int d = 0; d < configuration.window - 1; d++) {
            int startColumn = d * len / (configuration.window - 1);
            RealMatrix subMatrix = scores.getSubMatrix(0, scores.getRowDimension() - 1, startColumn,
                    startColumn + x.getColumnDimension());
            for (int sm = 0; sm < subMatrix.getRowDimension(); sm++) {
                probs.setSubMatrix(softmaxActivationFunction.applyMatrix(subMatrix.getRowMatrix(sm)).getData(),
                        sm, startColumn);
            }
        }

        RealMatrix correctLogProbs = MatrixUtils.createRealMatrix(x.getRowDimension(), 1);
        correctLogProbs.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return -Math.log(probs.getEntry(row, getMaxIndex(y.getRow(row))));
            }

            @Override
            public double end() {
                return 0;
            }
        });
        double dataLoss = correctLogProbs.walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
            private double d = 0;

            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public void visit(int row, int column, double value) {
                d += value;
            }

            @Override
            public double end() {
                return d;
            }
        }) / samples.length;

        double reg = 0d;
        reg += weights[0].walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
            private double d = 0d;

            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public void visit(int row, int column, double value) {
                d += Math.pow(value, 2);
            }

            @Override
            public double end() {
                return d;
            }
        });
        reg += weights[1].walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
            private double d = 0d;

            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public void visit(int row, int column, double value) {
                d += Math.pow(value, 2);
            }

            @Override
            public double end() {
                return d;
            }
        });

        double regLoss = 0.5 * configuration.regularizationLambda * reg;
        double newCost = dataLoss + regLoss;
        if (iterations == 0) {
            //                System.out.println("started with cost = " + dataLoss + " + " + regLoss + " = " + newCost);
        }

        if (Double.POSITIVE_INFINITY == newCost) {
            throw new Exception("failed to converge at iteration " + iterations + " with alpha "
                    + configuration.alpha + " : cost going from " + cost + " to " + newCost);
        } else if (iterations > 1
                && (newCost < configuration.threshold || iterations > configuration.maxIterations)) {
            cost = newCost;
            //                System.out.println("successfully converged after " + (iterations - 1) + " iterations (alpha:" + configuration.alpha + ",threshold:" + configuration.threshold + ") with cost " + newCost);
            break;
        } else if (Double.isNaN(newCost)) {
            throw new Exception("failed to converge at iteration " + iterations + " with alpha "
                    + configuration.alpha + " : cost calculation underflow");
        }

        // update registered cost
        cost = newCost;

        // calculate the derivatives to update the parameters

        RealMatrix dscores = probs.copy();
        dscores.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return (y.getEntry(row, column) == 1 ? (value - 1) : value) / samples.length;
            }

            @Override
            public double end() {
                return 0;
            }
        });

        // get derivative on second layer
        RealMatrix dW2 = hidden.transpose().multiply(dscores);

        // regularize dw2
        dW2.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return value + configuration.regularizationLambda * w1t.getEntry(row, column);
            }

            @Override
            public double end() {
                return 0;
            }
        });

        RealMatrix db2 = MatrixUtils.createRealMatrix(biases[1].getRowDimension(),
                biases[1].getColumnDimension());
        dscores.walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public void visit(int row, int column, double value) {
                db2.setEntry(0, column, db2.getEntry(0, column) + value);
            }

            @Override
            public double end() {
                return 0;
            }
        });

        RealMatrix dhidden = dscores.multiply(weights[1]);
        dhidden.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return value < 0 ? 0 : value;
            }

            @Override
            public double end() {
                return 0;
            }
        });

        RealMatrix db = MatrixUtils.createRealMatrix(biases[0].getRowDimension(),
                biases[0].getColumnDimension());
        dhidden.walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public void visit(int row, int column, double value) {
                db.setEntry(0, column, db.getEntry(0, column) + value);
            }

            @Override
            public double end() {
                return 0;
            }
        });

        // get derivative on first layer
        RealMatrix dW = x.transpose().multiply(dhidden);

        // regularize
        dW.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
            @Override
            public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) {

            }

            @Override
            public double visit(int row, int column, double value) {
                return value + configuration.regularizationLambda * w0t.getEntry(row, column);
            }

            @Override
            public double end() {
                return 0;
            }
        });

        RealMatrix dWt = dW.transpose();
        RealMatrix dWt2 = dW2.transpose();

        if (configuration.useNesterovMomentum) {

            // update nesterov momentum
            final RealMatrix vbPrev = vb.copy();
            final RealMatrix vb2Prev = vb2.copy();
            final RealMatrix vwPrev = vw.copy();
            final RealMatrix vw2Prev = vw2.copy();

            vb.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * db.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vb2.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * db2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vw.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * dWt.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vw2.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * dWt2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            // update bias
            biases[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.mu * vbPrev.getEntry(row, column)
                            + (1 + configuration.mu) * vb.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            biases[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.mu * vb2Prev.getEntry(row, column)
                            + (1 + configuration.mu) * vb2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            // update the weights
            weights[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.mu * vwPrev.getEntry(row, column)
                            + (1 + configuration.mu) * vw.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            weights[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.mu * vw2Prev.getEntry(row, column)
                            + (1 + configuration.mu) * vw2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });
        } else if (configuration.useMomentum) {
            // update momentum
            vb.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * db.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vb2.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * db2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vw.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * dWt.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            vw2.walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return configuration.mu * value - configuration.alpha * dWt2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            // update bias
            biases[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value + vb.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            biases[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value + vb2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            // update the weights
            weights[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value + vw.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            weights[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value + vw2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });
        } else {
            // standard parameter update

            // update bias
            biases[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.alpha * db.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            biases[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {
                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.alpha * db2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            // update the weights
            weights[0].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.alpha * dWt.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });

            weights[1].walkInOptimizedOrder(new RealMatrixChangingVisitor() {

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {

                }

                @Override
                public double visit(int row, int column, double value) {
                    return value - configuration.alpha * dWt2.getEntry(row, column);
                }

                @Override
                public double end() {
                    return 0;
                }
            });
        }

        iterations++;
    }

    return cost;
}