Search in sources :

Example 31 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class BatchDerivativeTester method test.

/**
 * Test tolerance statistics.
 *
 * @param log
 * @param component      the component
 * @param inputPrototype the input prototype
 * @return the tolerance statistics
 */
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer component, @Nonnull final Tensor... inputPrototype) {
    log.h1("Differential Validation");
    @Nonnull IOPair ioPair = new IOPair(component, inputPrototype[0]).invoke();
    if (verbose) {
        log.code(() -> {
            BatchDerivativeTester.log.info(String.format("Inputs: %s", Arrays.stream(inputPrototype).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).get()));
            BatchDerivativeTester.log.info(String.format("Inputs Statistics: %s", Arrays.stream(inputPrototype).map(x -> new ScalarStatistics().add(x.getData()).toString()).reduce((a, b) -> a + ",\n" + b).get()));
            BatchDerivativeTester.log.info(String.format("Output: %s", ioPair.getOutputPrototype().prettyPrint()));
            BatchDerivativeTester.log.info(String.format("Outputs Statistics: %s", new ScalarStatistics().add(ioPair.getOutputPrototype().getData())));
        });
    }
    ToleranceStatistics _statistics = new ToleranceStatistics();
    if (isTestFeedback()) {
        log.h2("Feedback Validation");
        log.p("We validate the agreement between the implemented derivative _of the inputs_ apply finite difference estimations:");
        ToleranceStatistics statistics = _statistics;
        _statistics = log.code(() -> {
            return testFeedback(component, ioPair, statistics);
        });
    }
    if (isTestLearning()) {
        log.h2("Learning Validation");
        log.p("We validate the agreement between the implemented derivative _of the internal weights_ apply finite difference estimations:");
        ToleranceStatistics statistics = _statistics;
        _statistics = log.code(() -> {
            return testLearning(component, ioPair, statistics);
        });
    }
    log.h2("Total Accuracy");
    log.p("The overall agreement accuracy between the implemented derivative and the finite difference estimations:");
    ToleranceStatistics statistics = _statistics;
    log.code(() -> {
        // log.info(String.format("Component: %s\nInputs: %s\noutput=%s", component, Arrays.toString(inputPrototype), outputPrototype));
        BatchDerivativeTester.log.info(String.format("Finite-Difference Derivative Accuracy:"));
        BatchDerivativeTester.log.info(String.format("absoluteTol: %s", statistics.absoluteTol));
        BatchDerivativeTester.log.info(String.format("relativeTol: %s", statistics.relativeTol));
    });
    log.h2("Frozen and Alive Status");
    log.code(() -> {
        testFrozen(component, ioPair.getInputPrototype());
        testUnFrozen(component, ioPair.getInputPrototype());
    });
    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) 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) Nonnull(javax.annotation.Nonnull) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics)

Example 32 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class StandardLayerTests method tests.

private void tests(final NotebookOutput log, final List<ComponentTest<?>> tests, @Nonnull final Invocation invocation, @Nonnull final ArrayList<TestError> exceptions) {
    tests.stream().filter(x -> null != x).forEach((ComponentTest<?> test) -> {
        @Nonnull Layer layer = invocation.getLayer().copy();
        Tensor[] inputs = randomize(invocation.getDims());
        try {
            test.test(log, layer, inputs);
        } catch (LifecycleException e) {
            throw e;
        } catch (Throwable e) {
            exceptions.add(new TestError(e, test, layer));
        } finally {
            for (@Nonnull Tensor tensor : inputs) tensor.freeRef();
            layer.freeRef();
            test.freeRef();
            System.gc();
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Graphviz(guru.nidi.graphviz.engine.Graphviz) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) NotebookReportBase(com.simiacryptus.mindseye.test.NotebookReportBase) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Format(guru.nidi.graphviz.engine.Format) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) CudaError(com.simiacryptus.mindseye.lang.cudnn.CudaError) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SysOutInterceptor(com.simiacryptus.util.test.SysOutInterceptor) Collection(java.util.Collection) TestUtil(com.simiacryptus.mindseye.test.TestUtil) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) Explodable(com.simiacryptus.mindseye.layers.cudnn.Explodable) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) LifecycleException(com.simiacryptus.mindseye.lang.LifecycleException) LifecycleException(com.simiacryptus.mindseye.lang.LifecycleException) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) Layer(com.simiacryptus.mindseye.lang.Layer)

Example 33 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class StandardLayerTests method bigTests.

/**
 * Big tests.
 *
 * @param log        the log
 * @param seed       the seed
 * @param perfLayer  the perf layer
 * @param exceptions the exceptions
 */
public void bigTests(NotebookOutput log, long seed, @Nonnull Layer perfLayer, @Nonnull ArrayList<TestError> exceptions) {
    getBigTests().stream().filter(x -> null != x).forEach(test -> {
        @Nonnull Layer layer = perfLayer.copy();
        try {
            Tensor[] input = randomize(getLargeDims(new Random(seed)));
            try {
                test.test(log, layer, input);
            } finally {
                for (@Nonnull Tensor t : input) {
                    t.freeRef();
                }
            }
        } catch (LifecycleException e) {
            throw e;
        } catch (CudaError e) {
            throw e;
        } catch (Throwable e) {
            exceptions.add(new TestError(e, test, layer));
        } finally {
            layer.freeRef();
            test.freeRef();
            System.gc();
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Graphviz(guru.nidi.graphviz.engine.Graphviz) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) NotebookReportBase(com.simiacryptus.mindseye.test.NotebookReportBase) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Format(guru.nidi.graphviz.engine.Format) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) CudaError(com.simiacryptus.mindseye.lang.cudnn.CudaError) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SysOutInterceptor(com.simiacryptus.util.test.SysOutInterceptor) Collection(java.util.Collection) TestUtil(com.simiacryptus.mindseye.test.TestUtil) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) Explodable(com.simiacryptus.mindseye.layers.cudnn.Explodable) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) LifecycleException(com.simiacryptus.mindseye.lang.LifecycleException) CudaError(com.simiacryptus.mindseye.lang.cudnn.CudaError) LifecycleException(com.simiacryptus.mindseye.lang.LifecycleException) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) Nonnull(javax.annotation.Nonnull) Layer(com.simiacryptus.mindseye.lang.Layer)

Example 34 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class TrainingTester method trainLBFGS.

/**
 * Train lbfgs list.
 *
 * @param log       the log
 * @param trainable the trainable
 * @return the list
 */
@Nonnull
public List<StepRecord> trainLBFGS(@Nonnull final NotebookOutput log, final Trainable trainable) {
    log.p("Next, we apply the same optimization using L-BFGS, which is nearly ideal for purely second-order or quadratic functions.");
    @Nonnull final List<StepRecord> history = new ArrayList<>();
    @Nonnull final TrainingMonitor monitor = TrainingTester.getMonitor(history);
    try {
        log.code(() -> {
            return new IterativeTrainer(trainable).setLineSearchFactory(label -> new ArmijoWolfeSearch()).setOrientation(new LBFGS()).setMonitor(monitor).setTimeout(30, TimeUnit.SECONDS).setIterationsPerSample(100).setMaxIterations(250).setTerminateThreshold(0).runAndFree();
        });
    } catch (Throwable e) {
        if (isThrowExceptions())
            throw new RuntimeException(e);
    }
    return history;
}
Also used : StepRecord(com.simiacryptus.mindseye.test.StepRecord) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) ArrayList(java.util.ArrayList) Trainable(com.simiacryptus.mindseye.eval.Trainable) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) QuadraticSearch(com.simiacryptus.mindseye.opt.line.QuadraticSearch) LBFGS(com.simiacryptus.mindseye.opt.orient.LBFGS) RecursiveSubspace(com.simiacryptus.mindseye.opt.orient.RecursiveSubspace) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Logger(org.slf4j.Logger) PlotCanvas(smile.plot.PlotCanvas) QQN(com.simiacryptus.mindseye.opt.orient.QQN) GradientDescent(com.simiacryptus.mindseye.opt.orient.GradientDescent) BasicTrainable(com.simiacryptus.mindseye.eval.BasicTrainable) StaticLearningRate(com.simiacryptus.mindseye.opt.line.StaticLearningRate) TestUtil(com.simiacryptus.mindseye.test.TestUtil) DAGNode(com.simiacryptus.mindseye.network.DAGNode) DoubleStream(java.util.stream.DoubleStream) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) Step(com.simiacryptus.mindseye.opt.Step) ProblemRun(com.simiacryptus.mindseye.test.ProblemRun) javax.swing(javax.swing) LBFGS(com.simiacryptus.mindseye.opt.orient.LBFGS) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 35 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class TrainingTester method testModelLearning.

/**
 * Test model learning.
 *
 * @param log            the log
 * @param component      the component
 * @param random         the randomize
 * @param inputPrototype the input prototype
 * @return the apply result
 */
public TestResult testModelLearning(@Nonnull final NotebookOutput log, @Nonnull final Layer component, final Random random, final Tensor[] inputPrototype) {
    @Nonnull final Layer network_target = shuffle(random, component.copy()).freeze();
    final Tensor[][] input_target = shuffleCopy(random, inputPrototype);
    log.p("In this apply, attempt to train a network to emulate a randomized network given an example input/output. The target state is:");
    log.code(() -> {
        return network_target.state().stream().map(Arrays::toString).reduce((a, b) -> a + "\n" + b).orElse("");
    });
    Result[] array = ConstantResult.batchResultArray(input_target);
    Result eval = network_target.eval(array);
    Arrays.stream(array).forEach(ReferenceCounting::freeRef);
    TensorList result = eval.getData();
    eval.freeRef();
    final Tensor[] output_target = result.stream().toArray(i -> new Tensor[i]);
    result.freeRef();
    if (output_target.length != input_target.length) {
        logger.info("Batch layers not supported");
        return null;
    }
    return trainAll("Model Convergence", log, append(input_target, output_target), shuffle(random, component.copy()));
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) ArrayList(java.util.ArrayList) Trainable(com.simiacryptus.mindseye.eval.Trainable) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) QuadraticSearch(com.simiacryptus.mindseye.opt.line.QuadraticSearch) LBFGS(com.simiacryptus.mindseye.opt.orient.LBFGS) RecursiveSubspace(com.simiacryptus.mindseye.opt.orient.RecursiveSubspace) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Logger(org.slf4j.Logger) PlotCanvas(smile.plot.PlotCanvas) QQN(com.simiacryptus.mindseye.opt.orient.QQN) GradientDescent(com.simiacryptus.mindseye.opt.orient.GradientDescent) BasicTrainable(com.simiacryptus.mindseye.eval.BasicTrainable) StaticLearningRate(com.simiacryptus.mindseye.opt.line.StaticLearningRate) TestUtil(com.simiacryptus.mindseye.test.TestUtil) DAGNode(com.simiacryptus.mindseye.network.DAGNode) DoubleStream(java.util.stream.DoubleStream) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) Step(com.simiacryptus.mindseye.opt.Step) ProblemRun(com.simiacryptus.mindseye.test.ProblemRun) javax.swing(javax.swing) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Arrays(java.util.Arrays) TensorList(com.simiacryptus.mindseye.lang.TensorList) Layer(com.simiacryptus.mindseye.lang.Layer) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult)

Aggregations

NotebookOutput (com.simiacryptus.util.io.NotebookOutput)48 Nonnull (javax.annotation.Nonnull)48 Tensor (com.simiacryptus.mindseye.lang.Tensor)46 Nullable (javax.annotation.Nullable)40 Layer (com.simiacryptus.mindseye.lang.Layer)39 Arrays (java.util.Arrays)38 List (java.util.List)37 IntStream (java.util.stream.IntStream)31 TestUtil (com.simiacryptus.mindseye.test.TestUtil)25 Logger (org.slf4j.Logger)25 LoggerFactory (org.slf4j.LoggerFactory)25 Stream (java.util.stream.Stream)23 Collectors (java.util.stream.Collectors)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)20 PipelineNetwork (com.simiacryptus.mindseye.network.PipelineNetwork)19 TrainingMonitor (com.simiacryptus.mindseye.opt.TrainingMonitor)19 TimeUnit (java.util.concurrent.TimeUnit)19 StepRecord (com.simiacryptus.mindseye.test.StepRecord)18