use of com.simiacryptus.mindseye.test.SimpleEval in project MindsEye by SimiaCryptus.
the class ActivationLayerTestBase method run.
@Override
public void run(final NotebookOutput log) {
super.run(log);
log.h3("Function Plots");
final Layer layer = getLayer(new int[][] { { 1 } }, new Random());
final List<double[]> plotData = scan().mapToObj(x -> {
@Nonnull Tensor tensor = new Tensor(x);
@Nonnull final SimpleEval eval = SimpleEval.run(layer, tensor);
tensor.freeRef();
@Nonnull double[] doubles = { x, eval.getOutput().get(0), eval.getDerivative()[0].get(0) };
eval.freeRef();
return doubles;
}).collect(Collectors.toList());
log.code(() -> {
return ActivationLayerTestBase.plot("Value Plot", plotData, x -> new double[] { x[0], x[1] });
});
log.code(() -> {
return ActivationLayerTestBase.plot("Derivative Plot", plotData, x -> new double[] { x[0], x[2] });
});
}
use of com.simiacryptus.mindseye.test.SimpleEval in project MindsEye by SimiaCryptus.
the class BatchingTester method test.
/**
* Test tolerance statistics.
*
* @param reference the reference
* @param inputPrototype the input prototype
* @return the tolerance statistics
*/
@Nonnull
public ToleranceStatistics test(@Nullable final Layer reference, @Nonnull final Tensor[] inputPrototype) {
if (null == reference)
return new ToleranceStatistics();
final TensorList[] inputTensorLists = Arrays.stream(inputPrototype).map(t -> TensorArray.wrap(IntStream.range(0, getBatchSize()).mapToObj(i -> t.map(v -> getRandom())).toArray(i -> new Tensor[i]))).toArray(i -> new TensorList[i]);
@Nonnull final SimpleResult asABatch;
final List<SimpleEval> oneAtATime;
try {
asABatch = SimpleListEval.run(reference, inputTensorLists);
oneAtATime = IntStream.range(0, getBatchSize()).mapToObj(batch -> {
Tensor[] inputTensors = IntStream.range(0, inputTensorLists.length).mapToObj(i -> inputTensorLists[i].get(batch)).toArray(i -> new Tensor[i]);
@Nonnull SimpleEval eval = SimpleEval.run(reference, inputTensors);
for (@Nonnull Tensor tensor : inputTensors) {
tensor.freeRef();
}
return eval;
}).collect(Collectors.toList());
} finally {
for (@Nonnull TensorList tensorList : inputTensorLists) {
tensorList.freeRef();
}
}
try {
TensorList batchOutput = asABatch.getOutput();
@Nonnull IntFunction<ToleranceStatistics> toleranceStatisticsIntFunction = batch -> {
@Nullable Tensor batchTensor = batchOutput.get(batch);
@Nonnull ToleranceStatistics accumulate = new ToleranceStatistics().accumulate(batchTensor.getData(), oneAtATime.get(batch).getOutput().getData());
batchTensor.freeRef();
return accumulate;
};
int batchLength = batchOutput.length();
@Nonnull final ToleranceStatistics outputAgreement = IntStream.range(0, Math.min(getBatchSize(), batchLength)).mapToObj(toleranceStatisticsIntFunction).reduce((a, b) -> a.combine(b)).get();
if (!(outputAgreement.absoluteTol.getMax() < tolerance)) {
logger.info("Batch Output: " + batchOutput.stream().map(x -> {
String str = x.prettyPrint();
x.freeRef();
return str;
}).collect(Collectors.toList()));
logger.info("Singular Output: " + oneAtATime.stream().map(x -> x.getOutput().prettyPrint()).collect(Collectors.toList()));
throw new AssertionError("Output Corrupt: " + outputAgreement);
}
ToleranceStatistics derivativeAgreement = IntStream.range(0, Math.min(getBatchSize(), batchLength)).mapToObj(batch -> {
IntFunction<ToleranceStatistics> statisticsFunction = input -> {
@Nullable Tensor a = asABatch.getInputDerivative()[input].get(batch);
Tensor b = oneAtATime.get(batch).getDerivative()[input];
@Nonnull Tensor diff = a.minus(b);
logger.info("Error: " + diff.prettyPrint());
logger.info("Scalar Statistics: " + new ScalarStatistics().add(diff.getData()).getMetrics());
double[][] points = Arrays.stream(diff.getData()).mapToObj(x -> new double[] { x }).toArray(i -> new double[i][]);
// logger.info("Density: " + new DensityTree("x").setMinSplitFract(1e-8).setSplitSizeThreshold(2).new Node(points));
diff.freeRef();
@Nonnull ToleranceStatistics toleranceStatistics = new ToleranceStatistics().accumulate(a.getData(), b.getData());
a.freeRef();
return toleranceStatistics;
};
return IntStream.range(0, Math.min(inputPrototype.length, batchLength)).mapToObj(statisticsFunction).reduce((a, b) -> a.combine(b)).orElse(null);
}).filter(x -> x != null).reduce((a, b) -> a.combine(b)).orElse(null);
if (null != derivativeAgreement && !(derivativeAgreement.absoluteTol.getMax() < tolerance)) {
throw new AssertionError("Derivatives Corrupt: " + derivativeAgreement);
}
return null != derivativeAgreement ? derivativeAgreement.combine(outputAgreement) : outputAgreement;
} finally {
asABatch.freeRef();
oneAtATime.forEach(x -> x.freeRef());
}
}
use of com.simiacryptus.mindseye.test.SimpleEval in project MindsEye by SimiaCryptus.
the class ReferenceIO method test.
@Nullable
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer layer, @Nonnull final Tensor... inputPrototype) {
if (!referenceIO.isEmpty()) {
log.h1("Reference Input/Output Pairs");
log.p("Display pre-setBytes input/output example pairs:");
referenceIO.forEach((input, output) -> {
log.code(() -> {
@Nonnull final SimpleEval eval = SimpleEval.run(layer, input);
Tensor add = output.scale(-1).addAndFree(eval.getOutput());
@Nonnull final DoubleStatistics error = new DoubleStatistics().accept(add.getData());
add.freeRef();
String format = String.format("--------------------\nInput: \n[%s]\n--------------------\nOutput: \n%s\nError: %s\n--------------------\nDerivative: \n%s", Arrays.stream(input).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).get(), eval.getOutput().prettyPrint(), error, Arrays.stream(eval.getDerivative()).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).get());
eval.freeRef();
return format;
});
});
} else {
log.h1("Example Input/Output Pair");
log.p("Display input/output pairs from random executions:");
log.code(() -> {
@Nonnull final SimpleEval eval = SimpleEval.run(layer, inputPrototype);
String format = String.format("--------------------\nInput: \n[%s]\n--------------------\nOutput: \n%s\n--------------------\nDerivative: \n%s", Arrays.stream(inputPrototype).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse(""), eval.getOutput().prettyPrint(), Arrays.stream(eval.getDerivative()).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse(""));
eval.freeRef();
return format;
});
}
return null;
}
use of com.simiacryptus.mindseye.test.SimpleEval in project MindsEye by SimiaCryptus.
the class ActivationLayerTest method run.
@Override
public void run(final NotebookOutput log) {
@Nonnull String logName = "cuda_" + log.getName() + "_all.log";
log.p(log.file((String) null, logName, "GPU Log"));
CudaSystem.addLog(new PrintStream(log.file(logName)));
super.run(log);
log.h3("Function Plots");
@Nonnull final Layer layer = getLayer(new int[][] { { 1, 1, 1 } }, new Random());
final List<double[]> plotData = IntStream.range(-1000, 1000).mapToDouble(x -> x / 300.0).mapToObj(x -> {
@Nonnull Tensor input = new Tensor(new double[] { x }, 1, 1, 1);
@Nonnull final SimpleEval eval = SimpleEval.run(layer, input);
input.freeRef();
@Nonnull double[] doubles = { x, eval.getOutput().get(0), eval.getDerivative()[0].get(0) };
eval.freeRef();
return doubles;
}).collect(Collectors.toList());
layer.freeRef();
log.code(() -> {
return ActivationLayerTestBase.plot("Value Plot", plotData, x -> new double[] { x[0], x[1] });
});
log.code(() -> {
return ActivationLayerTestBase.plot("Derivative Plot", plotData, x -> new double[] { x[0], x[2] });
});
}
Aggregations