use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method plot.
/**
* Plot plot canvas.
*
* @param history the history
* @return the plot canvas
*/
public static PlotCanvas plot(@Nonnull final List<StepRecord> history) {
try {
final DoubleSummaryStatistics valueStats = history.stream().mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
double[][] data = history.stream().map(step -> new double[] { step.iteration, Math.log10(Math.max(valueStats.getMin(), step.fitness)) }).filter(x -> Arrays.stream(x).allMatch(Double::isFinite)).toArray(i -> new double[i][]);
if (Arrays.stream(data).mapToInt(x -> x.length).sum() == 0)
return null;
@Nonnull final PlotCanvas plot = ScatterPlot.plot(data);
plot.setTitle("Convergence Plot");
plot.setAxisLabels("Iteration", "log10(Fitness)");
plot.setSize(600, 400);
return plot;
} catch (@Nonnull final Exception e) {
e.printStackTrace(System.out);
return null;
}
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method plotTime.
/**
* Plot plot canvas.
*
* @param history the history
* @return the plot canvas
*/
public static PlotCanvas plotTime(@Nonnull final List<StepRecord> history) {
try {
final LongSummaryStatistics timeStats = history.stream().mapToLong(x -> x.epochTime).summaryStatistics();
final DoubleSummaryStatistics valueStats = history.stream().mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
@Nonnull final PlotCanvas plot = ScatterPlot.plot(history.stream().map(step -> new double[] { (step.epochTime - timeStats.getMin()) / 1000.0, Math.log10(Math.max(valueStats.getMin(), step.fitness)) }).filter(x -> Arrays.stream(x).allMatch(Double::isFinite)).toArray(i -> new double[i][]));
plot.setTitle("Convergence Plot");
plot.setAxisLabels("Time", "log10(Fitness)");
plot.setSize(600, 400);
return plot;
} catch (@Nonnull final Exception e) {
e.printStackTrace(System.out);
return null;
}
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method printHistory.
/**
* Print history.
*
* @param log the log
* @param history the history
*/
public static void printHistory(@Nonnull final NotebookOutput log, @Nonnull final List<StepRecord> history) {
if (!history.isEmpty()) {
log.out("Convergence Plot: ");
log.code(() -> {
final DoubleSummaryStatistics valueStats = history.stream().mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
@Nonnull final PlotCanvas plot = ScatterPlot.plot(history.stream().map(step -> new double[] { step.iteration, Math.log10(Math.max(valueStats.getMin(), step.fitness)) }).toArray(i -> new double[i][]));
plot.setTitle("Convergence Plot");
plot.setAxisLabels("Iteration", "log10(Fitness)");
plot.setSize(600, 400);
return plot;
});
}
}
use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.
the class SoftmaxActivationLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
final int itemCnt = inObj[0].getData().length();
@Nonnull final double[] sumA = new double[itemCnt];
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
@Nonnull final Tensor[] expA = new Tensor[itemCnt];
final Tensor[] outputA = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
@Nullable final Tensor input = inObj[0].getData().get(dataIndex);
assert 1 < input.length() : "input.length() = " + input.length();
@Nullable final Tensor exp;
final DoubleSummaryStatistics summaryStatistics = DoubleStream.of(input.getData()).filter(x -> Double.isFinite(x)).summaryStatistics();
final double max = summaryStatistics.getMax();
// final double min = summaryStatistics.getMin();
exp = input.map(x -> {
double xx = Math.exp(x - max);
return Double.isFinite(xx) ? xx : 0;
});
input.freeRef();
assert Arrays.stream(exp.getData()).allMatch(Double::isFinite);
assert Arrays.stream(exp.getData()).allMatch(v -> v >= 0);
// assert exp.sum() > 0;
final double sum = 0 < exp.sum() ? exp.sum() : 1;
assert Double.isFinite(sum);
expA[dataIndex] = exp;
sumA[dataIndex] = sum;
@Nullable Tensor result = exp.map(x -> x / sum);
return result;
}).toArray(i -> new Tensor[i]);
assert Arrays.stream(outputA).flatMapToDouble(x -> Arrays.stream(x.getData())).allMatch(v -> Double.isFinite(v));
return new Result(TensorArray.wrap(outputA), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (inObj[0].isAlive()) {
final Tensor[] passbackA = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
Tensor deltaTensor = data.get(dataIndex);
@Nullable final double[] delta = deltaTensor.getData();
@Nullable final double[] expdata = expA[dataIndex].getData();
@Nonnull final Tensor passback = new Tensor(data.getDimensions());
final int dim = expdata.length;
double dot = 0;
for (int i = 0; i < expdata.length; i++) {
dot += delta[i] * expdata[i];
}
final double sum = sumA[dataIndex];
for (int i = 0; i < dim; i++) {
double value = 0;
value = (sum * delta[i] - dot) * expdata[i] / (sum * sum);
passback.set(i, value);
}
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]);
assert Arrays.stream(passbackA).flatMapToDouble(x -> Arrays.stream(x.getData())).allMatch(v -> Double.isFinite(v));
@Nonnull TensorArray tensorArray = TensorArray.wrap(passbackA);
inObj[0].accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
Arrays.stream(expA).forEach(ReferenceCounting::freeRef);
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return inObj[0].isAlive();
}
};
}
use of java.util.DoubleSummaryStatistics in project tutorials by eugenp.
the class Java8CollectorsUnitTest method whenSummarizing_shouldSummarize.
@Test
public void whenSummarizing_shouldSummarize() throws Exception {
final DoubleSummaryStatistics result = givenList.stream().collect(summarizingDouble(String::length));
assertThat(result.getAverage()).isEqualTo(2);
assertThat(result.getCount()).isEqualTo(4);
assertThat(result.getMax()).isEqualTo(3);
assertThat(result.getMin()).isEqualTo(1);
assertThat(result.getSum()).isEqualTo(8);
}
Aggregations