List of usage examples for java.lang Double isInfinite
public static boolean isInfinite(double v)
From source file:org.renjin.primitives.Types.java
@Generic @Primitive("is.finite") @AllowNA//from ww w.j av a 2s . c om public static boolean isFinite(@Recycle double value) { return !Double.isNaN(value) && !Double.isInfinite(value); }
From source file:com.opengamma.analytics.math.interpolation.MonotoneConvexSplineInterpolator.java
/** * Determine f(t) = \frac{\partial r(t) t}{\partial t} * @param xValues Data t_i//from ww w . j a v a2s. c o m * @param yValues Data r(t_i) * @return PiecewisePolynomialResult for f(t) */ public PiecewisePolynomialResult interpolateFwds(final double[] xValues, final double[] yValues) { ArgumentChecker.notNull(xValues, "xValues"); ArgumentChecker.notNull(yValues, "yValues"); ArgumentChecker.isTrue(xValues.length == yValues.length, " xValues length = yValues length"); ArgumentChecker.isTrue(xValues.length > 1, "Data points should be more than 1"); final int nDataPts = xValues.length; for (int i = 0; i < nDataPts; ++i) { ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity"); ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity"); } for (int i = 0; i < nDataPts; ++i) { for (int j = i + 1; j < nDataPts; ++j) { ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct"); } } for (int i = 0; i < nDataPts; ++i) { if (xValues[i] == 0.) { ArgumentChecker.isTrue(yValues[i] == 0., "r_i * t_i = 0 if t_i =0"); } } double[] spotTmp = new double[nDataPts]; for (int i = 0; i < nDataPts; ++i) { spotTmp[i] = xValues[i] == 0. ? 0. : yValues[i] / xValues[i]; } _time = Arrays.copyOf(xValues, nDataPts); _spotRates = Arrays.copyOf(spotTmp, nDataPts); ParallelArrayBinarySort.parallelBinarySort(_time, _spotRates); final DoubleMatrix2D coefMatrix = solve(_time, _spotRates); for (int i = 0; i < coefMatrix.getNumberOfRows(); ++i) { for (int j = 0; j < coefMatrix.getNumberOfColumns(); ++j) { ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input"); } } return new PiecewisePolynomialResult(new DoubleMatrix1D(_time), coefMatrix, coefMatrix.getNumberOfColumns(), 1); }
From source file:org.renjin.primitives.Types.java
@Generic @Primitive("is.infinite") @AllowNA//from www . j a v a2s. co m public static boolean isInfinite(@Recycle double value) { return Double.isInfinite(value); }
From source file:beast.evolution.tree.RandomTree.java
@SuppressWarnings("unchecked") @Override/*from ww w . j a va 2 s . c om*/ public void initStateNodes() { // find taxon sets we are dealing with taxonSets = new ArrayList<>(); m_bounds = new ArrayList<>(); distributions = new ArrayList<>(); taxonSetIDs = new ArrayList<>(); lastMonophyletic = 0; if (taxaInput.get() != null) { taxa.addAll(taxaInput.get().getTaxaNames()); } else { taxa.addAll(m_taxonset.get().asStringList()); } // pick up constraints from outputs, m_inititial input tree and output tree, if any List<MRCAPrior> calibrations = new ArrayList<>(); calibrations.addAll(calibrationsInput.get()); // for (BEASTObject beastObject : outputs) { // // pick up constraints in outputs // if (beastObject instanceof MRCAPrior && !calibrations.contains(beastObject)) { // calibrations.add((MRCAPrior) beastObject); // } else if (beastObject instanceof Tree) { // // pick up constraints in outputs if output tree // Tree tree = (Tree) beastObject; // if (tree.m_initial.get() == this) { // for (BEASTObject beastObject2 : tree.outputs) { // if (beastObject2 instanceof MRCAPrior && !calibrations.contains(beastObject2)) { // calibrations.add((MRCAPrior) beastObject2); // } // } // } // } // // } // pick up constraints in m_initial tree for (final Object beastObject : getOutputs()) { if (beastObject instanceof MRCAPrior && !calibrations.contains(beastObject)) { calibrations.add((MRCAPrior) beastObject); } } if (m_initial.get() != null) { for (final Object beastObject : m_initial.get().getOutputs()) { if (beastObject instanceof MRCAPrior && !calibrations.contains(beastObject)) { calibrations.add((MRCAPrior) beastObject); } } } for (final MRCAPrior prior : calibrations) { final TaxonSet taxonSet = prior.taxonsetInput.get(); if (taxonSet != null && !prior.onlyUseTipsInput.get()) { final Set<String> usedTaxa = new LinkedHashSet<>(); if (taxonSet.asStringList() == null) { taxonSet.initAndValidate(); } for (final String taxonID : taxonSet.asStringList()) { if (!taxa.contains(taxonID)) { throw new IllegalArgumentException("Taxon <" + taxonID + "> could not be found in list of taxa. Choose one of " + taxa); } usedTaxa.add(taxonID); } final ParametricDistribution distr = prior.distInput.get(); final Bound bounds = new Bound(); if (distr != null) { List<BEASTInterface> beastObjects = new ArrayList<>(); distr.getPredecessors(beastObjects); for (int i = beastObjects.size() - 1; i >= 0; i--) { beastObjects.get(i).initAndValidate(); } try { bounds.lower = distr.inverseCumulativeProbability(0.0) + distr.offsetInput.get(); bounds.upper = distr.inverseCumulativeProbability(1.0) + distr.offsetInput.get(); } catch (MathException e) { Log.warning.println("At RandomTree::initStateNodes, bound on MRCAPrior could not be set " + e.getMessage()); } } if (prior.isMonophyleticInput.get()) { // add any monophyletic constraint taxonSets.add(lastMonophyletic, usedTaxa); distributions.add(lastMonophyletic, distr); m_bounds.add(lastMonophyletic, bounds); taxonSetIDs.add(prior.getID()); lastMonophyletic++; } else { // only calibrations with finite bounds are added if (!Double.isInfinite(bounds.lower) || !Double.isInfinite(bounds.upper)) { taxonSets.add(usedTaxa); distributions.add(distr); m_bounds.add(bounds); taxonSetIDs.add(prior.getID()); } } } } // assume all calibration constraints are MonoPhyletic // TODO: verify that this is a reasonable assumption lastMonophyletic = taxonSets.size(); // sort constraints such that if taxon set i is subset of taxon set j, then i < j for (int i = 0; i < lastMonophyletic; i++) { for (int j = i + 1; j < lastMonophyletic; j++) { Set<String> intersection = new LinkedHashSet<>(taxonSets.get(i)); intersection.retainAll(taxonSets.get(j)); if (intersection.size() > 0) { final boolean isSubset = taxonSets.get(i).containsAll(taxonSets.get(j)); final boolean isSubset2 = taxonSets.get(j).containsAll(taxonSets.get(i)); // sanity check: make sure either // o taxonset1 is subset of taxonset2 OR // o taxonset1 is superset of taxonset2 OR // o taxonset1 does not intersect taxonset2 if (!(isSubset || isSubset2)) { throw new IllegalArgumentException( "333: Don't know how to generate a Random Tree for taxon sets that intersect, " + "but are not inclusive. Taxonset " + taxonSetIDs.get(i) + " and " + taxonSetIDs.get(j)); } // swap i & j if b1 subset of b2 if (isSubset) { swap(taxonSets, i, j); swap(distributions, i, j); swap(m_bounds, i, j); swap(taxonSetIDs, i, j); } } } } // build tree of mono constraints such that j is parent of i if i is a subset of j but i+1,i+2,...,j-1 are not. // The last one, standing for the virtual "root" of all monophyletic clades is not associated with an actual clade final int[] parent = new int[lastMonophyletic]; children = new List[lastMonophyletic + 1]; for (int i = 0; i < lastMonophyletic + 1; i++) { children[i] = new ArrayList<>(); } for (int i = 0; i < lastMonophyletic; i++) { int j = i + 1; while (j < lastMonophyletic && !taxonSets.get(j).containsAll(taxonSets.get(i))) { j++; } parent[i] = j; children[j].add(i); } // make sure upper bounds of a child does not exceed the upper bound of its parent for (int i = lastMonophyletic - 1; i >= 0; --i) { if (parent[i] < lastMonophyletic) { if (m_bounds.get(i).upper > m_bounds.get(parent[i]).upper) { m_bounds.get(i).upper = m_bounds.get(parent[i]).upper - 1e-100; } } } final PopulationFunction popFunction = populationFunctionInput.get(); simulateTree(taxa, popFunction); if (rootHeightInput.get() != null) { scaleToFit(rootHeightInput.get() / root.getHeight(), root); } nodeCount = 2 * taxa.size() - 1; internalNodeCount = taxa.size() - 1; leafNodeCount = taxa.size(); HashMap<String, Integer> taxonToNR = null; // preserve node numbers where possible if (m_initial.get() != null) { if (leafNodeCount == m_initial.get().getLeafNodeCount()) { // dont ask me how the initial tree is rubbish (i.e. 0:0.0) taxonToNR = new HashMap<>(); for (Node n : m_initial.get().getExternalNodes()) { taxonToNR.put(n.getID(), n.getNr()); } } } else { taxonToNR = new HashMap<>(); String[] taxa = getTaxaNames(); for (int k = 0; k < taxa.length; ++k) { taxonToNR.put(taxa[k], k); } } // multiple simulation tries may produce an excess of nodes with invalid nr's. reset those. setNodesNrs(root, 0, new int[1], taxonToNR); initArrays(); if (m_initial.get() != null) { m_initial.get().assignFromWithoutID(this); } for (int k = 0; k < lastMonophyletic; ++k) { final MRCAPrior p = calibrations.get(k); if (p.isMonophyleticInput.get()) { final TaxonSet taxonSet = p.taxonsetInput.get(); if (taxonSet == null) { throw new IllegalArgumentException("Something is wrong with constraint " + p.getID() + " -- a taxonset must be specified if a monophyletic constraint is enforced."); } final Set<String> usedTaxa = new LinkedHashSet<>(); if (taxonSet.asStringList() == null) { taxonSet.initAndValidate(); } usedTaxa.addAll(taxonSet.asStringList()); /* int c = */ traverse(root, usedTaxa, taxonSet.getTaxonCount(), new int[1]); // boolean b = c == nrOfTaxa + 127; } } }
From source file:org.eclipse.january.metadata.internal.StatisticsMetadataImpl.java
/** * Calculate summary statistics for a dataset * @param ignoreNaNs if true, ignore NaNs * @param ignoreInfs if true, ignore infinities *//*w w w. ja v a2 s.co m*/ @SuppressWarnings("unchecked") private SummaryStatistics[] createSummaryStats(final MaxMin<T> mm, final boolean ignoreNaNs, final boolean ignoreInfs) { final IndexIterator iter = dataset.getIterator(); SummaryStatistics[] istats = new SummaryStatistics[isize]; for (int i = 0; i < isize; i++) { istats[i] = new SummaryStatistics(); // sum of logs is slow and we don't use it, so blocking its calculation here istats[i].setSumLogImpl(new NullStorelessUnivariateStatistic()); } SummaryStatistics stats; if (isize == 1) { boolean hasNaNs = false; stats = istats[0]; if (dataset.hasFloatingPointElements() && (ignoreNaNs || ignoreInfs)) { while (iter.hasNext()) { final double val = dataset.getElementDoubleAbs(iter.index); hash = (int) (hash * 19 + Double.doubleToRawLongBits(val)); if (Double.isNaN(val)) { if (ignoreNaNs) continue; hasNaNs = true; } else if (Double.isInfinite(val)) { if (ignoreInfs) continue; } stats.addValue(val); } } else if (dataset.hasFloatingPointElements()) { while (iter.hasNext()) { final double val = dataset.getElementDoubleAbs(iter.index); hash = (int) (hash * 19 + Double.doubleToRawLongBits(val)); if (Double.isNaN(val)) { hasNaNs = true; } stats.addValue(val); } } else { while (iter.hasNext()) { final long val = dataset.getElementLongAbs(iter.index); hash = (int) (hash * 19 + val); stats.addValue(val); } } mm.maximum = (T) (hasNaNs ? Double.NaN : DTypeUtils.fromDoubleToBiggestNumber(stats.getMax(), dtype)); mm.minimum = (T) (hasNaNs ? Double.NaN : DTypeUtils.fromDoubleToBiggestNumber(stats.getMin(), dtype)); } else { double[] vals = new double[isize]; while (iter.hasNext()) { boolean okay = true; for (int j = 0; j < isize; j++) { final double val = dataset.getElementDoubleAbs(iter.index + j); if (ignoreNaNs && Double.isNaN(val)) { okay = false; break; } if (ignoreInfs && Double.isInfinite(val)) { okay = false; break; } vals[j] = val; } if (okay) { for (int j = 0; j < isize; j++) { double val = vals[j]; istats[j].addValue(val); hash = (int) (hash * 19 + Double.doubleToRawLongBits(val)); } } } double[] lmax = new double[isize]; double[] lmin = new double[isize]; for (int j = 0; j < isize; j++) { stats = istats[j]; lmax[j] = stats.getMax(); lmin[j] = stats.getMin(); } mm.maximum = (T) lmax; mm.minimum = (T) lmin; } hash = hash * 19 + dtype * 17 + isize; mm.maximumPositions = null; mm.minimumPositions = null; return istats; }
From source file:org.rhq.enterprise.server.measurement.MeasurementDataManagerBean.java
/** * Add metrics data to the database. Data that is passed can come from several Schedules, but needs to be of only * one type of MeasurementGathering. For good performance it is important that the agent sends batches as big as * possible (ok, perhaps not more than 100 items at a time). * * @param data the actual data points//w w w.ja va 2s . c o m */ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void addNumericData(Set<MeasurementDataNumeric> data) { if ((data == null) || (data.isEmpty())) { return; } int expectedCount = data.size(); Connection conn = null; DatabaseType dbType = null; Map<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>(); try { conn = rhqDs.getConnection(); dbType = DatabaseTypeFactory.getDatabaseType(conn); if (dbType instanceof Postgresql83DatabaseType) { Statement st = null; try { // Take advantage of async commit here st = conn.createStatement(); st.execute("SET synchronous_commit = off"); } finally { JDBCUtil.safeClose(st); } } for (MeasurementDataNumeric aData : data) { Double value = aData.getValue(); if ((value == null) || Double.isNaN(value) || Double.isInfinite(value)) { expectedCount--; continue; } String table = MeasurementDataManagerUtility.getTable(aData.getTimestamp()); PreparedStatement ps = statements.get(table); if (ps == null) { String insertSql = "INSERT /*+ APPEND */ INTO " + table + "(schedule_id,time_stamp,value) VALUES(?,?,?)"; ps = conn.prepareStatement(insertSql); statements.put(table, ps); } ps.setInt(1, aData.getScheduleId()); ps.setLong(2, aData.getTimestamp()); ps.setDouble(3, value); ps.addBatch(); } int count = 0; for (PreparedStatement ps : statements.values()) { int[] res = ps.executeBatch(); for (int updates : res) { if ((updates != 1) && (updates != -2)) // oracle returns -2 on success { throw new MeasurementStorageException("Unexpected batch update size [" + updates + "]"); } count++; } } if (count != expectedCount) { throw new MeasurementStorageException("Failure to store measurement data."); } notifyAlertConditionCacheManager("mergeMeasurementReport", data.toArray(new MeasurementData[data.size()])); } catch (SQLException e) { log.warn("Failure saving measurement numeric data:\n" + ThrowableUtil.getAllMessages(e)); } catch (Exception e) { log.error("Error persisting numeric data", e); } finally { for (PreparedStatement ps : statements.values()) { JDBCUtil.safeClose(ps); } JDBCUtil.safeClose(conn); } }
From source file:org.drugis.addis.presentation.wizard.TreatmentCategorizationWizardPresentationTest.java
@Test public void testRewriteDoNotConsiderDoseType() { Category foo = new Category(d_bean, "foo"); d_pm.getCategories().add(foo);/*from w w w .j ava 2s . c o m*/ Category bar = new Category(d_bean, "bar"); d_pm.getCategories().add(bar); d_pm.getModelForKnownDose() .setValue(TreatmentCategorizationWizardPresentation.CategorySpecifiers.DO_NOT_CONSIDER); d_pm.getModelForFixedDose().setValue(d_pm.getFixedRangeNode()); // Add ranges to tree (normally handled by RangeInputPresentation). RangeEdge edge0 = RangeEdge.createDefault(); d_pm.getBean().getDecisionTree().addChild(edge0, d_pm.getFixedRangeNode(), findLeafNode(d_pm.getOptionsForEdge(edge0), bar)); Pair<RangeEdge> splitRange = d_pm.splitRange(edge0, 20.0, false); d_pm.getModelForEdge(splitRange.getSecond()) .setValue(findLeafNode(d_pm.getOptionsForEdge(splitRange.getSecond()), foo)); d_pm.transformTree(); // The transformed tree should consider MIN_DOSE first. assertEquals(d_pm.getFlexibleLowerRangeNode(), d_pm.getModelForFlexibleDose().getValue()); // The ranges for MIN_DOSE should be identical to those for FixedDose List<DecisionTreeEdge> lowerEdges = d_pm.getFlexibleLowerRanges(); ObservableList<DecisionTreeEdge> fixedEdges = d_pm.getOutEdges(d_pm.getFixedRangeNode()); assertEquals(2, lowerEdges.size()); assertEquals(fixedEdges.get(0).toString(), lowerEdges.get(0).toString()); assertEquals(fixedEdges.get(1).toString(), lowerEdges.get(1).toString()); for (int i = 0; i < lowerEdges.size(); i++) { RangeEdge fixedRange = (RangeEdge) fixedEdges.get(i); RangeEdge lowerRange = (RangeEdge) lowerEdges.get(i); DoseQuantityChoiceNode upper = (DoseQuantityChoiceNode) d_pm.getModelForEdge(lowerRange).getValue(); // FIXME: test that the node is in the options list. // For each MIN_DOSE range, the MAX_DOSE should subsequently be considered assertEquals(FlexibleDose.class, upper.getBeanClass()); assertEquals(FlexibleDose.PROPERTY_MAX_DOSE, upper.getPropertyName()); assertSame(d_bean.getDoseUnit(), upper.getDoseUnit()); // There should be two out-edges, unless the upper bound is +infinity List<DecisionTreeEdge> upperEdges = d_pm.getOutEdges(upper); assertEquals(lowerRange.toString(), upperEdges.get(0).toString()); assertNodeHasCategory((DecisionTreeNode) d_pm.getModelForEdge(upperEdges.get(0)).getValue(), ((LeafNode) d_pm.getModelForEdge(fixedRange).getValue()).getCategory()); if (!Double.isInfinite(lowerRange.getUpperBound())) { assertEquals(2, upperEdges.size()); RangeEdge expected = new RangeEdge(lowerRange.getUpperBound(), !lowerRange.isUpperBoundOpen(), Double.POSITIVE_INFINITY, true); assertEquals(expected.toString(), upperEdges.get(1).toString()); assertNodeHasCategory((DecisionTreeNode) d_pm.getModelForEdge(upperEdges.get(1)).getValue(), null); } else { assertEquals(1, upperEdges.size()); } } }
From source file:com.rapidminer.gui.plotter.charts.Abstract2DChartPlotter.java
private void prepareNumericalData() { this.nominal = false; dataSet = new DefaultXYZDataset(); if (axis[X_AXIS] >= 0 && axis[Y_AXIS] >= 0) { this.minColor = Double.POSITIVE_INFINITY; this.maxColor = Double.NEGATIVE_INFINITY; List<double[]> dataList = new LinkedList<double[]>(); List<String> idList = new LinkedList<String>(); synchronized (dataTable) { Iterator<DataTableRow> i = this.dataTable.iterator(); while (i.hasNext()) { DataTableRow row = i.next(); double xValue = Double.NaN; if (axis[X_AXIS] >= 0) { xValue = row.getValue(axis[X_AXIS]); }/*w w w. ja va 2 s . co m*/ double yValue = Double.NaN; if (axis[Y_AXIS] >= 0) { yValue = row.getValue(axis[Y_AXIS]); } double colorValue = Double.NaN; if (colorColumn >= 0) { colorValue = row.getValue(colorColumn); } if (plotColumnsLogScale) { if (Tools.isLessEqual(colorValue, 0.0d)) { colorValue = 0; } else { colorValue = Math.log10(colorValue); } } // TM: removed check // if (!Double.isNaN(xValue) && !Double.isNaN(yValue)) { double[] data = new double[3]; data[X_AXIS] = xValue; data[Y_AXIS] = yValue; data[COLOR_AXIS] = colorValue; if (!Double.isNaN(colorValue)) { this.minColor = Math.min(this.minColor, colorValue); this.maxColor = Math.max(this.maxColor, colorValue); } dataList.add(data); idList.add(row.getId()); // } } } double[][] data = new double[3][dataList.size()]; double minX = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY; double minY = Double.POSITIVE_INFINITY; double maxY = Double.NEGATIVE_INFINITY; int index = 0; for (double[] d : dataList) { data[X_AXIS][index] = d[X_AXIS]; data[Y_AXIS][index] = d[Y_AXIS]; data[COLOR_AXIS][index] = d[COLOR_AXIS]; minX = MathFunctions.robustMin(minX, d[X_AXIS]); maxX = MathFunctions.robustMax(maxX, d[X_AXIS]); minY = MathFunctions.robustMin(minY, d[Y_AXIS]); maxY = MathFunctions.robustMax(maxY, d[Y_AXIS]); index++; } // jittering if (this.jitterAmount > 0) { Random jitterRandom = new Random(2001); double oldXRange = maxX - minX; double oldYRange = maxY - minY; for (int i = 0; i < dataList.size(); i++) { if (Double.isInfinite(oldXRange) || Double.isNaN(oldXRange)) { oldXRange = 0; } if (Double.isInfinite(oldYRange) || Double.isNaN(oldYRange)) { oldYRange = 0; } double pertX = oldXRange * (jitterAmount / 200.0d) * jitterRandom.nextGaussian(); double pertY = oldYRange * (jitterAmount / 200.0d) * jitterRandom.nextGaussian(); data[X_AXIS][i] += pertX; data[Y_AXIS][i] += pertY; } } // add data ((DefaultXYZDataset) dataSet).addSeries("All", data); // id handling int idCounter = 0; for (String id : idList) { idMap.put(new SeriesAndItem(0, idCounter++), id); } } }
From source file:org.apache.mahout.clustering.lda.LDADriver.java
static LDAState createState(Configuration job) throws IOException { String statePath = job.get(STATE_IN_KEY); int numTopics = Integer.parseInt(job.get(NUM_TOPICS_KEY)); int numWords = Integer.parseInt(job.get(NUM_WORDS_KEY)); double topicSmoothing = Double.parseDouble(job.get(TOPIC_SMOOTHING_KEY)); Path dir = new Path(statePath); FileSystem fs = dir.getFileSystem(job); DenseMatrix pWgT = new DenseMatrix(numTopics, numWords); double[] logTotals = new double[numTopics]; double ll = 0.0; IntPairWritable key = new IntPairWritable(); DoubleWritable value = new DoubleWritable(); for (FileStatus status : fs.globStatus(new Path(dir, "part-*"))) { Path path = status.getPath(); SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, job); while (reader.next(key, value)) { int topic = key.getFirst(); int word = key.getSecond(); if (word == TOPIC_SUM_KEY) { logTotals[topic] = value.get(); if (Double.isInfinite(value.get())) { throw new IllegalArgumentException(); }// ww w.j a v a 2s . c o m } else if (topic == LOG_LIKELIHOOD_KEY) { ll = value.get(); } else { if (!((topic >= 0) && (word >= 0))) { throw new IllegalArgumentException(topic + " " + word); } if (pWgT.getQuick(topic, word) != 0.0) { throw new IllegalArgumentException(); } pWgT.setQuick(topic, word, value.get()); if (Double.isInfinite(pWgT.getQuick(topic, word))) { throw new IllegalArgumentException(); } } } reader.close(); } return new LDAState(numTopics, numWords, topicSmoothing, pWgT, logTotals, ll); }
From source file:org.deeplearning4j.optimize.solvers.BackTrackLineSearch.java
/** * @param parameters the parameters to optimize * @param gradients the line/rate of change * @param searchDirection the point for the line search to go in * @return the next step size/*from ww w .j ava 2 s. co m*/ * @throws InvalidStepException */ @Override public double optimize(INDArray parameters, INDArray gradients, INDArray searchDirection) throws InvalidStepException { double test, stepMin, step, step2, oldStep, tmpStep; double rhs1, rhs2, a, b, disc, score, scoreAtStart, score2; minObjectiveFunction = (stepFunction instanceof NegativeDefaultStepFunction || stepFunction instanceof NegativeGradientStepFunction); Level1 l1Blas = Nd4j.getBlasWrapper().level1(); double sum = l1Blas.nrm2(searchDirection); double slope = -1f * Nd4j.getBlasWrapper().dot(searchDirection, gradients); log.debug("slope = {}", slope); INDArray maxOldParams = abs(parameters); Nd4j.getExecutioner().exec(new ScalarSetValue(maxOldParams, 1)); INDArray testMatrix = abs(gradients).divi(maxOldParams); test = testMatrix.max(Integer.MAX_VALUE).getDouble(0); step = 1.0; // initially, step = 1.0, i.e. take full Newton step stepMin = relTolx / test; // relative convergence tolerance oldStep = 0.0; step2 = 0.0; score = score2 = scoreAtStart = layer.score(); double bestScore = score; double bestStepSize = 1.0; if (log.isTraceEnabled()) { double norm1 = l1Blas.asum(searchDirection); int infNormIdx = l1Blas.iamax(searchDirection); double infNorm = FastMath.max(Float.NEGATIVE_INFINITY, searchDirection.getDouble(infNormIdx)); log.trace("ENTERING BACKTRACK\n"); log.trace("Entering BackTrackLineSearch, value = " + scoreAtStart + ",\ndirection.oneNorm:" + norm1 + " direction.infNorm:" + infNorm); } if (sum > stepMax) { log.warn("Attempted step too big. scaling: sum= {}, stepMax= {}", sum, stepMax); searchDirection.muli(stepMax / sum); } // if (slope >= 0.0) { // throw new InvalidStepException("Slope " + slope + " is >= 0.0. Expect slope < 0.0 when minimizing objective function"); // } // find maximum lambda // converge when (delta x) / x < REL_TOLX for all coordinates. // the largest step size that triggers this threshold is precomputed and saved in stepMin // look for step size in direction given by "line" INDArray candidateParameters = null; for (int iteration = 0; iteration < maxIterations; iteration++) { if (log.isTraceEnabled()) { log.trace("BackTrack loop iteration {} : step={}, oldStep={}", iteration, step, oldStep); log.trace("before step, x.1norm: {} \nstep: {} \noldStep: {}", parameters.norm1(Integer.MAX_VALUE), step, oldStep); } if (step == oldStep) throw new IllegalArgumentException("Current step == oldStep"); // step candidateParameters = parameters.dup('f'); stepFunction.step(candidateParameters, searchDirection, step); oldStep = step; if (log.isTraceEnabled()) { double norm1 = l1Blas.asum(candidateParameters); log.trace("after step, x.1norm: " + norm1); } // check for convergence on delta x if ((step < stepMin) || Nd4j.getExecutioner() .execAndReturn(new Eps(parameters, candidateParameters, Shape.toOffsetZeroCopy(candidateParameters, 'f'), candidateParameters.length())) .sum(Integer.MAX_VALUE).getDouble(0) == candidateParameters.length()) { score = setScoreFor(parameters); log.debug( "EXITING BACKTRACK: Jump too small (stepMin = {}). Exiting and using original params. Score = {}", stepMin, score); return 0.0; } score = setScoreFor(candidateParameters); log.debug("Model score after step = {}", score); //Score best step size for use if we terminate on maxIterations if ((minObjectiveFunction && score < bestScore) || (!minObjectiveFunction && score > bestScore)) { bestScore = score; bestStepSize = step; } //Sufficient decrease in cost/loss function (Wolfe condition / Armijo condition) if (minObjectiveFunction && score <= scoreAtStart + ALF * step * slope) { log.debug( "Sufficient decrease (Wolfe cond.), exiting backtrack on iter {}: score={}, scoreAtStart={}", iteration, score, scoreAtStart); if (score > scoreAtStart) throw new IllegalStateException( "Function did not decrease: score = " + score + " > " + scoreAtStart + " = oldScore"); return step; } //Sufficient increase in cost/loss function (Wolfe condition / Armijo condition) if (!minObjectiveFunction && score >= scoreAtStart + ALF * step * slope) { log.debug("Sufficient increase (Wolfe cond.), exiting backtrack on iter {}: score={}, bestScore={}", iteration, score, scoreAtStart); if (score < scoreAtStart) throw new IllegalStateException("Function did not increase: score = " + score + " < " + scoreAtStart + " = scoreAtStart"); return step; } // if value is infinite, i.e. we've jumped to unstable territory, then scale down jump else if (Double.isInfinite(score) || Double.isInfinite(score2) || Double.isNaN(score) || Double.isNaN(score2)) { log.warn("Value is infinite after jump. oldStep={}. score={}, score2={}. Scaling back step size...", oldStep, score, score2); tmpStep = .2 * step; if (step < stepMin) { //convergence on delta x score = setScoreFor(parameters); log.warn( "EXITING BACKTRACK: Jump too small (step={} < stepMin={}). Exiting and using previous parameters. Value={}", step, stepMin, score); return 0.0; } } // backtrack else if (minObjectiveFunction) { if (step == 1.0) // first time through tmpStep = -slope / (2.0 * (score - scoreAtStart - slope)); else { rhs1 = score - scoreAtStart - step * slope; rhs2 = score2 - scoreAtStart - step2 * slope; if (step == step2) throw new IllegalStateException( "FAILURE: dividing by step-step2 which equals 0. step=" + step); double stepSquared = step * step; double step2Squared = step2 * step2; a = (rhs1 / stepSquared - rhs2 / step2Squared) / (step - step2); b = (-step2 * rhs1 / stepSquared + step * rhs2 / step2Squared) / (step - step2); if (a == 0.0) tmpStep = -slope / (2.0 * b); else { disc = b * b - 3.0 * a * slope; if (disc < 0.0) { tmpStep = 0.5 * step; } else if (b <= 0.0) tmpStep = (-b + FastMath.sqrt(disc)) / (3.0 * a); else tmpStep = -slope / (b + FastMath.sqrt(disc)); } if (tmpStep > 0.5 * step) tmpStep = 0.5 * step; // lambda <= 0.5 lambda_1 } } else { if (step == 1.0) // first time through tmpStep = -slope / (2.0 * (scoreAtStart - score - slope)); else { rhs1 = scoreAtStart - score - step * slope; rhs2 = scoreAtStart - score2 - step2 * slope; if (step == step2) throw new IllegalStateException( "FAILURE: dividing by step-step2 which equals 0. step=" + step); double stepSquared = step * step; double step2Squared = step2 * step2; a = (rhs1 / stepSquared - rhs2 / step2Squared) / (step - step2); b = (-step2 * rhs1 / stepSquared + step * rhs2 / step2Squared) / (step - step2); if (a == 0.0) tmpStep = -slope / (2.0 * b); else { disc = b * b - 3.0 * a * slope; if (disc < 0.0) { tmpStep = 0.5 * step; } else if (b <= 0.0) tmpStep = (-b + FastMath.sqrt(disc)) / (3.0 * a); else tmpStep = -slope / (b + FastMath.sqrt(disc)); } if (tmpStep > 0.5 * step) tmpStep = 0.5 * step; // lambda <= 0.5 lambda_1 } } step2 = step; score2 = score; log.debug("tmpStep: {}", tmpStep); step = Math.max(tmpStep, .1f * step); // lambda >= .1*Lambda_1 } if (minObjectiveFunction && bestScore < scoreAtStart) { //Return best step size log.debug( "Exited line search after maxIterations termination condition; bestStepSize={}, bestScore={}, scoreAtStart={}", bestStepSize, bestScore, scoreAtStart); return bestStepSize; } else if (!minObjectiveFunction && bestScore > scoreAtStart) { //Return best step size log.debug( "Exited line search after maxIterations termination condition; bestStepSize={}, bestScore={}, scoreAtStart={}", bestStepSize, bestScore, scoreAtStart); return bestStepSize; } else { log.debug( "Exited line search after maxIterations termination condition; score did not improve (bestScore={}, scoreAtStart={}). Resetting parameters", bestScore, scoreAtStart); setScoreFor(parameters); return 0.0; } }