Search in sources :

Example 1 with SimpleEval

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] });
    });
}
Also used : IntStream(java.util.stream.IntStream) LayerTestBase(com.simiacryptus.mindseye.layers.LayerTestBase) PlotCanvas(smile.plot.PlotCanvas) ScatterPlot(smile.plot.ScatterPlot) ComponentTest(com.simiacryptus.mindseye.test.unit.ComponentTest) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) TrainingTester(com.simiacryptus.mindseye.test.unit.TrainingTester) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DoubleStream(java.util.stream.DoubleStream) List(java.util.List) Layer(com.simiacryptus.mindseye.lang.Layer) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) Layer(com.simiacryptus.mindseye.lang.Layer)

Example 2 with SimpleEval

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());
    }
}
Also used : IntStream(java.util.stream.IntStream) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Collectors(java.util.stream.Collectors) List(java.util.List) SimpleListEval(com.simiacryptus.mindseye.test.SimpleListEval) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) IntFunction(java.util.function.IntFunction) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 3 with SimpleEval

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;
}
Also used : Arrays(java.util.Arrays) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) Layer(com.simiacryptus.mindseye.lang.Layer) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) Nullable(javax.annotation.Nullable)

Example 4 with SimpleEval

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] });
    });
}
Also used : PrintStream(java.io.PrintStream) IntStream(java.util.stream.IntStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) ActivationLayerTestBase(com.simiacryptus.mindseye.layers.java.ActivationLayerTestBase) Collectors(java.util.stream.Collectors) ReLuActivationLayer(com.simiacryptus.mindseye.layers.java.ReLuActivationLayer) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) List(java.util.List) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) Layer(com.simiacryptus.mindseye.lang.Layer) SingleDerivativeTester(com.simiacryptus.mindseye.test.unit.SingleDerivativeTester) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) SigmoidActivationLayer(com.simiacryptus.mindseye.layers.java.SigmoidActivationLayer) Nonnull(javax.annotation.Nonnull) PrintStream(java.io.PrintStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) Nonnull(javax.annotation.Nonnull) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) ReLuActivationLayer(com.simiacryptus.mindseye.layers.java.ReLuActivationLayer) Layer(com.simiacryptus.mindseye.lang.Layer) SigmoidActivationLayer(com.simiacryptus.mindseye.layers.java.SigmoidActivationLayer)

Aggregations

Layer (com.simiacryptus.mindseye.lang.Layer)4 Tensor (com.simiacryptus.mindseye.lang.Tensor)4 SimpleEval (com.simiacryptus.mindseye.test.SimpleEval)4 NotebookOutput (com.simiacryptus.util.io.NotebookOutput)4 Nonnull (javax.annotation.Nonnull)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Nullable (javax.annotation.Nullable)3 ToleranceStatistics (com.simiacryptus.mindseye.test.ToleranceStatistics)2 Arrays (java.util.Arrays)2 Random (java.util.Random)2 ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)1 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)1 TensorList (com.simiacryptus.mindseye.lang.TensorList)1 CudaSystem (com.simiacryptus.mindseye.lang.cudnn.CudaSystem)1 Precision (com.simiacryptus.mindseye.lang.cudnn.Precision)1 LayerTestBase (com.simiacryptus.mindseye.layers.LayerTestBase)1 ActivationLayerTestBase (com.simiacryptus.mindseye.layers.java.ActivationLayerTestBase)1 ReLuActivationLayer (com.simiacryptus.mindseye.layers.java.ReLuActivationLayer)1