Search in sources :

Example 16 with ToleranceStatistics

use of com.simiacryptus.mindseye.test.ToleranceStatistics in project MindsEye by SimiaCryptus.

the class CudaLayerTester method testNonstandardBounds.

/**
 * Test nonstandard bounds tolerance statistics.
 *
 * @param log            the log
 * @param reference      the reference
 * @param inputPrototype the input prototype
 * @return the tolerance statistics
 */
@Nonnull
public ToleranceStatistics testNonstandardBounds(final NotebookOutput log, @Nullable final Layer reference, @Nonnull final Tensor[] inputPrototype) {
    log.h2("Irregular Input");
    log.p("This layer should be able to accept non-dense inputs.");
    return log.code(() -> {
        Tensor[] randomized = Arrays.stream(inputPrototype).map(x -> x.map(v -> getRandom())).toArray(i -> new Tensor[i]);
        logger.info("Input: " + Arrays.stream(randomized).map(Tensor::prettyPrint).collect(Collectors.toList()));
        Precision precision = Precision.Double;
        TensorList[] controlInput = CudaSystem.run(gpu -> {
            return Arrays.stream(randomized).map(original -> {
                TensorArray data = TensorArray.create(original);
                CudaTensorList wrap = CudaTensorList.wrap(gpu.getTensor(data, precision, MemoryType.Managed, false), 1, original.getDimensions(), precision);
                data.freeRef();
                return wrap;
            }).toArray(i -> new TensorList[i]);
        }, 0);
        @Nonnull final SimpleResult controlResult = CudaSystem.run(gpu -> {
            return SimpleGpuEval.run(reference, gpu, controlInput);
        }, 1);
        final TensorList[] irregularInput = CudaSystem.run(gpu -> {
            return Arrays.stream(randomized).map(original -> {
                return buildIrregularCudaTensor(gpu, precision, original);
            }).toArray(i -> new TensorList[i]);
        }, 0);
        @Nonnull final SimpleResult testResult = CudaSystem.run(gpu -> {
            return SimpleGpuEval.run(reference, gpu, irregularInput);
        }, 1);
        try {
            ToleranceStatistics compareOutput = compareOutput(controlResult, testResult);
            ToleranceStatistics compareDerivatives = compareDerivatives(controlResult, testResult);
            return compareDerivatives.combine(compareOutput);
        } finally {
            Arrays.stream(randomized).forEach(ReferenceCountingBase::freeRef);
            Arrays.stream(controlInput).forEach(ReferenceCounting::freeRef);
            Arrays.stream(irregularInput).forEach(x -> x.freeRef());
            controlResult.freeRef();
            testResult.freeRef();
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) SimpleGpuEval(com.simiacryptus.mindseye.test.SimpleGpuEval) Arrays(java.util.Arrays) CudaMemory(com.simiacryptus.mindseye.lang.cudnn.CudaMemory) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) Random(java.util.Random) Function(java.util.function.Function) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) CudnnHandle(com.simiacryptus.mindseye.lang.cudnn.CudnnHandle) Layer(com.simiacryptus.mindseye.lang.Layer) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) Logger(org.slf4j.Logger) CudaDevice(com.simiacryptus.mindseye.lang.cudnn.CudaDevice) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) MemoryType(com.simiacryptus.mindseye.lang.cudnn.MemoryType) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) Tensor(com.simiacryptus.mindseye.lang.Tensor) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) Nonnull(javax.annotation.Nonnull) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) TensorList(com.simiacryptus.mindseye.lang.TensorList) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nonnull(javax.annotation.Nonnull)

Example 17 with ToleranceStatistics

use of com.simiacryptus.mindseye.test.ToleranceStatistics in project MindsEye by SimiaCryptus.

the class CudaLayerTester method compareDerivatives.

/**
 * Compare derivatives tolerance statistics.
 *
 * @param expected the expected
 * @param actual   the actual
 * @return the tolerance statistics
 */
@Nonnull
public ToleranceStatistics compareDerivatives(final SimpleResult expected, final SimpleResult actual) {
    ToleranceStatistics derivativeAgreement = compareInputDerivatives(expected, actual);
    derivativeAgreement = derivativeAgreement.combine(compareLayerDerivatives(expected, actual));
    return derivativeAgreement;
}
Also used : ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) Nonnull(javax.annotation.Nonnull)

Example 18 with ToleranceStatistics

use of com.simiacryptus.mindseye.test.ToleranceStatistics 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 19 with ToleranceStatistics

use of com.simiacryptus.mindseye.test.ToleranceStatistics in project MindsEye by SimiaCryptus.

the class SingleDerivativeTester method test.

/**
 * Test tolerance statistics.
 *
 * @param output
 * @param component      the component
 * @param inputPrototype the input prototype
 * @return the tolerance statistics
 */
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput output, @Nonnull final Layer component, @Nonnull final Tensor... inputPrototype) {
    output.h1("Differential Validation");
    ToleranceStatistics _statistics = new ToleranceStatistics();
    final Tensor outputPrototype = SimpleEval.run(component, inputPrototype).getOutputAndFree();
    try {
        if (verbose) {
            output.code(() -> {
                log.info(String.format("Inputs: %s", Arrays.stream(inputPrototype).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse("")));
                log.info(String.format("Inputs Statistics: %s", Arrays.stream(inputPrototype).map(x -> new ScalarStatistics().add(x.getData()).toString()).reduce((a, b) -> a + ",\n" + b).orElse("")));
                log.info(String.format("Output: %s", null == outputPrototype ? null : outputPrototype.prettyPrint()));
                log.info(String.format("Outputs Statistics: %s", new ScalarStatistics().add(outputPrototype.getData())));
            });
        }
        if (isTestFeedback()) {
            output.h2("Feedback Validation");
            output.p("We validate the agreement between the implemented derivative _of the inputs_ apply finite difference estimations:");
            final ToleranceStatistics statistics = _statistics;
            _statistics = output.code(() -> {
                return testFeedback(statistics, component, inputPrototype, outputPrototype);
            });
        }
        if (isTestLearning()) {
            output.h2("Learning Validation");
            output.p("We validate the agreement between the implemented derivative _of the internal weights_ apply finite difference estimations:");
            final ToleranceStatistics statistics = _statistics;
            _statistics = output.code(() -> {
                return testLearning(statistics, component, inputPrototype, outputPrototype);
            });
        }
    } finally {
        outputPrototype.freeRef();
    }
    output.h2("Total Accuracy");
    output.p("The overall agreement accuracy between the implemented derivative and the finite difference estimations:");
    final ToleranceStatistics statistics = _statistics;
    output.code(() -> {
        // log.info(String.format("Component: %s\nInputs: %s\noutput=%s", component, Arrays.toString(inputPrototype), outputPrototype));
        log.info(String.format("Finite-Difference Derivative Accuracy:"));
        log.info(String.format("absoluteTol: %s", statistics.absoluteTol));
        log.info(String.format("relativeTol: %s", statistics.relativeTol));
    });
    output.h2("Frozen and Alive Status");
    output.code(() -> {
        testFrozen(component, inputPrototype);
        testUnFrozen(component, inputPrototype);
    });
    return _statistics;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Optional(java.util.Optional) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) 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) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics)

Example 20 with ToleranceStatistics

use of com.simiacryptus.mindseye.test.ToleranceStatistics in project MindsEye by SimiaCryptus.

the class CudaLayerTestBase method getReferenceIOTester.

@Nullable
@Override
protected ComponentTest<ToleranceStatistics> getReferenceIOTester() {
    @Nullable final ComponentTest<ToleranceStatistics> inner = super.getReferenceIOTester();
    return new ComponentTestBase<ToleranceStatistics>() {

        @Override
        protected void _free() {
            inner.freeRef();
            super._free();
        }

        @Override
        public ToleranceStatistics test(@Nonnull NotebookOutput log, Layer component, Tensor... inputPrototype) {
            @Nullable PrintStream apiLog = null;
            try {
                @Nonnull String logName = "cuda_" + log.getName() + "_io.log";
                log.p(log.file((String) null, logName, "GPU Log"));
                apiLog = new PrintStream(log.file(logName));
                CudaSystem.addLog(apiLog);
                return inner.test(log, component, inputPrototype);
            } finally {
                if (null != apiLog) {
                    apiLog.close();
                    CudaSystem.apiLog.remove(apiLog);
                }
            }
        }
    };
}
Also used : ComponentTestBase(com.simiacryptus.mindseye.test.unit.ComponentTestBase) PrintStream(java.io.PrintStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) Layer(com.simiacryptus.mindseye.lang.Layer) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Aggregations

ToleranceStatistics (com.simiacryptus.mindseye.test.ToleranceStatistics)21 Nonnull (javax.annotation.Nonnull)20 Layer (com.simiacryptus.mindseye.lang.Layer)19 Tensor (com.simiacryptus.mindseye.lang.Tensor)19 NotebookOutput (com.simiacryptus.util.io.NotebookOutput)19 Nullable (javax.annotation.Nullable)19 Arrays (java.util.Arrays)16 IntStream (java.util.stream.IntStream)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)13 TensorList (com.simiacryptus.mindseye.lang.TensorList)13 Collectors (java.util.stream.Collectors)13 SimpleEval (com.simiacryptus.mindseye.test.SimpleEval)9 ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)7 SimpleResult (com.simiacryptus.mindseye.test.SimpleResult)7 ScalarStatistics (com.simiacryptus.util.data.ScalarStatistics)7 List (java.util.List)7 IntFunction (java.util.function.IntFunction)7 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)6