/** * Query a regression algorithm using equilateral encoding. * * @param alg The algorithm being used. * @param theTrainingData The training data. * @param items The category items classified. * @param high The high value. * @param low The low value. */ public static void queryEquilateral( final RegressionAlgorithm alg, final List<BasicData> theTrainingData, final Map<String, Integer> items, final double high, final double low) { // first, we need to invert the items. Right now it maps from category to index. We need index // to category. final Map<Integer, String> invMap = new HashMap<>(); for (final Map.Entry<String, Integer> entry : items.entrySet()) { invMap.put(entry.getValue(), entry.getKey()); } // now we can query final Equilateral eq = new Equilateral(items.size(), high, low); for (final BasicData data : theTrainingData) { final double[] output = alg.computeRegression(data.getInput()); final int idealIndex = eq.decode(data.getIdeal()); final int actualIndex = eq.decode(output); System.out.println( Arrays.toString(data.getInput()) + " -> " + invMap.get(actualIndex) + ", Ideal: " + invMap.get(idealIndex)); } }
/** {@inheritDoc} */ @Override public void iteration() { this.network.setNetworkTraining(true); // alert the layers that a new batch is starting. for (Layer layer : this.network.getLayers()) { layer.trainingBatch(this.stochastic); } // begin the iteration this.gradients.reset(); this.errorCalc.clear(); int iterationSize = this.batchSize == 0 ? this.training.size() : Math.min(this.batchSize, this.training.size()); for (int i = 0; i < iterationSize; i++) { BasicData element; if (isOnlineTraining()) { if (this.stochastic != null) { int stochasticIndex = this.stochastic.nextInt(0, this.training.size()); element = this.training.get(stochasticIndex); } else { element = this.training.get(this.currentIndex++); } } else { element = this.training.get(i); } this.gradients.process(this.errorCalc, element.getInput(), element.getIdeal()); } if (this.currentIndex > this.training.size() || this.batchSize == 0) { this.currentIndex = 0; } this.currentError = this.errorCalc.calculate(); for (int i = 0; i < this.network.getWeights().length; i++) { double delta; if (this.nesterovUpdate) { double prevNesterov = this.lastDelta[i]; this.lastDelta[i] = (this.momentum * prevNesterov) + (this.gradients.getGradients()[i] * this.learningRate); delta = (this.momentum * prevNesterov) - ((1 + this.momentum) * this.lastDelta[i]); } else { delta = (this.gradients.getGradients()[i] * -this.learningRate) + (this.lastDelta[i] * this.momentum); this.lastDelta[i] = delta; } this.network.getWeights()[i] += delta; } this.network.setNetworkTraining(false); }
/** * Query a regression algorithm and see how close it matches the training data. * * @param alg The algorithm to evaluate. * @param theTrainingData The training data. */ public static void query(final RegressionAlgorithm alg, final List<BasicData> theTrainingData) { for (final BasicData data : theTrainingData) { final double[] output = alg.computeRegression(data.getInput()); System.out.println( Arrays.toString(data.getInput()) + " -> " + Arrays.toString(output) + ", Ideal: " + Arrays.toString(data.getIdeal())); } }