use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class BasicTrainable method eval.
/**
* Eval point sample.
*
* @param list the list
* @param monitor the monitor
* @return the point sample
*/
@Nonnull
protected PointSample eval(@Nonnull final List<Tensor[]> list, @Nullable final TrainingMonitor monitor) {
@Nonnull final TimedResult<PointSample> timedResult = TimedResult.time(() -> {
final Result[] nnContext = BasicTrainable.getNNContext(list, mask);
final Result result = network.eval(nnContext);
for (@Nonnull Result nnResult : nnContext) {
nnResult.getData().freeRef();
nnResult.freeRef();
}
final TensorList resultData = result.getData();
@Nonnull final DeltaSet<Layer> deltaSet = new DeltaSet<Layer>();
@Nonnull StateSet<Layer> stateSet = null;
try {
final DoubleSummaryStatistics statistics = resultData.stream().flatMapToDouble(x -> {
double[] array = Arrays.stream(x.getData()).toArray();
x.freeRef();
return Arrays.stream(array);
}).summaryStatistics();
final double sum = statistics.getSum();
result.accumulate(deltaSet, 1.0);
stateSet = new StateSet<>(deltaSet);
// log.info(String.format("Evaluated to %s delta buffers, %s mag", DeltaSet<LayerBase>.getMap().size(), DeltaSet<LayerBase>.getMagnitude()));
return new PointSample(deltaSet, stateSet, sum, 0.0, list.size());
} finally {
if (null != stateSet)
stateSet.freeRef();
resultData.freeRefAsync();
result.freeRefAsync();
deltaSet.freeRefAsync();
}
});
if (null != monitor && verbosity() > 0) {
monitor.log(String.format("Device completed %s items in %.3f sec", list.size(), timedResult.timeNanos / 1e9));
}
@Nonnull PointSample normalize = timedResult.result.normalize();
timedResult.result.freeRef();
return normalize;
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class TensorListTrainable method eval.
/**
* Eval point sample.
*
* @param list the list
* @param monitor the monitor
* @return the point sample
*/
@Nonnull
protected PointSample eval(@Nonnull final TensorList[] list, @Nullable final TrainingMonitor monitor) {
int inputs = data.length;
assert 0 < inputs;
int items = data[0].length();
assert 0 < items;
@Nonnull final TimedResult<PointSample> timedResult = TimedResult.time(() -> {
final Result[] nnContext = TensorListTrainable.getNNContext(list, mask);
final Result result = network.eval(nnContext);
for (@Nonnull Result nnResult : nnContext) {
nnResult.getData().freeRef();
nnResult.freeRef();
}
final TensorList resultData = result.getData();
final DoubleSummaryStatistics statistics = resultData.stream().flatMapToDouble(x -> {
double[] array = Arrays.stream(x.getData()).toArray();
x.freeRef();
return Arrays.stream(array);
}).summaryStatistics();
final double sum = statistics.getSum();
@Nonnull final DeltaSet<Layer> deltaSet = new DeltaSet<Layer>();
@Nonnull PointSample pointSample;
try {
result.accumulate(deltaSet, 1.0);
// log.info(String.format("Evaluated to %s delta buffers, %s mag", DeltaSet<LayerBase>.getMap().size(), DeltaSet<LayerBase>.getMagnitude()));
@Nonnull StateSet<Layer> stateSet = new StateSet<>(deltaSet);
pointSample = new PointSample(deltaSet, stateSet, sum, 0.0, items);
stateSet.freeRef();
} finally {
resultData.freeRef();
result.freeRef();
deltaSet.freeRef();
}
return pointSample;
});
if (null != monitor && verbosity() > 0) {
monitor.log(String.format("Device completed %s items in %.3f sec", items, timedResult.timeNanos / 1e9));
}
@Nonnull PointSample normalize = timedResult.result.normalize();
timedResult.result.freeRef();
return normalize;
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class DoubleArrayStatsFacade method sumSq.
/**
* Sum sq double.
*
* @return the double
*/
public double sumSq() {
final DoubleStream doubleStream = Arrays.stream(data).map((final double x) -> x * x);
final DoubleSummaryStatistics statistics = doubleStream.summaryStatistics();
return statistics.getSum();
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method compareTime.
/**
* Compare plot canvas.
*
* @param title the title
* @param trials the trials
* @return the plot canvas
*/
public static PlotCanvas compareTime(final String title, @Nonnull final ProblemRun... trials) {
try {
final DoubleSummaryStatistics[] xStatistics = Arrays.stream(trials).map(x -> x.history.stream().mapToDouble(step -> step.epochTime).filter(Double::isFinite).summaryStatistics()).toArray(i -> new DoubleSummaryStatistics[i]);
final double totalTime = Arrays.stream(xStatistics).mapToDouble(x -> x.getMax() - x.getMin()).max().getAsDouble();
final DoubleSummaryStatistics yStatistics = Arrays.stream(trials).flatMapToDouble(x -> x.history.stream().filter(y -> y.fitness > 0).mapToDouble(step -> Math.log10(step.fitness))).filter(Double::isFinite).summaryStatistics();
if (yStatistics.getCount() == 0) {
log.info("No Data");
return null;
}
@Nonnull final double[] lowerBound = { 0, yStatistics.getCount() == 0 ? 0 : yStatistics.getMin() };
@Nonnull final double[] upperBound = { totalTime / 1000.0, yStatistics.getCount() == 1 ? 0 : yStatistics.getMax() };
@Nonnull final PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound);
canvas.setTitle(title);
canvas.setAxisLabels("Time", "log10(Fitness)");
canvas.setSize(600, 400);
final List<ProblemRun> filtered = Arrays.stream(trials).filter(x -> !x.history.isEmpty()).collect(Collectors.toList());
if (filtered.isEmpty()) {
log.info("No Data");
return null;
}
DoubleSummaryStatistics valueStatistics = filtered.stream().flatMap(x -> x.history.stream()).mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
log.info(String.format("Plotting range=%s, %s; valueStats=%s", Arrays.toString(lowerBound), Arrays.toString(upperBound), valueStatistics));
for (int t = 0; t < filtered.size(); t++) {
final ProblemRun trial = filtered.get(t);
final DoubleSummaryStatistics trialStats = xStatistics[t];
final double[][] pts = trial.history.stream().map(step -> {
return new double[] { (step.epochTime - trialStats.getMin()) / 1000.0, Math.log10(Math.max(step.fitness, valueStatistics.getMin())) };
}).filter(x -> Arrays.stream(x).allMatch(Double::isFinite)).toArray(i -> new double[i][]);
if (pts.length > 1) {
log.info(String.format("Plotting %s points for %s", pts.length, trial.name));
canvas.add(trial.plot(pts));
} else {
log.info(String.format("Only %s points for %s", pts.length, trial.name));
}
}
return canvas;
} catch (@Nonnull final Exception e) {
e.printStackTrace(System.out);
return null;
}
}
use of java.util.DoubleSummaryStatistics in project guava by google.
the class StatsTest method testEquivalentStreams.
public void testEquivalentStreams() {
// and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
double[] array = values.asArray();
Stats stats = Stats.of(array);
// instance methods on Stats vs on instance methods on DoubleStream
assertThat(stats.count()).isEqualTo(stream(array).count());
assertEquivalent(stats.mean(), stream(array).average().getAsDouble());
assertEquivalent(stats.sum(), stream(array).sum());
assertEquivalent(stats.max(), stream(array).max().getAsDouble());
assertEquivalent(stats.min(), stream(array).min().getAsDouble());
// static method on Stats vs on instance method on DoubleStream
assertEquivalent(Stats.meanOf(array), stream(array).average().getAsDouble());
// instance methods on Stats vs instance methods on DoubleSummaryStatistics
DoubleSummaryStatistics streamStats = stream(array).summaryStatistics();
assertThat(stats.count()).isEqualTo(streamStats.getCount());
assertEquivalent(stats.mean(), streamStats.getAverage());
assertEquivalent(stats.sum(), streamStats.getSum());
assertEquivalent(stats.max(), streamStats.getMax());
assertEquivalent(stats.min(), streamStats.getMin());
}
}
Aggregations